Skip to content

Commit 84497c6

Browse files
authored
[SimplifyCFG] Remove limitation on sinking of load/store of alloca (#104788)
This is a followup to #104579 to remove the limitation on sinking loads/stores of allocas entirely, even if this would introduce a phi node. Nowadays, SROA supports speculating load/store over select/phi. Additionally, SimplifyCFG with sinking only runs at the end of the function simplification pipeline, after SROA. I checked that the two tests modified here still successfully SROA after the SimplifyCFG transform. We should, however, keep the limitation on lifetime intrinsics. SROA does not have speculation support for these, and I've also found that the way these are handled in the backend is very problematic (#104776), so I think we should leave them alone.
1 parent 28fe6dd commit 84497c6

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,21 +2051,10 @@ static bool canSinkInstructions(
20512051
return I->getOperand(OI) == I0->getOperand(OI);
20522052
};
20532053
if (!all_of(Insts, SameAsI0)) {
2054-
// Because SROA historically couldn't handle speculating stores of
2055-
// selects, we try not to sink loads, stores or lifetime markers of
2056-
// allocas when we'd have to create a PHI for the address operand.
2057-
// TODO: SROA supports speculation for loads and stores now -- remove
2058-
// this hack?
2059-
if (isa<StoreInst>(I0) && OI == 1 &&
2060-
any_of(Insts, [](const Instruction *I) {
2061-
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2062-
}))
2063-
return false;
2064-
if (isa<LoadInst>(I0) && OI == 0 &&
2065-
any_of(Insts, [](const Instruction *I) {
2066-
return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
2067-
}))
2068-
return false;
2054+
// SROA can't speculate lifetime markers of selects/phis, and the
2055+
// backend may handle such lifetimes incorrectly as well (#104776).
2056+
// Don't sink lifetimes if it would introduce a phi on the pointer
2057+
// argument.
20692058
if (isLifeTimeMarker(I0) && OI == 1 &&
20702059
any_of(Insts, [](const Instruction *I) {
20712060
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -801,14 +801,8 @@ define i32 @test_pr30188(i1 zeroext %flag, i32 %x) {
801801
; CHECK-NEXT: entry:
802802
; CHECK-NEXT: [[Y:%.*]] = alloca i32, align 4
803803
; CHECK-NEXT: [[Z:%.*]] = alloca i32, align 4
804-
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
805-
; CHECK: if.then:
806-
; CHECK-NEXT: store i32 [[X:%.*]], ptr [[Y]], align 4
807-
; CHECK-NEXT: br label [[IF_END:%.*]]
808-
; CHECK: if.else:
809-
; CHECK-NEXT: store i32 [[X]], ptr [[Z]], align 4
810-
; CHECK-NEXT: br label [[IF_END]]
811-
; CHECK: if.end:
804+
; CHECK-NEXT: [[Y_Z:%.*]] = select i1 [[FLAG:%.*]], ptr [[Y]], ptr [[Z]]
805+
; CHECK-NEXT: store i32 [[X:%.*]], ptr [[Y_Z]], align 4
812806
; CHECK-NEXT: ret i32 1
813807
;
814808
entry:
@@ -834,17 +828,14 @@ define i32 @test_pr30188a(i1 zeroext %flag, i32 %x) {
834828
; CHECK-NEXT: entry:
835829
; CHECK-NEXT: [[Y:%.*]] = alloca i32, align 4
836830
; CHECK-NEXT: [[Z:%.*]] = alloca i32, align 4
837-
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
831+
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
838832
; CHECK: if.then:
839833
; CHECK-NEXT: call void @g()
840-
; CHECK-NEXT: [[ONE:%.*]] = load i32, ptr [[Y]], align 4
841-
; CHECK-NEXT: br label [[IF_END:%.*]]
842-
; CHECK: if.else:
843-
; CHECK-NEXT: [[THREE:%.*]] = load i32, ptr [[Z]], align 4
844834
; CHECK-NEXT: br label [[IF_END]]
845835
; 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
836+
; CHECK-NEXT: [[Z_SINK:%.*]] = phi ptr [ [[Y]], [[IF_THEN]] ], [ [[Z]], [[ENTRY:%.*]] ]
837+
; CHECK-NEXT: [[THREE:%.*]] = load i32, ptr [[Z_SINK]], align 4
838+
; CHECK-NEXT: [[FOUR:%.*]] = add i32 [[THREE]], 2
848839
; CHECK-NEXT: store i32 [[FOUR]], ptr [[Y]], align 4
849840
; CHECK-NEXT: ret i32 1
850841
;

0 commit comments

Comments
 (0)