diff options
author | Matt Arsenault <Matthew.Arsenault@amd.com> | 2022-10-26 08:37:56 -0700 |
---|---|---|
committer | Matt Arsenault <arsenm2@gmail.com> | 2022-10-26 10:59:58 -0700 |
commit | f85ce1b236d50ebc1dd5960ce88026138506697a (patch) | |
tree | 66bc130c0ecdebe1fb2d70f7d058abab574bca8c /llvm/lib/IR/ConstantFold.cpp | |
parent | 09ff2e567a5f9bf9f3c82390020a5bd995feeaaf (diff) | |
download | llvm-f85ce1b236d50ebc1dd5960ce88026138506697a.zip llvm-f85ce1b236d50ebc1dd5960ce88026138506697a.tar.gz llvm-f85ce1b236d50ebc1dd5960ce88026138506697a.tar.bz2 |
ConstantFold: Reduce code duplication for checking commuted compare
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 8e7ecfc..74b7d19 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -1579,6 +1579,25 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, return ICmpInst::BAD_ICMP_PREDICATE; } +static Constant *constantFoldCompareGlobalToNull(CmpInst::Predicate Predicate, + Constant *C1, Constant *C2) { + const GlobalValue *GV = dyn_cast<GlobalValue>(C2); + if (!GV || !C1->isNullValue()) + return nullptr; + + // Don't try to evaluate aliases. External weak GV can be null. + if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() && + !NullPointerIsDefined(nullptr /* F */, + GV->getType()->getAddressSpace())) { + if (Predicate == ICmpInst::ICMP_EQ) + return ConstantInt::getFalse(C1->getContext()); + else if (Predicate == ICmpInst::ICMP_NE) + return ConstantInt::getTrue(C1->getContext()); + } + + return nullptr; +} + Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2) { Type *ResultTy; @@ -1618,31 +1637,14 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, } // icmp eq/ne(null,GV) -> false/true - if (C1->isNullValue()) { - if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) - // Don't try to evaluate aliases. External weak GV can be null. - if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() && - !NullPointerIsDefined(nullptr /* F */, - GV->getType()->getAddressSpace())) { - if (Predicate == ICmpInst::ICMP_EQ) - return ConstantInt::getFalse(C1->getContext()); - else if (Predicate == ICmpInst::ICMP_NE) - return ConstantInt::getTrue(C1->getContext()); - } + if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C1, C2)) + return Folded; + // icmp eq/ne(GV,null) -> false/true - } else if (C2->isNullValue()) { - if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) { - // Don't try to evaluate aliases. External weak GV can be null. - if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() && - !NullPointerIsDefined(nullptr /* F */, - GV->getType()->getAddressSpace())) { - if (Predicate == ICmpInst::ICMP_EQ) - return ConstantInt::getFalse(C1->getContext()); - else if (Predicate == ICmpInst::ICMP_NE) - return ConstantInt::getTrue(C1->getContext()); - } - } + if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C2, C1)) + return Folded; + if (C2->isNullValue()) { // The caller is expected to commute the operands if the constant expression // is C2. // C1 >= 0 --> true |