diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2021-03-08 17:12:12 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2021-03-08 17:12:12 +0100 |
commit | f08148e874088a07b972203a183db00de9c38a70 (patch) | |
tree | 86ea892846340ce03334889eae9186260c1a1b4b /llvm/lib/IR/ConstantFold.cpp | |
parent | 2ef03bc3a83f67a98d82a03a0c0f7ac9caf2426f (diff) | |
download | llvm-f08148e874088a07b972203a183db00de9c38a70.zip llvm-f08148e874088a07b972203a183db00de9c38a70.tar.gz llvm-f08148e874088a07b972203a183db00de9c38a70.tar.bz2 |
[ConstProp] Fix folding of pointer icmp with signed predicates
While @g ugt null is always true (ignoring weak symbols),
@g sgt null is not necessarily the case -- that would imply that
it is forbidden to place globals in the high half of the address
space.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 3903d72..2e0ea4c 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -1838,35 +1838,27 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, // If we are comparing a GEP to a null pointer, check to see if the base // of the GEP equals the null pointer. if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { - if (GV->hasExternalWeakLinkage()) - // Weak linkage GVals could be zero or not. We're comparing that - // to null pointer so its greater-or-equal - return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; - else - // If its not weak linkage, the GVal must have a non-zero address - // so the result is greater-than - return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; + // If its not weak linkage, the GVal must have a non-zero address + // so the result is greater-than + if (!GV->hasExternalWeakLinkage()) + return ICmpInst::ICMP_UGT; } else if (isa<ConstantPointerNull>(CE1Op0)) { // If we are indexing from a null pointer, check to see if we have any // non-zero indices. for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) if (!CE1->getOperand(i)->isNullValue()) // Offsetting from null, must not be equal. - return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; + return ICmpInst::ICMP_UGT; // Only zero indexes from null, must still be zero. return ICmpInst::ICMP_EQ; } // Otherwise, we can't really say if the first operand is null or not. } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { if (isa<ConstantPointerNull>(CE1Op0)) { - if (GV2->hasExternalWeakLinkage()) - // Weak linkage GVals could be zero or not. We're comparing it to - // a null pointer, so its less-or-equal - return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; - else - // If its not weak linkage, the GVal must have a non-zero address - // so the result is less-than - return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; + // If its not weak linkage, the GVal must have a non-zero address + // so the result is less-than + if (!GV2->hasExternalWeakLinkage()) + return ICmpInst::ICMP_ULT; } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { if (GV == GV2) { // If this is a getelementptr of the same global, then it must be @@ -1876,7 +1868,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, assert(CE1->getNumOperands() == 2 && !CE1->getOperand(1)->isNullValue() && "Surprising getelementptr!"); - return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; + return ICmpInst::ICMP_UGT; } else { if (CE1GEP->hasAllZeroIndices()) return areGlobalsPotentiallyEqual(GV, GV2); |