Index: llvm/lib/IR/ConstantFold.cpp =================================================================== --- llvm/lib/IR/ConstantFold.cpp +++ llvm/lib/IR/ConstantFold.cpp @@ -1547,9 +1547,26 @@ if (isa(CE1Op0) && isa(CE2Op0)) { // Don't know relative ordering, but check for inequality. if (CE1Op0 != CE2Op0) { - if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices()) + DataLayout DL(cast(CE1Op0)->getParent()); + Type *GVT; + unsigned BitWidth; + APInt Dist; + GVT = CE1Op0->getType(); + auto CE1Op0Size = DL.getTypeAllocSize(GVT); + GVT = CE2Op0->getType(); + auto CE2Op0Size = DL.getTypeAllocSize(GVT); + BitWidth = DL.getIndexTypeSizeInBits(CE1Op0->getType()); + APInt CE1GEPOffset(BitWidth, 0); + BitWidth = DL.getIndexTypeSizeInBits(CE2Op0->getType()); + APInt CE2GEPOffset(BitWidth, 0); + CE1GEP->accumulateConstantOffset(DL, CE1GEPOffset); + CE2GEP->accumulateConstantOffset(DL, CE2GEPOffset); + + Dist = CE1GEPOffset - CE2GEPOffset; + if (Dist.isNonNegative() ? Dist.ult(CE1Op0Size) : (-Dist).ult(CE2Op0Size)) return areGlobalsPotentiallyEqual(cast(CE1Op0), cast(CE2Op0)); + return ICmpInst::BAD_ICMP_PREDICATE; } } Index: llvm/test/Transforms/InstSimplify/compare.ll =================================================================== --- llvm/test/Transforms/InstSimplify/compare.ll +++ llvm/test/Transforms/InstSimplify/compare.ll @@ -2778,10 +2778,9 @@ ret i1 %res } -; TODO: Never equal define i1 @globals_offset_inequal() { ; CHECK-LABEL: @globals_offset_inequal( -; CHECK-NEXT: ret i1 icmp ne (ptr getelementptr (i8, ptr @A, i32 1), ptr getelementptr (i8, ptr @B, i32 1)) +; CHECK-NEXT: ret i1 true ; %a.off = getelementptr i8, ptr @A, i32 1 %b.off = getelementptr i8, ptr @B, i32 1 Index: llvm/test/Transforms/InstSimplify/past-the-end.ll =================================================================== --- llvm/test/Transforms/InstSimplify/past-the-end.ll +++ llvm/test/Transforms/InstSimplify/past-the-end.ll @@ -21,13 +21,12 @@ define zeroext i1 @both_past_the_end() { ; CHECK-LABEL: @both_past_the_end( -; CHECK: ret i1 icmp eq (ptr getelementptr inbounds (i32, ptr @opte_a, i32 1), ptr getelementptr inbounds (i32, ptr @opte_b, i32 1)) +; CHECK: ret i1 false ; %x = getelementptr i32, ptr @opte_a, i32 1 %y = getelementptr i32, ptr @opte_b, i32 1 %t = icmp eq ptr %x, %y ret i1 %t - ; TODO: refine this } ; Comparing past-the-end addresses of one global to the base address