@@ -352,16 +352,19 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
352352// / loop-invariant portions of expressions, after considering what
353353// / can be folded using target addressing modes.
354354// /
355- Value *SCEVExpander::expandAddToGEP (const SCEV *Offset, Value *V) {
355+ Value *SCEVExpander::expandAddToGEP (const SCEV *Offset, Value *V,
356+ SCEV::NoWrapFlags Flags) {
356357 assert (!isa<Instruction>(V) ||
357358 SE.DT .dominates (cast<Instruction>(V), &*Builder.GetInsertPoint ()));
358359
359360 Value *Idx = expand (Offset);
361+ GEPNoWrapFlags NW = (Flags & SCEV::FlagNUW) ? GEPNoWrapFlags::noUnsignedWrap ()
362+ : GEPNoWrapFlags::none ();
360363
361364 // Fold a GEP with constant operands.
362365 if (Constant *CLHS = dyn_cast<Constant>(V))
363366 if (Constant *CRHS = dyn_cast<Constant>(Idx))
364- return Builder.CreatePtrAdd (CLHS, CRHS);
367+ return Builder.CreatePtrAdd (CLHS, CRHS, " " , NW );
365368
366369 // Do a quick scan to see if we have this GEP nearby. If so, reuse it.
367370 unsigned ScanLimit = 6 ;
@@ -380,7 +383,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V) {
380383 GEP->getSourceElementType () == Builder.getInt8Ty () &&
381384 GEP->getOperand (1 ) == Idx) {
382385 rememberFlags (GEP);
383- GEP->setNoWrapFlags (GEPNoWrapFlags::none () );
386+ GEP->setNoWrapFlags (GEP-> getNoWrapFlags () & NW );
384387 return &*IP;
385388 }
386389 }
@@ -402,7 +405,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V) {
402405 }
403406
404407 // Emit a GEP.
405- return Builder.CreatePtrAdd (V, Idx, " scevgep" );
408+ return Builder.CreatePtrAdd (V, Idx, " scevgep" , NW );
406409}
407410
408411// / PickMostRelevantLoop - Given two loops pick the one that's most relevant for
@@ -549,7 +552,7 @@ Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
549552 X = SE.getSCEV (U->getValue ());
550553 NewOps.push_back (X);
551554 }
552- Sum = expandAddToGEP (SE.getAddExpr (NewOps), Sum);
555+ Sum = expandAddToGEP (SE.getAddExpr (NewOps), Sum, S-> getNoWrapFlags () );
553556 } else if (Op->isNonConstantNegative ()) {
554557 // Instead of doing a negate and add, just do a subtract.
555558 Value *W = expand (SE.getNegativeSCEV (Op));
@@ -1251,7 +1254,8 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
12511254 if (!S->getStart ()->isZero ()) {
12521255 if (isa<PointerType>(S->getType ())) {
12531256 Value *StartV = expand (SE.getPointerBase (S));
1254- return expandAddToGEP (SE.removePointerBase (S), StartV);
1257+ return expandAddToGEP (SE.removePointerBase (S), StartV,
1258+ S->getNoWrapFlags (SCEV::FlagNUW));
12551259 }
12561260
12571261 SmallVector<const SCEV *, 4 > NewOps (S->operands ());
0 commit comments