Skip to content

Commit 28d7301

Browse files
committed
Fix and comments addressed
refined fix with only instructions in block
1 parent 83a292c commit 28d7301

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -627,28 +627,23 @@ bool CodeExtractor::isEligible() const {
627627
return false;
628628
}
629629
}
630-
// stacksave as input implies stackrestore in the outlined function
631-
// This can confuse prologue epilogue insertion phase
632-
// stacksave's uses must not cross outlined function
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.
633633
for (BasicBlock *BB : Blocks) {
634-
for (Instruction &II : *BB) {
635-
if (IntrinsicInst *Intrin = dyn_cast<IntrinsicInst>(&II)) {
636-
if (Intrin->getIntrinsicID() == Intrinsic::stacksave) {
637-
for (User *U : Intrin->users())
638-
if (!definedInRegion(Blocks, U)) {
639-
return false; // stack-restore outside outlined region
640-
}
641-
}
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;
642644
}
643-
for (auto &OI : II.operands()) {
644-
Value *V = OI;
645-
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(V)) {
646-
if (II->getIntrinsicID() == Intrinsic::stacksave) {
647-
if (definedInCaller(Blocks, V)) {
648-
return false;
649-
}
650-
}
651-
}
645+
if (IsRestore && !definedInRegion(Blocks, II->getArgOperand(0))) {
646+
return false;
652647
}
653648
}
654649
}

llvm/test/Transforms/HotColdSplit/outline-inner-region-stacktoocomplex.ll

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
; RUN: opt -S -passes=hotcoldsplit -hotcoldsplit-max-params=1 < %s | FileCheck %s
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
23

34
target datalayout = "E-m:a-p:32:32-i64:64-n32"
4-
target triple = "powerpc64-ibm-aix7.2.0.0"
55

66
define void @foo(i32 %cond) {
7-
; CHECK-LABEL: define {{.*}}@foo(
8-
; CHECK: if.then:
9-
; CHECK: if.then1:
10-
; CHECK: if.else:
11-
; CHECK: call {{.*}}@sink
12-
; CHECK: if.end:
13-
; CHECK: if.end2:
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
1434
;
1535
entry:
1636
%cond.addr = alloca i32

0 commit comments

Comments
 (0)