aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Analysis/ConstantFolding.cpp4
-rw-r--r--lib/VMCore/ConstantFold.cpp4
-rw-r--r--test/CodeGen/X86/ptrtoint-constexpr.ll2
3 files changed, 7 insertions, 3 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index f3f9a5102f..1d23fe0b6c 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -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))) {
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index c79eb7ef2c..36b307ce88 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -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.
diff --git a/test/CodeGen/X86/ptrtoint-constexpr.ll b/test/CodeGen/X86/ptrtoint-constexpr.ll
index dd9790568a..d1cb34bec8 100644
--- a/test/CodeGen/X86/ptrtoint-constexpr.ll
+++ b/test/CodeGen/X86/ptrtoint-constexpr.ll
@@ -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))