llvm.org GIT mirror llvm / 5139e4f
Merging r278938: ------------------------------------------------------------------------ r278938 | mcrosier | 2016-08-17 08:54:39 -0700 (Wed, 17 Aug 2016) | 5 lines Revert "Reassociate: Reprocess RedoInsts after each inst". This reverts commit r258830, which introduced a bug described in PR28367. PR28367 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_39@278993 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 4 years ago
5 changed file(s) with 33 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
6464 PreservedAnalyses run(Function &F, FunctionAnalysisManager &);
6565
6666 private:
67 void BuildRankMap(Function &F, ReversePostOrderTraversal &RPOT);
67 void BuildRankMap(Function &F);
6868 unsigned getRank(Value *V);
6969 void canonicalizeOperands(Instruction *I);
7070 void ReassociateExpression(BinaryOperator *I);
144144 return nullptr;
145145 }
146146
147 void ReassociatePass::BuildRankMap(
148 Function &F, ReversePostOrderTraversal &RPOT) {
147 void ReassociatePass::BuildRankMap(Function &F) {
149148 unsigned i = 2;
150149
151150 // Assign distinct ranks to function arguments.
154153 DEBUG(dbgs() << "Calculated Rank[" << I->getName() << "] = " << i << "\n");
155154 }
156155
156 ReversePostOrderTraversal RPOT(&F);
157157 for (BasicBlock *BB : RPOT) {
158158 unsigned BBRank = RankMap[BB] = ++i << 16;
159159
21712171 }
21722172
21732173 PreservedAnalyses ReassociatePass::run(Function &F, FunctionAnalysisManager &) {
2174 // Reassociate needs for each instruction to have its operands already
2175 // processed, so we first perform a RPOT of the basic blocks so that
2176 // when we process a basic block, all its dominators have been processed
2177 // before.
2178 ReversePostOrderTraversal RPOT(&F);
2179 BuildRankMap(F, RPOT);
2174 // Calculate the rank map for F.
2175 BuildRankMap(F);
21802176
21812177 MadeChange = false;
2182 for (BasicBlock *BI : RPOT) {
2183 // Use a worklist to keep track of which instructions have been processed
2184 // (and which insts won't be optimized again) so when redoing insts,
2185 // optimize insts rightaway which won't be processed later.
2186 SmallSet Worklist;
2187
2188 // Insert all instructions in the BB
2189 for (Instruction &I : *BI)
2190 Worklist.insert(&I);
2191
2178 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
21922179 // Optimize every instruction in the basic block.
2193 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;) {
2194 // This instruction has been processed.
2195 Worklist.erase(&*II);
2180 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;)
21962181 if (isInstructionTriviallyDead(&*II)) {
21972182 EraseInst(&*II++);
21982183 } else {
22012186 ++II;
22022187 }
22032188
2204 // If the above optimizations produced new instructions to optimize or
2205 // made modifications which need to be redone, do them now if they won't
2206 // be handled later.
2207 while (!RedoInsts.empty()) {
2208 Instruction *I = RedoInsts.pop_back_val();
2209 // Process instructions that won't be processed later, either
2210 // inside the block itself or in another basic block (based on rank),
2211 // since these will be processed later.
2212 if ((I->getParent() != BI || !Worklist.count(I)) &&
2213 RankMap[I->getParent()] <= RankMap[BI]) {
2214 if (isInstructionTriviallyDead(I))
2215 EraseInst(I);
2216 else
2217 OptimizeInst(I);
2218 }
2219 }
2189 // Make a copy of all the instructions to be redone so we can remove dead
2190 // instructions.
2191 SetVector> ToRedo(RedoInsts);
2192 // Iterate over all instructions to be reevaluated and remove trivially dead
2193 // instructions. If any operand of the trivially dead instruction becomes
2194 // dead mark it for deletion as well. Continue this process until all
2195 // trivially dead instructions have been removed.
2196 while (!ToRedo.empty()) {
2197 Instruction *I = ToRedo.pop_back_val();
2198 if (isInstructionTriviallyDead(I))
2199 RecursivelyEraseDeadInsts(I, ToRedo);
2200 }
2201
2202 // Now that we have removed dead instructions, we can reoptimize the
2203 // remaining instructions.
2204 while (!RedoInsts.empty()) {
2205 Instruction *I = RedoInsts.pop_back_val();
2206 if (isInstructionTriviallyDead(I))
2207 EraseInst(I);
2208 else
2209 OptimizeInst(I);
22202210 }
22212211 }
22222212
+0
-57
test/Transforms/Reassociate/prev_insts_canonicalized.ll less more
None ; RUN: opt < %s -reassociate -S | FileCheck %s
1
2 ; These tests make sure that before processing insts
3 ; any previous instructions are already canonicalized.
4 define i32 @foo(i32 %in) {
5 ; CHECK-LABEL: @foo
6 ; CHECK-NEXT: %factor = mul i32 %in, -4
7 ; CHECK-NEXT: %factor1 = mul i32 %in, 2
8 ; CHECK-NEXT: %_3 = add i32 %factor, 1
9 ; CHECK-NEXT: %_5 = add i32 %_3, %factor1
10 ; CHECK-NEXT: ret i32 %_5
11 %_0 = add i32 %in, 1
12 %_1 = mul i32 %in, -2
13 %_2 = add i32 %_0, %_1
14 %_3 = add i32 %_1, %_2
15 %_4 = add i32 %_3, 1
16 %_5 = add i32 %in, %_3
17 ret i32 %_5
18 }
19
20 ; CHECK-LABEL: @foo1
21 define void @foo1(float %in, i1 %cmp) {
22 wrapper_entry:
23 br label %foo1
24
25 for.body:
26 %0 = fadd float %in1, %in1
27 br label %foo1
28
29 foo1:
30 %_0 = fmul fast float %in, -3.000000e+00
31 %_1 = fmul fast float %_0, 3.000000e+00
32 %in1 = fadd fast float -3.000000e+00, %_1
33 %in1use = fadd fast float %in1, %in1
34 br label %for.body
35
36
37 }
38
39 ; CHECK-LABEL: @foo2
40 define void @foo2(float %in, i1 %cmp) {
41 wrapper_entry:
42 br label %for.body
43
44 for.body:
45 ; If the operands of the phi are sheduled for processing before
46 ; foo1 is processed, the invariant of reassociate are not preserved
47 %unused = phi float [%in1, %foo1], [undef, %wrapper_entry]
48 br label %foo1
49
50 foo1:
51 %_0 = fmul fast float %in, -3.000000e+00
52 %_1 = fmul fast float %_0, 3.000000e+00
53 %in1 = fadd fast float -3.000000e+00, %_1
54 %in1use = fadd fast float %in1, %in1
55 br label %for.body
56 }
0 ; RUN: opt < %s -reassociate -S | FileCheck %s
11 ; CHECK-LABEL: faddsubAssoc1
2 ; CHECK: [[TMP1:%.*]] = fsub fast half 0xH8000, %a
3 ; CHECK: [[TMP2:%.*]] = fadd fast half %b, [[TMP1]]
4 ; CHECK: fmul fast half [[TMP2]], 0xH4500
2 ; CHECK: [[TMP1:%tmp.*]] = fmul fast half %a, 0xH4500
3 ; CHECK: [[TMP2:%tmp.*]] = fmul fast half %b, 0xH4500
4 ; CHECK: fsub fast half [[TMP2]], [[TMP1]]
55 ; CHECK: ret
66 ; Input is A op (B op C)
77 define half @faddsubAssoc1(half %a, half %b) {
8787 %xor1 = xor i32 %xor, %and
8888 ret i32 %xor1
8989 ; CHECK-LABEL: @xor_special2(
90 ; CHECK: %xor = xor i32 %y, 123
91 ; CHECK: %xor1 = xor i32 %xor, %x
90 ; CHECK: %xor = xor i32 %x, 123
91 ; CHECK: %xor1 = xor i32 %xor, %y
9292 ; CHECK: ret i32 %xor1
9393 }
9494