@@ -2803,6 +2803,8 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
2803
2803
break;
2804
2804
// If we have an add, expand the add operands onto the end of the operands
2805
2805
// list.
2806
+ // CommonFlags = maskFlags(CommonFlags, setFlags(Add->getNoWrapFlags(),
2807
+ // static_cast<SCEV::NoWrapFlags>(Ops[Idx].getInt())));
2806
2808
Ops.erase(Ops.begin()+Idx);
2807
2809
append_range(Ops, Add->operands());
2808
2810
DeletedAdd = true;
@@ -3822,13 +3824,14 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
3822
3824
}
3823
3825
3824
3826
SCEVUse 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);
3827
3830
}
3828
3831
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 ) {
3832
3835
SCEVUse BaseExpr = getSCEV(GEP->getPointerOperand());
3833
3836
// getSCEV(Base)->getType() has the same address space as Base->getType()
3834
3837
// because SCEV::getType() preserves the address space.
@@ -3901,6 +3904,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3901
3904
auto GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
3902
3905
assert(BaseExpr->getType() == GEPExpr->getType() &&
3903
3906
"GEP should not change type mid-flight.");
3907
+ if (UseCtx && BaseWrap != SCEV::FlagNUW && GEP->isInBounds() &&
3908
+ isKnownNonNegative(Offset))
3909
+ GEPExpr = SCEVUse(&*GEPExpr, 2);
3904
3910
return GEPExpr;
3905
3911
}
3906
3912
@@ -4636,12 +4642,12 @@ void ScalarEvolution::insertValueToMap(Value *V, SCEVUse S) {
4636
4642
4637
4643
/// Return an existing SCEV if it exists, otherwise analyze the expression and
4638
4644
/// create a new one.
4639
- SCEVUse ScalarEvolution::getSCEV(Value *V) {
4645
+ SCEVUse ScalarEvolution::getSCEV(Value *V, bool UseCtx ) {
4640
4646
assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
4641
4647
4642
4648
if (SCEVUse S = getExistingSCEV(V))
4643
4649
return S;
4644
- return createSCEVIter(V);
4650
+ return createSCEVIter(V, UseCtx );
4645
4651
}
4646
4652
4647
4653
SCEVUse ScalarEvolution::getExistingSCEV(Value *V) {
@@ -6334,14 +6340,14 @@ SCEVUse ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond,
6334
6340
6335
6341
/// Expand GEP instructions into add and multiply operations. This allows them
6336
6342
/// to be analyzed by regular SCEV code.
6337
- SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6343
+ SCEVUse ScalarEvolution::createNodeForGEP(GEPOperator *GEP, bool UseCtx ) {
6338
6344
assert(GEP->getSourceElementType()->isSized() &&
6339
6345
"GEP source element type must be sized");
6340
6346
6341
6347
SmallVector<SCEVUse, 4> IndexExprs;
6342
6348
for (Value *Index : GEP->indices())
6343
6349
IndexExprs.push_back(getSCEV(Index));
6344
- return getGEPExpr(GEP, IndexExprs);
6350
+ return getGEPExpr(GEP, IndexExprs, UseCtx );
6345
6351
}
6346
6352
6347
6353
APInt ScalarEvolution::getConstantMultipleImpl(SCEVUse S) {
@@ -7489,7 +7495,7 @@ bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) {
7489
7495
return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
7490
7496
}
7491
7497
7492
- SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
7498
+ SCEVUse ScalarEvolution::createSCEVIter(Value *V, bool UseCtx ) {
7493
7499
// Worklist item with a Value and a bool indicating whether all operands have
7494
7500
// been visited already.
7495
7501
using PointerTy = PointerIntPair<Value *, 1, bool>;
@@ -7508,7 +7514,7 @@ SCEVUse ScalarEvolution::createSCEVIter(Value *V) {
7508
7514
SCEVUse CreatedSCEV = nullptr;
7509
7515
// If all operands have been visited already, create the SCEV.
7510
7516
if (E.getInt()) {
7511
- CreatedSCEV = createSCEV(CurV);
7517
+ CreatedSCEV = createSCEV(CurV, UseCtx );
7512
7518
} else {
7513
7519
// Otherwise get the operands we need to create SCEV's for before creating
7514
7520
// the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
@@ -7717,7 +7723,7 @@ SCEVUse ScalarEvolution::getOperandsToCreate(Value *V,
7717
7723
return nullptr;
7718
7724
}
7719
7725
7720
- SCEVUse ScalarEvolution::createSCEV(Value *V) {
7726
+ SCEVUse ScalarEvolution::createSCEV(Value *V, bool UseCtx ) {
7721
7727
if (!isSCEVable(V->getType()))
7722
7728
return getUnknown(V);
7723
7729
@@ -8126,7 +8132,7 @@ SCEVUse ScalarEvolution::createSCEV(Value *V) {
8126
8132
break;
8127
8133
8128
8134
case Instruction::GetElementPtr:
8129
- return createNodeForGEP(cast<GEPOperator>(U));
8135
+ return createNodeForGEP(cast<GEPOperator>(U), UseCtx );
8130
8136
8131
8137
case Instruction::PHI:
8132
8138
return createNodeForPHI(cast<PHINode>(U));
@@ -13952,8 +13958,8 @@ void ScalarEvolution::print(raw_ostream &OS) const {
13952
13958
if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
13953
13959
OS << I << '\n';
13954
13960
OS << " --> ";
13955
- SCEVUse SV = SE.getSCEV(&I);
13956
- SV-> print(OS);
13961
+ SCEVUse SV = SE.getSCEV(&I, /*UseCtx=*/true );
13962
+ SV. print(OS);
13957
13963
if (!isa<SCEVCouldNotCompute>(SV)) {
13958
13964
OS << " U: ";
13959
13965
SE.getUnsignedRange(SV).print(OS);
0 commit comments