@@ -2741,6 +2741,8 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
27412741 break;
27422742 // If we have an add, expand the add operands onto the end of the operands
27432743 // list.
2744+ // CommonFlags = maskFlags(CommonFlags, setFlags(Add->getNoWrapFlags(),
2745+ // static_cast<SCEV::NoWrapFlags>(Ops[Idx].getInt())));
27442746 Ops.erase(Ops.begin()+Idx);
27452747 append_range(Ops, Add->operands());
27462748 DeletedAdd = true;
@@ -3759,13 +3761,14 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
37593761}
37603762
37613763SCEVUse ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3762- ArrayRef<const SCEV *> IndexExprs) {
3763- return getGEPExpr(GEP, SmallVector<SCEVUse>(IndexExprs));
3764+ ArrayRef<const SCEV *> IndexExprs,
3765+ bool UseCtx) {
3766+ return getGEPExpr(GEP, SmallVector<SCEVUse>(IndexExprs), UseCtx);
37643767}
37653768
3766- SCEVUse
3767- ScalarEvolution::getGEPExpr(GEPOperator *GEP ,
3768- const SmallVectorImpl<SCEVUse> &IndexExprs ) {
3769+ SCEVUse ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3770+ const SmallVectorImpl<SCEVUse> &IndexExprs ,
3771+ bool UseCtx ) {
37693772 SCEVUse BaseExpr = getSCEV(GEP->getPointerOperand());
37703773 // getSCEV(Base)->getType() has the same address space as Base->getType()
37713774 // because SCEV::getType() preserves the address space.
@@ -3835,6 +3838,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
38353838 auto GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
38363839 assert(BaseExpr->getType() == GEPExpr->getType() &&
38373840 "GEP should not change type mid-flight.");
3841+ if (UseCtx && BaseWrap != SCEV::FlagNUW && GEP->isInBounds() &&
3842+ isKnownNonNegative(Offset))
3843+ GEPExpr = SCEVUse(&*GEPExpr, 2);
38383844 return GEPExpr;
38393845}
38403846
@@ -4587,12 +4593,12 @@ void ScalarEvolution::insertValueToMap(Value *V, SCEVUse S) {
45874593
45884594/// Return an existing SCEV if it exists, otherwise analyze the expression and
45894595/// create a new one.
4590- SCEVUse ScalarEvolution::getSCEV(Value *V) {
4596+ SCEVUse ScalarEvolution::getSCEV(Value *V, bool UseCtx ) {
45914597 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
45924598
45934599 if (SCEVUse S = getExistingSCEV(V))
45944600 return S;
4595- return createSCEVIter(V);
4601+ return createSCEVIter(V, UseCtx );
45964602}
45974603
45984604SCEVUse ScalarEvolution::getExistingSCEV(Value *V) {
@@ -6283,14 +6289,14 @@ SCEVUse ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond,
62836289
62846290/// Expand GEP instructions into add and multiply operations. This allows them
62856291/// to be analyzed by regular SCEV code.
6286- SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6292+ SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP, bool UseCtx ) {
62876293 assert(GEP->getSourceElementType()->isSized() &&
62886294 "GEP source element type must be sized");
62896295
62906296 SmallVector<SCEVUse, 4> IndexExprs;
62916297 for (Value *Index : GEP->indices())
62926298 IndexExprs.push_back(getSCEV(Index));
6293- return getGEPExpr(GEP, IndexExprs);
6299+ return getGEPExpr(GEP, IndexExprs, UseCtx );
62946300}
62956301
62966302APInt ScalarEvolution::getConstantMultipleImpl(SCEVUse S) {
@@ -7440,7 +7446,7 @@ bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) {
74407446 return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
74417447}
74427448
7443- SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
7449+ SCEVUse ScalarEvolution::createSCEVIter(Value *V, bool UseCtx ) {
74447450 // Worklist item with a Value and a bool indicating whether all operands have
74457451 // been visited already.
74467452 using PointerTy = PointerIntPair<Value *, 1, bool>;
@@ -7459,7 +7465,7 @@ SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
74597465 SCEVUse CreatedSCEV = nullptr;
74607466 // If all operands have been visited already, create the SCEV.
74617467 if (E.getInt()) {
7462- CreatedSCEV = createSCEV(CurV);
7468+ CreatedSCEV = createSCEV(CurV, UseCtx );
74637469 } else {
74647470 // Otherwise get the operands we need to create SCEV's for before creating
74657471 // the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
@@ -7668,7 +7674,7 @@ SCEVUse ScalarEvolution::getOperandsToCreate(Value *V,
76687674 return nullptr;
76697675}
76707676
7671- SCEVUse ScalarEvolution::createSCEV(Value *V) {
7677+ SCEVUse ScalarEvolution::createSCEV(Value *V, bool UseCtx ) {
76727678 if (!isSCEVable(V->getType()))
76737679 return getUnknown(V);
76747680
@@ -8077,7 +8083,7 @@ SCEVUse ScalarEvolution::createSCEV(Value *V) {
80778083 break;
80788084
80798085 case Instruction::GetElementPtr:
8080- return createNodeForGEP(cast<GEPOperator>(U));
8086+ return createNodeForGEP(cast<GEPOperator>(U), UseCtx );
80818087
80828088 case Instruction::PHI:
80838089 return createNodeForPHI(cast<PHINode>(U));
@@ -13621,8 +13627,8 @@ void ScalarEvolution::print(raw_ostream &OS) const {
1362113627 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
1362213628 OS << I << '\n';
1362313629 OS << " --> ";
13624- SCEVUse SV = SE.getSCEV(&I);
13625- SV-> print(OS);
13630+ SCEVUse SV = SE.getSCEV(&I, /*UseCtx=*/true );
13631+ SV. print(OS);
1362613632 if (!isa<SCEVCouldNotCompute>(SV)) {
1362713633 OS << " U: ";
1362813634 SE.getUnsignedRange(SV).print(OS);
0 commit comments