implement rdar://7293527, a trivial instcombine that llvm-gcc
authorChris Lattner <sabre@nondot.org>
Sun, 11 Oct 2009 07:53:15 +0000 (07:53 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 11 Oct 2009 07:53:15 +0000 (07:53 +0000)
gets but clang doesn't, because it is implemented in GCC's
fold routine.

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

lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/mul.ll

index bb0e0996bf27074e7de636e7fbd1889e68cb81dc..06a5660f72ec8b8af6e3f45274d30f897074af4c 100644 (file)
@@ -2661,7 +2661,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
   if (isa<UndefValue>(I.getOperand(1)))              // undef * X -> 0
     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
 
-  // Simplify mul instructions with a constant RHS...
+  // Simplify mul instructions with a constant RHS.
   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
 
@@ -2765,9 +2765,20 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
     }
   }
 
+  /// i1 mul -> i1 and.
   if (I.getType() == Type::getInt1Ty(*Context))
     return BinaryOperator::CreateAnd(Op0, I.getOperand(1));
 
+  // X*(1 << Y) --> X << Y
+  // (1 << Y)*X --> X << Y
+  {
+    Value *Y;
+    if (match(Op0, m_Shl(m_One(), m_Value(Y))))
+      return BinaryOperator::CreateShl(I.getOperand(1), Y);
+    if (match(I.getOperand(1), m_Shl(m_One(), m_Value(Y))))
+      return BinaryOperator::CreateShl(Op0, Y);
+  }
+  
   // If one of the operands of the multiply is a cast from a boolean value, then
   // we know the bool is either zero or one, so this is a 'masking' multiply.
   // See if we can simplify things based on how the boolean was originally
index 7d8593a74ce44c874b9eb1de91c8d6f4ddd37d61..e127efb9dcfd658ebaeb260a34083c4efd1eaca2 100644 (file)
@@ -88,3 +88,11 @@ define <16 x i8> @test14(<16 x i8> %a) {
         %b = mul <16 x i8> %a, zeroinitializer
         ret <16 x i8> %b
 }
+
+; rdar://7293527
+define i32 @test15(i32 %A, i32 %B) {
+entry:
+  %shl = shl i32 1, %B
+  %m = mul i32 %shl, %A
+  ret i32 %m
+}