[RewriteStatepointsForGC] Missed review comment from 234651 & build fix
[oota-llvm.git] / lib / Transforms / Scalar / StraightLineStrengthReduce.cpp
index 940ab58f1586b7b604f8d18eb7389accf07369f5..681a70c517d37735b245e01513d63d3eadb91e44 100644 (file)
@@ -353,8 +353,6 @@ void StraightLineStrengthReduce::factorArrayIndex(Value *ArrayIdx,
       ArrayIdx, ElementSize, GEP);
   Value *LHS = nullptr;
   ConstantInt *RHS = nullptr;
-  // TODO: handle shl. e.g., we could treat (S << 2) as (S * 4).
-  //
   // One alternative is matching the SCEV of ArrayIdx instead of ArrayIdx
   // itself. This would allow us to handle the shl case for free. However,
   // matching SCEVs has two issues:
@@ -370,6 +368,13 @@ void StraightLineStrengthReduce::factorArrayIndex(Value *ArrayIdx,
     // SLSR is currently unsafe if i * S may overflow.
     // GEP = Base + sext(LHS *nsw RHS) * ElementSize
     allocateCandidateAndFindBasisForGEP(Base, RHS, LHS, ElementSize, GEP);
+  } else if (match(ArrayIdx, m_NSWShl(m_Value(LHS), m_ConstantInt(RHS)))) {
+    // GEP = Base + sext(LHS <<nsw RHS) * ElementSize
+    //     = Base + sext(LHS *nsw (1 << RHS)) * ElementSize
+    APInt One(RHS->getBitWidth(), 1);
+    ConstantInt *PowerOf2 =
+        ConstantInt::get(RHS->getContext(), One << RHS->getValue());
+    allocateCandidateAndFindBasisForGEP(Base, PowerOf2, LHS, ElementSize, GEP);
   }
 }
 
@@ -480,18 +485,19 @@ void StraightLineStrengthReduce::rewriteCandidateWithBasis(
         Type *CharTy = Type::getInt8PtrTy(Basis.Ins->getContext(), AS);
         Reduced = Builder.CreateBitCast(Basis.Ins, CharTy);
         if (InBounds)
-          Reduced = Builder.CreateInBoundsGEP(Reduced, Bump);
+          Reduced =
+              Builder.CreateInBoundsGEP(Builder.getInt8Ty(), Reduced, Bump);
         else
-          Reduced = Builder.CreateGEP(Reduced, Bump);
+          Reduced = Builder.CreateGEP(Builder.getInt8Ty(), Reduced, Bump);
         Reduced = Builder.CreateBitCast(Reduced, C.Ins->getType());
       } else {
         // C = gep Basis, Bump
         // Canonicalize bump to pointer size.
         Bump = Builder.CreateSExtOrTrunc(Bump, IntPtrTy);
         if (InBounds)
-          Reduced = Builder.CreateInBoundsGEP(Basis.Ins, Bump);
+          Reduced = Builder.CreateInBoundsGEP(nullptr, Basis.Ins, Bump);
         else
-          Reduced = Builder.CreateGEP(Basis.Ins, Bump);
+          Reduced = Builder.CreateGEP(nullptr, Basis.Ins, Bump);
       }
     }
     break;