[PATCH] D19390: folding compares for distinct allocations
Anna Thomas via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 22 07:17:15 PDT 2016
anna removed rL LLVM as the repository for this revision.
anna updated this revision to Diff 54644.
anna added a comment.
Added tests to catch cases in future when ordering of optimization changes (or changes to `LikeAllocFn` with `LookThroughBitCast` is set to true) which causes `LikeAllocFn` to return true.
Also, improved the check to see if 2 allocations are actually of the same pointer, by stripping through bitcasts and zero GEPs.
http://reviews.llvm.org/D19390
Files:
lib/Transforms/InstCombine/InstructionCombining.cpp
test/Transforms/InstCombine/compare-unescaped.ll
Index: test/Transforms/InstCombine/compare-unescaped.ll
===================================================================
--- test/Transforms/InstCombine/compare-unescaped.ll
+++ test/Transforms/InstCombine/compare-unescaped.ll
@@ -40,3 +40,53 @@
ret i32 %rt
; CHECK: ret i32 %rt
}
+
+define i1 @compare_distinct_mallocs() {
+ %m = call i8* @malloc(i64 4)
+ %n = call i8* @malloc(i64 4)
+ %cmp = icmp eq i8* %m, %n
+ ret i1 %cmp
+ ; CHECK-LABEL: compare_distinct_mallocs
+ ; CHECK: ret i1 false
+}
+
+; the compare is folded to true since the folding compare looks through bitcasts.
+; call to malloc and the bitcast instructions are elided after that since there are no uses of the malloc
+define i1 @compare_samepointer_under_bitcast() {
+ %m = call i8* @malloc(i64 4)
+ %bc = bitcast i8* %m to i32*
+ %bcback = bitcast i32* %bc to i8*
+ %cmp = icmp eq i8* %m, %bcback
+ ret i1 %cmp
+; CHECK-LABEL: compare_samepointer_under_bitcast
+; CHECK: ret i1 true
+}
+
+; the compare is folded to true since the folding compare looks through bitcasts.
+; call to malloc and the bitcast instructions are elided after that since there are no uses of the malloc
+define i1 @compare_samepointer_escaped() {
+ %m = call i8* @malloc(i64 4)
+ %bc = bitcast i8* %m to i32*
+ %bcback = bitcast i32* %bc to i8*
+ %cmp = icmp eq i8* %m, %bcback
+ call void @f() [ "deopt"(i8* %m) ]
+ ret i1 %cmp
+; CHECK-LABEL: compare_samepointer_escaped
+; CHECK_NEXT: %m = call i8* @malloc(i64 4)
+; CHECK_NEXT: call void @f() [ "deopt"(i8* %m) ]
+; CHECK: ret i1 true
+}
+
+; The malloc call for %m cannot be elided since it is used in the call to function f.
+; However, the cmp can be folded to true as %n doesnt escape and %m, %n are distinct allocations
+define i1 @compare_distinct_pointer_escape() {
+ %m = call i8* @malloc(i64 4)
+ %n = call i8* @malloc(i64 4)
+ tail call void @f() [ "deopt"(i8* %m) ]
+ %cmp = icmp ne i8* %m, %n
+ ret i1 %cmp
+; CHECK-LABEL: compare_distinct_pointer_escape
+; CHECK_NEXT: %m = call i8* @malloc(i64 4)
+; CHECK_NEXT: tail call void @f() [ "deopt"(i8* %m) ]
+; CHECK_NEXT: ret i1 true
+}
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1863,12 +1863,14 @@
return nullptr;
}
-static bool isNeverEqualToUnescapedAlloc(Value *V) {
+static bool isNeverEqualToUnescapedAlloc(Value *V,
+ const TargetLibraryInfo *TLI) {
if (isa<ConstantPointerNull>(V))
return true;
if (auto *LI = dyn_cast<LoadInst>(V))
return isa<GlobalVariable>(LI->getPointerOperand());
- return false;
+ // Two distinct allocations will never be equal.
+ return isAllocLikeFn(V, TLI);
}
static bool
@@ -1900,7 +1902,12 @@
if (!ICI->isEquality())
return false;
unsigned OtherIndex = (ICI->getOperand(0) == PI) ? 1 : 0;
- if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex)))
+ // We avoid the case of allocation being compared to itself.
+ // Strip off pointer and zero GEPs
+ if (PI ==
+ ICI->getOperand(OtherIndex)
+ ->stripPointerCastsNoFollowAliases() ||
+ !isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex), TLI))
return false;
Users.emplace_back(I);
continue;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19390.54644.patch
Type: text/x-patch
Size: 3489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160422/5fc91c83/attachment.bin>
More information about the llvm-commits
mailing list