Skip to content

Commit 116a31e

Browse files
author
Serguei Katkov
committed
[GuardUtils] Allow intermmediate blocks between widenable branch and deopt block
Reviewed By: anna Differential Revision: https://reviews.llvm.org/D151082
1 parent 1264849 commit 116a31e

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

llvm/lib/Analysis/GuardUtils.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ bool llvm::isGuardAsWidenableBranch(const User *U) {
3232
if (!parseWidenableBranch(U, Condition, WidenableCondition, GuardedBB,
3333
DeoptBB))
3434
return false;
35-
for (auto &Insn : *DeoptBB) {
36-
if (match(&Insn, m_Intrinsic<Intrinsic::experimental_deoptimize>()))
37-
return true;
38-
if (Insn.mayHaveSideEffects())
35+
SmallPtrSet<const BasicBlock *, 2> Visited;
36+
Visited.insert(DeoptBB);
37+
do {
38+
for (auto &Insn : *DeoptBB) {
39+
if (match(&Insn, m_Intrinsic<Intrinsic::experimental_deoptimize>()))
40+
return true;
41+
if (Insn.mayHaveSideEffects())
42+
return false;
43+
}
44+
DeoptBB = DeoptBB->getUniqueSuccessor();
45+
if (!DeoptBB)
3946
return false;
40-
}
47+
} while (Visited.insert(DeoptBB).second);
4148
return false;
4249
}
4350

llvm/test/Transforms/LoopPredication/basic_widenable_branch_guards.ll

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,81 @@ exit: ; preds = %guarded, %entry
20702070
ret i32 %result
20712071
}
20722072

2073+
define i32 @unsigned_loop_0_to_n_ult_check_deep_deopt(ptr %array, i32 %length, i32 %n) {
2074+
; CHECK-LABEL: @unsigned_loop_0_to_n_ult_check_deep_deopt(
2075+
; CHECK-NEXT: entry:
2076+
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i32 [[N:%.*]], 0
2077+
; CHECK-NEXT: br i1 [[TMP5]], label [[EXIT:%.*]], label [[LOOP_PREHEADER:%.*]]
2078+
; CHECK: loop.preheader:
2079+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ule i32 [[N]], [[LENGTH:%.*]]
2080+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 0, [[LENGTH]]
2081+
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[TMP0]]
2082+
; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[TMP2]]
2083+
; CHECK-NEXT: br label [[LOOP:%.*]]
2084+
; CHECK: loop:
2085+
; CHECK-NEXT: [[LOOP_ACC:%.*]] = phi i32 [ [[LOOP_ACC_NEXT:%.*]], [[GUARDED:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
2086+
; CHECK-NEXT: [[I:%.*]] = phi i32 [ [[I_NEXT:%.*]], [[GUARDED]] ], [ 0, [[LOOP_PREHEADER]] ]
2087+
; CHECK-NEXT: [[WITHIN_BOUNDS:%.*]] = icmp ult i32 [[I]], [[LENGTH]]
2088+
; CHECK-NEXT: [[WIDENABLE_COND:%.*]] = call i1 @llvm.experimental.widenable.condition()
2089+
; CHECK-NEXT: [[TMP4:%.*]] = and i1 [[TMP3]], [[WIDENABLE_COND]]
2090+
; CHECK-NEXT: br i1 [[TMP4]], label [[GUARDED]], label [[DEOPT:%.*]], !prof [[PROF0]]
2091+
; CHECK: deopt:
2092+
; CHECK-NEXT: br label [[REAL_DEOPT:%.*]]
2093+
; CHECK: real_deopt:
2094+
; CHECK-NEXT: [[DEOPTCALL:%.*]] = call i32 (...) @llvm.experimental.deoptimize.i32(i32 9) [ "deopt"() ]
2095+
; CHECK-NEXT: ret i32 [[DEOPTCALL]]
2096+
; CHECK: guarded:
2097+
; CHECK-NEXT: call void @llvm.assume(i1 [[WITHIN_BOUNDS]])
2098+
; CHECK-NEXT: [[I_I64:%.*]] = zext i32 [[I]] to i64
2099+
; CHECK-NEXT: [[ARRAY_I_PTR:%.*]] = getelementptr inbounds i32, ptr [[ARRAY:%.*]], i64 [[I_I64]]
2100+
; CHECK-NEXT: [[ARRAY_I:%.*]] = load i32, ptr [[ARRAY_I_PTR]], align 4
2101+
; CHECK-NEXT: [[LOOP_ACC_NEXT]] = add i32 [[LOOP_ACC]], [[ARRAY_I]]
2102+
; CHECK-NEXT: [[I_NEXT]] = add nuw i32 [[I]], 1
2103+
; CHECK-NEXT: [[CONTINUE:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
2104+
; CHECK-NEXT: br i1 [[CONTINUE]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]], !prof [[PROF1]]
2105+
; CHECK: exit.loopexit:
2106+
; CHECK-NEXT: [[LOOP_ACC_NEXT_LCSSA:%.*]] = phi i32 [ [[LOOP_ACC_NEXT]], [[GUARDED]] ]
2107+
; CHECK-NEXT: br label [[EXIT]]
2108+
; CHECK: exit:
2109+
; CHECK-NEXT: [[RESULT:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[LOOP_ACC_NEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
2110+
; CHECK-NEXT: ret i32 [[RESULT]]
2111+
;
2112+
entry:
2113+
%tmp5 = icmp eq i32 %n, 0
2114+
br i1 %tmp5, label %exit, label %loop.preheader
2115+
2116+
loop.preheader: ; preds = %entry
2117+
br label %loop
2118+
2119+
loop: ; preds = %guarded, %loop.preheader
2120+
%loop.acc = phi i32 [ %loop.acc.next, %guarded ], [ 0, %loop.preheader ]
2121+
%i = phi i32 [ %i.next, %guarded ], [ 0, %loop.preheader ]
2122+
%within.bounds = icmp ult i32 %i, %length
2123+
%widenable_cond = call i1 @llvm.experimental.widenable.condition()
2124+
%exiplicit_guard_cond = and i1 %within.bounds, %widenable_cond
2125+
br i1 %exiplicit_guard_cond, label %guarded, label %deopt, !prof !0
2126+
2127+
deopt: ; preds = %loop
2128+
br label %real_deopt
2129+
2130+
real_deopt: ; preds = %deopt
2131+
%deoptcall = call i32 (...) @llvm.experimental.deoptimize.i32(i32 9) [ "deopt"() ]
2132+
ret i32 %deoptcall
2133+
2134+
guarded: ; preds = %loop
2135+
%i.i64 = zext i32 %i to i64
2136+
%array.i.ptr = getelementptr inbounds i32, ptr %array, i64 %i.i64
2137+
%array.i = load i32, ptr %array.i.ptr, align 4
2138+
%loop.acc.next = add i32 %loop.acc, %array.i
2139+
%i.next = add nuw i32 %i, 1
2140+
%continue = icmp ult i32 %i.next, %n
2141+
br i1 %continue, label %loop, label %exit, !prof !2
2142+
2143+
exit: ; preds = %guarded, %entry
2144+
%result = phi i32 [ 0, %entry ], [ %loop.acc.next, %guarded ]
2145+
ret i32 %result
2146+
}
2147+
20732148
declare i32 @llvm.experimental.deoptimize.i32(...)
20742149

20752150
; Function Attrs: inaccessiblememonly nounwind

0 commit comments

Comments
 (0)