@@ -2803,6 +2803,8 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
28032803 break;
28042804 // If we have an add, expand the add operands onto the end of the operands
28052805 // list.
2806+ // CommonFlags = maskFlags(CommonFlags, setFlags(Add->getNoWrapFlags(),
2807+ // static_cast<SCEV::NoWrapFlags>(Ops[Idx].getInt())));
28062808 Ops.erase(Ops.begin()+Idx);
28072809 append_range(Ops, Add->operands());
28082810 DeletedAdd = true;
@@ -3822,13 +3824,14 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
38223824}
38233825
38243826SCEVUse ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3825- ArrayRef<const SCEV *> IndexExprs) {
3826- return getGEPExpr(GEP, SmallVector<SCEVUse>(IndexExprs));
3827+ ArrayRef<const SCEV *> IndexExprs,
3828+ bool UseCtx) {
3829+ return getGEPExpr(GEP, SmallVector<SCEVUse>(IndexExprs), UseCtx);
38273830}
38283831
3829- SCEVUse
3830- ScalarEvolution::getGEPExpr(GEPOperator *GEP ,
3831- const SmallVectorImpl<SCEVUse> &IndexExprs ) {
3832+ SCEVUse ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3833+ const SmallVectorImpl<SCEVUse> &IndexExprs ,
3834+ bool UseCtx ) {
38323835 SCEVUse BaseExpr = getSCEV(GEP->getPointerOperand());
38333836 // getSCEV(Base)->getType() has the same address space as Base->getType()
38343837 // because SCEV::getType() preserves the address space.
@@ -3901,6 +3904,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
39013904 auto GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
39023905 assert(BaseExpr->getType() == GEPExpr->getType() &&
39033906 "GEP should not change type mid-flight.");
3907+ if (UseCtx && BaseWrap != SCEV::FlagNUW && GEP->isInBounds() &&
3908+ isKnownNonNegative(Offset))
3909+ GEPExpr = SCEVUse(&*GEPExpr, 2);
39043910 return GEPExpr;
39053911}
39063912
@@ -4636,12 +4642,12 @@ void ScalarEvolution::insertValueToMap(Value *V, SCEVUse S) {
46364642
46374643/// Return an existing SCEV if it exists, otherwise analyze the expression and
46384644/// create a new one.
4639- SCEVUse ScalarEvolution::getSCEV(Value *V) {
4645+ SCEVUse ScalarEvolution::getSCEV(Value *V, bool UseCtx ) {
46404646 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
46414647
46424648 if (SCEVUse S = getExistingSCEV(V))
46434649 return S;
4644- return createSCEVIter(V);
4650+ return createSCEVIter(V, UseCtx );
46454651}
46464652
46474653SCEVUse ScalarEvolution::getExistingSCEV(Value *V) {
@@ -6334,14 +6340,14 @@ SCEVUse ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond,
63346340
63356341/// Expand GEP instructions into add and multiply operations. This allows them
63366342/// to be analyzed by regular SCEV code.
6337- SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6343+ SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP, bool UseCtx ) {
63386344 assert(GEP->getSourceElementType()->isSized() &&
63396345 "GEP source element type must be sized");
63406346
63416347 SmallVector<SCEVUse, 4> IndexExprs;
63426348 for (Value *Index : GEP->indices())
63436349 IndexExprs.push_back(getSCEV(Index));
6344- return getGEPExpr(GEP, IndexExprs);
6350+ return getGEPExpr(GEP, IndexExprs, UseCtx );
63456351}
63466352
63476353APInt ScalarEvolution::getConstantMultipleImpl(SCEVUse S) {
@@ -7489,7 +7495,7 @@ bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) {
74897495 return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
74907496}
74917497
7492- SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
7498+ SCEVUse ScalarEvolution::createSCEVIter(Value *V, bool UseCtx ) {
74937499 // Worklist item with a Value and a bool indicating whether all operands have
74947500 // been visited already.
74957501 using PointerTy = PointerIntPair<Value *, 1, bool>;
@@ -7508,7 +7514,7 @@ SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
75087514 SCEVUse CreatedSCEV = nullptr;
75097515 // If all operands have been visited already, create the SCEV.
75107516 if (E.getInt()) {
7511- CreatedSCEV = createSCEV(CurV);
7517+ CreatedSCEV = createSCEV(CurV, UseCtx );
75127518 } else {
75137519 // Otherwise get the operands we need to create SCEV's for before creating
75147520 // the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
@@ -7717,7 +7723,7 @@ SCEVUse ScalarEvolution::getOperandsToCreate(Value *V,
77177723 return nullptr;
77187724}
77197725
7720- SCEVUse ScalarEvolution::createSCEV(Value *V) {
7726+ SCEVUse ScalarEvolution::createSCEV(Value *V, bool UseCtx ) {
77217727 if (!isSCEVable(V->getType()))
77227728 return getUnknown(V);
77237729
@@ -8126,7 +8132,7 @@ SCEVUse ScalarEvolution::createSCEV(Value *V) {
81268132 break;
81278133
81288134 case Instruction::GetElementPtr:
8129- return createNodeForGEP(cast<GEPOperator>(U));
8135+ return createNodeForGEP(cast<GEPOperator>(U), UseCtx );
81308136
81318137 case Instruction::PHI:
81328138 return createNodeForPHI(cast<PHINode>(U));
@@ -13952,8 +13958,8 @@ void ScalarEvolution::print(raw_ostream &OS) const {
1395213958 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
1395313959 OS << I << '\n';
1395413960 OS << " --> ";
13955- SCEVUse SV = SE.getSCEV(&I);
13956- SV-> print(OS);
13961+ SCEVUse SV = SE.getSCEV(&I, /*UseCtx=*/true );
13962+ SV. print(OS);
1395713963 if (!isa<SCEVCouldNotCompute>(SV)) {
1395813964 OS << " U: ";
1395913965 SE.getUnsignedRange(SV).print(OS);
0 commit comments