Skip to content

[SimplifyCFG] Don't block sinking for allocas if no phi created #104579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1994,28 +1994,6 @@ static bool canSinkInstructions(
return false;
}

// Because SROA can't handle speculating stores of selects, try not to sink
// loads, stores or lifetime markers of allocas when we'd have to create a
// PHI for the address operand. Also, because it is likely that loads or
// stores of allocas will disappear when Mem2Reg/SROA is run, don't sink
// them.
// This can cause code churn which can have unintended consequences down
// the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
// FIXME: This is a workaround for a deficiency in SROA - see
// https://llvm.org/bugs/show_bug.cgi?id=30188
if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
}))
return false;
if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
}))
return false;
if (isLifeTimeMarker(I0) && any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
}))
return false;

// For calls to be sinkable, they must all be indirect, or have same callee.
// I.e. if we have two direct calls to different callees, we don't want to
// turn that into an indirect call. Likewise, if we have an indirect call,
Expand Down Expand Up @@ -2053,6 +2031,27 @@ static bool canSinkInstructions(
return I->getOperand(OI) == I0->getOperand(OI);
};
if (!all_of(Insts, SameAsI0)) {
// Because SROA historically couldn't handle speculating stores of
// selects, we try not to sink loads, stores or lifetime markers of
// allocas when we'd have to create a PHI for the address operand.
// TODO: SROA supports speculation for loads and stores now -- remove
// this hack?
if (isa<StoreInst>(I0) && OI == 1 &&
any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
}))
return false;
if (isa<LoadInst>(I0) && OI == 0 &&
any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
}))
return false;
if (isLifeTimeMarker(I0) && OI == 1 &&
any_of(Insts, [](const Instruction *I) {
return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
}))
return false;

if ((isa<Constant>(Op) && !replacingOperandWithVariableIsCheap(I0, OI)) ||
!canReplaceOperandWithVariable(I0, OI))
// We can't create a PHI from this GEP.
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/Hexagon/block-addr.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc -march=hexagon < %s | FileCheck %s
; RUN: llc -march=hexagon -hexagon-initial-cfg-cleanup=0 < %s | FileCheck %s

; CHECK-DAG: r[[REG:[0-9]+]] = memw(r{{[0-9]+<<#[0-9]+}}+##.LJTI{{.*}})
; CHECK-DAG: jumpr r[[REG]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc -filetype=asm -asm-verbose=0 < %s | FileCheck %s
; RUN: llc -filetype=asm -asm-verbose=0 -arm-atomic-cfg-tidy=0 < %s | FileCheck %s

; int main()
; {
Expand Down
18 changes: 9 additions & 9 deletions llvm/test/Transforms/SimplifyCFG/X86/sink-common-code.ll
Original file line number Diff line number Diff line change
Expand Up @@ -838,15 +838,14 @@ define i32 @test_pr30188a(i1 zeroext %flag, i32 %x) {
; CHECK: if.then:
; CHECK-NEXT: call void @g()
; CHECK-NEXT: [[ONE:%.*]] = load i32, ptr [[Y]], align 4
; CHECK-NEXT: [[TWO:%.*]] = add i32 [[ONE]], 2
; CHECK-NEXT: store i32 [[TWO]], ptr [[Y]], align 4
; CHECK-NEXT: br label [[IF_END:%.*]]
; CHECK: if.else:
; CHECK-NEXT: [[THREE:%.*]] = load i32, ptr [[Z]], align 4
; CHECK-NEXT: [[FOUR:%.*]] = add i32 [[THREE]], 2
; CHECK-NEXT: store i32 [[FOUR]], ptr [[Y]], align 4
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
; CHECK-NEXT: [[THREE_SINK:%.*]] = phi i32 [ [[THREE]], [[IF_ELSE]] ], [ [[ONE]], [[IF_THEN]] ]
; CHECK-NEXT: [[FOUR:%.*]] = add i32 [[THREE_SINK]], 2
; CHECK-NEXT: store i32 [[FOUR]], ptr [[Y]], align 4
; CHECK-NEXT: ret i32 1
;
entry:
Expand Down Expand Up @@ -914,15 +913,16 @@ define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA,
; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[BLKSA:%.*]], [[NBLKS:%.*]]
; CHECK-NEXT: [[FROMBOOL1:%.*]] = zext i1 [[CMP]] to i8
; CHECK-NEXT: store i8 [[FROMBOOL1]], ptr [[P]], align 1
; CHECK-NEXT: br label [[IF_END:%.*]]
; CHECK-NEXT: br label [[IF_END_SINK_SPLIT:%.*]]
; CHECK: if.else:
; CHECK-NEXT: br i1 [[FLAG2:%.*]], label [[IF_THEN2:%.*]], label [[IF_END]]
; CHECK-NEXT: br i1 [[FLAG2:%.*]], label [[IF_THEN2:%.*]], label [[IF_END:%.*]]
; CHECK: if.then2:
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[NBLKS]], [[BLKSB:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i32 [[ADD]], [[BLKSA]]
; CHECK-NEXT: [[FROMBOOL3:%.*]] = zext i1 [[CMP2]] to i8
; CHECK-NEXT: br label [[IF_END_SINK_SPLIT]]
; CHECK: if.end.sink.split:
; CHECK-NEXT: [[CMP2_SINK:%.*]] = phi i1 [ [[CMP2]], [[IF_THEN2]] ], [ [[CMP]], [[IF_THEN]] ]
; CHECK-NEXT: [[FROMBOOL3:%.*]] = zext i1 [[CMP2_SINK]] to i8
; CHECK-NEXT: store i8 [[FROMBOOL3]], ptr [[P]], align 1
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
Expand Down
19 changes: 2 additions & 17 deletions llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,8 @@ define void @pr104567(i8 %x, ptr %f) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[Y:%.*]] = alloca [1 x i8], align 1
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 1, ptr nonnull [[Y]])
; CHECK-NEXT: switch i8 [[X]], label %[[DEFAULT_UNREACHABLE:.*]] [
; CHECK-NEXT: i8 0, label %[[BB4:.*]]
; CHECK-NEXT: i8 1, label %[[BB3:.*]]
; CHECK-NEXT: i8 2, label %[[BB2:.*]]
; CHECK-NEXT: ]
; CHECK: [[DEFAULT_UNREACHABLE]]:
; CHECK-NEXT: unreachable
; CHECK: [[BB4]]:
; CHECK-NEXT: store i8 4, ptr [[Y]], align 1
; CHECK-NEXT: br label %[[BB5:.*]]
; CHECK: [[BB3]]:
; CHECK-NEXT: store i8 5, ptr [[Y]], align 1
; CHECK-NEXT: br label %[[BB5]]
; CHECK: [[BB2]]:
; CHECK-NEXT: store i8 6, ptr [[Y]], align 1
; CHECK-NEXT: br label %[[BB5]]
; CHECK: [[BB5]]:
; CHECK-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i8 [[X]], 4
; CHECK-NEXT: store i8 [[SWITCH_OFFSET]], ptr [[Y]], align 1
; CHECK-NEXT: call void [[F]](ptr [[Y]])
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 1, ptr nonnull [[Y]])
; CHECK-NEXT: ret void
Expand Down
Loading