Skip to content

Commit cafe582

Browse files
committed
LoopUnroller: Also handle > exit condition
1 parent 533044d commit cafe582

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,23 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
126126
IntegerLiteralInst *End;
127127
SILValue RecNext;
128128

129+
unsigned Adjust = 0;
130+
129131
if (!match(CondBr->getCondition(),
130132
m_BuiltinInst(BuiltinValueKind::ICMP_EQ, m_SILValue(RecNext),
131133
m_IntegerLiteralInst(End))) &&
132134
!match(CondBr->getCondition(),
133135
m_BuiltinInst(BuiltinValueKind::ICMP_SGE, m_SILValue(RecNext),
134-
m_IntegerLiteralInst(End))))
135-
return None;
136+
m_IntegerLiteralInst(End)))) {
137+
if (!match(CondBr->getCondition(),
138+
m_BuiltinInst(BuiltinValueKind::ICMP_SGT, m_SILValue(RecNext),
139+
m_IntegerLiteralInst(End))))
140+
return None;
141+
// Otherwise, we have a greater than comparison.
142+
else
143+
Adjust = 1;
144+
}
145+
136146
if (!match(RecNext,
137147
m_TupleExtractInst(m_ApplyInst(BuiltinValueKind::SAddOver,
138148
m_SILPHIArgument(RecArg), m_One()),
@@ -162,7 +172,7 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
162172
if (Dist == 0)
163173
return None;
164174

165-
return Dist.getZExtValue();
175+
return Dist.getZExtValue() + Adjust;
166176
}
167177

168178
/// Check whether we can duplicate the instructions in the loop and use a

test/SILOptimizer/loop_unroll.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,42 @@ bb3:
107107
return %8 : $()
108108
}
109109

110+
// CHECK-LABEL: sil @loop_unroll_4
111+
// CHECK: bb0:
112+
// CHECK: br bb1
113+
// CHECK: bb1
114+
// CHECK: = builtin "sadd_with_overflow_Int64
115+
// CHECK: cond_br {{.*}}, bb3, bb2
116+
// CHECK: bb2:
117+
// CHECK: br bb4
118+
// CHECK: bb3:
119+
// CHECK: return
120+
// CHECK: bb4
121+
// CHECK: = builtin "sadd_with_overflow_Int64
122+
// CHECK: br bb3
123+
124+
sil @loop_unroll_4 : $@convention(thin) () -> () {
125+
bb0:
126+
%0 = integer_literal $Builtin.Int64, 0
127+
%1 = integer_literal $Builtin.Int64, 1
128+
%2 = integer_literal $Builtin.Int64, 1
129+
%3 = integer_literal $Builtin.Int1, 1
130+
br bb1(%0 : $Builtin.Int64)
131+
132+
bb1(%4 : $Builtin.Int64):
133+
%5 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %1 : $Builtin.Int64, %3 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
134+
%6 = tuple_extract %5 : $(Builtin.Int64, Builtin.Int1), 0
135+
%7 = builtin "cmp_sgt_Int64"(%6 : $Builtin.Int64, %2 : $Builtin.Int64) : $Builtin.Int1
136+
cond_br %7, bb3, bb2
137+
138+
bb2:
139+
br bb1(%6 : $Builtin.Int64)
140+
141+
bb3:
142+
%8 = tuple()
143+
return %8 : $()
144+
}
145+
110146
class B {}
111147

112148
// CHECK-LABEL: sil @unroll_with_stack_allocation

0 commit comments

Comments
 (0)