Index: lib/Transforms/Scalar/StraightLineStrengthReduce.cpp =================================================================== --- lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ 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 <getBitWidth(), 1); + ConstantInt *PowerOf2 = + ConstantInt::get(RHS->getContext(), One << RHS->getValue()); + allocateCandidateAndFindBasisForGEP(Base, PowerOf2, LHS, ElementSize, GEP); } } Index: test/Transforms/StraightLineStrengthReduce/slsr-gep.ll =================================================================== --- test/Transforms/StraightLineStrengthReduce/slsr-gep.ll +++ 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