diff options
author | Chris Lattner <sabre@nondot.org> | 2009-11-27 17:42:22 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-11-27 17:42:22 +0000 |
commit | 3d9823b9cfcd6c3099b680e4dc3c51f0500ab2f9 (patch) | |
tree | 1b11823c46a8ca5c3f8a975c0508ff603cc02b02 /llvm/lib/Analysis/InstructionSimplify.cpp | |
parent | 83a4a9868f7b2a086d30807c9d70a411219fe1f4 (diff) | |
download | bcm5719-llvm-3d9823b9cfcd6c3099b680e4dc3c51f0500ab2f9.tar.gz bcm5719-llvm-3d9823b9cfcd6c3099b680e4dc3c51f0500ab2f9.zip |
factor some logic out of instcombine into a new SimplifyAddInst method.
llvm-svn: 90011
Diffstat (limited to 'llvm/lib/Analysis/InstructionSimplify.cpp')
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 7a7eb6b6829..b53ac13925b 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -21,13 +21,41 @@ using namespace llvm; using namespace llvm::PatternMatch; -/// SimplifyAndInst - Given operands for an And, see if we can +/// SimplifyAddInst - Given operands for an Add, see if we can /// fold the result. If not, this returns null. -Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, +Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const TargetData *TD) { if (Constant *CLHS = dyn_cast<Constant>(Op0)) { if (Constant *CRHS = dyn_cast<Constant>(Op1)) { Constant *Ops[] = { CLHS, CRHS }; + return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), + Ops, 2, TD); + } + + // Canonicalize the constant to the RHS. + std::swap(Op0, Op1); + } + + if (Constant *Op1C = dyn_cast<Constant>(Op1)) { + // X + undef -> undef + if (isa<UndefValue>(Op1C)) + return Op1C; + + // X + 0 --> X + if (Op1C->isNullValue()) + return Op0; + } + + // FIXME: Could pull several more out of instcombine. + return 0; +} + +/// SimplifyAndInst - Given operands for an And, see if we can +/// fold the result. If not, this returns null. +Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) { + if (Constant *CLHS = dyn_cast<Constant>(Op0)) { + if (Constant *CRHS = dyn_cast<Constant>(Op1)) { + Constant *Ops[] = { CLHS, CRHS }; return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), Ops, 2, TD); } @@ -83,8 +111,7 @@ Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, /// SimplifyOrInst - Given operands for an Or, see if we can /// fold the result. If not, this returns null. -Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, - const TargetData *TD) { +Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) { if (Constant *CLHS = dyn_cast<Constant>(Op0)) { if (Constant *CRHS = dyn_cast<Constant>(Op1)) { Constant *Ops[] = { CLHS, CRHS }; @@ -142,8 +169,6 @@ Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, } - - static const Type *GetCompareTy(Value *Op) { return CmpInst::makeCmpResultType(Op->getType()); } @@ -327,6 +352,10 @@ Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) { switch (I->getOpcode()) { default: return ConstantFoldInstruction(I, TD); + case Instruction::Add: + return SimplifyAddInst(I->getOperand(0), I->getOperand(1), + cast<BinaryOperator>(I)->hasNoSignedWrap(), + cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD); case Instruction::And: return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD); case Instruction::Or: |