Skip to content

[SILOptimizer] add support for always false comparisons to ArrayBoundsCheckOpts #5622

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 1 commit into from
Nov 3, 2016
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
41 changes: 41 additions & 0 deletions lib/SILOptimizer/LoopTransforms/ArrayBoundsCheckOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,38 @@ static bool isComparisonKnownTrue(BuiltinInst *Builtin, InductionInfo &IndVar) {
m_Specific(IndVar.End)));
}

/// Based on the induction variable information this comparison is known to be
/// false.
static bool isComparisonKnownFalse(BuiltinInst *Builtin,
InductionInfo &IndVar) {
if (!IndVar.IsOverflowCheckInserted ||
IndVar.Cmp != BuiltinValueKind::ICMP_EQ)
return false;

// Pattern match a false condition patterns that we can detect and optimize:
// Iteration count < 0 (start)
// Iteration count + 1 <= 0 (start)
// Iteration count + 1 < 0 (start)
// Iteration count + 1 == 0 (start)
auto MatchIndVarHeader = m_Specific(IndVar.HeaderVal);
auto MatchIncrementIndVar = m_TupleExtractInst(
m_ApplyInst(BuiltinValueKind::SAddOver, MatchIndVarHeader, m_One()), 0);
auto MatchIndVarStart = m_Specific(IndVar.Start);

if (match(Builtin,
m_ApplyInst(BuiltinValueKind::ICMP_SLT,
m_CombineOr(MatchIndVarHeader, MatchIncrementIndVar),
MatchIndVarStart)) ||
match(Builtin, m_ApplyInst(BuiltinValueKind::ICMP_EQ,
MatchIncrementIndVar, MatchIndVarStart)) ||
match(Builtin, m_ApplyInst(BuiltinValueKind::ICMP_SLE,
MatchIncrementIndVar, MatchIndVarStart))) {
return true;
}

return false;
}

/// Analyse the loop for arrays that are not modified and perform dominator tree
/// based redundant bounds check removal.
static bool hoistBoundsChecks(SILLoop *Loop, DominanceInfo *DT, SILLoopInfo *LI,
Expand Down Expand Up @@ -1200,6 +1232,15 @@ static bool hoistBoundsChecks(SILLoop *Loop, DominanceInfo *DT, SILLoopInfo *LI,
Changed = true;
continue;
}
if (isComparisonKnownFalse(Builtin, *IV)) {
if (!FalseVal) {
FalseVal = SILValue(B.createIntegerLiteral(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need the SILValue(...) here? I think just assigning the instruction should just work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using same style as Arnold's isComparisonKnownTrue - but I think yes, as you can see you create the instruction just once if you have multiple comparisons / exists

Builtin->getLoc(), Builtin->getType(), 0));
}
Builtin->replaceAllUsesWith(FalseVal);
Changed = true;
continue;
}
// Check whether a dominating check of the condition let's us
// replace
// the condition by false.
Expand Down
61 changes: 61 additions & 0 deletions test/SILOptimizer/abcopts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,67 @@ bb3:
return %23 : $Int32
}

// HOIST-LABEL: sil @always_false_hoist
// HOIST: bb4
// HOIST: builtin "sadd_with_overflow_Int32"
// HOIST: tuple_extract
// HOIST-NOT: builtin "cmp_slt_Int32"
// HOIST-NOT: builtin "cmp_sle_Int32"
// HOIST-NOT: builtin "cmp_eq_Int32"
// HOIST: cond_fail
// HOIST: cond_fail
// HOIST: cond_fail
// HOIST: cond_fail
// HOIST: builtin "cmp_eq_Int32"
// HOIST: cond_br
// HOIST: }
sil @always_false_hoist : $@convention(thin) (@owned Array<Int>) -> () {
bb0(%0 : $Array<Int>):
%z0 = integer_literal $Builtin.Int32, 0
%f1 = function_ref @getCount2 : $@convention(method) (@owned Array<Int>) -> Int32
retain_value %0 : $Array<Int>
%t1 = apply %f1(%0) : $@convention(method) (@owned Array<Int>) -> Int32
%c1 = struct_extract %t1 : $Int32, #Int32._value
%t2 = builtin "cmp_eq_Int32"(%z0 : $Builtin.Int32, %c1 : $Builtin.Int32) : $Builtin.Int1
cond_br %t2, bb5, bb1

bb1:
br bb2(%z0 : $Builtin.Int32)

bb2(%i0 : $Builtin.Int32):
cond_br undef, bb3, bb4

bb3:
%f2 = function_ref @checkbounds2 : $@convention(method) (Int32, Bool, @owned Array<Int>) -> _DependenceToken
retain_value %0 : $Array<Int>
%t3 = struct $Int32(%i0 : $Builtin.Int32)

br bb4

bb4:
%t5 = integer_literal $Builtin.Int1, 0
%i2 = integer_literal $Builtin.Int32, 1
%t6 = builtin "sadd_with_overflow_Int32"(%i0 : $Builtin.Int32, %i2 : $Builtin.Int32, %t5 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
%t7 = tuple_extract %t6 : $(Builtin.Int32, Builtin.Int1), 0
%t8 = tuple_extract %t6 : $(Builtin.Int32, Builtin.Int1), 1
cond_fail %t8 : $Builtin.Int1
%af1 = builtin "cmp_slt_Int32"(%i0 : $Builtin.Int32, %z0 : $Builtin.Int32) : $Builtin.Int1
cond_fail %af1 : $Builtin.Int1
%af2 = builtin "cmp_slt_Int32"(%t7 : $Builtin.Int32, %z0 : $Builtin.Int32) : $Builtin.Int1
cond_fail %af2 : $Builtin.Int1
%af3 = builtin "cmp_sle_Int32"(%t7 : $Builtin.Int32, %z0 : $Builtin.Int32) : $Builtin.Int1
cond_fail %af3 : $Builtin.Int1
%af4 = builtin "cmp_eq_Int32"(%t7 : $Builtin.Int32, %z0 : $Builtin.Int32) : $Builtin.Int1
cond_fail %af4 : $Builtin.Int1
%8 = builtin "cmp_eq_Int32"(%t7 : $Builtin.Int32, %c1 : $Builtin.Int32) : $Builtin.Int1
cond_br %8, bb5, bb2(%t7 : $Builtin.Int32)

bb5:
%r1 = tuple ()
return %r1 : $()
}


// HOIST-LABEL: sil @hoistinvariant

// Preheader.
Expand Down