codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / runtime / vm / jit / ir-instruction-inl.h
blob95007af9988054ccec7e0a5991d45dbdbffaa91c
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/vm/jit/edge.h"
19 #include <utility>
21 namespace HPHP { namespace jit {
22 ///////////////////////////////////////////////////////////////////////////////
24 inline IRInstruction::IRInstruction(Opcode op,
25 BCContext bcctx,
26 Edge* edges,
27 uint32_t numSrcs,
28 SSATmp** srcs)
29 : m_op(op)
30 , m_iroff(bcctx.iroff)
31 , m_numSrcs(numSrcs)
32 , m_numDsts(0)
33 , m_hasTypeParam{false}
34 , m_marker(bcctx.marker)
35 , m_srcs(srcs)
36 , m_dest(nullptr)
37 , m_edges(edges)
39 if (op != DefConst) {
40 // DefConst is the only opcode that's allowed to not have a marker, since
41 // it's not part of the instruction stream.
42 assertx(m_marker.valid());
46 ///////////////////////////////////////////////////////////////////////////////
48 inline bool IRInstruction::hasDst() const {
49 return opcodeHasFlags(op(), HasDest);
52 inline bool IRInstruction::naryDst() const {
53 return opcodeHasFlags(op(), NaryDest);
56 inline bool IRInstruction::consumesReferences() const {
57 return opcodeHasFlags(op(), ConsumesRC);
60 inline bool IRInstruction::mayRaiseError() const {
61 return opcodeHasFlags(op(), MayRaiseError);
64 inline bool IRInstruction::isTerminal() const {
65 return opcodeHasFlags(op(), Terminal);
68 inline bool IRInstruction::hasEdges() const {
69 return jit::hasEdges(op());
72 inline bool IRInstruction::isPassthrough() const {
73 return opcodeHasFlags(op(), Passthrough);
76 inline bool IRInstruction::producesReference() const {
77 return opcodeHasFlags(op(), ProducesRC);
80 inline SSATmp* IRInstruction::getPassthroughValue() const {
81 assertx(isPassthrough());
82 assertx(is(IncRef,
83 CheckType, AssertType, AssertNonNull,
84 MapAddElemC, ColAddNewElemC,
85 Mov));
86 return src(0);
89 ///////////////////////////////////////////////////////////////////////////////
91 inline uint32_t IRInstruction::id() const {
92 assertx(m_id != kTransient);
93 return m_id;
96 inline bool IRInstruction::isTransient() const {
97 return m_id == kTransient;
100 inline uint16_t IRInstruction::iroff() const {
101 return m_iroff;
104 template<typename... Args>
105 bool IRInstruction::is(Opcode op, Args&&... args) const {
106 return m_op == op || is(std::forward<Args>(args)...);
109 inline bool IRInstruction::is() const {
110 return false;
113 inline Opcode IRInstruction::op() const {
114 return m_op;
117 inline const BCMarker& IRInstruction::marker() const {
118 return m_marker;
121 inline BCMarker& IRInstruction::marker() {
122 return m_marker;
125 inline BCContext IRInstruction::bcctx() const {
126 return BCContext { m_marker, m_iroff };
129 inline const Func* IRInstruction::func() const {
130 return m_marker.hasFunc() ? m_marker.func() : nullptr;
133 inline const Class* IRInstruction::ctx() const {
134 return m_marker.hasFunc() ? m_marker.func()->cls() : nullptr;
137 inline bool IRInstruction::hasTypeParam() const { return m_hasTypeParam; }
139 inline Type IRInstruction::typeParam() const {
140 assertx(m_hasTypeParam);
141 return m_typeParam;
144 inline void IRInstruction::setTypeParam(Type t) {
145 m_hasTypeParam = true;
146 m_typeParam = t;
149 ///////////////////////////////////////////////////////////////////////////////
151 inline void IRInstruction::initializeSrcs(uint32_t numSrcs, SSATmp** srcs) {
152 assertx(!m_srcs && !m_numSrcs);
153 m_numSrcs = numSrcs;
154 m_srcs = srcs;
157 inline uint32_t IRInstruction::numSrcs() const {
158 return m_numSrcs;
161 inline uint32_t IRInstruction::numDsts() const {
162 return m_numDsts;
165 inline SSATmp* IRInstruction::src(uint32_t i) const {
166 always_assert(i < numSrcs());
167 return m_srcs[i];
170 inline SSATmp* IRInstruction::dst() const {
171 assertx(!naryDst());
172 return m_dest;
175 inline folly::Range<SSATmp**> IRInstruction::srcs() const {
176 return folly::Range<SSATmp**>(m_srcs, m_numSrcs);
179 inline folly::Range<SSATmp**> IRInstruction::dsts() {
180 assertx(naryDst() || m_numDsts <= 1);
181 if (hasDst()) return folly::Range<SSATmp**>(&m_dest, m_numDsts);
182 return folly::Range<SSATmp**>(m_dsts, m_numDsts);
185 inline void IRInstruction::setSrc(uint32_t i, SSATmp* newSrc) {
186 always_assert(i < numSrcs());
187 m_srcs[i] = newSrc;
190 inline void IRInstruction::setSrcs(uint32_t numSrcs, SSATmp** newSrcs) {
191 m_numSrcs = numSrcs;
192 m_srcs = newSrcs;
195 inline void IRInstruction::deleteSrc(uint32_t i) {
196 always_assert(i < numSrcs());
197 std::copy(m_srcs + i + 1, m_srcs + m_numSrcs, m_srcs + i);
198 --m_numSrcs;
201 inline void IRInstruction::setDst(SSATmp* newDst) {
202 assertx(hasDst());
203 m_dest = newDst;
204 m_numDsts = newDst ? 1 : 0;
207 inline void IRInstruction::setDsts(uint32_t numDsts, SSATmp** newDsts) {
208 assertx(naryDst());
209 m_numDsts = numDsts;
210 m_dsts = newDsts;
213 inline void IRInstruction::deleteDst(uint32_t i) {
214 always_assert(i < numDsts());
215 assertx(naryDst());
216 std::copy(m_dsts + i + 1, m_dsts + m_numDsts, m_dsts + i);
217 --m_numDsts;
220 ///////////////////////////////////////////////////////////////////////////////
222 inline bool IRInstruction::hasExtra() const {
223 return m_extra;
226 template<Opcode opc>
227 const typename IRExtraDataType<opc>::type* IRInstruction::extra() const {
228 assertx(opc == op() && "ExtraData type error");
229 assertx(m_extra != nullptr);
230 return static_cast<typename IRExtraDataType<opc>::type*>(m_extra);
233 template<Opcode opc>
234 typename IRExtraDataType<opc>::type* IRInstruction::extra() {
235 assertx(opc == op() && "ExtraData type error");
236 return static_cast<typename IRExtraDataType<opc>::type*>(m_extra);
239 template<class T>
240 const T* IRInstruction::extra() const {
241 if (debug) assert_opcode_extra<T>(op());
242 return static_cast<const T*>(m_extra);
245 inline const IRExtraData* IRInstruction::rawExtra() const {
246 return m_extra;
249 inline void IRInstruction::setExtra(IRExtraData* data) {
250 assertx(!m_extra);
251 m_extra = data;
254 inline void IRInstruction::clearExtra() {
255 m_extra = nullptr;
258 ///////////////////////////////////////////////////////////////////////////////
260 inline Block* IRInstruction::block() const {
261 return m_block;
264 inline void IRInstruction::setBlock(Block* b) {
265 m_block = b;
268 inline Block* IRInstruction::next() const {
269 return succ(0);
272 inline Edge* IRInstruction::nextEdge() {
273 return succEdge(0);
276 inline void IRInstruction::setNext(Block* b) {
277 return setSucc(0, b);
280 inline Block* IRInstruction::taken() const {
281 return succ(1);
284 inline Edge* IRInstruction::takenEdge() {
285 return succEdge(1);
288 inline void IRInstruction::setTaken(Block* b) {
289 return setSucc(1, b);
292 inline bool IRInstruction::isControlFlow() const {
293 return bool(taken());
296 inline bool IRInstruction::isBlockEnd() const {
297 return taken() || isTerminal();
300 inline Block* IRInstruction::succ(int i) const {
301 assertx(!m_edges || hasEdges());
302 return m_edges ? m_edges[i].to() : nullptr;
305 inline Edge* IRInstruction::succEdge(int i) {
306 assertx(!m_edges || hasEdges());
307 return m_edges && m_edges[i].to() ? &m_edges[i] : nullptr;
310 inline void IRInstruction::setSucc(int i, Block* b) {
311 if (hasEdges()) {
312 if (isTransient()) m_edges[i].setTransientTo(b);
313 else m_edges[i].setTo(b);
314 } else {
315 assertx(!b && !m_edges);
319 inline void IRInstruction::clearEdges() {
320 setSucc(0, nullptr);
321 setSucc(1, nullptr);
322 m_edges = nullptr;
325 ///////////////////////////////////////////////////////////////////////////////