@@ -201,11 +201,20 @@ static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
201
201
// / which works well enough for us.
202
202
// /
203
203
// / 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.
207
215
static bool DominatesMergePoint (Value *V, BasicBlock *BB,
208
- SmallPtrSet<Instruction*, 4 > *AggressiveInsts) {
216
+ SmallPtrSet<Instruction*, 4 > *AggressiveInsts,
217
+ unsigned &CostRemaining) {
209
218
Instruction *I = dyn_cast<Instruction>(V);
210
219
if (!I) {
211
220
// Non-instructions all dominate instructions, but not all constantexprs
@@ -232,12 +241,17 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
232
241
// instructions in the 'if region'.
233
242
if (AggressiveInsts == 0 ) return false ;
234
243
244
+ // If we have seen this instruction before, don't count it again.
245
+ if (AggressiveInsts->count (I)) return true ;
246
+
235
247
// Okay, it looks like the instruction IS in the "condition". Check to
236
248
// see if it's a cheap instruction to unconditionally compute, and if it
237
249
// only uses stuff defined outside of the condition. If so, hoist it out.
238
250
if (!I->isSafeToSpeculativelyExecute ())
239
251
return false ;
240
252
253
+ unsigned Cost = 0 ;
254
+
241
255
switch (I->getOpcode ()) {
242
256
default : return false ; // Cannot hoist this out safely.
243
257
case Instruction::Load:
@@ -246,11 +260,13 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
246
260
// predecessor.
247
261
if (PBB->getFirstNonPHIOrDbg () != I)
248
262
return false ;
263
+ Cost = 1 ;
249
264
break ;
250
265
case Instruction::GetElementPtr:
251
266
// GEPs are cheap if all indices are constant.
252
267
if (!cast<GetElementPtrInst>(I)->hasAllConstantIndices ())
253
268
return false ;
269
+ Cost = 1 ;
254
270
break ;
255
271
case Instruction::Add:
256
272
case Instruction::Sub:
@@ -264,13 +280,23 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
264
280
case Instruction::Trunc:
265
281
case Instruction::ZExt:
266
282
case Instruction::SExt:
283
+ Cost = 1 ;
267
284
break ; // These are all cheap and non-trapping instructions.
285
+
286
+ case Instruction::Select:
287
+ Cost = 2 ;
288
+ break ;
268
289
}
269
290
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.
272
298
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 ))
274
300
return false ;
275
301
// Okay, it's safe to do this! Remember this instruction.
276
302
AggressiveInsts->insert (I);
@@ -1220,6 +1246,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
1220
1246
// instructions. While we are at it, keep track of the instructions
1221
1247
// that need to be moved to the dominating block.
1222
1248
SmallPtrSet<Instruction*, 4 > AggressiveInsts;
1249
+ unsigned MaxCostVal0 = 1 , MaxCostVal1 = 1 ;
1223
1250
1224
1251
for (BasicBlock::iterator II = BB->begin (); isa<PHINode>(II);) {
1225
1252
PHINode *PN = cast<PHINode>(II++);
@@ -1229,8 +1256,10 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
1229
1256
continue ;
1230
1257
}
1231
1258
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))
1234
1263
return false ;
1235
1264
}
1236
1265
0 commit comments