From f08148e874088a07b972203a183db00de9c38a70 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 8 Mar 2021 17:12:12 +0100 Subject: [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. --- llvm/lib/IR/ConstantFold.cpp | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'llvm/lib/IR/ConstantFold.cpp') 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(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(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(V2)) { if (isa(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(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); -- cgit v1.1