Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit f15907f

Browse files
committed
SimplifyCFG: Add CostRemaining parameter to DominatesMergePoint
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130527 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8a70192 commit f15907f

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,20 @@ static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
201201
/// which works well enough for us.
202202
///
203203
/// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
204-
/// see if V (which must be an instruction) is cheap to compute and is
205-
/// non-trapping. If both are true, the instruction is inserted into the set
206-
/// and true is returned.
204+
/// see if V (which must be an instruction) and its recursive operands
205+
/// that do not dominate BB have a combined cost lower than CostRemaining and
206+
/// are non-trapping. If both are true, the instruction is inserted into the
207+
/// set and true is returned.
208+
///
209+
/// The cost for most non-trapping instructions is defined as 1 except for
210+
/// Select whose cost is 2.
211+
///
212+
/// After this function returns, CostRemaining is decreased by the cost of
213+
/// V plus its non-dominating operands. If that cost is greater than
214+
/// CostRemaining, false is returned and CostRemaining is undefined.
207215
static bool DominatesMergePoint(Value *V, BasicBlock *BB,
208-
SmallPtrSet<Instruction*, 4> *AggressiveInsts) {
216+
SmallPtrSet<Instruction*, 4> *AggressiveInsts,
217+
unsigned &CostRemaining) {
209218
Instruction *I = dyn_cast<Instruction>(V);
210219
if (!I) {
211220
// Non-instructions all dominate instructions, but not all constantexprs
@@ -232,12 +241,17 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
232241
// instructions in the 'if region'.
233242
if (AggressiveInsts == 0) return false;
234243

244+
// If we have seen this instruction before, don't count it again.
245+
if (AggressiveInsts->count(I)) return true;
246+
235247
// Okay, it looks like the instruction IS in the "condition". Check to
236248
// see if it's a cheap instruction to unconditionally compute, and if it
237249
// only uses stuff defined outside of the condition. If so, hoist it out.
238250
if (!I->isSafeToSpeculativelyExecute())
239251
return false;
240252

253+
unsigned Cost = 0;
254+
241255
switch (I->getOpcode()) {
242256
default: return false; // Cannot hoist this out safely.
243257
case Instruction::Load:
@@ -246,11 +260,13 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
246260
// predecessor.
247261
if (PBB->getFirstNonPHIOrDbg() != I)
248262
return false;
263+
Cost = 1;
249264
break;
250265
case Instruction::GetElementPtr:
251266
// GEPs are cheap if all indices are constant.
252267
if (!cast<GetElementPtrInst>(I)->hasAllConstantIndices())
253268
return false;
269+
Cost = 1;
254270
break;
255271
case Instruction::Add:
256272
case Instruction::Sub:
@@ -264,13 +280,23 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
264280
case Instruction::Trunc:
265281
case Instruction::ZExt:
266282
case Instruction::SExt:
283+
Cost = 1;
267284
break; // These are all cheap and non-trapping instructions.
285+
286+
case Instruction::Select:
287+
Cost = 2;
288+
break;
268289
}
269290

270-
// Okay, we can only really hoist these out if their operands are not
271-
// defined in the conditional region.
291+
if (Cost > CostRemaining)
292+
return false;
293+
294+
CostRemaining -= Cost;
295+
296+
// Okay, we can only really hoist these out if their operands do
297+
// not take us over the cost threshold.
272298
for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
273-
if (!DominatesMergePoint(*i, BB, 0))
299+
if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining))
274300
return false;
275301
// Okay, it's safe to do this! Remember this instruction.
276302
AggressiveInsts->insert(I);
@@ -1220,6 +1246,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
12201246
// instructions. While we are at it, keep track of the instructions
12211247
// that need to be moved to the dominating block.
12221248
SmallPtrSet<Instruction*, 4> AggressiveInsts;
1249+
unsigned MaxCostVal0 = 1, MaxCostVal1 = 1;
12231250

12241251
for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
12251252
PHINode *PN = cast<PHINode>(II++);
@@ -1229,8 +1256,10 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
12291256
continue;
12301257
}
12311258

1232-
if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts) ||
1233-
!DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts))
1259+
if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts,
1260+
MaxCostVal0) ||
1261+
!DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts,
1262+
MaxCostVal1))
12341263
return false;
12351264
}
12361265

test/Transforms/SimplifyCFG/PhiBlockMerge.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
define i32 @test(i1 %a, i1 %b) {
77
; CHECK: br i1 %a
88
br i1 %a, label %M, label %O
9+
; CHECK: O:
910
O: ; preds = %0
1011
; CHECK: select i1 %b, i32 0, i32 1
1112
; CHECK-NOT: phi

0 commit comments

Comments
 (0)