Skip to content

Commit 30c6111

Browse files
authored
[MergeICmps] Fix miss-compile in MergeICmps in presence of blockaddresses (#145925)
1 parent a9ed84b commit 30c6111

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

llvm/lib/Transforms/Scalar/MergeICmps.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ BCECmpChain::BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
479479
BaseIdentifier BaseId;
480480
for (BasicBlock *const Block : Blocks) {
481481
assert(Block && "invalid block");
482+
if (Block->hasAddressTaken()) {
483+
LLVM_DEBUG(dbgs() << "cannot merge blocks with blockaddress\n");
484+
return;
485+
}
482486
std::optional<BCECmpBlock> Comparison = visitCmpBlock(
483487
Phi.getIncomingValueForBlock(Block), Block, Phi.getParent(), BaseId);
484488
if (!Comparison) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=mergeicmps -S | FileCheck %s
3+
4+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
%type = type { i64, i64, i64, i64 }
8+
9+
define fastcc i1 @test(ptr byval(%type) %a, ptr byval(%type) %b, i1 %cond) {
10+
; CHECK-LABEL: define fastcc i1 @test(
11+
; CHECK-SAME: ptr byval([[TYPE:%.*]]) [[A:%.*]], ptr byval([[TYPE]]) [[B:%.*]], i1 [[COND:%.*]]) {
12+
; CHECK-NEXT: [[ENTRY:.*:]]
13+
; CHECK-NEXT: [[VALUE401:%.*]] = select i1 [[COND]], ptr blockaddress(@test, %[[BLOCK307:.*]]), ptr blockaddress(@test, %[[EXIT:.*]])
14+
; CHECK-NEXT: indirectbr ptr [[VALUE401]], [label %[[BLOCK307]], label %exit]
15+
; CHECK: [[BLOCK307]]:
16+
; CHECK-NEXT: [[VALUE403:%.*]] = load i64, ptr [[A]], align 8
17+
; CHECK-NEXT: [[VALUE404:%.*]] = load i64, ptr [[A]], align 8
18+
; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq i64 [[VALUE403]], [[VALUE404]]
19+
; CHECK-NEXT: br i1 [[CMP_I]], label %[[LAND_LHS_TRUE_I:.*]], label %[[TEST_EXIT:.*]]
20+
; CHECK: [[LAND_LHS_TRUE_I]]:
21+
; CHECK-NEXT: [[B_I:%.*]] = getelementptr [[TYPE]], ptr [[A]], i64 0, i32 0
22+
; CHECK-NEXT: [[VALUE405:%.*]] = load i64, ptr [[B_I]], align 8
23+
; CHECK-NEXT: [[B3_I:%.*]] = getelementptr [[TYPE]], ptr [[B]], i64 0, i32 0
24+
; CHECK-NEXT: [[VALUE406:%.*]] = load i64, ptr [[B3_I]], align 8
25+
; CHECK-NEXT: [[CMP4_I:%.*]] = icmp eq i64 [[VALUE405]], [[VALUE406]]
26+
; CHECK-NEXT: br i1 [[CMP4_I]], label %[[LAND_LHS_TRUE5_I:.*]], label %[[TEST_EXIT]]
27+
; CHECK: [[LAND_LHS_TRUE5_I]]:
28+
; CHECK-NEXT: [[C_I:%.*]] = getelementptr [[TYPE]], ptr [[A]], i64 0, i32 2
29+
; CHECK-NEXT: [[VALUE407:%.*]] = load i64, ptr [[C_I]], align 8
30+
; CHECK-NEXT: [[C6_I:%.*]] = getelementptr [[TYPE]], ptr [[B]], i64 0, i32 2
31+
; CHECK-NEXT: [[VALUE408:%.*]] = load i64, ptr [[C6_I]], align 8
32+
; CHECK-NEXT: [[CMP7_I:%.*]] = icmp eq i64 [[VALUE407]], [[VALUE408]]
33+
; CHECK-NEXT: br i1 [[CMP7_I]], label %[[LAND_RHS_I:.*]], label %[[TEST_EXIT]]
34+
; CHECK: [[LAND_RHS_I]]:
35+
; CHECK-NEXT: [[D_I:%.*]] = getelementptr [[TYPE]], ptr [[A]], i64 0, i32 3
36+
; CHECK-NEXT: [[VALUE409:%.*]] = load i64, ptr [[D_I]], align 8
37+
; CHECK-NEXT: [[D8_I:%.*]] = getelementptr [[TYPE]], ptr [[B]], i64 0, i32 3
38+
; CHECK-NEXT: [[VALUE410:%.*]] = load i64, ptr [[D8_I]], align 8
39+
; CHECK-NEXT: [[CMP9_I:%.*]] = icmp eq i64 [[VALUE409]], [[VALUE410]]
40+
; CHECK-NEXT: br label %[[TEST_EXIT]]
41+
; CHECK: [[TEST_EXIT]]:
42+
; CHECK-NEXT: [[VALUE411:%.*]] = phi i1 [ false, %[[LAND_LHS_TRUE5_I]] ], [ false, %[[LAND_LHS_TRUE_I]] ], [ false, %[[BLOCK307]] ], [ [[CMP9_I]], %[[LAND_RHS_I]] ]
43+
; CHECK-NEXT: ret i1 false
44+
; CHECK: [[EXIT]]:
45+
; CHECK-NEXT: ret i1 false
46+
;
47+
entry:
48+
%Value401 = select i1 %cond, ptr blockaddress(@test, %Block307), ptr blockaddress(@test, %exit)
49+
indirectbr ptr %Value401, [label %Block307, label %exit]
50+
51+
Block307: ; preds = %entry
52+
%Value403 = load i64, ptr %a, align 8
53+
%Value404 = load i64, ptr %a, align 8
54+
%cmp.i = icmp eq i64 %Value403, %Value404
55+
br i1 %cmp.i, label %land.lhs.true.i, label %test.exit
56+
57+
land.lhs.true.i: ; preds = %Block307
58+
%b.i = getelementptr %type, ptr %a, i64 0, i32 0
59+
%Value405 = load i64, ptr %b.i, align 8
60+
%b3.i = getelementptr %type, ptr %b, i64 0, i32 0
61+
%Value406 = load i64, ptr %b3.i, align 8
62+
%cmp4.i = icmp eq i64 %Value405, %Value406
63+
br i1 %cmp4.i, label %land.lhs.true5.i, label %test.exit
64+
65+
land.lhs.true5.i: ; preds = %land.lhs.true.i
66+
%c.i = getelementptr %type, ptr %a, i64 0, i32 2
67+
%Value407 = load i64, ptr %c.i, align 8
68+
%c6.i = getelementptr %type, ptr %b, i64 0, i32 2
69+
%Value408 = load i64, ptr %c6.i, align 8
70+
%cmp7.i = icmp eq i64 %Value407, %Value408
71+
br i1 %cmp7.i, label %land.rhs.i, label %test.exit
72+
73+
land.rhs.i: ; preds = %land.lhs.true5.i
74+
%d.i = getelementptr %type, ptr %a, i64 0, i32 3
75+
%Value409 = load i64, ptr %d.i, align 8
76+
%d8.i = getelementptr %type, ptr %b, i64 0, i32 3
77+
%Value410 = load i64, ptr %d8.i, align 8
78+
%cmp9.i = icmp eq i64 %Value409, %Value410
79+
br label %test.exit
80+
81+
test.exit: ; preds = %land.rhs.i, %land.lhs.true5.i, %land.lhs.true.i, %Block307
82+
%Value411 = phi i1 [ false, %land.lhs.true5.i ], [ false, %land.lhs.true.i ], [ false, %Block307 ], [ %cmp9.i, %land.rhs.i ]
83+
ret i1 false
84+
85+
exit: ; preds = %entry
86+
ret i1 false
87+
}

0 commit comments

Comments
 (0)