Canonicalize ConstantInts to the right operand of commutative
authorDan Gohman <gohman@apple.com>
Mon, 22 Feb 2010 22:43:23 +0000 (22:43 +0000)
committerDan Gohman <gohman@apple.com>
Mon, 22 Feb 2010 22:43:23 +0000 (22:43 +0000)
operators.

The test difference is just due to the multiplication operands
being commuted (and thus requiring a more elaborate match). In
optimized code, that expression would be folded.

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

lib/Analysis/ConstantFolding.cpp
lib/VMCore/ConstantFold.cpp
test/CodeGen/X86/ptrtoint-constexpr.ll

index f3f9a5102f89e1484c745226ce2e9356b4dbf114..1d23fe0b6c911e0498d139560e99fc4d4467b8d2 100644 (file)
@@ -794,8 +794,8 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
         // it is casted back to a pointer, see if the expression can be
         // converted into a GEP.
         if (CE->getOpcode() == Instruction::Add)
-          if (ConstantInt *L = dyn_cast<ConstantInt>(CE->getOperand(0)))
-            if (ConstantExpr *R = dyn_cast<ConstantExpr>(CE->getOperand(1)))
+          if (ConstantInt *L = dyn_cast<ConstantInt>(CE->getOperand(1)))
+            if (ConstantExpr *R = dyn_cast<ConstantExpr>(CE->getOperand(0)))
               if (R->getOpcode() == Instruction::PtrToInt)
                 if (GlobalVariable *GV =
                       dyn_cast<GlobalVariable>(R->getOperand(0))) {
index c79eb7ef2c9ca69f83cdfcc83620897b3957d78c..36b307ce881d3a4580b661ace018357ae128636b 100644 (file)
@@ -1105,6 +1105,10 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
           return ConstantExpr::getLShr(C1, C2);
       break;
     }
+  } else if (isa<ConstantInt>(C1)) {
+    // If C1 is a ConstantInt and C2 is not, swap the operands.
+    if (Instruction::isCommutative(Opcode))
+      return ConstantExpr::get(Opcode, C2, C1);
   }
 
   // At this point we know neither constant is an UndefValue.
index dd9790568abee3e01fcebb6741fc972b01505577..d1cb34bec8a09b6146b12ecf01f117d16b8d63bf 100644 (file)
@@ -9,6 +9,6 @@
 
 ; CHECK:       .globl x
 ; CHECK: x:
-; CHECK: .quad 3
+; CHECK: .quad ((0+1)&4294967295)*3
 
 @x = global i64 mul (i64 3, i64 ptrtoint (i2* getelementptr (i2* null, i64 1) to i64))