Skip to content

[SILOptimizer] Remove Identity instructions - Peephole in SimplifyInstruction #5633

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
11 changes: 11 additions & 0 deletions lib/SILOptimizer/Analysis/SimplifyInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,20 @@ static SILValue simplifyBuiltin(BuiltinInst *BI) {
switch (Builtin.ID) {
default: break;

case BuiltinValueKind::SExtOrBitCast: {
const SILValue &Op = Args[0];
// extOrBitCast_N_N(x) -> x
if (Op->getType() == BI->getType())
return Op;
}
break;

case BuiltinValueKind::TruncOrBitCast: {
const SILValue &Op = Args[0];
SILValue Result;
// truncOrBitCast_N_N(x) -> x
if (Op->getType() == BI->getType())
return Op;
// trunc(extOrBitCast(x)) -> x
if (match(Op, m_ExtOrBitCast(m_SILValue(Result)))) {
// Truncated back to the same bits we started with.
Expand Down
22 changes: 22 additions & 0 deletions test/SILOptimizer/sil_simplify_instrs.sil
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ bb0(%x : $Builtin.Word):
// CHECK: return %0 : $Builtin.Word
}

// Simplify trunc((x)) -> x with same type
sil @fold_trunc_n_to_n : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
bb0(%x : $Builtin.Int64):
%trunc = builtin "truncOrBitCast_Int64_Word"(%x : $Builtin.Int64) : $Builtin.Int64
return %trunc : $Builtin.Int64

// CHECK-LABEL: sil @fold_trunc_n_to_n
// CHECK-NOT: builtin
// CHECK: return %0 : $Builtin.Int64
}

// Simplify sext((x)) -> x with same type
sil @fold_sext_n_to_n : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
bb0(%x : $Builtin.Int64):
%trunc = builtin "sextOrBitCast_Int64_Int64"(%x : $Builtin.Int64) : $Builtin.Int64
return %trunc : $Builtin.Int64

// CHECK-LABEL: sil @fold_sext_n_to_n
// CHECK-NOT: builtin
// CHECK: return %0 : $Builtin.Int64
}

class IntClass {
@sil_stored var value: Builtin.Int32 { get set }
init(value: Builtin.Int32)
Expand Down