Skip to content

Commit cc246d4

Browse files
authored
[Transforms][CodeExtraction] bug fix regions with stackrestore (#118564)
Ensure code extraction for outlining to a function does not create a function with stacksave of caller to restore stack (e.g. tail call).
1 parent d56edc1 commit cc246d4

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,24 @@ bool CodeExtractor::isEligible() const {
627627
return false;
628628
}
629629
}
630+
// stacksave as input implies stackrestore in the outlined function.
631+
// This can confuse prolog epilog insertion phase.
632+
// stacksave's uses must not cross outlined function.
633+
for (BasicBlock *BB : Blocks) {
634+
for (Instruction &I : *BB) {
635+
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
636+
if (!II)
637+
continue;
638+
bool IsSave = II->getIntrinsicID() == Intrinsic::stacksave;
639+
bool IsRestore = II->getIntrinsicID() == Intrinsic::stackrestore;
640+
if (IsSave && any_of(II->users(), [&Blks = this->Blocks](User *U) {
641+
return !definedInRegion(Blks, U);
642+
}))
643+
return false;
644+
if (IsRestore && !definedInRegion(Blocks, II->getArgOperand(0)))
645+
return false;
646+
}
647+
}
630648
return true;
631649
}
632650

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S --passes=hotcoldsplit --hotcoldsplit-max-params=1 < %s | FileCheck %s
3+
4+
target datalayout = "E-m:a-p:32:32-i64:64-n32"
5+
6+
define void @foo(i32 %cond) {
7+
; CHECK-LABEL: define void @foo(
8+
; CHECK-SAME: i32 [[COND:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: [[COND_ADDR:%.*]] = alloca i32, align 4
11+
; CHECK-NEXT: store i32 [[COND]], ptr [[COND_ADDR]], align 4
12+
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[COND_ADDR]], align 4
13+
; CHECK-NEXT: [[STKS:%.*]] = tail call ptr @llvm.stacksave.p0()
14+
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[TMP0]], 0
15+
; CHECK-NEXT: br i1 [[TOBOOL]], label %[[IF_THEN:.*]], label %[[IF_END2:.*]]
16+
; CHECK: [[IF_THEN]]:
17+
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[COND_ADDR]], align 4
18+
; CHECK-NEXT: call void @sink(i32 [[TMP0]])
19+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[TMP1]], 10
20+
; CHECK-NEXT: br i1 [[CMP]], label %[[IF_THEN1:.*]], label %[[IF_ELSE:.*]]
21+
; CHECK: [[IF_THEN1]]:
22+
; CHECK-NEXT: call void @sideeffect(i32 2)
23+
; CHECK-NEXT: br label %[[IF_END:.*]]
24+
; CHECK: [[IF_ELSE]]:
25+
; CHECK-NEXT: call void @sink(i32 0)
26+
; CHECK-NEXT: call void @sideeffect(i32 0)
27+
; CHECK-NEXT: call void @llvm.stackrestore.p0(ptr [[STKS]])
28+
; CHECK-NEXT: br label %[[IF_END]]
29+
; CHECK: [[IF_END]]:
30+
; CHECK-NEXT: br label %[[IF_END2]]
31+
; CHECK: [[IF_END2]]:
32+
; CHECK-NEXT: call void @sideeffect(i32 1)
33+
; CHECK-NEXT: ret void
34+
;
35+
entry:
36+
%cond.addr = alloca i32
37+
store i32 %cond, ptr %cond.addr
38+
%0 = load i32, ptr %cond.addr
39+
%stks = tail call ptr @llvm.stacksave.p0()
40+
%tobool = icmp ne i32 %0, 0
41+
br i1 %tobool, label %if.then, label %if.end2
42+
43+
if.then: ; preds = %entry
44+
%1 = load i32, ptr %cond.addr
45+
call void @sink(i32 %0)
46+
%cmp = icmp sgt i32 %1, 10
47+
br i1 %cmp, label %if.then1, label %if.else
48+
49+
if.then1: ; preds = %if.then
50+
call void @sideeffect(i32 2)
51+
br label %if.end
52+
53+
if.else: ; preds = %if.then
54+
call void @sink(i32 0)
55+
call void @sideeffect(i32 0)
56+
call void @llvm.stackrestore.p0(ptr %stks)
57+
br label %if.end
58+
59+
60+
if.end: ; preds = %if.else, %if.then1
61+
br label %if.end2
62+
63+
if.end2: ; preds = %entry
64+
call void @sideeffect(i32 1)
65+
ret void
66+
}
67+
68+
69+
declare void @sideeffect(i32)
70+
71+
declare void @sink(i32) cold

0 commit comments

Comments
 (0)