@@ -703,6 +703,7 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
703703 // Add U as additional user of V.
704704 void addAdditionalUser (Value *V, User *U) { AdditionalUsers[V].insert (U); }
705705
706+ void handlePredicate (Instruction *I, Value *CopyOf, const PredicateBase *PI);
706707 void handleCallOverdefined (CallBase &CB);
707708 void handleCallResult (CallBase &CB);
708709 void handleCallArguments (CallBase &CB);
@@ -1927,6 +1928,75 @@ void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
19271928 }
19281929}
19291930
1931+ void SCCPInstVisitor::handlePredicate (Instruction *I, Value *CopyOf,
1932+ const PredicateBase *PI) {
1933+ ValueLatticeElement CopyOfVal = getValueState (CopyOf);
1934+ const std::optional<PredicateConstraint> &Constraint = PI->getConstraint ();
1935+ if (!Constraint) {
1936+ mergeInValue (ValueState[I], I, CopyOfVal);
1937+ return ;
1938+ }
1939+
1940+ CmpInst::Predicate Pred = Constraint->Predicate ;
1941+ Value *OtherOp = Constraint->OtherOp ;
1942+
1943+ // Wait until OtherOp is resolved.
1944+ if (getValueState (OtherOp).isUnknown ()) {
1945+ addAdditionalUser (OtherOp, I);
1946+ return ;
1947+ }
1948+
1949+ ValueLatticeElement CondVal = getValueState (OtherOp);
1950+ ValueLatticeElement &IV = ValueState[I];
1951+ if (CondVal.isConstantRange () || CopyOfVal.isConstantRange ()) {
1952+ auto ImposedCR =
1953+ ConstantRange::getFull (DL.getTypeSizeInBits (CopyOf->getType ()));
1954+
1955+ // Get the range imposed by the condition.
1956+ if (CondVal.isConstantRange ())
1957+ ImposedCR = ConstantRange::makeAllowedICmpRegion (
1958+ Pred, CondVal.getConstantRange ());
1959+
1960+ // Combine range info for the original value with the new range from the
1961+ // condition.
1962+ auto CopyOfCR = CopyOfVal.asConstantRange (CopyOf->getType (),
1963+ /* UndefAllowed=*/ true );
1964+ // Treat an unresolved input like a full range.
1965+ if (CopyOfCR.isEmptySet ())
1966+ CopyOfCR = ConstantRange::getFull (CopyOfCR.getBitWidth ());
1967+ auto NewCR = ImposedCR.intersectWith (CopyOfCR);
1968+ // If the existing information is != x, do not use the information from
1969+ // a chained predicate, as the != x information is more likely to be
1970+ // helpful in practice.
1971+ if (!CopyOfCR.contains (NewCR) && CopyOfCR.getSingleMissingElement ())
1972+ NewCR = CopyOfCR;
1973+
1974+ // The new range is based on a branch condition. That guarantees that
1975+ // neither of the compare operands can be undef in the branch targets,
1976+ // unless we have conditions that are always true/false (e.g. icmp ule
1977+ // i32, %a, i32_max). For the latter overdefined/empty range will be
1978+ // inferred, but the branch will get folded accordingly anyways.
1979+ addAdditionalUser (OtherOp, I);
1980+ mergeInValue (
1981+ IV, I, ValueLatticeElement::getRange (NewCR, /* MayIncludeUndef*/ false ));
1982+ return ;
1983+ } else if (Pred == CmpInst::ICMP_EQ &&
1984+ (CondVal.isConstant () || CondVal.isNotConstant ())) {
1985+ // For non-integer values or integer constant expressions, only
1986+ // propagate equal constants or not-constants.
1987+ addAdditionalUser (OtherOp, I);
1988+ mergeInValue (IV, I, CondVal);
1989+ return ;
1990+ } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant ()) {
1991+ // Propagate inequalities.
1992+ addAdditionalUser (OtherOp, I);
1993+ mergeInValue (IV, I, ValueLatticeElement::getNot (CondVal.getConstant ()));
1994+ return ;
1995+ }
1996+
1997+ return (void )mergeInValue (IV, I, CopyOfVal);
1998+ }
1999+
19302000void SCCPInstVisitor::handleCallResult (CallBase &CB) {
19312001 Function *F = CB.getCalledFunction ();
19322002
@@ -1936,77 +2006,10 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
19362006 return ;
19372007
19382008 Value *CopyOf = CB.getOperand (0 );
1939- ValueLatticeElement CopyOfVal = getValueState (CopyOf);
1940- const auto *PI = getPredicateInfoFor (&CB);
2009+ const PredicateBase *PI = getPredicateInfoFor (&CB);
19412010 assert (PI && " Missing predicate info for ssa.copy" );
1942-
1943- const std::optional<PredicateConstraint> &Constraint =
1944- PI->getConstraint ();
1945- if (!Constraint) {
1946- mergeInValue (ValueState[&CB], &CB, CopyOfVal);
1947- return ;
1948- }
1949-
1950- CmpInst::Predicate Pred = Constraint->Predicate ;
1951- Value *OtherOp = Constraint->OtherOp ;
1952-
1953- // Wait until OtherOp is resolved.
1954- if (getValueState (OtherOp).isUnknown ()) {
1955- addAdditionalUser (OtherOp, &CB);
1956- return ;
1957- }
1958-
1959- ValueLatticeElement CondVal = getValueState (OtherOp);
1960- ValueLatticeElement &IV = ValueState[&CB];
1961- if (CondVal.isConstantRange () || CopyOfVal.isConstantRange ()) {
1962- auto ImposedCR =
1963- ConstantRange::getFull (DL.getTypeSizeInBits (CopyOf->getType ()));
1964-
1965- // Get the range imposed by the condition.
1966- if (CondVal.isConstantRange ())
1967- ImposedCR = ConstantRange::makeAllowedICmpRegion (
1968- Pred, CondVal.getConstantRange ());
1969-
1970- // Combine range info for the original value with the new range from the
1971- // condition.
1972- auto CopyOfCR = CopyOfVal.asConstantRange (CopyOf->getType (),
1973- /* UndefAllowed=*/ true );
1974- // Treat an unresolved input like a full range.
1975- if (CopyOfCR.isEmptySet ())
1976- CopyOfCR = ConstantRange::getFull (CopyOfCR.getBitWidth ());
1977- auto NewCR = ImposedCR.intersectWith (CopyOfCR);
1978- // If the existing information is != x, do not use the information from
1979- // a chained predicate, as the != x information is more likely to be
1980- // helpful in practice.
1981- if (!CopyOfCR.contains (NewCR) && CopyOfCR.getSingleMissingElement ())
1982- NewCR = CopyOfCR;
1983-
1984- // The new range is based on a branch condition. That guarantees that
1985- // neither of the compare operands can be undef in the branch targets,
1986- // unless we have conditions that are always true/false (e.g. icmp ule
1987- // i32, %a, i32_max). For the latter overdefined/empty range will be
1988- // inferred, but the branch will get folded accordingly anyways.
1989- addAdditionalUser (OtherOp, &CB);
1990- mergeInValue (
1991- IV, &CB,
1992- ValueLatticeElement::getRange (NewCR, /* MayIncludeUndef*/ false ));
1993- return ;
1994- } else if (Pred == CmpInst::ICMP_EQ &&
1995- (CondVal.isConstant () || CondVal.isNotConstant ())) {
1996- // For non-integer values or integer constant expressions, only
1997- // propagate equal constants or not-constants.
1998- addAdditionalUser (OtherOp, &CB);
1999- mergeInValue (IV, &CB, CondVal);
2000- return ;
2001- } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant ()) {
2002- // Propagate inequalities.
2003- addAdditionalUser (OtherOp, &CB);
2004- mergeInValue (IV, &CB,
2005- ValueLatticeElement::getNot (CondVal.getConstant ()));
2006- return ;
2007- }
2008-
2009- return (void )mergeInValue (IV, &CB, CopyOfVal);
2011+ handlePredicate (&CB, CopyOf, PI);
2012+ return ;
20102013 }
20112014
20122015 if (II->getIntrinsicID () == Intrinsic::vscale) {
0 commit comments