summaryrefslogtreecommitdiff
path: root/lib/VMCore/ConstantFold.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-01-12 18:42:52 +0000
committerChris Lattner <sabre@nondot.org>2007-01-12 18:42:52 +0000
commitd333d906734088b074806721ee16aa68359e41c3 (patch)
tree538f692287665af724f673d57b2568eec08f7f70 /lib/VMCore/ConstantFold.cpp
parentf6e7bb4a5ef6209b76a696d9afa7ccb85e17d30e (diff)
downloadllvm-d333d906734088b074806721ee16aa68359e41c3.tar.gz
llvm-d333d906734088b074806721ee16aa68359e41c3.tar.bz2
llvm-d333d906734088b074806721ee16aa68359e41c3.tar.xz
Remove a bunch of duplicated code. Among other things, this fixes
constant folding of signed comparisons of bool. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33134 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r--lib/VMCore/ConstantFold.cpp143
1 files changed, 50 insertions, 93 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 8aeca7b47a..dcd8657bd4 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -554,71 +554,55 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
// so look at directly computing the value.
if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
- if (CI1->getType() == Type::Int1Ty && CI2->getType() == Type::Int1Ty) {
- switch (Opcode) {
- default:
- break;
- case Instruction::And:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() & CI2->getZExtValue());
- case Instruction::Or:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() | CI2->getZExtValue());
- case Instruction::Xor:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() ^ CI2->getZExtValue());
- }
- } else {
- uint64_t C1Val = CI1->getZExtValue();
- uint64_t C2Val = CI2->getZExtValue();
- switch (Opcode) {
- default:
- break;
- case Instruction::Add:
- return ConstantInt::get(C1->getType(), C1Val + C2Val);
- case Instruction::Sub:
- return ConstantInt::get(C1->getType(), C1Val - C2Val);
- case Instruction::Mul:
- return ConstantInt::get(C1->getType(), C1Val * C2Val);
- case Instruction::UDiv:
- if (CI2->isNullValue()) // X / 0 -> can't fold
- return 0;
- return ConstantInt::get(C1->getType(), C1Val / C2Val);
- case Instruction::SDiv:
- if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
- if (CI2->isAllOnesValue() &&
- (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
- (CI1->getSExtValue() == INT64_MIN)) ||
- (CI1->getSExtValue() == -CI1->getSExtValue())))
- return 0; // MIN_INT / -1 -> overflow
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() / CI2->getSExtValue());
- case Instruction::URem:
- if (C2->isNullValue()) return 0; // X / 0 -> can't fold
- return ConstantInt::get(C1->getType(), C1Val % C2Val);
- case Instruction::SRem:
- if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
- if (CI2->isAllOnesValue() &&
- (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
- (CI1->getSExtValue() == INT64_MIN)) ||
- (CI1->getSExtValue() == -CI1->getSExtValue())))
- return 0; // MIN_INT % -1 -> overflow
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() % CI2->getSExtValue());
- case Instruction::And:
- return ConstantInt::get(C1->getType(), C1Val & C2Val);
- case Instruction::Or:
- return ConstantInt::get(C1->getType(), C1Val | C2Val);
- case Instruction::Xor:
- return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
- case Instruction::Shl:
- return ConstantInt::get(C1->getType(), C1Val << C2Val);
- case Instruction::LShr:
- return ConstantInt::get(C1->getType(), C1Val >> C2Val);
- case Instruction::AShr:
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() >> C2Val);
- }
+ uint64_t C1Val = CI1->getZExtValue();
+ uint64_t C2Val = CI2->getZExtValue();
+ switch (Opcode) {
+ default:
+ break;
+ case Instruction::Add:
+ return ConstantInt::get(C1->getType(), C1Val + C2Val);
+ case Instruction::Sub:
+ return ConstantInt::get(C1->getType(), C1Val - C2Val);
+ case Instruction::Mul:
+ return ConstantInt::get(C1->getType(), C1Val * C2Val);
+ case Instruction::UDiv:
+ if (CI2->isNullValue()) // X / 0 -> can't fold
+ return 0;
+ return ConstantInt::get(C1->getType(), C1Val / C2Val);
+ case Instruction::SDiv:
+ if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
+ if (CI2->isAllOnesValue() &&
+ (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
+ (CI1->getSExtValue() == INT64_MIN)) ||
+ (CI1->getSExtValue() == -CI1->getSExtValue())))
+ return 0; // MIN_INT / -1 -> overflow
+ return ConstantInt::get(C1->getType(),
+ CI1->getSExtValue() / CI2->getSExtValue());
+ case Instruction::URem:
+ if (C2->isNullValue()) return 0; // X / 0 -> can't fold
+ return ConstantInt::get(C1->getType(), C1Val % C2Val);
+ case Instruction::SRem:
+ if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
+ if (CI2->isAllOnesValue() &&
+ (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
+ (CI1->getSExtValue() == INT64_MIN)) ||
+ (CI1->getSExtValue() == -CI1->getSExtValue())))
+ return 0; // MIN_INT % -1 -> overflow
+ return ConstantInt::get(C1->getType(),
+ CI1->getSExtValue() % CI2->getSExtValue());
+ case Instruction::And:
+ return ConstantInt::get(C1->getType(), C1Val & C2Val);
+ case Instruction::Or:
+ return ConstantInt::get(C1->getType(), C1Val | C2Val);
+ case Instruction::Xor:
+ return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
+ case Instruction::Shl:
+ return ConstantInt::get(C1->getType(), C1Val << C2Val);
+ case Instruction::LShr:
+ return ConstantInt::get(C1->getType(), C1Val >> C2Val);
+ case Instruction::AShr:
+ return ConstantInt::get(C1->getType(),
+ CI1->getSExtValue() >> C2Val);
}
}
} else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
@@ -1059,34 +1043,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
return ConstantInt::getTrue();
}
- if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2) &&
- C1->getType() == Type::Int1Ty && C2->getType() == Type::Int1Ty) {
- bool C1Val = cast<ConstantInt>(C1)->getZExtValue();
- bool C2Val = cast<ConstantInt>(C2)->getZExtValue();
- switch (pred) {
- default: assert(0 && "Invalid ICmp Predicate"); return 0;
- case ICmpInst::ICMP_EQ:
- return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
- case ICmpInst::ICMP_NE:
- return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
- case ICmpInst::ICMP_ULT:
- return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
- case ICmpInst::ICMP_UGT:
- return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
- case ICmpInst::ICMP_ULE:
- return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
- case ICmpInst::ICMP_UGE:
- return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
- case ICmpInst::ICMP_SLT:
- return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
- case ICmpInst::ICMP_SGT:
- return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
- case ICmpInst::ICMP_SLE:
- return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
- case ICmpInst::ICMP_SGE:
- return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
- }
- } else if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
+ if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
if (ICmpInst::isSignedPredicate(ICmpInst::Predicate(pred))) {
int64_t V1 = cast<ConstantInt>(C1)->getSExtValue();
int64_t V2 = cast<ConstantInt>(C2)->getSExtValue();