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/CodeGen/TargetMachine.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 //---------------------------------------------------------------------------
62 class MachineOperand {
64 enum MachineOperandType {
65 MO_VirtualRegister, // virtual register for *value
66 MO_MachineRegister, // pre-assigned machine register `regNum'
74 MachineOperandType opType;
77 Value* value; // BasicBlockVal for a label operand.
78 // ConstantVal for a non-address immediate.
79 // Virtual register for an SSA operand,
80 // including hidden operands required for
81 // the generated machine code.
83 unsigned int regNum; // register number for an explicit register
85 int64_t immedVal; // constant value for an explicit constant
88 bool isDef; // is this a defition for the value
89 // made public for faster access
92 /*ctor*/ MachineOperand ();
93 /*ctor*/ MachineOperand (MachineOperandType operandType,
95 /*copy ctor*/ MachineOperand (const MachineOperand&);
96 /*dtor*/ ~MachineOperand () {}
98 // Accessor methods. Caller is responsible for checking the
99 // operand type before invoking the corresponding accessor.
101 inline MachineOperandType getOperandType () const {
104 inline Value* getVRegValue () const {
105 assert(opType == MO_VirtualRegister || opType == MO_CCRegister);
108 inline unsigned int getMachineRegNum() const {
109 assert(opType == MO_MachineRegister);
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 MachineOperand::MachineOperand()
143 : opType(MO_VirtualRegister),
151 MachineOperand::MachineOperand(MachineOperandType operandType,
153 : opType(operandType),
161 MachineOperand::MachineOperand(const MachineOperand& mo)
166 case MO_VirtualRegister:
167 case MO_CCRegister: value = mo.value; break;
168 case MO_MachineRegister: regNum = mo.regNum; break;
169 case MO_SignExtendedImmed:
170 case MO_UnextendedImmed:
171 case MO_PCRelativeDisp: immedVal = mo.immedVal; break;
177 MachineOperand::Initialize(MachineOperandType operandType,
180 opType = operandType;
185 MachineOperand::InitializeConst(MachineOperandType operandType,
188 opType = operandType;
194 MachineOperand::InitializeReg(unsigned int _regNum)
196 opType = MO_MachineRegister;
202 //---------------------------------------------------------------------------
203 // class MachineInstr
206 // Representation of each machine instruction.
208 // MachineOpCode must be an enum, defined separately for each target.
209 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
211 // opCodeMask is used to record variants of an instruction.
212 // E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
213 // ANNUL: if 1: Annul delay slot instruction.
214 // PREDICT-NOT-TAKEN: if 1: predict branch not taken.
215 // Instead of creating 4 different opcodes for BNZ, we create a single
216 // opcode and set bits in opCodeMask for each of these flags.
217 //---------------------------------------------------------------------------
219 class MachineInstr : public NonCopyable {
221 MachineOpCode opCode;
222 OpCodeMask opCodeMask; // extra bits for variants of an opcode
223 vector<MachineOperand> operands;
226 typedef ValOpIterator<const MachineInstr, const Value> val_op_const_iterator;
227 typedef ValOpIterator< MachineInstr, Value> val_op_iterator;
230 /*ctor*/ MachineInstr (MachineOpCode _opCode,
231 OpCodeMask _opCodeMask = 0x0);
232 /*ctor*/ MachineInstr (MachineOpCode _opCode,
233 unsigned numOperands,
234 OpCodeMask _opCodeMask = 0x0);
235 inline ~MachineInstr () {}
237 const MachineOpCode getOpCode () const;
239 unsigned int getNumOperands () const;
241 const MachineOperand& getOperand (unsigned int i) const;
242 MachineOperand& getOperand (unsigned int i);
244 bool operandIsDefined(unsigned int i) const;
246 void dump (unsigned int indent = 0) const;
249 friend ostream& operator<<(ostream& os, const MachineInstr& minstr);
250 friend val_op_const_iterator;
251 friend val_op_iterator;
254 // Access to set the operands when building the machine instruction
255 void SetMachineOperand(unsigned int i,
256 MachineOperand::MachineOperandType operandType,
257 Value* _val, bool isDef=false);
258 void SetMachineOperand(unsigned int i,
259 MachineOperand::MachineOperandType operandType,
260 int64_t intValue, bool isDef=false);
261 void SetMachineOperand(unsigned int i,
266 inline const MachineOpCode
267 MachineInstr::getOpCode() const
273 MachineInstr::getNumOperands() const
275 return operands.size();
278 inline MachineOperand&
279 MachineInstr::getOperand(unsigned int i)
281 assert(i < operands.size() && "getOperand() out of range!");
285 inline const MachineOperand&
286 MachineInstr::getOperand(unsigned int i) const
288 assert(i < operands.size() && "getOperand() out of range!");
293 MachineInstr::operandIsDefined(unsigned int i) const
295 return getOperand(i).opIsDef();
299 template<class _MI, class _V>
300 class ValOpIterator : public std::forward_iterator<_V, ptrdiff_t> {
306 inline void skipToNextVal() {
307 while (i < minstr->getNumOperands() &&
308 ! ((minstr->operands[i].opType == MachineOperand::MO_VirtualRegister
309 || minstr->operands[i].opType == MachineOperand::MO_CCRegister)
310 && minstr->operands[i].value != NULL))
315 typedef ValOpIterator<_MI, _V> _Self;
317 inline ValOpIterator(_MI* _minstr) : i(0), minstr(_minstr) {
318 resultPos = TargetInstrDescriptors[minstr->opCode].resultPos;
322 inline _V* operator*() const { return minstr->getOperand(i).getVRegValue();}
323 inline _V* operator->() const { return operator*(); }
324 // inline bool isDef () const { return (((int) i) == resultPos); }
326 inline bool isDef () const { return minstr->getOperand(i).isDef; }
327 inline bool done () const { return (i == minstr->getNumOperands()); }
329 inline _Self& operator++() { i++; skipToNextVal(); return *this; }
330 inline _Self operator++(int) { _Self tmp = *this; ++*this; return tmp; }
334 //---------------------------------------------------------------------------
335 // class MachineCodeForVMInstr
338 // Representation of the sequence of machine instructions created
339 // for a single VM instruction. Additionally records any temporary
340 // "values" used as intermediate values in this sequence.
341 // Note that such values should be treated as pure SSA values with
342 // no interpretation of their operands (i.e., as a TmpInstruction object
343 // which actually represents such a value).
345 //---------------------------------------------------------------------------
347 class MachineCodeForVMInstr: public vector<MachineInstr*>
350 vector<Value*> tempVec;
353 /*ctor*/ MachineCodeForVMInstr () {}
354 /*ctor*/ ~MachineCodeForVMInstr ();
356 const vector<Value*>&
357 getTempValues () const { return tempVec; }
359 void addTempValue (Value* val)
360 { tempVec.push_back(val); }
362 // dropAllReferences() - This function drops all references within
363 // temporary (hidden) instructions created in implementing the original
364 // VM intruction. This ensures there are no remaining "uses" within
365 // these hidden instructions, before the values of a method are freed.
367 // Make this inline because it has to be called from class Instruction
368 // and inlining it avoids a serious circurality in link order.
369 inline void dropAllReferences() {
370 for (unsigned i=0, N=tempVec.size(); i < N; i++)
371 if (tempVec[i]->getValueType() == Value::InstructionVal)
372 ((Instruction*) tempVec[i])->dropAllReferences();
377 MachineCodeForVMInstr::~MachineCodeForVMInstr()
379 // Free the Value objects created to hold intermediate values
380 for (unsigned i=0, N=tempVec.size(); i < N; i++)
383 // Free the MachineInstr objects allocated, if any.
384 for (unsigned i=0, N=this->size(); i < N; i++)
389 //---------------------------------------------------------------------------
390 // class MachineCodeForBasicBlock
393 // Representation of the sequence of machine instructions created
394 // for a basic block.
395 //---------------------------------------------------------------------------
398 class MachineCodeForBasicBlock: public vector<const MachineInstr*> {
400 typedef vector<const MachineInstr*>::iterator iterator;
401 typedef vector<const MachineInstr*>::const_iterator const_iterator;
405 //---------------------------------------------------------------------------
406 // Target-independent utility routines for creating machine instructions
407 //---------------------------------------------------------------------------
410 //------------------------------------------------------------------------
411 // Function Set2OperandsFromInstr
412 // Function Set3OperandsFromInstr
414 // For the common case of 2- and 3-operand arithmetic/logical instructions,
415 // set the m/c instr. operands directly from the VM instruction's operands.
416 // Check whether the first or second operand is 0 and can use a dedicated
418 // Check whether the second operand should use an immediate field or register.
419 // (First and third operands are never immediates for such instructions.)
422 // canDiscardResult: Specifies that the result operand can be discarded
423 // by using the dedicated "0"
425 // op1position, op2position and resultPosition: Specify in which position
426 // in the machine instruction the 3 operands (arg1, arg2
427 // and result) should go.
429 // RETURN VALUE: unsigned int flags, where
430 // flags & 0x01 => operand 1 is constant and needs a register
431 // flags & 0x02 => operand 2 is constant and needs a register
432 //------------------------------------------------------------------------
434 void Set2OperandsFromInstr (MachineInstr* minstr,
435 InstructionNode* vmInstrNode,
436 const TargetMachine& targetMachine,
437 bool canDiscardResult = false,
439 int resultPosition = 1);
441 void Set3OperandsFromInstr (MachineInstr* minstr,
442 InstructionNode* vmInstrNode,
443 const TargetMachine& targetMachine,
444 bool canDiscardResult = false,
447 int resultPosition = 2);
449 MachineOperand::MachineOperandType
450 ChooseRegOrImmed(Value* val,
451 MachineOpCode opCode,
452 const TargetMachine& targetMachine,
454 unsigned int& getMachineRegNum,
455 int64_t& getImmedValue);
458 ostream& operator<<(ostream& os, const MachineInstr& minstr);
461 ostream& operator<<(ostream& os, const MachineOperand& mop);
464 void PrintMachineInstructions (Method* method);
467 //**************************************************************************/