[llvm] r211258 - InstCombine: Stop two transforms dueling

David Majnemer david.majnemer at gmail.com
Thu Jun 19 00:14:33 PDT 2014


Author: majnemer
Date: Thu Jun 19 02:14:33 2014
New Revision: 211258

URL: http://llvm.org/viewvc/llvm-project?rev=211258&view=rev
Log:
InstCombine: Stop two transforms dueling

InstCombineMulDivRem has:
// Canonicalize (X+C1)*CI -> X*CI+C1*CI.

InstCombineAddSub has:
// W*X + Y*Z --> W * (X+Z)  iff W == Y

These two transforms could fight with each other if C1*CI would not fold
away to something simpler than a ConstantExpr mul.

The InstCombineMulDivRem transform only acted on ConstantInts until
r199602 when it was changed to operate on all Constants in order to
let it fire on ConstantVectors.

To fix this, make this transform more careful by checking to see if we
actually folded away C1*CI.

This fixes PR20079.

Added:
    llvm/trunk/test/Transforms/InstCombine/pr20079.ll
Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=211258&r1=211257&r2=211258&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Thu Jun 19 02:14:33 2014
@@ -203,8 +203,11 @@ Instruction *InstCombiner::visitMul(Bina
       Value *X;
       Constant *C1;
       if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
-        Value *Add = Builder->CreateMul(X, Op1);
-        return BinaryOperator::CreateAdd(Add, Builder->CreateMul(C1, Op1));
+        Value *Mul = Builder->CreateMul(C1, Op1);
+        // Only go forward with the transform if C1*CI simplifies to a tidier
+        // constant.
+        if (!match(Mul, m_Mul(m_Value(), m_Value())))
+          return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
       }
     }
   }

Added: llvm/trunk/test/Transforms/InstCombine/pr20079.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/pr20079.ll?rev=211258&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/pr20079.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/pr20079.ll Thu Jun 19 02:14:33 2014
@@ -0,0 +1,13 @@
+; RUN: opt -S -instcombine < %s | FileCheck %s
+ at b = internal global [1 x i32] zeroinitializer, align 4
+ at c = internal global i32 0, align 4
+
+; CHECK-LABEL: @fn1
+; CHECK: [[ADD:%.*]] = add i32 %a, -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[ADD]], sub (i32 0, i32 zext (i1 icmp eq (i32* getelementptr inbounds ([1 x i32]* @b, i64 0, i64 0), i32* @c) to i32))
+; CHECK-NEXT: ret i32 [[AND]]
+define i32 @fn1(i32 %a) {
+  %xor = add i32 %a, -1
+  %mul = mul nsw i32 %xor, zext (i1 icmp eq (i32* getelementptr inbounds ([1 x i32]* @b, i64 0, i64 0), i32* @c) to i32)
+  ret i32 %mul
+}





More information about the llvm-commits mailing list