llvm.org GIT mirror llvm / f41fcbb
Enhance GVN to propagate simple conditionals. This fixes PR3921. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@68172 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
2 changed file(s) with 90 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
120120 AliasAnalysis* AA;
121121 MemoryDependenceAnalysis* MD;
122122 DominatorTree* DT;
123 uint32_t true_vn, false_vn;
123124
124125 uint32_t nextValueNumber;
125126
137138 Expression create_expression(CallInst* C);
138139 Expression create_expression(Constant* C);
139140 public:
140 ValueTable() : nextValueNumber(1) { }
141 ValueTable() : nextValueNumber(1) {
142 true_vn = lookup_or_add(ConstantInt::getTrue());
143 false_vn = lookup_or_add(ConstantInt::getFalse());
144 }
145
146 uint32_t getTrueVN() { return true_vn; }
147 uint32_t getFalseVN() { return false_vn; }
148
141149 uint32_t lookup_or_add(Value* V);
142150 uint32_t lookup(Value* V) const;
143151 void add(Value* V, uint32_t num);
12931301 uint32_t nextNum = VN.getNextUnusedValueNumber();
12941302 unsigned num = VN.lookup_or_add(I);
12951303
1304 if (BranchInst* BI = dyn_cast(I)) {
1305 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
1306
1307 if (!BI->isConditional() || isa(BI->getCondition()))
1308 return false;
1309
1310 Value* branchCond = BI->getCondition();
1311 uint32_t condVN = VN.lookup_or_add(branchCond);
1312
1313 BasicBlock* trueSucc = BI->getSuccessor(0);
1314 BasicBlock* falseSucc = BI->getSuccessor(1);
1315
1316 localAvail[trueSucc]->table.insert(std::make_pair(condVN,
1317 ConstantInt::getTrue()));
1318 localAvail[falseSucc]->table.insert(std::make_pair(condVN,
1319 ConstantInt::getFalse()));
1320 return false;
1321
12961322 // Allocations are always uniquely numbered, so we can save time and memory
1297 // by fast failing them.
1298 if (isa(I) || isa(I)) {
1323 // by fast failing them.
1324 } else if (isa(I) || isa(I)) {
12991325 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
13001326 return false;
13011327 }
14041430
14051431
14061432 bool GVN::processBlock(BasicBlock* BB) {
1407 DomTreeNode* DTN = DT->getNode(BB);
14081433 // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
14091434 // incrementing BI before processing an instruction).
14101435 SmallVector toErase;
14111436 bool changed_function = false;
1412
1413 if (DTN->getIDom())
1414 localAvail[BB] =
1415 new ValueNumberScope(localAvail[DTN->getIDom()->getBlock()]);
1416 else
1417 localAvail[BB] = new ValueNumberScope(0);
14181437
14191438 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
14201439 BI != BE;) {
16061625 bool GVN::iterateOnFunction(Function &F) {
16071626 cleanupGlobalSets();
16081627
1628 for (df_iterator DI = df_begin(DT->getRootNode()),
1629 DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
1630 if (DI->getIDom())
1631 localAvail[DI->getBlock()] =
1632 new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]);
1633 else
1634 localAvail[DI->getBlock()] = new ValueNumberScope(0);
1635 }
1636
16091637 // Top-down walk of the dominator tree
16101638 bool changed = false;
16111639 #if 0
0 ; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {br i1 false}
1
2 @a = external global i32 ; [#uses=7]
3
4 define i32 @foo() nounwind {
5 entry:
6 %0 = load i32* @a, align 4 ; [#uses=1]
7 %1 = icmp eq i32 %0, 4 ; [#uses=1]
8 br i1 %1, label %bb, label %bb1
9
10 bb: ; preds = %entry
11 br label %bb8
12
13 bb1: ; preds = %entry
14 %2 = load i32* @a, align 4 ; [#uses=1]
15 %3 = icmp eq i32 %2, 5 ; [#uses=1]
16 br i1 %3, label %bb2, label %bb3
17
18 bb2: ; preds = %bb1
19 br label %bb8
20
21 bb3: ; preds = %bb1
22 %4 = load i32* @a, align 4 ; [#uses=1]
23 %5 = icmp eq i32 %4, 4 ; [#uses=1]
24 br i1 %5, label %bb4, label %bb5
25
26 bb4: ; preds = %bb3
27 %6 = load i32* @a, align 4 ; [#uses=1]
28 %7 = add i32 %6, 5 ; [#uses=1]
29 br label %bb8
30
31 bb5: ; preds = %bb3
32 %8 = load i32* @a, align 4 ; [#uses=1]
33 %9 = icmp eq i32 %8, 5 ; [#uses=1]
34 br i1 %9, label %bb6, label %bb7
35
36 bb6: ; preds = %bb5
37 %10 = load i32* @a, align 4 ; [#uses=1]
38 %11 = add i32 %10, 4 ; [#uses=1]
39 br label %bb8
40
41 bb7: ; preds = %bb5
42 %12 = load i32* @a, align 4 ; [#uses=1]
43 br label %bb8
44
45 bb8: ; preds = %bb7, %bb6, %bb4, %bb2, %bb
46 %.0 = phi i32 [ %12, %bb7 ], [ %11, %bb6 ], [ %7, %bb4 ], [ 4, %bb2 ], [ 5, %bb ] ; [#uses=1]
47 br label %return
48
49 return: ; preds = %bb8
50 ret i32 %.0
51 }