llvm.org GIT mirror llvm / df2f118
Optimize conditional branch on i1 phis with non-constant inputs. This turns: eq: %3 = icmp eq i32 %1, %2 br label %join ne: %4 = icmp ne i32 %1, %2 br label %join join: %5 = phi i1 [%3, %eq], [%4, %ne] br i1 %5, label %yes, label %no => eq: %3 = icmp eq i32 %1, %2 br i1 %3, label %yes, label %no ne: %4 = icmp ne i32 %1, %2 br i1 %4, label %yes, label %no git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69102 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 10 years ago
2 changed file(s) with 81 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
5050 void SimplifyPredecessors(BranchInst *BI);
5151 void SimplifyPredecessors(SwitchInst *SI);
5252 void RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB);
53 bool RevectorBlockTo(BasicBlock *FromBB, Value *Cond, BranchInst *BI);
5354 };
5455 }
5556
159160 // Ok, we have this really simple case, walk the PHI operands, looking for
160161 // constants. Walk from the end to remove operands from the end when
161162 // possible, and to avoid invalidating "i".
162 for (unsigned i = PN->getNumIncomingValues(); i != 0; --i)
163 if (ConstantInt *CB = dyn_cast(PN->getIncomingValue(i-1))) {
164 // If we have a constant, forward the edge from its current to its
165 // ultimate destination.
166 RevectorBlockTo(PN->getIncomingBlock(i-1),
167 BI->getSuccessor(CB->isZero()));
168 ++NumBrThread;
169
170 // If there were two predecessors before this simplification, or if the
171 // PHI node contained all the same value except for the one we just
172 // substituted, the PHI node may be deleted. Don't iterate through it the
173 // last time.
174 if (BI->getCondition() != PN) return;
175 }
163 for (unsigned i = PN->getNumIncomingValues(); i != 0; --i) {
164 Value *InVal = PN->getIncomingValue(i-1);
165 if (!RevectorBlockTo(PN->getIncomingBlock(i-1), InVal, BI))
166 continue;
167
168 ++NumBrThread;
169
170 // If there were two predecessors before this simplification, or if the
171 // PHI node contained all the same value except for the one we just
172 // substituted, the PHI node may be deleted. Don't iterate through it the
173 // last time.
174 if (BI->getCondition() != PN) return;
175 }
176176 }
177177
178178 // SimplifyPredecessors(switch) - We know that SI is switch based on a PHI node
241241
242242 MadeChange = true;
243243 }
244
245 bool CondProp::RevectorBlockTo(BasicBlock *FromBB, Value *Cond, BranchInst *BI){
246 BranchInst *FromBr = cast(FromBB->getTerminator());
247 if (!FromBr->isUnconditional())
248 return false;
249
250 // Get the old block we are threading through.
251 BasicBlock *OldSucc = FromBr->getSuccessor(0);
252
253 // If the condition is a constant, simply revector the unconditional branch at
254 // the end of FromBB to one of the successors of its current successor.
255 if (ConstantInt *CB = dyn_cast(Cond)) {
256 BasicBlock *ToBB = BI->getSuccessor(CB->isZero());
257
258 // OldSucc had multiple successors. If ToBB has multiple predecessors, then
259 // the edge between them would be critical, which we already took care of.
260 // If ToBB has single operand PHI node then take care of it here.
261 FoldSingleEntryPHINodes(ToBB);
262
263 // Update PHI nodes in OldSucc to know that FromBB no longer branches to it.
264 OldSucc->removePredecessor(FromBB);
265
266 // Change FromBr to branch to the new destination.
267 FromBr->setSuccessor(0, ToBB);
268 } else {
269 // Insert the new conditional branch.
270 BranchInst::Create(BI->getSuccessor(0), BI->getSuccessor(1), Cond, FromBr);
271
272 FoldSingleEntryPHINodes(BI->getSuccessor(0));
273 FoldSingleEntryPHINodes(BI->getSuccessor(1));
274
275 // Update PHI nodes in OldSucc to know that FromBB no longer branches to it.
276 OldSucc->removePredecessor(FromBB);
277
278 // Delete the old branch.
279 FromBr->eraseFromParent();
280 }
281
282 MadeChange = true;
283 return true;
284 }
0 ; RUN: llvm-as < %s | opt -condprop | llvm-dis | not grep phi
1
2 define i32 @foo(i1, i32, i32) {
3 prologue:
4 br i1 %0, label %eq, label %ne
5
6 eq: ; preds = %prologue
7 store i32 0, i32* inttoptr (i32 10000 to i32*)
8 %3 = icmp eq i32 %1, %2 ; [#uses=1]
9 br label %join
10
11 ne: ; preds = %prologue
12 %4 = icmp ne i32 %1, %2 ; [#uses=1]
13 br label %join
14
15 join: ; preds = %ne, %eq
16 %5 = phi i1 [ %3, %eq ], [ %4, %ne ] ; [#uses=1]
17 br i1 %5, label %yes, label %no
18
19 yes: ; preds = %join
20 store i32 0, i32* inttoptr (i32 20000 to i32*)
21 ret i32 5
22
23 no: ; preds = %join
24 ret i32 20
25 }