@@ -90,13 +90,12 @@ static cl::opt<bool> SpecializeLiteralConstant(
90
90
" Enable specialization of functions that take a literal constant as an "
91
91
" argument" ));
92
92
93
- bool InstCostVisitor::canEliminateSuccessor (BasicBlock *BB, BasicBlock *Succ,
94
- DenseSet< BasicBlock *> &DeadBlocks) {
93
+ bool InstCostVisitor::canEliminateSuccessor (BasicBlock *BB,
94
+ BasicBlock *Succ) const {
95
95
unsigned I = 0 ;
96
- return all_of (predecessors (Succ),
97
- [&I, BB, Succ, &DeadBlocks] (BasicBlock *Pred) {
96
+ return all_of (predecessors (Succ), [&I, BB, Succ, this ](BasicBlock *Pred) {
98
97
return I++ < MaxBlockPredecessors &&
99
- (Pred == BB || Pred == Succ || DeadBlocks. contains (Pred));
98
+ (Pred == BB || Pred == Succ || ! isBlockExecutable (Pred));
100
99
});
101
100
}
102
101
@@ -116,6 +115,7 @@ Cost InstCostVisitor::estimateBasicBlocks(
116
115
// These blocks are considered dead as far as the InstCostVisitor
117
116
// is concerned. They haven't been proven dead yet by the Solver,
118
117
// but may become if we propagate the specialization arguments.
118
+ assert (Solver.isBlockExecutable (BB) && " BB already found dead by IPSCCP!" );
119
119
if (!DeadBlocks.insert (BB).second )
120
120
continue ;
121
121
@@ -134,16 +134,17 @@ Cost InstCostVisitor::estimateBasicBlocks(
134
134
// Keep adding dead successors to the list as long as they are
135
135
// executable and only reachable from dead blocks.
136
136
for (BasicBlock *SuccBB : successors (BB))
137
- if (isBlockExecutable (SuccBB) &&
138
- canEliminateSuccessor (BB, SuccBB, DeadBlocks))
137
+ if (isBlockExecutable (SuccBB) && canEliminateSuccessor (BB, SuccBB))
139
138
WorkList.push_back (SuccBB);
140
139
}
141
140
return CodeSize;
142
141
}
143
142
144
- static Constant *findConstantFor (Value *V, ConstMap &KnownConstants) {
143
+ Constant *InstCostVisitor:: findConstantFor (Value *V) const {
145
144
if (auto *C = dyn_cast<Constant>(V))
146
145
return C;
146
+ if (auto *C = Solver.getConstantOrNull (V))
147
+ return C;
147
148
return KnownConstants.lookup (V);
148
149
}
149
150
@@ -266,7 +267,7 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
266
267
for (const auto &Case : I.cases ()) {
267
268
BasicBlock *BB = Case.getCaseSuccessor ();
268
269
if (BB != Succ && isBlockExecutable (BB) &&
269
- canEliminateSuccessor (I.getParent (), BB, DeadBlocks ))
270
+ canEliminateSuccessor (I.getParent (), BB))
270
271
WorkList.push_back (BB);
271
272
}
272
273
@@ -283,8 +284,7 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
283
284
// Initialize the worklist with the dead successor as long as
284
285
// it is executable and has a unique predecessor.
285
286
SmallVector<BasicBlock *> WorkList;
286
- if (isBlockExecutable (Succ) &&
287
- canEliminateSuccessor (I.getParent (), Succ, DeadBlocks))
287
+ if (isBlockExecutable (Succ) && canEliminateSuccessor (I.getParent (), Succ))
288
288
WorkList.push_back (Succ);
289
289
290
290
return estimateBasicBlocks (WorkList);
@@ -312,10 +312,10 @@ bool InstCostVisitor::discoverTransitivelyIncomingValues(
312
312
313
313
// Disregard self-references and dead incoming values.
314
314
if (auto *Inst = dyn_cast<Instruction>(V))
315
- if (Inst == PN || DeadBlocks. contains (PN->getIncomingBlock (I)))
315
+ if (Inst == PN || ! isBlockExecutable (PN->getIncomingBlock (I)))
316
316
continue ;
317
317
318
- if (Constant *C = findConstantFor (V, KnownConstants )) {
318
+ if (Constant *C = findConstantFor (V)) {
319
319
// Not all incoming values are the same constant. Bail immediately.
320
320
if (C != Const)
321
321
return false ;
@@ -347,10 +347,10 @@ Constant *InstCostVisitor::visitPHINode(PHINode &I) {
347
347
348
348
// Disregard self-references and dead incoming values.
349
349
if (auto *Inst = dyn_cast<Instruction>(V))
350
- if (Inst == &I || DeadBlocks. contains (I.getIncomingBlock (Idx)))
350
+ if (Inst == &I || ! isBlockExecutable (I.getIncomingBlock (Idx)))
351
351
continue ;
352
352
353
- if (Constant *C = findConstantFor (V, KnownConstants )) {
353
+ if (Constant *C = findConstantFor (V)) {
354
354
if (!Const)
355
355
Const = C;
356
356
// Not all incoming values are the same constant. Bail immediately.
@@ -415,7 +415,7 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
415
415
416
416
for (unsigned Idx = 0 , E = I.getNumOperands () - 1 ; Idx != E; ++Idx) {
417
417
Value *V = I.getOperand (Idx);
418
- Constant *C = findConstantFor (V, KnownConstants );
418
+ Constant *C = findConstantFor (V);
419
419
if (!C)
420
420
return nullptr ;
421
421
Operands.push_back (C);
@@ -439,7 +439,7 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
439
439
440
440
for (unsigned Idx = 0 , E = I.getNumOperands (); Idx != E; ++Idx) {
441
441
Value *V = I.getOperand (Idx);
442
- Constant *C = findConstantFor (V, KnownConstants );
442
+ Constant *C = findConstantFor (V);
443
443
if (!C)
444
444
return nullptr ;
445
445
Operands.push_back (C);
@@ -455,9 +455,9 @@ Constant *InstCostVisitor::visitSelectInst(SelectInst &I) {
455
455
if (I.getCondition () == LastVisited->first ) {
456
456
Value *V = LastVisited->second ->isZeroValue () ? I.getFalseValue ()
457
457
: I.getTrueValue ();
458
- return findConstantFor (V, KnownConstants );
458
+ return findConstantFor (V);
459
459
}
460
- if (Constant *Condition = findConstantFor (I.getCondition (), KnownConstants ))
460
+ if (Constant *Condition = findConstantFor (I.getCondition ()))
461
461
if ((I.getTrueValue () == LastVisited->first && Condition->isOneValue ()) ||
462
462
(I.getFalseValue () == LastVisited->first && Condition->isZeroValue ()))
463
463
return LastVisited->second ;
@@ -475,7 +475,7 @@ Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
475
475
Constant *Const = LastVisited->second ;
476
476
bool ConstOnRHS = I.getOperand (1 ) == LastVisited->first ;
477
477
Value *V = ConstOnRHS ? I.getOperand (0 ) : I.getOperand (1 );
478
- Constant *Other = findConstantFor (V, KnownConstants );
478
+ Constant *Other = findConstantFor (V);
479
479
480
480
if (Other) {
481
481
if (ConstOnRHS)
@@ -503,7 +503,7 @@ Constant *InstCostVisitor::visitBinaryOperator(BinaryOperator &I) {
503
503
504
504
bool ConstOnRHS = I.getOperand (1 ) == LastVisited->first ;
505
505
Value *V = ConstOnRHS ? I.getOperand (0 ) : I.getOperand (1 );
506
- Constant *Other = findConstantFor (V, KnownConstants );
506
+ Constant *Other = findConstantFor (V);
507
507
Value *OtherVal = Other ? Other : V;
508
508
Value *ConstVal = LastVisited->second ;
509
509
0 commit comments