[PATCH] [SLSR] consider &B[S << i] as &B[(1 << i) * S]

Jingyue Wu jingyue at google.com
Mon Apr 6 10:18:45 PDT 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8837

Files:
  llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
  llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll

Index: llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
===================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
@@ -14,7 +14,7 @@
   %v1 = load i32, i32* %p1
 
   ; v2 = input[s * 2];
-  %s2 = mul nsw i64 %s, 2
+  %s2 = shl nsw i64 %s, 1
   %p2 = getelementptr inbounds i32, i32* %input, i64 %s2
 ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %s
   %v2 = load i32, i32* %p2
@@ -38,7 +38,7 @@
   %v1 = load i32, i32* %p1
 
   ; v2 = input[(long)(s * 2)];
-  %s2 = mul nsw i32 %s, 2
+  %s2 = shl nsw i32 %s, 1
   %t2 = sext i32 %s2 to i64
   %p2 = getelementptr inbounds i32, i32* %input, i64 %t2
 ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %t
@@ -57,7 +57,7 @@
   %v0 = load i32, i32* %p0
 
   ; v1 = input[s * 2][t];
-  %s2 = mul nsw i64 %s, 2
+  %s2 = shl nsw i64 %s, 1
 ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 5
   %p1 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s2, i64 %t
 ; CHECK: %p1 = getelementptr inbounds i32, i32* %p0, i64 [[BUMP]]
@@ -90,7 +90,7 @@
   %v0 = load i64, i64* %p0
 
   ; v1 = input[s * 2][t].f1;
-  %s2 = mul nsw i64 %s, 2
+  %s2 = shl nsw i64 %s, 1
 ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 60
   %p1 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s2, i64 %t, i32 0
 ; CHECK: getelementptr inbounds i8, i8* %{{[0-9]+}}, i64 [[BUMP]]
@@ -121,7 +121,7 @@
   %v1 = load i32, i32* %p1
 
   ; v2 = input[(long)(s * 2)];
-  %s2 = mul nsw i32 %s, 2
+  %s2 = shl nsw i32 %s, 1
   %t2 = sext i32 %s2 to i64
   %p2 = getelementptr i32, i32* %input, i64 %t2
 ; CHECK: %p2 = getelementptr i32, i32* %p1, i64 %t
Index: llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
===================================================================
--- llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -353,8 +353,6 @@
       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 @@
     // 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);
   }
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8837.23283.patch
Type: text/x-patch
Size: 3089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150406/d6cf9851/attachment.bin>


More information about the llvm-commits mailing list