[llvm] r234180 - [SLSR] consider &B[S << i] as &B[(1 << i) * S]
Jingyue Wu
jingyue at google.com
Mon Apr 6 10:15:48 PDT 2015
Author: jingyue
Date: Mon Apr 6 12:15:48 2015
New Revision: 234180
URL: http://llvm.org/viewvc/llvm-project?rev=234180&view=rev
Log:
[SLSR] consider &B[S << i] as &B[(1 << i) * S]
Summary: This reduces handling &B[(1 << i) * s] to handling &B[i * S].
Test Plan: slsr-gep.ll
Reviewers: meheff
Subscribers: sanjoy, llvm-commits
Differential Revision: http://reviews.llvm.org/D8837
Modified:
llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
Modified: llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp?rev=234180&r1=234179&r2=234180&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp Mon Apr 6 12:15:48 2015
@@ -353,8 +353,6 @@ void StraightLineStrengthReduce::factorA
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::factorA
// 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);
}
}
Modified: llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll?rev=234180&r1=234179&r2=234180&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll (original)
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll Mon Apr 6 12:15:48 2015
@@ -14,7 +14,7 @@ define i32 @slsr_gep(i32* %input, i64 %s
%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 @@ define i32 @slsr_gep_sext(i32* %input, i
%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 @@ define i32 @slsr_gep_2d([10 x [5 x i32]]
%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 @@ define i64 @slsr_gep_uglygep([10 x [5 x
%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 @@ define i32 @slsr_out_of_bounds_gep(i32*
%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
More information about the llvm-commits
mailing list