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 | 178414c3cff00f32c72076db7efb5e0c7b6d81ec (patch) | |
| tree | ef4cae728cff13b65b9dc107e01df7822f55111d /lib/VMCore | |
| parent | f51825e3fb74db0e6c85583a9a662000eb1e8388 (diff) | |
| download | external_llvm-178414c3cff00f32c72076db7efb5e0c7b6d81ec.zip external_llvm-178414c3cff00f32c72076db7efb5e0c7b6d81ec.tar.gz external_llvm-178414c3cff00f32c72076db7efb5e0c7b6d81ec.tar.bz2 | |
Move code out of header file
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2498 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
| -rw-r--r-- | lib/VMCore/ConstantFold.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index ee7d43e..0dae628 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.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 //===----------------------------------------------------------------------===// |
