diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-07-20 17:18:45 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-07-20 17:18:45 +0000 |
commit | 683170bf561dcaf79edccac3291319a023c7c0ca (patch) | |
tree | 79db519d7c3e5bd75dc65542cf3166e6096beb11 /llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp | |
parent | 481232e991365c806043cb228590f95f8eab28d0 (diff) | |
download | llvm-683170bf561dcaf79edccac3291319a023c7c0ca.zip llvm-683170bf561dcaf79edccac3291319a023c7c0ca.tar.gz llvm-683170bf561dcaf79edccac3291319a023c7c0ca.tar.bz2 |
move decomposeBitTestICmp() to Transforms/Utils; NFC
As noted in https://reviews.llvm.org/D22537 , we can use this functionality in
visitSelectInstWithICmp() and InstSimplify, but currently we have duplicated
code.
llvm-svn: 276140
Diffstat (limited to 'llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp b/llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp index 0b13005..60ae374 100644 --- a/llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp +++ b/llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp @@ -62,3 +62,47 @@ bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) || (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1)); } + +bool llvm::decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred, + Value *&X, Value *&Y, Value *&Z) { + ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)); + if (!C) + return false; + + switch (I->getPredicate()) { + default: + return false; + case ICmpInst::ICMP_SLT: + // X < 0 is equivalent to (X & SignBit) != 0. + if (!C->isZero()) + return false; + Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth())); + Pred = ICmpInst::ICMP_NE; + break; + case ICmpInst::ICMP_SGT: + // X > -1 is equivalent to (X & SignBit) == 0. + if (!C->isAllOnesValue()) + return false; + Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth())); + Pred = ICmpInst::ICMP_EQ; + break; + case ICmpInst::ICMP_ULT: + // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0. + if (!C->getValue().isPowerOf2()) + return false; + Y = ConstantInt::get(I->getContext(), -C->getValue()); + Pred = ICmpInst::ICMP_EQ; + break; + case ICmpInst::ICMP_UGT: + // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0. + if (!(C->getValue() + 1).isPowerOf2()) + return false; + Y = ConstantInt::get(I->getContext(), ~C->getValue()); + Pred = ICmpInst::ICMP_NE; + break; + } + + X = I->getOperand(0); + Z = ConstantInt::getNullValue(C->getType()); + return true; +} |