[PATCH] D153700: [InstSimplify] Fold icmp comparing GEPs with global values
hanbum via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 27 05:26:37 PDT 2023
ParkHanbum updated this revision to Diff 534924.
ParkHanbum added a comment.
updated the code according to nikic's comment.
> This condition isn't wrong, but it's also not as accurate as it could be.
>
> Let's say `LHSSize = 4`, `LHSOffset = 0`, `RHSSize = 2`, `RHSOffset = 2` with `LHSOffset - RHSOffset = -2`. These can be equal.
>
> Then consider `LHSSize = 4`, `LHSOffset = 2`, `RHSSize = 2`, `RHSOffset = 0` with `LHSOffset - RHSOffset = 2`. These cannot be equal.
>
> As you can see, the sign of the result matters and we should not just take the absolute value.
>
> I believe the condition should be `Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize)`.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D153700/new/
https://reviews.llvm.org/D153700
Files:
llvm/lib/IR/ConstantFold.cpp
llvm/test/Transforms/InstSimplify/compare.ll
llvm/test/Transforms/InstSimplify/past-the-end.ll
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
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/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -1547,9 +1547,26 @@
if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
// Don't know relative ordering, but check for inequality.
if (CE1Op0 != CE2Op0) {
- if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
+ DataLayout DL(cast<GlobalValue>(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<GlobalValue>(CE1Op0),
cast<GlobalValue>(CE2Op0));
+
return ICmpInst::BAD_ICMP_PREDICATE;
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153700.534924.patch
Type: text/x-patch
Size: 2847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230627/8cb96ed3/attachment-0001.bin>
More information about the llvm-commits
mailing list