Skip to content

Commit 074a99c

Browse files
committed
LoopRotate: don't rotate a loop if the new header is loop exiting as well
This doesn't give any performance benefit.
1 parent 8000802 commit 074a99c

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ static bool rotateLoop(SILLoop *loop, DominanceInfo *domInfo,
376376
assert(loop->contains(newHeader) && !loop->contains(exit)
377377
&& "Could not find loop header and exit block");
378378

379+
// It does not make sense to rotate the loop if the new header is loop
380+
// exiting as well.
381+
if (loop->isLoopExiting(newHeader)) {
382+
return false;
383+
}
384+
379385
// We don't want to rotate such that we merge two headers of separate loops
380386
// into one. This can be turned into an assert again once we have guaranteed
381387
// preheader insertions.

test/SILOptimizer/looprotate_nontrivial_ossa.sil

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ exit:
203203
// A guaranteed value whose ownership has been forwarded must not be reborrowed.
204204
//
205205
// CHECK-LABEL: sil [ossa] @forwarded_borrow_cant_be_reborrowed : $@convention(thin) (@owned BoxStruct) -> () {
206-
// CHECK: {{bb[0-9]+}}({{%[^,]+}} : @reborrow $BoxStruct, {{%[^,]+}} : @guaranteed $Klass):
207206
// CHECK-LABEL: } // end sil function 'forwarded_borrow_cant_be_reborrowed'
208207
sil [ossa] @forwarded_borrow_cant_be_reborrowed : $@convention(thin) (@owned BoxStruct) -> () {
209208
bb0(%0 : @owned $BoxStruct):
@@ -214,37 +213,34 @@ bb1:
214213
cond_br %2, bb2, bb3
215214

216215
bb2:
217-
br bb10
216+
br bb9
218217

219218
bb3:
220219
%5 = begin_borrow %0 : $BoxStruct
221220
%6 = struct_extract %5 : $BoxStruct, #BoxStruct.guts
222221
%7 = integer_literal $Builtin.Int1, 0
223-
cond_br %7, bb4, bb5
222+
br bb4
224223

225224
bb4:
226-
unreachable
227-
228-
bb5:
229225
%10 = begin_borrow %6 : $Klass
230226
end_borrow %10 : $Klass
231227
end_borrow %5 : $BoxStruct
228+
br bb5
229+
230+
bb5:
232231
br bb6
233232

234-
bb6:
235-
br bb7
236233

234+
bb6:
235+
%16 = integer_literal $Builtin.Int1, -1
236+
cond_br %16, bb7, bb8
237237

238238
bb7:
239-
%16 = integer_literal $Builtin.Int1, -1
240-
cond_br %16, bb8, bb9
239+
br bb9
241240

242241
bb8:
243-
br bb10
244-
245-
bb9:
246242
br bb1
247243

248-
bb10:
244+
bb9:
249245
unreachable
250246
}

test/SILOptimizer/looprotate_ossa.sil

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,58 @@ bb3:
8484
return %23 : $Int32
8585
}
8686

87+
// CHECK-LABEL: sil [ossa] @dont_rotate_multi_exit_loop :
88+
// CHECK: bb1({{.*}}):
89+
// CHECK: cond_br %{{.*}}, bb4, bb2
90+
// CHECK: bb2:
91+
// CHECK: cond_br undef, bb3, bb5
92+
// CHECK: bb6:
93+
// CHECK: return
94+
// CHECK-NOT: bb7
95+
// CHECK: } // end sil function 'dont_rotate_multi_exit_loop'
96+
sil [ossa] @dont_rotate_multi_exit_loop : $@convention(thin) (Int32, @owned Bar) -> Int32 {
97+
bb0(%0 : $Int32, %25: @owned $Bar):
98+
%1 = struct_extract %0 : $Int32, #Int32._value
99+
%2 = integer_literal $Builtin.Int32, 0
100+
%30 = alloc_box $<τ_0_0> { var τ_0_0 } <Bool>
101+
%30a = project_box %30 : $<τ_0_0> { var τ_0_0 } <Bool>, 0
102+
br bb1(%1 : $Builtin.Int32, %2 : $Builtin.Int32, %25: $Bar, %30 : $<τ_0_0> { var τ_0_0 } <Bool>)
103+
104+
bb1(%4 : $Builtin.Int32, %5 : $Builtin.Int32, %26: @owned $Bar, %31 : @owned $<τ_0_0> { var τ_0_0 } <Bool>):
105+
%24 = class_method %26 : $Bar, #Bar.foo : (Bar) -> () -> (), $@convention(method) (@guaranteed Bar) -> () // user: %6
106+
%27 = apply %24(%26) : $@convention(method) (@guaranteed Bar) -> ()
107+
%6 = struct $Int32 (%5 : $Builtin.Int32)
108+
%8 = builtin "cmp_eq_Word"(%5 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1
109+
cond_br %8, bb4, bb2
110+
111+
bb2:
112+
%10 = integer_literal $Builtin.Int32, 1
113+
%12 = integer_literal $Builtin.Int1, -1
114+
%13 = builtin "sadd_with_overflow_Word"(%5 : $Builtin.Int32, %10 : $Builtin.Int32, %12 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
115+
%14 = tuple_extract %13 : $(Builtin.Int32, Builtin.Int1), 0
116+
%15 = enum $Optional<Int32>, #Optional.some!enumelt, %6 : $Int32
117+
%16 = unchecked_enum_data %15 : $Optional<Int32>, #Optional.some!enumelt
118+
%17 = struct_extract %16 : $Int32, #Int32._value
119+
%19 = integer_literal $Builtin.Int1, -1
120+
%20 = builtin "sadd_with_overflow_Word"(%4 : $Builtin.Int32, %17 : $Builtin.Int32, %19 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
121+
%21 = tuple_extract %20 : $(Builtin.Int32, Builtin.Int1), 0
122+
cond_br undef, bb3, bb5
123+
bb3:
124+
br bb1(%21 : $Builtin.Int32, %14 : $Builtin.Int32, %26: $Bar, %31 : $<τ_0_0> { var τ_0_0 } <Bool>)
125+
126+
bb4:
127+
br bb6
128+
129+
bb5:
130+
br bb6
131+
132+
bb6:
133+
destroy_value %26 : $Bar
134+
destroy_value %31 : $<τ_0_0> { var τ_0_0 } <Bool>
135+
%23 = struct $Int32 (%4 : $Builtin.Int32)
136+
return %23 : $Int32
137+
}
138+
87139
// CHECK-LABEL: sil [ossa] @dont_duplicate_large_block :
88140
// CHECK: bb0(%0 : $Int32, %1 : @owned $Bar):
89141
// CHECK-NOT: class_method

0 commit comments

Comments
 (0)