diff options
author | Chris Lattner <sabre@nondot.org> | 2009-10-05 05:26:04 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-10-05 05:26:04 +0000 |
commit | 59d939894b3db5ec69834330e6f4c32c905258fe (patch) | |
tree | 15bfb4befe9244b3a6b61f629df1e4e4fd521469 /llvm/lib/Analysis/ConstantFolding.cpp | |
parent | 351534f9b1e1035303775f8da83d64957fd0f133 (diff) | |
download | bcm5719-llvm-59d939894b3db5ec69834330e6f4c32c905258fe.tar.gz bcm5719-llvm-59d939894b3db5ec69834330e6f4c32c905258fe.zip |
teach the optimizer how to constant fold uadd/usub intrinsics.
llvm-svn: 83295
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index d624deb2c95..16f2c4db4c4 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -677,6 +677,8 @@ llvm::canConstantFoldCallTo(const Function *F) { case Intrinsic::ctpop: case Intrinsic::ctlz: case Intrinsic::cttz: + case Intrinsic::uadd_with_overflow: + case Intrinsic::usub_with_overflow: return true; default: return false; @@ -756,7 +758,7 @@ llvm::ConstantFoldCall(Function *F, if (!F->hasName()) return 0; LLVMContext &Context = F->getContext(); StringRef Name = F->getName(); - + const Type *Ty = F->getReturnType(); if (NumOperands == 1) { if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { @@ -881,6 +883,32 @@ llvm::ConstantFoldCall(Function *F, } return 0; } + + + if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) { + if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) { + switch (F->getIntrinsicID()) { + default: break; + case Intrinsic::uadd_with_overflow: { + Constant *Res = ConstantExpr::getAdd(Op1, Op2); // result. + Constant *Ops[] = { + Res, ConstantExpr::getICmp(CmpInst::ICMP_ULT, Res, Op1) // overflow. + }; + return ConstantStruct::get(F->getContext(), Ops, 2, false); + } + case Intrinsic::usub_with_overflow: { + Constant *Res = ConstantExpr::getSub(Op1, Op2); // result. + Constant *Ops[] = { + Res, ConstantExpr::getICmp(CmpInst::ICMP_UGT, Res, Op1) // overflow. + }; + return ConstantStruct::get(F->getContext(), Ops, 2, false); + } + } + } + + return 0; + } + return 0; } return 0; } |