2 //***************************************************************************
12 // 7/2/01 - Vikram Adve - Created
13 //**************************************************************************/
15 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
16 #define LLVM_CODEGEN_MACHINEINSTR_H
19 #include "llvm/CodeGen/InstrForest.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/NonCopyable.h"
22 #include "llvm/Target/MachineInstrInfo.h"
24 template<class _MI, class _V> class ValOpIterator;
27 //---------------------------------------------------------------------------
28 // class MachineOperand
31 // Representation of each machine instruction operand.
32 // This class is designed so that you can allocate a vector of operands
33 // first and initialize each one later.
35 // E.g, for this VM instruction:
36 // ptr = alloca type, numElements
37 // we generate 2 machine instructions on the SPARC:
39 // mul Constant, Numelements -> Reg
40 // add %sp, Reg -> Ptr
42 // Each instruction has 3 operands, listed above. Of those:
43 // - Reg, NumElements, and Ptr are of operand type MO_Register.
44 // - Constant is of operand type MO_SignExtendedImmed on the SPARC.
46 // For the register operands, the virtual register type is as follows:
48 // - Reg will be of virtual register type MO_MInstrVirtualReg. The field
49 // MachineInstr* minstr will point to the instruction that computes reg.
51 // - %sp will be of virtual register type MO_MachineReg.
52 // The field regNum identifies the machine register.
54 // - NumElements will be of virtual register type MO_VirtualReg.
55 // The field Value* value identifies the value.
57 // - Ptr will also be of virtual register type MO_VirtualReg.
58 // Again, the field Value* value identifies the value.
60 //---------------------------------------------------------------------------
63 class MachineOperand {
65 enum MachineOperandType {
66 MO_VirtualRegister, // virtual register for *value
67 MO_MachineRegister, // pre-assigned machine register `regNum'
75 MachineOperandType opType;
78 Value* value; // BasicBlockVal for a label operand.
79 // ConstantVal for a non-address immediate.
80 // Virtual register for an SSA operand,
81 // including hidden operands required for
82 // the generated machine code.
83 int64_t immedVal; // constant value for an explicit constant
86 int regNum; // register number for an explicit register
87 // will be set for a value after reg allocation
88 bool isDef; // is this a defition for the value
91 /*ctor*/ MachineOperand ();
92 /*ctor*/ MachineOperand (MachineOperandType operandType,
94 /*copy ctor*/ MachineOperand (const MachineOperand&);
95 /*dtor*/ ~MachineOperand () {}
97 // Accessor methods. Caller is responsible for checking the
98 // operand type before invoking the corresponding accessor.
100 inline MachineOperandType getOperandType () const {
103 inline Value* getVRegValue () const {
104 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
105 opType == MO_PCRelativeDisp);
108 inline unsigned int getMachineRegNum() const {
109 assert(opType == MO_MachineRegister);
110 return (unsigned) regNum;
112 inline int64_t getImmedValue () const {
113 assert(opType >= MO_SignExtendedImmed || opType <= MO_PCRelativeDisp);
116 inline bool opIsDef () const {
121 friend ostream& operator<<(ostream& os, const MachineOperand& mop);
125 // These functions are provided so that a vector of operands can be
126 // statically allocated and individual ones can be initialized later.
127 // Give class MachineInstr gets access to these functions.
129 void Initialize (MachineOperandType operandType,
131 void InitializeConst (MachineOperandType operandType,
133 void InitializeReg (unsigned int regNum);
135 friend class MachineInstr;
136 friend class ValOpIterator<const MachineInstr, const Value>;
137 friend class ValOpIterator< MachineInstr, Value>;
142 // replaces the Value with its corresponding physical register afeter
143 // register allocation is complete
144 void setRegForValue(int reg) {
145 assert(opType == MO_VirtualRegister || opType == MO_CCRegister);
149 // used to get the reg number if when one is allocted (must be
150 // called only after reg alloc)
151 inline int getAllocatedRegNum() const {
152 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
153 opType == MO_MachineRegister);
162 MachineOperand::MachineOperand()
163 : opType(MO_VirtualRegister),
170 MachineOperand::MachineOperand(MachineOperandType operandType,
172 : opType(operandType),
180 MachineOperand::MachineOperand(const MachineOperand& mo)
185 case MO_VirtualRegister:
186 case MO_CCRegister: value = mo.value; break;
187 case MO_MachineRegister: regNum = mo.regNum; break;
188 case MO_SignExtendedImmed:
189 case MO_UnextendedImmed:
190 case MO_PCRelativeDisp: immedVal = mo.immedVal; break;
196 MachineOperand::Initialize(MachineOperandType operandType,
199 opType = operandType;
205 MachineOperand::InitializeConst(MachineOperandType operandType,
208 opType = operandType;
215 MachineOperand::InitializeReg(unsigned int _regNum)
217 opType = MO_MachineRegister;
219 regNum = (int) _regNum;
223 //---------------------------------------------------------------------------
224 // class MachineInstr
227 // Representation of each machine instruction.
229 // MachineOpCode must be an enum, defined separately for each target.
230 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
232 // opCodeMask is used to record variants of an instruction.
233 // E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
234 // ANNUL: if 1: Annul delay slot instruction.
235 // PREDICT-NOT-TAKEN: if 1: predict branch not taken.
236 // Instead of creating 4 different opcodes for BNZ, we create a single
237 // opcode and set bits in opCodeMask for each of these flags.
239 // There are 2 kinds of operands:
241 // (1) Explicit operands of the machine instruction in vector operands[]
243 // (2) "Implicit operands" are values implicitly used or defined by the
244 // machine instruction, such as arguments to a CALL, return value of
245 // a CALL (if any), and return value of a RETURN.
246 //---------------------------------------------------------------------------
248 class MachineInstr : public NonCopyable {
250 MachineOpCode opCode;
251 OpCodeMask opCodeMask; // extra bits for variants of an opcode
252 vector<MachineOperand> operands;
253 vector<Value*> implicitRefs; // values implicitly referenced by this
254 vector<bool> implicitIsDef; // machine instruction (eg, call args)
257 typedef ValOpIterator<const MachineInstr, const Value> val_op_const_iterator;
258 typedef ValOpIterator<const MachineInstr, Value> val_op_iterator;
261 /*ctor*/ MachineInstr (MachineOpCode _opCode,
262 OpCodeMask _opCodeMask = 0x0);
263 /*ctor*/ MachineInstr (MachineOpCode _opCode,
264 unsigned numOperands,
265 OpCodeMask _opCodeMask = 0x0);
266 inline ~MachineInstr () {}
267 const MachineOpCode getOpCode () const { return opCode; }
270 // Information about explicit operands of the instruction
272 unsigned int getNumOperands () const { return operands.size(); }
274 bool operandIsDefined(unsigned int i) const;
276 const MachineOperand& getOperand (unsigned int i) const;
277 MachineOperand& getOperand (unsigned int i);
280 // Information about implicit operands of the instruction
282 unsigned int getNumImplicitRefs() const{return implicitRefs.size();}
284 bool implicitRefIsDefined(unsigned int i) const;
286 const Value* getImplicitRef (unsigned int i) const;
287 Value* getImplicitRef (unsigned int i);
292 void dump (unsigned int indent = 0) const;
296 friend ostream& operator<<(ostream& os, const MachineInstr& minstr);
297 friend val_op_const_iterator;
298 friend val_op_iterator;
301 // Access to set the operands when building the machine instruction
302 void SetMachineOperand(unsigned int i,
303 MachineOperand::MachineOperandType operandType,
304 Value* _val, bool isDef=false);
305 void SetMachineOperand(unsigned int i,
306 MachineOperand::MachineOperandType operandType,
307 int64_t intValue, bool isDef=false);
308 void SetMachineOperand(unsigned int i,
312 void addImplicitRef (Value* val,
315 void setImplicitRef (unsigned int i,
321 inline MachineOperand&
322 MachineInstr::getOperand(unsigned int i)
324 assert(i < operands.size() && "getOperand() out of range!");
328 inline const MachineOperand&
329 MachineInstr::getOperand(unsigned int i) const
331 assert(i < operands.size() && "getOperand() out of range!");
336 MachineInstr::operandIsDefined(unsigned int i) const
338 return getOperand(i).opIsDef();
342 MachineInstr::implicitRefIsDefined(unsigned int i) const
344 assert(i < implicitIsDef.size() && "operand out of range!");
345 return implicitIsDef[i];
349 MachineInstr::getImplicitRef(unsigned int i) const
351 assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
352 return implicitRefs[i];
356 MachineInstr::getImplicitRef(unsigned int i)
358 assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
359 return implicitRefs[i];
363 MachineInstr::addImplicitRef(Value* val,
366 implicitRefs.push_back(val);
367 implicitIsDef.push_back(isDef);
371 MachineInstr::setImplicitRef(unsigned int i,
375 assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
376 implicitRefs[i] = val;
377 implicitIsDef[i] = isDef;
381 template<class _MI, class _V>
382 class ValOpIterator : public std::forward_iterator<_V, ptrdiff_t> {
388 inline void skipToNextVal() {
389 while (i < minstr->getNumOperands() &&
390 ! ((minstr->operands[i].opType == MachineOperand::MO_VirtualRegister
391 || minstr->operands[i].opType == MachineOperand::MO_CCRegister)
392 && minstr->operands[i].value != NULL))
397 typedef ValOpIterator<_MI, _V> _Self;
399 inline ValOpIterator(_MI* _minstr) : i(0), minstr(_minstr) {
400 resultPos = TargetInstrDescriptors[minstr->opCode].resultPos;
404 inline _V* operator*() const { return minstr->getOperand(i).getVRegValue();}
406 const MachineOperand & getMachineOperand() const { return minstr->getOperand(i); }
408 inline _V* operator->() const { return operator*(); }
409 // inline bool isDef () const { return (((int) i) == resultPos); }
411 inline bool isDef () const { return minstr->getOperand(i).isDef; }
412 inline bool done () const { return (i == minstr->getNumOperands()); }
414 inline _Self& operator++() { i++; skipToNextVal(); return *this; }
415 inline _Self operator++(int) { _Self tmp = *this; ++*this; return tmp; }
419 //---------------------------------------------------------------------------
420 // class MachineCodeForVMInstr
423 // Representation of the sequence of machine instructions created
424 // for a single VM instruction. Additionally records information
425 // about hidden and implicit values used by the machine instructions:
426 // about hidden values used by the machine instructions:
428 // "Temporary values" are intermediate values used in the machine
429 // instruction sequence, but not in the VM instruction
430 // Note that such values should be treated as pure SSA values with
431 // no interpretation of their operands (i.e., as a TmpInstruction
432 // object which actually represents such a value).
434 // (2) "Implicit uses" are values used in the VM instruction but not in
435 // the machine instruction sequence
437 //---------------------------------------------------------------------------
439 class MachineCodeForVMInstr: public vector<MachineInstr*>
442 vector<Value*> tempVec; // used by m/c instr but not VM instr
445 /*ctor*/ MachineCodeForVMInstr () {}
446 /*ctor*/ ~MachineCodeForVMInstr ();
448 const vector<Value*>& getTempValues () const { return tempVec; }
449 vector<Value*>& getTempValues () { return tempVec; }
451 void addTempValue (Value* val) { tempVec.push_back(val); }
453 // dropAllReferences() - This function drops all references within
454 // temporary (hidden) instructions created in implementing the original
455 // VM intruction. This ensures there are no remaining "uses" within
456 // these hidden instructions, before the values of a method are freed.
458 // Make this inline because it has to be called from class Instruction
459 // and inlining it avoids a serious circurality in link order.
460 inline void dropAllReferences() {
461 for (unsigned i=0, N=tempVec.size(); i < N; i++)
462 if (Instruction *I = dyn_cast<Instruction>(tempVec[i]))
463 I->dropAllReferences();
468 MachineCodeForVMInstr::~MachineCodeForVMInstr()
470 // Free the Value objects created to hold intermediate values
471 for (unsigned i=0, N=tempVec.size(); i < N; i++)
474 // Free the MachineInstr objects allocated, if any.
475 for (unsigned i=0, N=this->size(); i < N; i++)
480 //---------------------------------------------------------------------------
481 // class MachineCodeForBasicBlock
484 // Representation of the sequence of machine instructions created
485 // for a basic block.
486 //---------------------------------------------------------------------------
489 class MachineCodeForBasicBlock: public vector<MachineInstr*> {
491 typedef vector<MachineInstr*>::iterator iterator;
492 typedef vector<const MachineInstr*>::const_iterator const_iterator;
496 //---------------------------------------------------------------------------
498 //---------------------------------------------------------------------------
501 ostream& operator<< (ostream& os, const MachineInstr& minstr);
504 ostream& operator<< (ostream& os, const MachineOperand& mop);
507 void PrintMachineInstructions(const Method *method);
510 //**************************************************************************/