summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorEvan Phoenix <evan@fallingsnow.net>2009-10-05 22:53:52 +0000
committerEvan Phoenix <evan@fallingsnow.net>2009-10-05 22:53:52 +0000
commit44e5dbcaf035aa04f000505b39be657b26b10754 (patch)
tree99f15c1e74d813c2b3eed34fcfee994e77e6f6b0 /llvm/lib/Analysis/ConstantFolding.cpp
parent3472ae5bac95965083490b746c7da045054658f8 (diff)
downloadbcm5719-llvm-44e5dbcaf035aa04f000505b39be657b26b10754.tar.gz
bcm5719-llvm-44e5dbcaf035aa04f000505b39be657b26b10754.zip
Extend ConstantFolding to understand signed overflow variants
llvm-svn: 83338
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 1ca7cb05b41..0ce1c24bed6 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -678,6 +678,8 @@ llvm::canConstantFoldCallTo(const Function *F) {
case Intrinsic::cttz:
case Intrinsic::uadd_with_overflow:
case Intrinsic::usub_with_overflow:
+ case Intrinsic::sadd_with_overflow:
+ case Intrinsic::ssub_with_overflow:
return true;
default:
return false;
@@ -902,6 +904,28 @@ llvm::ConstantFoldCall(Function *F,
};
return ConstantStruct::get(F->getContext(), Ops, 2, false);
}
+ case Intrinsic::sadd_with_overflow: {
+ Constant *Res = ConstantExpr::getAdd(Op1, Op2); // result.
+ Constant *Overflow = ConstantExpr::getSelect(
+ ConstantExpr::getICmp(CmpInst::ICMP_SGT,
+ ConstantInt::get(Op1->getType(), 0), Op1),
+ ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op2),
+ ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op2)); // overflow.
+
+ Constant *Ops[] = { Res, Overflow };
+ return ConstantStruct::get(F->getContext(), Ops, 2, false);
+ }
+ case Intrinsic::ssub_with_overflow: {
+ Constant *Res = ConstantExpr::getSub(Op1, Op2); // result.
+ Constant *Overflow = ConstantExpr::getSelect(
+ ConstantExpr::getICmp(CmpInst::ICMP_SGT,
+ ConstantInt::get(Op2->getType(), 0), Op2),
+ ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op1),
+ ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op1)); // overflow.
+
+ Constant *Ops[] = { Res, Overflow };
+ return ConstantStruct::get(F->getContext(), Ops, 2, false);
+ }
}
}
OpenPOWER on IntegriCloud