diff options
| author | Chris Lattner <sabre@nondot.org> | 2002-05-06 17:54:27 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2002-05-06 17:54:27 +0000 |
| commit | 9f3077342b47b70da3177f62839c91ef3bd88745 (patch) | |
| tree | e21868104c0d5b7f5619b3ea72d7183d1b0f39fd /llvm/lib/VMCore/ConstantHandling.cpp | |
| parent | 4c3366405aadcfaafd8416413fa17c260a38a57a (diff) | |
| download | bcm5719-llvm-9f3077342b47b70da3177f62839c91ef3bd88745.tar.gz bcm5719-llvm-9f3077342b47b70da3177f62839c91ef3bd88745.zip | |
Move code out of header file
llvm-svn: 2498
Diffstat (limited to 'llvm/lib/VMCore/ConstantHandling.cpp')
| -rw-r--r-- | llvm/lib/VMCore/ConstantHandling.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/ConstantHandling.cpp b/llvm/lib/VMCore/ConstantHandling.cpp index ee7d43e9190..0dae62865c9 100644 --- a/llvm/lib/VMCore/ConstantHandling.cpp +++ b/llvm/lib/VMCore/ConstantHandling.cpp @@ -5,11 +5,92 @@ //===----------------------------------------------------------------------===// #include "llvm/ConstantHandling.h" +#include "llvm/Instruction.h" #include <cmath> AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules", &ConstRules::find)); +// ConstantFoldInstruction - Attempt to constant fold the specified instruction. +// If successful, the constant result is returned, if not, null is returned. +// +Constant *ConstantFoldInstruction(Instruction *I) { + Constant *Op0 = 0; + Constant *Op1 = 0; + + if (I->getNumOperands() != 0) { // Get first operand if it's a constant... + Op0 = dyn_cast<Constant>(I->getOperand(0)); + if (Op0 == 0) return 0; // Not a constant?, can't fold + + if (I->getNumOperands() != 1) { // Get second operand if it's a constant... + Op1 = dyn_cast<Constant>(I->getOperand(1)); + if (Op1 == 0) return 0; // Not a constant?, can't fold + } + } + + switch (I->getOpcode()) { + case Instruction::Cast: + return ConstRules::get(*Op0)->castTo(Op0, I->getType()); + case Instruction::Not: return ~*Op0; + case Instruction::Add: return *Op0 + *Op1; + case Instruction::Sub: return *Op0 - *Op1; + case Instruction::Mul: return *Op0 * *Op1; + case Instruction::Div: return *Op0 / *Op1; + case Instruction::Rem: return *Op0 % *Op1; + + case Instruction::SetEQ: return *Op0 == *Op1; + case Instruction::SetNE: return *Op0 != *Op1; + case Instruction::SetLE: return *Op0 <= *Op1; + case Instruction::SetGE: return *Op0 >= *Op1; + case Instruction::SetLT: return *Op0 < *Op1; + case Instruction::SetGT: return *Op0 > *Op1; + case Instruction::Shl: return *Op0 << *Op1; + case Instruction::Shr: return *Op0 >> *Op1; + default: + return 0; + } +} + +Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) { + return ConstRules::get(*V)->castTo(V, DestTy); +} + +Constant *ConstantFoldUnaryInstruction(unsigned Opcode, const Constant *V) { + switch (Opcode) { + case Instruction::Not: return ~*V; + } + return 0; +} + +Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1, + const Constant *V2) { + switch (Opcode) { + case Instruction::Add: return *V1 + *V2; + case Instruction::Sub: return *V1 - *V2; + case Instruction::Mul: return *V1 * *V2; + case Instruction::Div: return *V1 / *V2; + case Instruction::Rem: return *V1 % *V2; + + case Instruction::SetEQ: return *V1 == *V2; + case Instruction::SetNE: return *V1 != *V2; + case Instruction::SetLE: return *V1 <= *V2; + case Instruction::SetGE: return *V1 >= *V2; + case Instruction::SetLT: return *V1 < *V2; + case Instruction::SetGT: return *V1 > *V2; + } + return 0; +} + +Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1, + const Constant *V2) { + switch (Opcode) { + case Instruction::Shl: return *V1 << *V2; + case Instruction::Shr: return *V1 >> *V2; + default: return 0; + } +} + + //===----------------------------------------------------------------------===// // TemplateRules Class //===----------------------------------------------------------------------===// |

