[PATCH] D153700: [InstSimplify] Fold icmp comparing GEPs with global values
hanbum via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 26 03:44:17 PDT 2023
ParkHanbum updated this revision to Diff 534486.
ParkHanbum retitled this revision from "[InstSimplify] Fold icmp between GEP" to "[InstSimplify] Fold icmp comparing GEPs with global values".
ParkHanbum added a comment.
I modified it according to nikic's advice.
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,27 @@
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;
+ dist = dist.abs();
+ if (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.534486.patch
Type: text/x-patch
Size: 2854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230626/f3cefcea/attachment.bin>
More information about the llvm-commits
mailing list