@@ -327,6 +327,10 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
327
327
return BBExecutable.count (BB);
328
328
}
329
329
330
+ // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
331
+ // block to the 'To' basic block is currently feasible.
332
+ bool isEdgeFeasible (BasicBlock *From, BasicBlock *To);
333
+
330
334
std::vector<LatticeVal> getStructLatticeValueFor (Value *V) const {
331
335
std::vector<LatticeVal> StructValues;
332
336
auto *STy = dyn_cast<StructType>(V->getType ());
@@ -531,9 +535,9 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
531
535
532
536
// / markEdgeExecutable - Mark a basic block as executable, adding it to the BB
533
537
// / work list if it is not already executable.
534
- void markEdgeExecutable (BasicBlock *Source, BasicBlock *Dest) {
538
+ bool markEdgeExecutable (BasicBlock *Source, BasicBlock *Dest) {
535
539
if (!KnownFeasibleEdges.insert (Edge (Source, Dest)).second )
536
- return ; // This edge is already known to be executable!
540
+ return false ; // This edge is already known to be executable!
537
541
538
542
if (!MarkBlockExecutable (Dest)) {
539
543
// If the destination is already executable, we just made an *edge*
@@ -545,16 +549,13 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
545
549
for (PHINode &PN : Dest->phis ())
546
550
visitPHINode (PN);
547
551
}
552
+ return true ;
548
553
}
549
554
550
555
// getFeasibleSuccessors - Return a vector of booleans to indicate which
551
556
// successors are reachable from a given terminator instruction.
552
557
void getFeasibleSuccessors (TerminatorInst &TI, SmallVectorImpl<bool > &Succs);
553
558
554
- // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
555
- // block to the 'To' basic block is currently feasible.
556
- bool isEdgeFeasible (BasicBlock *From, BasicBlock *To);
557
-
558
559
// OperandChangedState - This method is invoked on all of the users of an
559
560
// instruction that was just changed state somehow. Based on this
560
561
// information, we need to update the specified user of this instruction.
@@ -705,61 +706,10 @@ void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
705
706
// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
706
707
// block to the 'To' basic block is currently feasible.
707
708
bool SCCPSolver::isEdgeFeasible (BasicBlock *From, BasicBlock *To) {
708
- assert (BBExecutable.count (To) && " Dest should always be alive!" );
709
-
710
- // Make sure the source basic block is executable!!
711
- if (!BBExecutable.count (From)) return false ;
712
-
713
- // Check to make sure this edge itself is actually feasible now.
714
- TerminatorInst *TI = From->getTerminator ();
715
- if (auto *BI = dyn_cast<BranchInst>(TI)) {
716
- if (BI->isUnconditional ())
717
- return true ;
718
-
719
- LatticeVal BCValue = getValueState (BI->getCondition ());
720
-
721
- // Overdefined condition variables mean the branch could go either way,
722
- // undef conditions mean that neither edge is feasible yet.
723
- ConstantInt *CI = BCValue.getConstantInt ();
724
- if (!CI)
725
- return !BCValue.isUnknown ();
726
-
727
- // Constant condition variables mean the branch can only go a single way.
728
- return BI->getSuccessor (CI->isZero ()) == To;
729
- }
730
-
731
- // Unwinding instructions successors are always executable.
732
- if (TI->isExceptional ())
733
- return true ;
734
-
735
- if (auto *SI = dyn_cast<SwitchInst>(TI)) {
736
- if (SI->getNumCases () < 1 )
737
- return true ;
738
-
739
- LatticeVal SCValue = getValueState (SI->getCondition ());
740
- ConstantInt *CI = SCValue.getConstantInt ();
741
-
742
- if (!CI)
743
- return !SCValue.isUnknown ();
744
-
745
- return SI->findCaseValue (CI)->getCaseSuccessor () == To;
746
- }
747
-
748
- // In case of indirect branch and its address is a blockaddress, we mark
749
- // the target as executable.
750
- if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
751
- LatticeVal IBRValue = getValueState (IBR->getAddress ());
752
- BlockAddress *Addr = IBRValue.getBlockAddress ();
753
-
754
- if (!Addr)
755
- return !IBRValue.isUnknown ();
756
-
757
- // At this point, the indirectbr is branching on a blockaddress.
758
- return Addr->getBasicBlock () == To;
759
- }
760
-
761
- LLVM_DEBUG (dbgs () << " Unknown terminator instruction: " << *TI << ' \n ' );
762
- llvm_unreachable (" SCCP: Don't know how to handle this terminator!" );
709
+ // Check if we've called markEdgeExecutable on the edge yet. (We could
710
+ // be more aggressive and try to consider edges which haven't been marked
711
+ // yet, but there isn't any need.)
712
+ return KnownFeasibleEdges.count (Edge (From, To));
763
713
}
764
714
765
715
// visit Implementations - Something changed in this instruction, either an
@@ -1567,11 +1517,14 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1567
1517
}
1568
1518
1569
1519
// Otherwise, it is a branch on a symbolic value which is currently
1570
- // considered to be undef. Handle this by forcing the input value to the
1571
- // branch to false.
1572
- markForcedConstant (BI->getCondition (),
1573
- ConstantInt::getFalse (TI->getContext ()));
1574
- return true ;
1520
+ // considered to be undef. Make sure some edge is executable, so a
1521
+ // branch on "undef" always flows somewhere.
1522
+ // FIXME: Distinguish between dead code and an LLVM "undef" value.
1523
+ BasicBlock *DefaultSuccessor = TI->getSuccessor (1 );
1524
+ if (markEdgeExecutable (&BB, DefaultSuccessor))
1525
+ return true ;
1526
+
1527
+ continue ;
1575
1528
}
1576
1529
1577
1530
if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
@@ -1592,11 +1545,15 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1592
1545
}
1593
1546
1594
1547
// Otherwise, it is a branch on a symbolic value which is currently
1595
- // considered to be undef. Handle this by forcing the input value to the
1596
- // branch to the first successor.
1597
- markForcedConstant (IBR->getAddress (),
1598
- BlockAddress::get (IBR->getSuccessor (0 )));
1599
- return true ;
1548
+ // considered to be undef. Make sure some edge is executable, so a
1549
+ // branch on "undef" always flows somewhere.
1550
+ // FIXME: IndirectBr on "undef" doesn't actually need to go anywhere:
1551
+ // we can assume the branch has undefined behavior instead.
1552
+ BasicBlock *DefaultSuccessor = IBR->getSuccessor (0 );
1553
+ if (markEdgeExecutable (&BB, DefaultSuccessor))
1554
+ return true ;
1555
+
1556
+ continue ;
1600
1557
}
1601
1558
1602
1559
if (auto *SI = dyn_cast<SwitchInst>(TI)) {
@@ -1611,8 +1568,15 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1611
1568
return true ;
1612
1569
}
1613
1570
1614
- markForcedConstant (SI->getCondition (), SI->case_begin ()->getCaseValue ());
1615
- return true ;
1571
+ // Otherwise, it is a branch on a symbolic value which is currently
1572
+ // considered to be undef. Make sure some edge is executable, so a
1573
+ // branch on "undef" always flows somewhere.
1574
+ // FIXME: Distinguish between dead code and an LLVM "undef" value.
1575
+ BasicBlock *DefaultSuccessor = SI->case_begin ()->getCaseSuccessor ();
1576
+ if (markEdgeExecutable (&BB, DefaultSuccessor))
1577
+ return true ;
1578
+
1579
+ continue ;
1616
1580
}
1617
1581
}
1618
1582
@@ -1992,6 +1956,31 @@ bool llvm::runIPSCCP(Module &M, const DataLayout &DL,
1992
1956
if (!I) continue ;
1993
1957
1994
1958
bool Folded = ConstantFoldTerminator (I->getParent ());
1959
+ if (!Folded) {
1960
+ // If the branch can't be folded, we must have forced an edge
1961
+ // for an indeterminate value. Force the terminator to fold
1962
+ // to that edge.
1963
+ Constant *C;
1964
+ BasicBlock *Dest;
1965
+ if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1966
+ Dest = SI->case_begin ()->getCaseSuccessor ();
1967
+ C = SI->case_begin ()->getCaseValue ();
1968
+ } else if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1969
+ Dest = BI->getSuccessor (1 );
1970
+ C = ConstantInt::getFalse (BI->getContext ());
1971
+ } else if (IndirectBrInst *IBR = dyn_cast<IndirectBrInst>(I)) {
1972
+ Dest = IBR->getSuccessor (0 );
1973
+ C = BlockAddress::get (IBR->getSuccessor (0 ));
1974
+ } else {
1975
+ llvm_unreachable (" Unexpected terminator instruction" );
1976
+ }
1977
+ assert (Solver.isEdgeFeasible (I->getParent (), Dest) &&
1978
+ " Didn't find feasible edge?" );
1979
+ (void )Dest;
1980
+
1981
+ I->setOperand (0 , C);
1982
+ Folded = ConstantFoldTerminator (I->getParent ());
1983
+ }
1995
1984
assert (Folded &&
1996
1985
" Expect TermInst on constantint or blockaddress to be folded" );
1997
1986
(void ) Folded;
0 commit comments