/
vasm-simplify-arm.cpp
158 lines (127 loc) · 5.06 KB
/
vasm-simplify-arm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/vasm-simplify-internal.h"
#include "hphp/runtime/vm/jit/vasm.h"
#include "hphp/runtime/vm/jit/vasm-gen.h"
#include "hphp/runtime/vm/jit/vasm-instr.h"
#include "hphp/runtime/vm/jit/vasm-unit.h"
#include "hphp/runtime/vm/jit/vasm-util.h"
#include "hphp/vixl/a64/assembler-a64.h"
namespace HPHP { namespace jit { namespace arm {
namespace {
///////////////////////////////////////////////////////////////////////////////
template <typename Inst>
bool simplify(Env&, const Inst& /*inst*/, Vlabel /*b*/, size_t /*i*/) {
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool operand_one(Env& env, Vreg op) {
auto const op_it = env.unit.regToConst.find(op);
if (op_it == env.unit.regToConst.end()) return false;
auto const op_const = op_it->second;
if (op_const.isUndef) return false;
if (op_const.val != 1) return false;
return true;
}
// Reduce use of immediate one possibly removing def as dead code.
// Specific to ARM using hard-coded zero register.
template <typename Out, typename Inst>
bool cmov_fold_one(Env& env, const Inst& inst, Vlabel b, size_t i) {
if (operand_one(env, inst.f)) {
return simplify_impl(env, b, i, [&] (Vout& v) {
v << Out{inst.cc, inst.sf, PhysReg(vixl::wzr), inst.t, inst.d};
return 1;
});
}
if (operand_one(env, inst.t)) {
return simplify_impl(env, b, i, [&] (Vout& v) {
v << Out{ccNegate(inst.cc), inst.sf, PhysReg(vixl::wzr), inst.f, inst.d};
return 1;
});
}
return false;
}
bool simplify(Env& env, const cmovb& inst, Vlabel b, size_t i) {
return cmov_fold_one<csincb>(env, inst, b, i);
}
bool simplify(Env& env, const cmovw& inst, Vlabel b, size_t i) {
return cmov_fold_one<csincw>(env, inst, b, i);
}
bool simplify(Env& env, const cmovl& inst, Vlabel b, size_t i) {
return cmov_fold_one<csincl>(env, inst, b, i);
}
bool simplify(Env& env, const cmovq& inst, Vlabel b, size_t i) {
return cmov_fold_one<csincq>(env, inst, b, i);
}
///////////////////////////////////////////////////////////////////////////////
bool simplify(Env& env, const loadb& inst, Vlabel b, size_t i) {
return if_inst<Vinstr::movzbl>(env, b, i + 1, [&] (const movzbl& mov) {
// loadb{s, tmp}; movzbl{tmp, d}; -> loadzbl{s, d};
if (!(env.use_counts[inst.d] == 1 &&
inst.d == mov.s)) return false;
return simplify_impl(env, b, i, [&] (Vout& v) {
v << loadzbl{inst.s, mov.d};
return 2;
});
});
}
///////////////////////////////////////////////////////////////////////////////
bool simplify(Env& env, const ldimmq& inst, Vlabel b, size_t i) {
return if_inst<Vinstr::lea>(env, b, i + 1, [&] (const lea& ea) {
// ldimmq{s, index}; lea{base[index], d} -> lea{base[s],d}
if (!(env.use_counts[inst.d] == 1 &&
inst.s.q() <= 4095 &&
inst.s.q() >= -4095 &&
inst.d == ea.s.index &&
ea.s.disp == 0 &&
ea.s.base.isValid())) return false;
return simplify_impl(env, b, i, [&] (Vout& v) {
v << lea{ea.s.base[inst.s.l()], ea.d};
return 2;
});
});
}
///////////////////////////////////////////////////////////////////////////////
bool simplify(Env& env, const movzbl& inst, Vlabel b, size_t i) {
// movzbl{s, d}; shrli{2, s, d} --> ubfmli{2, 7, s, d}
return if_inst<Vinstr::shrli>(env, b, i + 1, [&](const shrli& sh) {
if (!(sh.s0.l() == 2 &&
env.use_counts[inst.d] == 1 &&
env.use_counts[sh.sf] == 0 &&
inst.d == sh.s1)) return false;
return simplify_impl(env, b, i, [&] (Vout& v) {
v << copy{inst.s, inst.d};
v << ubfmli{2, 7, inst.d, sh.d};
return 2;
});
});
}
///////////////////////////////////////////////////////////////////////////////
}
bool simplify(Env& env, Vlabel b, size_t i) {
assertx(i <= env.unit.blocks[b].code.size());
auto const& inst = env.unit.blocks[b].code[i];
switch (inst.op) {
#define O(name, ...) \
case Vinstr::name: \
return simplify(env, inst.name##_, b, i); \
VASM_OPCODES
#undef O
}
not_reached();
}
///////////////////////////////////////////////////////////////////////////////
}}}