@@ -1444,22 +1444,19 @@ void SCCPInstVisitor::solve() {
14441444 }
14451445}
14461446
1447- // / resolvedUndefsIn - While solving the dataflow for a function, we assume
1448- // / that branches on undef values cannot reach any of their successors.
1449- // / However, this is not a safe assumption. After we solve dataflow, this
1450- // / method should be use to handle this. If this returns true, the solver
1451- // / should be rerun.
1447+ // / While solving the dataflow for a function, we don't compute a result for
1448+ // / operations with an undef operand, to allow undef to be lowered to a
1449+ // / constant later. For example, constant folding of "zext i8 undef to i16"
1450+ // / would result in "i16 0", and if undef is later lowered to "i8 1", then the
1451+ // / zext result would become "i16 1" and would result into an overdefined
1452+ // / lattice value once merged with the previous result. Not computing the
1453+ // / result of the zext (treating undef the same as unknown) allows us to handle
1454+ // / a later undef->constant lowering more optimally.
14521455// /
1453- // / This method handles this by finding an unresolved branch and marking it one
1454- // / of the edges from the block as being feasible, even though the condition
1455- // / doesn't say it would otherwise be. This allows SCCP to find the rest of the
1456- // / CFG and only slightly pessimizes the analysis results (by marking one,
1457- // / potentially infeasible, edge feasible). This cannot usefully modify the
1458- // / constraints on the condition of the branch, as that would impact other users
1459- // / of the value.
1460- // /
1461- // / This scan also checks for values that use undefs. It conservatively marks
1462- // / them as overdefined.
1456+ // / However, if the operand remains undef when the solver returns, we do need
1457+ // / to assign some result to the instruction (otherwise we would treat it as
1458+ // / unreachable). For simplicity, we mark any instructions that are still
1459+ // / unknown/undef as overdefined.
14631460bool SCCPInstVisitor::resolvedUndefsIn (Function &F) {
14641461 bool MadeChange = false ;
14651462 for (BasicBlock &BB : F) {
@@ -1520,91 +1517,6 @@ bool SCCPInstVisitor::resolvedUndefsIn(Function &F) {
15201517 markOverdefined (&I);
15211518 MadeChange = true ;
15221519 }
1523-
1524- // Check to see if we have a branch or switch on an undefined value. If so
1525- // we force the branch to go one way or the other to make the successor
1526- // values live. It doesn't really matter which way we force it.
1527- Instruction *TI = BB.getTerminator ();
1528- if (auto *BI = dyn_cast<BranchInst>(TI)) {
1529- if (!BI->isConditional ())
1530- continue ;
1531- if (!getValueState (BI->getCondition ()).isUnknownOrUndef ())
1532- continue ;
1533-
1534- // If the input to SCCP is actually branch on undef, fix the undef to
1535- // false.
1536- if (isa<UndefValue>(BI->getCondition ())) {
1537- BI->setCondition (ConstantInt::getFalse (BI->getContext ()));
1538- markEdgeExecutable (&BB, TI->getSuccessor (1 ));
1539- MadeChange = true ;
1540- continue ;
1541- }
1542-
1543- // Otherwise, it is a branch on a symbolic value which is currently
1544- // considered to be undef. Make sure some edge is executable, so a
1545- // branch on "undef" always flows somewhere.
1546- // FIXME: Distinguish between dead code and an LLVM "undef" value.
1547- BasicBlock *DefaultSuccessor = TI->getSuccessor (1 );
1548- if (markEdgeExecutable (&BB, DefaultSuccessor))
1549- MadeChange = true ;
1550-
1551- continue ;
1552- }
1553-
1554- if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
1555- // Indirect branch with no successor ?. Its ok to assume it branches
1556- // to no target.
1557- if (IBR->getNumSuccessors () < 1 )
1558- continue ;
1559-
1560- if (!getValueState (IBR->getAddress ()).isUnknownOrUndef ())
1561- continue ;
1562-
1563- // If the input to SCCP is actually branch on undef, fix the undef to
1564- // the first successor of the indirect branch.
1565- if (isa<UndefValue>(IBR->getAddress ())) {
1566- IBR->setAddress (BlockAddress::get (IBR->getSuccessor (0 )));
1567- markEdgeExecutable (&BB, IBR->getSuccessor (0 ));
1568- MadeChange = true ;
1569- continue ;
1570- }
1571-
1572- // Otherwise, it is a branch on a symbolic value which is currently
1573- // considered to be undef. Make sure some edge is executable, so a
1574- // branch on "undef" always flows somewhere.
1575- // FIXME: IndirectBr on "undef" doesn't actually need to go anywhere:
1576- // we can assume the branch has undefined behavior instead.
1577- BasicBlock *DefaultSuccessor = IBR->getSuccessor (0 );
1578- if (markEdgeExecutable (&BB, DefaultSuccessor))
1579- MadeChange = true ;
1580-
1581- continue ;
1582- }
1583-
1584- if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1585- if (!SI->getNumCases () ||
1586- !getValueState (SI->getCondition ()).isUnknownOrUndef ())
1587- continue ;
1588-
1589- // If the input to SCCP is actually switch on undef, fix the undef to
1590- // the first constant.
1591- if (isa<UndefValue>(SI->getCondition ())) {
1592- SI->setCondition (SI->case_begin ()->getCaseValue ());
1593- markEdgeExecutable (&BB, SI->case_begin ()->getCaseSuccessor ());
1594- MadeChange = true ;
1595- continue ;
1596- }
1597-
1598- // Otherwise, it is a branch on a symbolic value which is currently
1599- // considered to be undef. Make sure some edge is executable, so a
1600- // branch on "undef" always flows somewhere.
1601- // FIXME: Distinguish between dead code and an LLVM "undef" value.
1602- BasicBlock *DefaultSuccessor = SI->case_begin ()->getCaseSuccessor ();
1603- if (markEdgeExecutable (&BB, DefaultSuccessor))
1604- MadeChange = true ;
1605-
1606- continue ;
1607- }
16081520 }
16091521
16101522 return MadeChange;
0 commit comments