diff options
| author | Dan Gohman <gohman@apple.com> | 2010-02-22 22:43:23 +0000 |
|---|---|---|
| committer | Dan Gohman <gohman@apple.com> | 2010-02-22 22:43:23 +0000 |
| commit | 6c5ac6de5c0605332cdb44218401b854ea8658c3 (patch) | |
| tree | 9fb5a67c83a0383f5428503c49eff4a967236d38 /llvm/lib | |
| parent | 85ef93f13017b0ab86bda84651fb5bf88e3d0455 (diff) | |
| download | bcm5719-llvm-6c5ac6de5c0605332cdb44218401b854ea8658c3.tar.gz bcm5719-llvm-6c5ac6de5c0605332cdb44218401b854ea8658c3.zip | |
Canonicalize ConstantInts to the right operand of commutative
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.
llvm-svn: 96816
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 4 | ||||
| -rw-r--r-- | llvm/lib/VMCore/ConstantFold.cpp | 4 |
2 files changed, 6 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index f3f9a5102f8..1d23fe0b6c9 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/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/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp index c79eb7ef2c9..36b307ce881 100644 --- a/llvm/lib/VMCore/ConstantFold.cpp +++ b/llvm/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. |

