[SLSR] handles non-canonicalized Mul candidates
authorJingyue Wu <jingyue@google.com>
Wed, 13 May 2015 00:03:17 +0000 (00:03 +0000)
committerJingyue Wu <jingyue@google.com>
Wed, 13 May 2015 00:03:17 +0000 (00:03 +0000)
such as (2 + B) * S.

Tested by @non_canonicalized in slsr-mul.ll

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237216 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
test/Transforms/StraightLineStrengthReduce/slsr-mul.ll

index afe0c9d2fc4cf68ab0ae02277e46f9f72e2ad182..a68a828736bf2763f5cf0c9f61b570ed11c47cf0 100644 (file)
@@ -408,8 +408,8 @@ void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForMul(
     Value *LHS, Value *RHS, Instruction *I) {
   Value *B = nullptr;
   ConstantInt *Idx = nullptr;
-  // Only handle the canonical operand ordering.
-  if (match(LHS, m_Add(m_Value(B), m_ConstantInt(Idx)))) {
+  if (match(LHS, m_Add(m_Value(B), m_ConstantInt(Idx))) ||
+      match(LHS, m_Add(m_ConstantInt(Idx), m_Value(B)))) {
     // If LHS is in the form of "Base + Index", then I is in the form of
     // "(Base + Index) * RHS".
     allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(B), Idx, RHS, I);
index 97e68d5bf51f1e5e7a46978e1912f447dbdf4d85..9b9fc9de4ff35674443319eea4c13d9876ba2fcd 100644 (file)
@@ -23,6 +23,27 @@ define void @slsr1(i32 %b, i32 %s) {
   ret void
 }
 
+define void @non_canonicalized(i32 %b, i32 %s) {
+; CHECK-LABEL: @non_canonicalized(
+  ; foo(b * s);
+  %mul0 = mul i32 %b, %s
+; CHECK: mul i32
+; CHECK-NOT: mul i32
+  call void @foo(i32 %mul0)
+
+  ; foo((1 + b) * s);
+  %b1 = add i32 1, %b
+  %mul1 = mul i32 %b1, %s
+  call void @foo(i32 %mul1)
+
+  ; foo((2 + b) * s);
+  %b2 = add i32 2, %b
+  %mul2 = mul i32 %b2, %s
+  call void @foo(i32 %mul2)
+
+  ret void
+}
+
 ; foo(a * b)
 ; foo((a + 1) * b)
 ; foo(a * (b + 1))