Skip to content

Commit 83879f4

Browse files
authored
[SimplifyCFG] Don't block sinking for allocas if no phi created (#104579)
SimplifyCFG sinking currently does not sink loads/stores of allocas, because historically SROA was unable to handle the resulting IR. Since then, SROA both learned to speculate loads/stores over selects and phis, *and* SimplifyCFG sinking has been deferred to the end of the function simplification pipeline, which means that SROA happens before it. As such, I believe that this workaround should no longer be necessary. Given how sensitive SimplifyCFG sinking seems to be, this patch takes a very conservative step towards removing this, by allowing sinking if we don't actually need to form a phi over the pointer argument. This fixes #104567, where sinking a store to an escaped alloca allows converting a switch into arithmetic.
1 parent 985d64b commit 83879f4

File tree

5 files changed

+34
-50
lines changed

5 files changed

+34
-50
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,28 +1994,6 @@ static bool canSinkInstructions(
19941994
return false;
19951995
}
19961996

1997-
// Because SROA can't handle speculating stores of selects, try not to sink
1998-
// loads, stores or lifetime markers of allocas when we'd have to create a
1999-
// PHI for the address operand. Also, because it is likely that loads or
2000-
// stores of allocas will disappear when Mem2Reg/SROA is run, don't sink
2001-
// them.
2002-
// This can cause code churn which can have unintended consequences down
2003-
// the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
2004-
// FIXME: This is a workaround for a deficiency in SROA - see
2005-
// https://llvm.org/bugs/show_bug.cgi?id=30188
2006-
if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) {
2007-
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2008-
}))
2009-
return false;
2010-
if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) {
2011-
return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
2012-
}))
2013-
return false;
2014-
if (isLifeTimeMarker(I0) && any_of(Insts, [](const Instruction *I) {
2015-
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2016-
}))
2017-
return false;
2018-
20191997
// For calls to be sinkable, they must all be indirect, or have same callee.
20201998
// I.e. if we have two direct calls to different callees, we don't want to
20211999
// turn that into an indirect call. Likewise, if we have an indirect call,
@@ -2053,6 +2031,27 @@ static bool canSinkInstructions(
20532031
return I->getOperand(OI) == I0->getOperand(OI);
20542032
};
20552033
if (!all_of(Insts, SameAsI0)) {
2034+
// Because SROA historically couldn't handle speculating stores of
2035+
// selects, we try not to sink loads, stores or lifetime markers of
2036+
// allocas when we'd have to create a PHI for the address operand.
2037+
// TODO: SROA supports speculation for loads and stores now -- remove
2038+
// this hack?
2039+
if (isa<StoreInst>(I0) && OI == 1 &&
2040+
any_of(Insts, [](const Instruction *I) {
2041+
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2042+
}))
2043+
return false;
2044+
if (isa<LoadInst>(I0) && OI == 0 &&
2045+
any_of(Insts, [](const Instruction *I) {
2046+
return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
2047+
}))
2048+
return false;
2049+
if (isLifeTimeMarker(I0) && OI == 1 &&
2050+
any_of(Insts, [](const Instruction *I) {
2051+
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2052+
}))
2053+
return false;
2054+
20562055
if ((isa<Constant>(Op) && !replacingOperandWithVariableIsCheap(I0, OI)) ||
20572056
!canReplaceOperandWithVariable(I0, OI))
20582057
// We can't create a PHI from this GEP.

llvm/test/CodeGen/Hexagon/block-addr.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -march=hexagon < %s | FileCheck %s
1+
; RUN: llc -march=hexagon -hexagon-initial-cfg-cleanup=0 < %s | FileCheck %s
22

33
; CHECK-DAG: r[[REG:[0-9]+]] = memw(r{{[0-9]+<<#[0-9]+}}+##.LJTI{{.*}})
44
; CHECK-DAG: jumpr r[[REG]]

llvm/test/DebugInfo/ARM/single-constant-use-preserves-dbgloc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -filetype=asm -asm-verbose=0 < %s | FileCheck %s
1+
; RUN: llc -filetype=asm -asm-verbose=0 -arm-atomic-cfg-tidy=0 < %s | FileCheck %s
22

33
; int main()
44
; {

llvm/test/Transforms/SimplifyCFG/X86/sink-common-code.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,14 @@ define i32 @test_pr30188a(i1 zeroext %flag, i32 %x) {
838838
; CHECK: if.then:
839839
; CHECK-NEXT: call void @g()
840840
; CHECK-NEXT: [[ONE:%.*]] = load i32, ptr [[Y]], align 4
841-
; CHECK-NEXT: [[TWO:%.*]] = add i32 [[ONE]], 2
842-
; CHECK-NEXT: store i32 [[TWO]], ptr [[Y]], align 4
843841
; CHECK-NEXT: br label [[IF_END:%.*]]
844842
; CHECK: if.else:
845843
; CHECK-NEXT: [[THREE:%.*]] = load i32, ptr [[Z]], align 4
846-
; CHECK-NEXT: [[FOUR:%.*]] = add i32 [[THREE]], 2
847-
; CHECK-NEXT: store i32 [[FOUR]], ptr [[Y]], align 4
848844
; CHECK-NEXT: br label [[IF_END]]
849845
; CHECK: if.end:
846+
; CHECK-NEXT: [[THREE_SINK:%.*]] = phi i32 [ [[THREE]], [[IF_ELSE]] ], [ [[ONE]], [[IF_THEN]] ]
847+
; CHECK-NEXT: [[FOUR:%.*]] = add i32 [[THREE_SINK]], 2
848+
; CHECK-NEXT: store i32 [[FOUR]], ptr [[Y]], align 4
850849
; CHECK-NEXT: ret i32 1
851850
;
852851
entry:
@@ -914,15 +913,16 @@ define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA,
914913
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
915914
; CHECK: if.then:
916915
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[BLKSA:%.*]], [[NBLKS:%.*]]
917-
; CHECK-NEXT: [[FROMBOOL1:%.*]] = zext i1 [[CMP]] to i8
918-
; CHECK-NEXT: store i8 [[FROMBOOL1]], ptr [[P]], align 1
919-
; CHECK-NEXT: br label [[IF_END:%.*]]
916+
; CHECK-NEXT: br label [[IF_END_SINK_SPLIT:%.*]]
920917
; CHECK: if.else:
921-
; CHECK-NEXT: br i1 [[FLAG2:%.*]], label [[IF_THEN2:%.*]], label [[IF_END]]
918+
; CHECK-NEXT: br i1 [[FLAG2:%.*]], label [[IF_THEN2:%.*]], label [[IF_END:%.*]]
922919
; CHECK: if.then2:
923920
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[NBLKS]], [[BLKSB:%.*]]
924921
; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i32 [[ADD]], [[BLKSA]]
925-
; CHECK-NEXT: [[FROMBOOL3:%.*]] = zext i1 [[CMP2]] to i8
922+
; CHECK-NEXT: br label [[IF_END_SINK_SPLIT]]
923+
; CHECK: if.end.sink.split:
924+
; CHECK-NEXT: [[CMP2_SINK:%.*]] = phi i1 [ [[CMP2]], [[IF_THEN2]] ], [ [[CMP]], [[IF_THEN]] ]
925+
; CHECK-NEXT: [[FROMBOOL3:%.*]] = zext i1 [[CMP2_SINK]] to i8
926926
; CHECK-NEXT: store i8 [[FROMBOOL3]], ptr [[P]], align 1
927927
; CHECK-NEXT: br label [[IF_END]]
928928
; CHECK: if.end:

llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,8 @@ define void @pr104567(i8 %x, ptr %f) {
99
; CHECK-NEXT: [[START:.*:]]
1010
; CHECK-NEXT: [[Y:%.*]] = alloca [1 x i8], align 1
1111
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 1, ptr nonnull [[Y]])
12-
; CHECK-NEXT: switch i8 [[X]], label %[[DEFAULT_UNREACHABLE:.*]] [
13-
; CHECK-NEXT: i8 0, label %[[BB4:.*]]
14-
; CHECK-NEXT: i8 1, label %[[BB3:.*]]
15-
; CHECK-NEXT: i8 2, label %[[BB2:.*]]
16-
; CHECK-NEXT: ]
17-
; CHECK: [[DEFAULT_UNREACHABLE]]:
18-
; CHECK-NEXT: unreachable
19-
; CHECK: [[BB4]]:
20-
; CHECK-NEXT: store i8 4, ptr [[Y]], align 1
21-
; CHECK-NEXT: br label %[[BB5:.*]]
22-
; CHECK: [[BB3]]:
23-
; CHECK-NEXT: store i8 5, ptr [[Y]], align 1
24-
; CHECK-NEXT: br label %[[BB5]]
25-
; CHECK: [[BB2]]:
26-
; CHECK-NEXT: store i8 6, ptr [[Y]], align 1
27-
; CHECK-NEXT: br label %[[BB5]]
28-
; CHECK: [[BB5]]:
12+
; CHECK-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i8 [[X]], 4
13+
; CHECK-NEXT: store i8 [[SWITCH_OFFSET]], ptr [[Y]], align 1
2914
; CHECK-NEXT: call void [[F]](ptr [[Y]])
3015
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 1, ptr nonnull [[Y]])
3116
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)