Skip to content

Commit b12417f

Browse files
committed
[SimplifyCFG] Select the first instruction that we can handle in passingValueIsAlwaysUndefined
1 parent eba860d commit b12417f

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7573,13 +7573,25 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
75737573
return false;
75747574

75757575
if (C->isNullValue() || isa<UndefValue>(C)) {
7576-
// Only look at the first use, avoid hurting compile time with long uselists
7577-
auto *Use = cast<Instruction>(*I->user_begin());
7578-
// Bail out if Use is not in the same BB as I or Use == I or Use comes
7579-
// before I in the block. The latter two can be the case if Use is a PHI
7580-
// node.
7581-
if (Use->getParent() != I->getParent() || Use == I || Use->comesBefore(I))
7576+
// Only look at the first use we can hanle, avoid hurting compile time with
7577+
// long uselists
7578+
auto FindUse = llvm::find_if(I->users(), [&I](auto *U) {
7579+
auto *Use = cast<Instruction>(U);
7580+
// Bail out if Use is not in the same BB as I or Use == I or Use comes
7581+
// before I in the block. The latter two can be the case if Use is a
7582+
// PHI node.
7583+
if (Use->getParent() != I->getParent() || Use == I || Use->comesBefore(I))
7584+
return false;
7585+
// Change this list when we want to add new instructions.
7586+
if (!isa<GetElementPtrInst>(Use) && !isa<ReturnInst>(Use) &&
7587+
!isa<BitCastInst>(Use) && !isa<LoadInst>(Use) &&
7588+
!isa<StoreInst>(Use) && !isa<AssumeInst>(Use) && !isa<CallBase>(Use))
7589+
return false;
7590+
return true;
7591+
});
7592+
if (FindUse == I->user_end())
75827593
return false;
7594+
auto *Use = cast<Instruction>(*FindUse);
75837595

75847596
// Now make sure that there are no instructions in between that can alter
75857597
// control flow (eg. calls)

llvm/test/Transforms/PhaseOrdering/pr98799-inline-simplifycfg-ub.ll

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,26 @@ bb4:
2020
define i32 @foo(ptr %arg, i1 %arg1) {
2121
; CUSTOM-LABEL: define i32 @foo(
2222
; CUSTOM-SAME: ptr [[ARG:%.*]], i1 [[ARG1:%.*]]) {
23-
; CUSTOM-NEXT: [[BB:.*]]:
24-
; CUSTOM-NEXT: br i1 [[ARG1]], label %[[BAR_EXIT:.*]], label %[[BB2_I:.*]]
25-
; CUSTOM: [[BB2_I]]:
23+
; CUSTOM-NEXT: [[BB:.*:]]
24+
; CUSTOM-NEXT: [[TMP0:%.*]] = xor i1 [[ARG1]], true
25+
; CUSTOM-NEXT: call void @llvm.assume(i1 [[TMP0]])
2626
; CUSTOM-NEXT: [[I_I:%.*]] = load ptr, ptr [[ARG]], align 8
2727
; CUSTOM-NEXT: [[I3_I:%.*]] = getelementptr inbounds i8, ptr [[I_I]], i64 1
2828
; CUSTOM-NEXT: store ptr [[I3_I]], ptr [[ARG]], align 8
29-
; CUSTOM-NEXT: br label %[[BAR_EXIT]]
30-
; CUSTOM: [[BAR_EXIT]]:
31-
; CUSTOM-NEXT: [[I5_I:%.*]] = phi ptr [ [[I_I]], %[[BB2_I]] ], [ null, %[[BB]] ]
32-
; CUSTOM-NEXT: [[I2:%.*]] = icmp ne ptr [[I5_I]], null
29+
; CUSTOM-NEXT: [[I2:%.*]] = icmp ne ptr [[I_I]], null
3330
; CUSTOM-NEXT: call void @llvm.assume(i1 [[I2]])
34-
; CUSTOM-NEXT: [[I3:%.*]] = load i32, ptr [[I5_I]], align 4
31+
; CUSTOM-NEXT: [[I3:%.*]] = load i32, ptr [[I_I]], align 4
3532
; CUSTOM-NEXT: ret i32 [[I3]]
3633
;
3734
; O2-LABEL: define i32 @foo(
3835
; O2-SAME: ptr nocapture [[ARG:%.*]], i1 [[ARG1:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
39-
; O2-NEXT: [[BB:.*]]:
40-
; O2-NEXT: br i1 [[ARG1]], label %[[BAR_EXIT:.*]], label %[[BB2_I:.*]]
41-
; O2: [[BB2_I]]:
42-
; O2-NEXT: [[I_I:%.*]] = load ptr, ptr [[ARG]], align 8
36+
; O2-NEXT: [[BB:.*:]]
37+
; O2-NEXT: [[TMP0:%.*]] = xor i1 [[ARG1]], true
38+
; O2-NEXT: tail call void @llvm.assume(i1 [[TMP0]])
39+
; O2-NEXT: [[I_I:%.*]] = load ptr, ptr [[ARG]], align 8, !nonnull [[META0:![0-9]+]], !noundef [[META0]]
4340
; O2-NEXT: [[I3_I:%.*]] = getelementptr inbounds i8, ptr [[I_I]], i64 1
4441
; O2-NEXT: store ptr [[I3_I]], ptr [[ARG]], align 8
45-
; O2-NEXT: br label %[[BAR_EXIT]]
46-
; O2: [[BAR_EXIT]]:
47-
; O2-NEXT: [[I5_I:%.*]] = phi ptr [ [[I_I]], %[[BB2_I]] ], [ null, %[[BB]] ]
48-
; O2-NEXT: [[I2:%.*]] = icmp ne ptr [[I5_I]], null
49-
; O2-NEXT: tail call void @llvm.assume(i1 [[I2]])
50-
; O2-NEXT: [[I3:%.*]] = load i32, ptr [[I5_I]], align 4
42+
; O2-NEXT: [[I3:%.*]] = load i32, ptr [[I_I]], align 4
5143
; O2-NEXT: ret i32 [[I3]]
5244
;
5345
bb:
@@ -59,3 +51,6 @@ bb:
5951
}
6052

6153
declare void @llvm.assume(i1)
54+
;.
55+
; O2: [[META0]] = !{}
56+
;.

llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,20 +857,17 @@ exit:
857857
ret i32 %res
858858
}
859859

860-
; FIXME: From bb to bb5 is UB.
860+
; From bb to bb5 is UB.
861861
define i32 @test9_null_user_order_1(ptr %arg, i1 %arg1, ptr %arg2) {
862862
; CHECK-LABEL: @test9_null_user_order_1(
863863
; CHECK-NEXT: bb:
864-
; CHECK-NEXT: br i1 [[ARG1:%.*]], label [[BB5:%.*]], label [[BB3:%.*]]
865-
; CHECK: bb3:
864+
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[ARG1:%.*]], true
865+
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
866866
; CHECK-NEXT: [[I:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
867867
; CHECK-NEXT: [[I4:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 1
868868
; CHECK-NEXT: store ptr [[I4]], ptr [[ARG]], align 8
869-
; CHECK-NEXT: br label [[BB5]]
870-
; CHECK: bb5:
871-
; CHECK-NEXT: [[I6:%.*]] = phi ptr [ [[I]], [[BB3]] ], [ null, [[BB:%.*]] ]
872-
; CHECK-NEXT: [[I7:%.*]] = load i32, ptr [[I6]], align 4
873-
; CHECK-NEXT: [[I8:%.*]] = icmp ne ptr [[I6]], [[ARG2:%.*]]
869+
; CHECK-NEXT: [[I7:%.*]] = load i32, ptr [[I]], align 4
870+
; CHECK-NEXT: [[I8:%.*]] = icmp ne ptr [[I]], [[ARG2:%.*]]
874871
; CHECK-NEXT: call void @fn_ptr_arg(i1 [[I8]])
875872
; CHECK-NEXT: ret i32 [[I7]]
876873
;

0 commit comments

Comments
 (0)