[llvm] r274324 - LoadStoreVectorizer: improvements: better pointer analysis

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 30 19:16:24 PDT 2016


Author: arsenm
Date: Thu Jun 30 21:16:24 2016
New Revision: 274324

URL: http://llvm.org/viewvc/llvm-project?rev=274324&view=rev
Log:
LoadStoreVectorizer: improvements: better pointer analysis

If OpB has an ADD NSW/NUW, we can use that to prove that adding 1
to OpA won't wrap if OpA + 1 == OpB.

Patch by Fiona Glaser

Modified:
    llvm/trunk/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp

Modified: llvm/trunk/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp?rev=274324&r1=274323&r2=274324&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp Thu Jun 30 21:16:24 2016
@@ -286,20 +286,41 @@ bool Vectorizer::isConsecutiveAccess(Val
   if (!isa<SExtInst>(OpA) && !isa<ZExtInst>(OpA))
     return false;
 
+  bool Signed = isa<SExtInst>(OpA);
+
   OpA = dyn_cast<Instruction>(OpA->getOperand(0));
   OpB = dyn_cast<Instruction>(OpB->getOperand(0));
   if (!OpA || !OpB || OpA->getType() != OpB->getType())
     return false;
 
   // Now we need to prove that adding 1 to OpA won't overflow.
+  bool Safe = false;
+  // First attempt: if OpB is an add with NSW/NUW, and OpB is 1 added to OpA,
+  // we're okay.
+  if (OpB->getOpcode() == Instruction::Add &&
+      isa<ConstantInt>(OpB->getOperand(1)) &&
+      cast<ConstantInt>(OpB->getOperand(1))->getSExtValue() > 0) {
+    if (Signed)
+      Safe = cast<BinaryOperator>(OpB)->hasNoSignedWrap();
+    else
+      Safe = cast<BinaryOperator>(OpB)->hasNoUnsignedWrap();
+  }
+
   unsigned BitWidth = OpA->getType()->getScalarSizeInBits();
-  APInt KnownZero = APInt(BitWidth, 0);
-  APInt KnownOne = APInt(BitWidth, 0);
-  computeKnownBits(OpA, KnownZero, KnownOne, DL, 0, nullptr, OpA, &DT);
+
+  // Second attempt:
   // If any bits are known to be zero other than the sign bit in OpA, we can
   // add 1 to it while guaranteeing no overflow of any sort.
-  KnownZero &= ~APInt::getHighBitsSet(BitWidth, 1);
-  if (KnownZero == 0)
+  if (!Safe) {
+    APInt KnownZero(BitWidth, 0);
+    APInt KnownOne(BitWidth, 0);
+    computeKnownBits(OpA, KnownZero, KnownOne, DL, 0, nullptr, OpA, &DT);
+    KnownZero &= ~APInt::getHighBitsSet(BitWidth, 1);
+    if (KnownZero != 0)
+      Safe = true;
+  }
+
+  if (!Safe)
     return false;
 
   const SCEV *OffsetSCEVA = SE.getSCEV(OpA);




More information about the llvm-commits mailing list