llvm.org GIT mirror llvm / 88554df
Be more aggressive in doing LoadPRE by tracing backwards when a block only has a single predecessor. Patch by Jakub Staszak. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@72661 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
2 changed file(s) with 71 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
10541054 // that we only have to insert *one* load (which means we're basically moving
10551055 // the load, not inserting a new one).
10561056
1057 // Everything we do here is based on local predecessors of LI's block. If it
1058 // only has one predecessor, bail now.
1057 SmallPtrSet Blockers;
1058 for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
1059 Blockers.insert(UnavailableBlocks[i]);
1060
1061 // Lets find first basic block with more than one predecessor. Walk backwards
1062 // through predecessors if needed.
10591063 BasicBlock *LoadBB = LI->getParent();
1060 if (LoadBB->getSinglePredecessor())
1061 return false;
1064 BasicBlock *TmpBB = LoadBB;
1065
1066 bool isSinglePred = false;
1067 while (TmpBB->getSinglePredecessor()) {
1068 isSinglePred = true;
1069 TmpBB = TmpBB->getSinglePredecessor();
1070 if (!TmpBB) // If haven't found any, bail now.
1071 return false;
1072 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1073 return false;
1074 if (Blockers.count(TmpBB))
1075 return false;
1076 }
1077
1078 assert(TmpBB);
1079 LoadBB = TmpBB;
10621080
10631081 // If we have a repl set with LI itself in it, this means we have a loop where
10641082 // at least one of the values is LI. Since this means that we won't be able
10681086 if (ValuesPerBlock[i].second == LI)
10691087 return false;
10701088
1089 if (isSinglePred) {
1090 bool isHot = false;
1091 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1092 if (Instruction *I = dyn_cast(ValuesPerBlock[i].second))
1093 // "Hot" Instruction is in some loop (because it dominates its dep.
1094 // instruction).
1095 if (DT->dominates(LI, I)) {
1096 isHot = true;
1097 break;
1098 }
1099
1100 // We are interested only in "hot" instructions. We don't want to do any
1101 // mis-optimizations here.
1102 if (!isHot)
1103 return false;
1104 }
1105
10711106 // Okay, we have some hope :). Check to see if the loaded value is fully
10721107 // available in all but one predecessor.
10731108 // FIXME: If we could restructure the CFG, we could make a common pred with
0 ; RUN: llvm-as < %s | opt -gvn -enable-load-pre | llvm-dis | not grep {tmp3 = load}
1
2 define i32 @f(i32* nocapture %p, i32 %n) nounwind {
3 entry:
4 br label %for.cond
5
6 for.cond: ; preds = %for.inc, %entry
7 %i.0 = phi i32 [ 0, %entry ], [ %indvar.next, %for.inc ] ; [#uses=2]
8 %cmp = icmp slt i32 %i.0, %n ; [#uses=1]
9 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
10
11 for.cond.for.end_crit_edge: ; preds = %for.cond
12 br label %for.end
13
14 for.body: ; preds = %for.cond
15 %tmp3 = load i32* %p ; [#uses=1]
16 %dec = add i32 %tmp3, -1 ; [#uses=2]
17 store i32 %dec, i32* %p
18 %cmp6 = icmp slt i32 %dec, 0 ; [#uses=1]
19 br i1 %cmp6, label %for.body.for.end_crit_edge, label %for.inc
20
21 for.body.for.end_crit_edge: ; preds = %for.body
22 br label %for.end
23
24 for.inc: ; preds = %for.body
25 %indvar.next = add i32 %i.0, 1 ; [#uses=1]
26 br label %for.cond
27
28 for.end: ; preds = %for.body.for.end_crit_edge, %for.cond.for.end_crit_edge
29 %tmp9 = load i32* %p ; [#uses=1]
30 ret i32 %tmp9
31 }