From 10483b93ea6d225a0921a6d41abef8a8ebe8b2d5 Mon Sep 17 00:00:00 2001 From: Jingyue Wu Date: Mon, 6 Apr 2015 17:15:48 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@234180 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/StraightLineStrengthReduce.cpp | 9 +++++++-- test/Transforms/StraightLineStrengthReduce/slsr-gep.ll | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp index d2eb0e9fcf5..681a70c517d 100644 --- a/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -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 <getBitWidth(), 1); + ConstantInt *PowerOf2 = + ConstantInt::get(RHS->getContext(), One << RHS->getValue()); + allocateCandidateAndFindBasisForGEP(Base, PowerOf2, LHS, ElementSize, GEP); } } diff --git a/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll b/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll index e22013b7824..b1cdc825e1a 100644 --- a/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll +++ b/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll @@ -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, i32 %s) { %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]]* %input, i64 %s, i64 %t) { %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 %struct.S]]* %input, i64 %s, i64 %t) { %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* %input, i32 %s) { %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