Skip to content

LoopUnroller: Teach the loop unroller about >= terminated loops #8943

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 2 commits into from
Apr 23, 2017
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
19 changes: 16 additions & 3 deletions lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,23 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
IntegerLiteralInst *End;
SILValue RecNext;

unsigned Adjust = 0;

if (!match(CondBr->getCondition(),
m_BuiltinInst(BuiltinValueKind::ICMP_EQ, m_SILValue(RecNext),
m_IntegerLiteralInst(End))))
return None;
m_IntegerLiteralInst(End))) &&
!match(CondBr->getCondition(),
m_BuiltinInst(BuiltinValueKind::ICMP_SGE, m_SILValue(RecNext),
m_IntegerLiteralInst(End)))) {
if (!match(CondBr->getCondition(),
m_BuiltinInst(BuiltinValueKind::ICMP_SGT, m_SILValue(RecNext),
m_IntegerLiteralInst(End))))
return None;
// Otherwise, we have a greater than comparison.
else
Adjust = 1;
}

if (!match(RecNext,
m_TupleExtractInst(m_ApplyInst(BuiltinValueKind::SAddOver,
m_SILPHIArgument(RecArg), m_One()),
Expand Down Expand Up @@ -159,7 +172,7 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
if (Dist == 0)
return None;

return Dist.getZExtValue();
return Dist.getZExtValue() + Adjust;
}

/// Check whether we can duplicate the instructions in the loop and use a
Expand Down
72 changes: 72 additions & 0 deletions test/SILOptimizer/loop_unroll.sil
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,78 @@ bb3:
return %8 : $()
}

// CHECK-LABEL: sil @loop_unroll_3
// CHECK: bb0:
// CHECK: br bb1
// CHECK: bb1
// CHECK: = builtin "sadd_with_overflow_Int64
// CHECK: cond_br {{.*}}, bb3, bb2
// CHECK: bb2:
// CHECK: br bb4
// CHECK: bb3:
// CHECK: return
// CHECK: bb4
// CHECK: = builtin "sadd_with_overflow_Int64
// CHECK: br bb3

sil @loop_unroll_3 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int64, 0
%1 = integer_literal $Builtin.Int64, 1
%2 = integer_literal $Builtin.Int64, 2
%3 = integer_literal $Builtin.Int1, 1
br bb1(%0 : $Builtin.Int64)

bb1(%4 : $Builtin.Int64):
%5 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %1 : $Builtin.Int64, %3 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
%6 = tuple_extract %5 : $(Builtin.Int64, Builtin.Int1), 0
%7 = builtin "cmp_sge_Int64"(%6 : $Builtin.Int64, %2 : $Builtin.Int64) : $Builtin.Int1
cond_br %7, bb3, bb2

bb2:
br bb1(%6 : $Builtin.Int64)

bb3:
%8 = tuple()
return %8 : $()
}

// CHECK-LABEL: sil @loop_unroll_4
// CHECK: bb0:
// CHECK: br bb1
// CHECK: bb1
// CHECK: = builtin "sadd_with_overflow_Int64
// CHECK: cond_br {{.*}}, bb3, bb2
// CHECK: bb2:
// CHECK: br bb4
// CHECK: bb3:
// CHECK: return
// CHECK: bb4
// CHECK: = builtin "sadd_with_overflow_Int64
// CHECK: br bb3

sil @loop_unroll_4 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int64, 0
%1 = integer_literal $Builtin.Int64, 1
%2 = integer_literal $Builtin.Int64, 1
%3 = integer_literal $Builtin.Int1, 1
br bb1(%0 : $Builtin.Int64)

bb1(%4 : $Builtin.Int64):
%5 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %1 : $Builtin.Int64, %3 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
%6 = tuple_extract %5 : $(Builtin.Int64, Builtin.Int1), 0
%7 = builtin "cmp_sgt_Int64"(%6 : $Builtin.Int64, %2 : $Builtin.Int64) : $Builtin.Int1
cond_br %7, bb3, bb2

bb2:
br bb1(%6 : $Builtin.Int64)

bb3:
%8 = tuple()
return %8 : $()
}

class B {}

// CHECK-LABEL: sil @unroll_with_stack_allocation
Expand Down