@@ -90,13 +90,12 @@ static cl::opt<bool> SpecializeLiteralConstant(
9090 " Enable specialization of functions that take a literal constant as an "
9191 " argument" ));
9292
93- bool InstCostVisitor::canEliminateSuccessor (BasicBlock *BB, BasicBlock *Succ,
94- DenseSet< BasicBlock *> &DeadBlocks) {
93+ bool InstCostVisitor::canEliminateSuccessor (BasicBlock *BB,
94+ BasicBlock *Succ) const {
9595 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) {
9897 return I++ < MaxBlockPredecessors &&
99- (Pred == BB || Pred == Succ || DeadBlocks. contains (Pred));
98+ (Pred == BB || Pred == Succ || ! isBlockExecutable (Pred));
10099 });
101100}
102101
@@ -116,6 +115,7 @@ Cost InstCostVisitor::estimateBasicBlocks(
116115 // These blocks are considered dead as far as the InstCostVisitor
117116 // is concerned. They haven't been proven dead yet by the Solver,
118117 // but may become if we propagate the specialization arguments.
118+ assert (Solver.isBlockExecutable (BB) && " BB already found dead by IPSCCP!" );
119119 if (!DeadBlocks.insert (BB).second )
120120 continue ;
121121
@@ -134,16 +134,17 @@ Cost InstCostVisitor::estimateBasicBlocks(
134134 // Keep adding dead successors to the list as long as they are
135135 // executable and only reachable from dead blocks.
136136 for (BasicBlock *SuccBB : successors (BB))
137- if (isBlockExecutable (SuccBB) &&
138- canEliminateSuccessor (BB, SuccBB, DeadBlocks))
137+ if (isBlockExecutable (SuccBB) && canEliminateSuccessor (BB, SuccBB))
139138 WorkList.push_back (SuccBB);
140139 }
141140 return CodeSize;
142141}
143142
144- static Constant *findConstantFor (Value *V, ConstMap &KnownConstants) {
143+ Constant *InstCostVisitor:: findConstantFor (Value *V) const {
145144 if (auto *C = dyn_cast<Constant>(V))
146145 return C;
146+ if (auto *C = Solver.getConstantOrNull (V))
147+ return C;
147148 return KnownConstants.lookup (V);
148149}
149150
@@ -266,7 +267,7 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
266267 for (const auto &Case : I.cases ()) {
267268 BasicBlock *BB = Case.getCaseSuccessor ();
268269 if (BB != Succ && isBlockExecutable (BB) &&
269- canEliminateSuccessor (I.getParent (), BB, DeadBlocks ))
270+ canEliminateSuccessor (I.getParent (), BB))
270271 WorkList.push_back (BB);
271272 }
272273
@@ -283,8 +284,7 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
283284 // Initialize the worklist with the dead successor as long as
284285 // it is executable and has a unique predecessor.
285286 SmallVector<BasicBlock *> WorkList;
286- if (isBlockExecutable (Succ) &&
287- canEliminateSuccessor (I.getParent (), Succ, DeadBlocks))
287+ if (isBlockExecutable (Succ) && canEliminateSuccessor (I.getParent (), Succ))
288288 WorkList.push_back (Succ);
289289
290290 return estimateBasicBlocks (WorkList);
@@ -312,10 +312,10 @@ bool InstCostVisitor::discoverTransitivelyIncomingValues(
312312
313313 // Disregard self-references and dead incoming values.
314314 if (auto *Inst = dyn_cast<Instruction>(V))
315- if (Inst == PN || DeadBlocks. contains (PN->getIncomingBlock (I)))
315+ if (Inst == PN || ! isBlockExecutable (PN->getIncomingBlock (I)))
316316 continue ;
317317
318- if (Constant *C = findConstantFor (V, KnownConstants )) {
318+ if (Constant *C = findConstantFor (V)) {
319319 // Not all incoming values are the same constant. Bail immediately.
320320 if (C != Const)
321321 return false ;
@@ -347,10 +347,10 @@ Constant *InstCostVisitor::visitPHINode(PHINode &I) {
347347
348348 // Disregard self-references and dead incoming values.
349349 if (auto *Inst = dyn_cast<Instruction>(V))
350- if (Inst == &I || DeadBlocks. contains (I.getIncomingBlock (Idx)))
350+ if (Inst == &I || ! isBlockExecutable (I.getIncomingBlock (Idx)))
351351 continue ;
352352
353- if (Constant *C = findConstantFor (V, KnownConstants )) {
353+ if (Constant *C = findConstantFor (V)) {
354354 if (!Const)
355355 Const = C;
356356 // Not all incoming values are the same constant. Bail immediately.
@@ -415,7 +415,7 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
415415
416416 for (unsigned Idx = 0 , E = I.getNumOperands () - 1 ; Idx != E; ++Idx) {
417417 Value *V = I.getOperand (Idx);
418- Constant *C = findConstantFor (V, KnownConstants );
418+ Constant *C = findConstantFor (V);
419419 if (!C)
420420 return nullptr ;
421421 Operands.push_back (C);
@@ -439,7 +439,7 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
439439
440440 for (unsigned Idx = 0 , E = I.getNumOperands (); Idx != E; ++Idx) {
441441 Value *V = I.getOperand (Idx);
442- Constant *C = findConstantFor (V, KnownConstants );
442+ Constant *C = findConstantFor (V);
443443 if (!C)
444444 return nullptr ;
445445 Operands.push_back (C);
@@ -455,9 +455,9 @@ Constant *InstCostVisitor::visitSelectInst(SelectInst &I) {
455455 if (I.getCondition () == LastVisited->first ) {
456456 Value *V = LastVisited->second ->isZeroValue () ? I.getFalseValue ()
457457 : I.getTrueValue ();
458- return findConstantFor (V, KnownConstants );
458+ return findConstantFor (V);
459459 }
460- if (Constant *Condition = findConstantFor (I.getCondition (), KnownConstants ))
460+ if (Constant *Condition = findConstantFor (I.getCondition ()))
461461 if ((I.getTrueValue () == LastVisited->first && Condition->isOneValue ()) ||
462462 (I.getFalseValue () == LastVisited->first && Condition->isZeroValue ()))
463463 return LastVisited->second ;
@@ -475,7 +475,7 @@ Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
475475 Constant *Const = LastVisited->second ;
476476 bool ConstOnRHS = I.getOperand (1 ) == LastVisited->first ;
477477 Value *V = ConstOnRHS ? I.getOperand (0 ) : I.getOperand (1 );
478- Constant *Other = findConstantFor (V, KnownConstants );
478+ Constant *Other = findConstantFor (V);
479479
480480 if (Other) {
481481 if (ConstOnRHS)
@@ -503,7 +503,7 @@ Constant *InstCostVisitor::visitBinaryOperator(BinaryOperator &I) {
503503
504504 bool ConstOnRHS = I.getOperand (1 ) == LastVisited->first ;
505505 Value *V = ConstOnRHS ? I.getOperand (0 ) : I.getOperand (1 );
506- Constant *Other = findConstantFor (V, KnownConstants );
506+ Constant *Other = findConstantFor (V);
507507 Value *OtherVal = Other ? Other : V;
508508 Value *ConstVal = LastVisited->second ;
509509
0 commit comments