Skip to content

Commit ad87761

Browse files
committed
[SimplifyCFG] HoistThenElseCodeToIf(): don't hoist if either block has it's address taken
This problem is exposed by D104598, after it tail-merges `ret` in `@test_inline_constraint_S_label`, the verifier would start complaining `invalid operand for inline asm constraint 'S'`. Essentially, taking address of a block is mismodelled in IR. It should probably be an explicit instruction, a first one in block, that isn't identical to any other instruction of the same type, so that it can't be hoisted.
1 parent 09e8c0d commit ad87761

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,12 @@ bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst *BI,
14041404
BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
14051405
BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
14061406

1407+
// If either of the blocks has it's address taken, then we can't do this fold,
1408+
// because the code we'd hoist would no longer run when we jump into the block
1409+
// by it's address.
1410+
if (BB1->hasAddressTaken() || BB2->hasAddressTaken())
1411+
return false;
1412+
14071413
BasicBlock::iterator BB1_Itr = BB1->begin();
14081414
BasicBlock::iterator BB2_Itr = BB2->begin();
14091415

llvm/test/CodeGen/AArch64/inlineasm-S-constraint.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ loc:
1818
loc2:
1919
ret i32 42
2020
}
21+
define i32 @test_inline_constraint_S_label_tailmerged(i1 %in) {
22+
; CHECK-LABEL: test_inline_constraint_S_label_tailmerged:
23+
call void asm sideeffect "adr x0, $0", "S"(i8* blockaddress(@test_inline_constraint_S_label_tailmerged, %loc))
24+
; CHECK: adr x0, .Ltmp{{[0-9]+}}
25+
br i1 %in, label %loc, label %loc2
26+
loc:
27+
br label %common.ret
28+
loc2:
29+
br label %common.ret
30+
common.ret:
31+
%common.retval = phi i32 [ 0, %loc ], [ 42, %loc2 ]
32+
ret i32 %common.retval
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -mem2reg -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
3+
4+
define i32 @test_inline_constraint_S_label_tailmerged(i1 %in) {
5+
; CHECK-LABEL: @test_inline_constraint_S_label_tailmerged(
6+
; CHECK-NEXT: call void asm sideeffect "adr x0, $0", "S"(i8* blockaddress(@test_inline_constraint_S_label_tailmerged, [[COMMON_RET:%.*]]))
7+
; CHECK-NEXT: [[COMMON_RETVAL:%.*]] = select i1 [[IN:%.*]], i32 0, i32 42
8+
; CHECK-NEXT: br label [[COMMON_RET]]
9+
; CHECK: common.ret:
10+
; CHECK-NEXT: ret i32 [[COMMON_RETVAL]]
11+
;
12+
call void asm sideeffect "adr x0, $0", "S"(i8* blockaddress(@test_inline_constraint_S_label_tailmerged, %loc))
13+
br i1 %in, label %loc, label %loc2
14+
loc:
15+
br label %common.ret
16+
loc2:
17+
br label %common.ret
18+
common.ret:
19+
%common.retval = phi i32 [ 0, %loc ], [ 42, %loc2 ]
20+
ret i32 %common.retval
21+
}

0 commit comments

Comments
 (0)