Skip to content

Commit e524979

Browse files
author
Joe Shajrawi
authored
Merge pull request swiftlang#5633 from shajrawi/identity_SimplifyInstruction
[SILOptimizer] Remove Identity instructions - Peephole in SimplifyInstruction
2 parents f7d94d9 + a9b7405 commit e524979

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,20 @@ static SILValue simplifyBuiltin(BuiltinInst *BI) {
477477
switch (Builtin.ID) {
478478
default: break;
479479

480+
case BuiltinValueKind::SExtOrBitCast: {
481+
const SILValue &Op = Args[0];
482+
// extOrBitCast_N_N(x) -> x
483+
if (Op->getType() == BI->getType())
484+
return Op;
485+
}
486+
break;
487+
480488
case BuiltinValueKind::TruncOrBitCast: {
481489
const SILValue &Op = Args[0];
482490
SILValue Result;
491+
// truncOrBitCast_N_N(x) -> x
492+
if (Op->getType() == BI->getType())
493+
return Op;
483494
// trunc(extOrBitCast(x)) -> x
484495
if (match(Op, m_ExtOrBitCast(m_SILValue(Result)))) {
485496
// Truncated back to the same bits we started with.

test/SILOptimizer/sil_simplify_instrs.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ bb0(%x : $Builtin.Word):
9696
// CHECK: return %0 : $Builtin.Word
9797
}
9898

99+
// Simplify trunc((x)) -> x with same type
100+
sil @fold_trunc_n_to_n : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
101+
bb0(%x : $Builtin.Int64):
102+
%trunc = builtin "truncOrBitCast_Int64_Word"(%x : $Builtin.Int64) : $Builtin.Int64
103+
return %trunc : $Builtin.Int64
104+
105+
// CHECK-LABEL: sil @fold_trunc_n_to_n
106+
// CHECK-NOT: builtin
107+
// CHECK: return %0 : $Builtin.Int64
108+
}
109+
110+
// Simplify sext((x)) -> x with same type
111+
sil @fold_sext_n_to_n : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
112+
bb0(%x : $Builtin.Int64):
113+
%trunc = builtin "sextOrBitCast_Int64_Int64"(%x : $Builtin.Int64) : $Builtin.Int64
114+
return %trunc : $Builtin.Int64
115+
116+
// CHECK-LABEL: sil @fold_sext_n_to_n
117+
// CHECK-NOT: builtin
118+
// CHECK: return %0 : $Builtin.Int64
119+
}
120+
99121
class IntClass {
100122
@sil_stored var value: Builtin.Int32 { get set }
101123
init(value: Builtin.Int32)

0 commit comments

Comments
 (0)