Skip to content

Commit 2b7ae47

Browse files
committed
[MergeICmps] Do not perform the transformation if GEP is used outside of block
Summary: This patch prevents MergeICmps to performn the transformation if the address operand GEP of the load instruction has a use outside of the load's parent block. Without this patch, compiler crashes with the given test case because the use of `%first.i` is still around when the basic block is erased from https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/Scalar/MergeICmps.cpp#L620. I think checking `isUsedOutsideOfBlock` with `GEP` is the original intention of the code, as the checking for `LoadI` is already performed in the same function. This patch is incomplete though, as this makes the pass overly conservative and fails the test `tuple-four-int8.ll`. I believe what needs to be done is checking if GEP has a use outside of block that is not the part of "Comparisons" chain. Submit the patch as of now to prevent compiler crash. Reviewers: courbet, trentxintong Reviewed By: courbet Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D54089 llvm-svn: 346151
1 parent 1cfba9b commit 2b7ae47

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

llvm/lib/Transforms/Scalar/MergeICmps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ BCEAtom visitICmpLoadOperand(Value *const Val) {
9898
Value *const Addr = LoadI->getOperand(0);
9999
if (auto *const GEP = dyn_cast<GetElementPtrInst>(Addr)) {
100100
LLVM_DEBUG(dbgs() << "GEP\n");
101-
if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) {
101+
if (GEP->isUsedOutsideOfBlock(LoadI->getParent())) {
102102
LLVM_DEBUG(dbgs() << "used outside of block\n");
103103
return {};
104104
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -mergeicmps -mtriple=x86_64-unknown-unknown -S | FileCheck %s
3+
4+
%"struct.std::pair" = type { i32, i32 }
5+
6+
; Check that the transformation is avoided when GEP has a use outside of the
7+
; parant block of the load instruction.
8+
9+
define zeroext i32 @opeq1(
10+
; CHECK-LABEL: @opeq1(
11+
; CHECK-NOT: [[MEMCMP:%.*]] = call i32 @memcmp
12+
13+
%"struct.std::pair"* nocapture readonly dereferenceable(16) %a,
14+
%"struct.std::pair"* nocapture readonly dereferenceable(16) %b) local_unnamed_addr #0 {
15+
entry:
16+
%first.i = getelementptr inbounds %"struct.std::pair", %"struct.std::pair"* %a, i64 0, i32 1
17+
%0 = load i32, i32* %first.i, align 4
18+
%first1.i = getelementptr inbounds %"struct.std::pair", %"struct.std::pair"* %b, i64 0, i32 1
19+
%1 = load i32, i32* %first1.i, align 4
20+
%cmp.i = icmp eq i32 %0, %1
21+
br i1 %cmp.i, label %land.rhs.i, label %opeq1.exit
22+
23+
land.rhs.i:
24+
%second.i = getelementptr inbounds %"struct.std::pair", %"struct.std::pair"* %a, i64 0, i32 0
25+
%2 = load i32, i32* %second.i, align 4
26+
%second2.i = getelementptr inbounds %"struct.std::pair", %"struct.std::pair"* %b, i64 0, i32 0
27+
%3 = load i32, i32* %second2.i, align 4
28+
%cmp3.i = icmp eq i32 %2, %3
29+
br label %opeq1.exit
30+
31+
opeq1.exit:
32+
%4 = phi i1 [ false, %entry ], [ %cmp3.i, %land.rhs.i]
33+
%5 = load i32, i32* %first.i, align 4
34+
%6 = select i1 %4, i32 %5, i32 0
35+
ret i32 %6
36+
}

llvm/test/Transforms/MergeICmps/X86/tuple-four-int8.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; XFAIL: *
12
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
23
; RUN: opt < %s -mergeicmps -mtriple=x86_64-unknown-unknown -S | FileCheck %s
34

0 commit comments

Comments
 (0)