MachineInstr.h revision ce3d3eadef1c2a72a0a6e16735e5091fb1e6dc16
1a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner//===-- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*--=//
2a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner//
3a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner// This file contains the declaration of the MachineInstr class, which is the
4a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner// basic representation for all target dependant machine instructions used by
5a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner// the back end.
6a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner//
7a730c864227b287cdfad5f5f3d5d0808c9f422bbChris Lattner//===----------------------------------------------------------------------===//
823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve#ifndef LLVM_CODEGEN_MACHINEINSTR_H
1023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve#define LLVM_CODEGEN_MACHINEINSTR_H
1123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
128eb04905e84470a0baff867fa99b0de097f49a3bVikram S. Adve#include "llvm/Annotation.h"
134a63b72df95b5c0d4af064cef19377f811ba6060Chris Lattner#include "Support/iterator"
14054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattner#include "Support/NonCopyable.h"
15054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattner#include <vector>
16054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattnerclass Value;
17054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattnerclass Function;
187db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattnerclass MachineBasicBlock;
19fa78fbf446b505767e838f9c188707183c57fc9cChris Lattnerclass TargetMachine;
20054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattner
21054c1f6cb6f3a680fe4b8447880ed960fd7fe441Chris Lattnertypedef int MachineOpCode;
226a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve
2332f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner/// MOTy - MachineOperandType - This namespace contains an enum that describes
2432f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner/// how the machine operand is used by the instruction: is it read, defined, or
2532f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner/// both?  Note that the MachineInstr/Operator class currently uses bool
2632f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner/// arguments to represent this information instead of an enum.  Eventually this
2732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner/// should change over to use this _easier to read_ representation instead.
2832f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner///
2932f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattnernamespace MOTy {
3032f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  enum UseType {
3132f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    Use,             /// This machine operand is only read by the instruction
3232f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    Def,             /// This machine operand is only written by the instruction
3332f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    UseAndDef        /// This machine operand is read AND written
3432f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  };
3532f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner}
3632f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner
3723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
3823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve// class MachineOperand
3923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
4023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve// Purpose:
4123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   Representation of each machine instruction operand.
4223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   This class is designed so that you can allocate a vector of operands
4323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   first and initialize each one later.
4423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
4523ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   E.g, for this VM instruction:
4623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//		ptr = alloca type, numElements
4723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   we generate 2 machine instructions on the SPARC:
4823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
4923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//		mul Constant, Numelements -> Reg
5023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//		add %sp, Reg -> Ptr
5123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
5223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   Each instruction has 3 operands, listed above.  Of those:
5323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -	Reg, NumElements, and Ptr are of operand type MO_Register.
5423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -	Constant is of operand type MO_SignExtendedImmed on the SPARC.
5523ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
5623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   For the register operands, the virtual register type is as follows:
5723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
5823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
5923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//	MachineInstr* minstr will point to the instruction that computes reg.
6023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
6123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -	%sp will be of virtual register type MO_MachineReg.
6223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//	The field regNum identifies the machine register.
6323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
6423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -	NumElements will be of virtual register type MO_VirtualReg.
6523ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//	The field Value* value identifies the value.
6623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
6723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   -	Ptr will also be of virtual register type MO_VirtualReg.
6823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//	Again, the field Value* value identifies the value.
6923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
7023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
7123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
7223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adveclass MachineOperand {
7323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Advepublic:
7423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve  enum MachineOperandType {
756a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    MO_VirtualRegister,		// virtual register for *value
766a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    MO_MachineRegister,		// pre-assigned machine register `regNum'
7723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve    MO_CCRegister,
7823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve    MO_SignExtendedImmed,
7923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve    MO_UnextendedImmed,
8023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve    MO_PCRelativeDisp,
8123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve  };
8223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
836a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adveprivate:
8469cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  // Bit fields of the flags variable used for different operand properties
8569cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char DEFFLAG    = 0x1;  // this is a def of the operand
8669cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char DEFUSEFLAG = 0x2;  // this is both a def and a use
8769cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char HIFLAG32   = 0x4;  // operand is %hi32(value_or_immedVal)
8869cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char LOFLAG32   = 0x8;  // operand is %lo32(value_or_immedVal)
8969cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char HIFLAG64   = 0x10; // operand is %hi64(value_or_immedVal)
9069cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  static const char LOFLAG64   = 0x20; // operand is %lo64(value_or_immedVal)
9169cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve
9269cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adveprivate:
936a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  union {
946a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    Value*	value;		// BasicBlockVal for a label operand.
9523ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve				// ConstantVal for a non-address immediate.
966a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve				// Virtual register for an SSA operand,
976a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve				// including hidden operands required for
98427a5273113274ee35cff78534dba2ae812c79ceRuchira Sasanka				// the generated machine code.
996a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    int64_t immedVal;		// constant value for an explicit constant
1006a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  };
101773fc471bdc36a221ff9302a07e58f8f7210d87dRuchira Sasanka
1021a33e6eb7477ecc015f3aadbd47f1c1434003a66Chris Lattner  MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
1031a33e6eb7477ecc015f3aadbd47f1c1434003a66Chris Lattner  char flags;                   // see bit field definitions above
10421721b63c3b5a314dfa0be14823b10273860787cRuchira Sasanka  int regNum;	                // register number for an explicit register
105427a5273113274ee35cff78534dba2ae812c79ceRuchira Sasanka                                // will be set for a value after reg allocation
106413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattnerprivate:
107413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  MachineOperand()
108a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    : immedVal(0),
109a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      opType(MO_VirtualRegister),
110a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      flags(0),
111a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      regNum(-1) {}
112a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
113413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  MachineOperand(int64_t ImmVal, MachineOperandType OpTy)
114a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    : immedVal(ImmVal),
115a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      opType(OpTy),
116a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      flags(0),
117a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      regNum(-1) {}
118a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
11932f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  MachineOperand(int Reg, MachineOperandType OpTy, MOTy::UseType UseTy)
120a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    : immedVal(0),
121a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      opType(OpTy),
12232f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner      regNum(Reg) {
12332f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    switch (UseTy) {
12432f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::Use:       flags = 0; break;
12532f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::Def:       flags = DEFFLAG; break;
12632f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::UseAndDef: flags = DEFUSEFLAG; break;
12732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    default: assert(0 && "Invalid value for UseTy!");
12832f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    }
12932f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  }
130a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
13132f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  MachineOperand(Value *V, MachineOperandType OpTy, MOTy::UseType UseTy)
13232f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    : value(V), opType(OpTy), regNum(-1) {
13332f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    switch (UseTy) {
13432f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::Use:       flags = 0; break;
13532f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::Def:       flags = DEFFLAG; break;
13632f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    case MOTy::UseAndDef: flags = DEFUSEFLAG; break;
13732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    default: assert(0 && "Invalid value for UseTy!");
13832f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    }
139413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
140413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
1416a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Advepublic:
142572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  MachineOperand(const MachineOperand &M)
143a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    : immedVal(M.immedVal),
144a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      opType(M.opType),
145a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      flags(M.flags),
146a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve      regNum(M.regNum) {}
147a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
148572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  ~MachineOperand() {}
14923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
1506a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  // Accessor methods.  Caller is responsible for checking the
1516a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  // operand type before invoking the corresponding accessor.
1526a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  //
153133f079c8cf966d2222c2dda2de56d2cc600497eChris Lattner  MachineOperandType getType() const { return opType; }
154572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner
15598f2f8053bebaad7683de074bfb74239364098d2Vikram S. Adve  inline Value*		getVRegValue	() const {
156746e0014a6c59f285ffefc30c722ef2cf69eb95dChris Lattner    assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
157746e0014a6c59f285ffefc30c722ef2cf69eb95dChris Lattner	   opType == MO_PCRelativeDisp);
1586a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    return value;
1596a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  }
160a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve  inline Value*		getVRegValueOrNull() const {
161a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve    return (opType == MO_VirtualRegister || opType == MO_CCRegister ||
162a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve            opType == MO_PCRelativeDisp)? value : NULL;
163a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve  }
164df1c3b8398d1df253ebd389ac1068ec732a2f28fVikram S. Adve  inline int            getMachineRegNum() const {
1656a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    assert(opType == MO_MachineRegister);
166df1c3b8398d1df253ebd389ac1068ec732a2f28fVikram S. Adve    return regNum;
1676a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  }
16898f2f8053bebaad7683de074bfb74239364098d2Vikram S. Adve  inline int64_t	getImmedValue	() const {
169dd52255e9a26fbc9b7e0cd22a2dd99b0b6bae991Vikram S. Adve    assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
1706a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    return immedVal;
1716a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  }
172572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool		opIsDef		() const { return flags & DEFFLAG; }
173572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool		opIsDefAndUse	() const { return flags & DEFUSEFLAG; }
174572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool          opHiBits32      () const { return flags & HIFLAG32; }
175572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool          opLoBits32      () const { return flags & LOFLAG32; }
176572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool          opHiBits64      () const { return flags & HIFLAG64; }
177572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool          opLoBits64      () const { return flags & LOFLAG64; }
178504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve
179504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve  // used to check if a machine register has been allocated to this operand
180504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve  inline bool   hasAllocatedReg() const {
181504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve    return (regNum >= 0 &&
182504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve            (opType == MO_VirtualRegister || opType == MO_CCRegister ||
183504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve             opType == MO_MachineRegister));
184504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve  }
185504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve
186504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve  // used to get the reg number if when one is allocated
18769cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  inline int  getAllocatedRegNum() const {
18869cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve    assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
18969cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve	   opType == MO_MachineRegister);
19069cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve    return regNum;
1917a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  }
192504fc5b7b5a9ffa9f82e95e7212015575030c7a7Vikram S. Adve
193ce3d3eadef1c2a72a0a6e16735e5091fb1e6dc16Chris Lattner  inline unsigned getReg() const {
194ce3d3eadef1c2a72a0a6e16735e5091fb1e6dc16Chris Lattner    assert(hasAllocatedReg() && "Cannot call MachineOperand::getReg()!");
195ce3d3eadef1c2a72a0a6e16735e5091fb1e6dc16Chris Lattner    return regNum;
196ce3d3eadef1c2a72a0a6e16735e5091fb1e6dc16Chris Lattner  }
1976a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve
198697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
19998f2f8053bebaad7683de074bfb74239364098d2Vikram S. Adve
2006a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adveprivate:
2016a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve
20269cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  // Construction methods needed for fine-grain control.
20369cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  // These must be accessed via coresponding methods in MachineInstr.
20469cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  void markHi32()      { flags |= HIFLAG32; }
20569cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  void markLo32()      { flags |= LOFLAG32; }
20669cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  void markHi64()      { flags |= HIFLAG64; }
20769cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  void markLo64()      { flags |= LOFLAG64; }
20869cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve
2097a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  // Replaces the Value with its corresponding physical register after
210427a5273113274ee35cff78534dba2ae812c79ceRuchira Sasanka  // register allocation is complete
211eda6806f6aaec9a64707a8e5609ae21b15e1440aRuchira Sasanka  void setRegForValue(int reg) {
2121876f92599b90f0a4b276aae413a1b965954174dVikram S. Adve    assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
2131876f92599b90f0a4b276aae413a1b965954174dVikram S. Adve	   opType == MO_MachineRegister);
214427a5273113274ee35cff78534dba2ae812c79ceRuchira Sasanka    regNum = reg;
215427a5273113274ee35cff78534dba2ae812c79ceRuchira Sasanka  }
2167a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve
21769cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve  friend class MachineInstr;
21823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve};
21923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
22023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
22123ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
22223ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve// class MachineInstr
22323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
22423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve// Purpose:
22523ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   Representation of each machine instruction.
22623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
22723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   MachineOpCode must be an enum, defined separately for each target.
22823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
22923ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//
230a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//  There are 2 kinds of operands:
231a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//
232a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//  (1) Explicit operands of the machine instruction in vector operands[]
233a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//
234a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//  (2) "Implicit operands" are values implicitly used or defined by the
235a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//      machine instruction, such as arguments to a CALL, return value of
236a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve//      a CALL (if any), and return value of a RETURN.
23723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
23823ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
239a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adveclass MachineInstr: public NonCopyable {      // Disable copy operations
240a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
2417a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  MachineOpCode    opCode;              // the opcode
242756a55000be056a8c6e11adaa8c19d5a06d03291Chris Lattner  std::vector<MachineOperand> operands; // the operands
243a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  unsigned numImplicitRefs;             // number of implicit operands
24427a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner
245a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  MachineOperand& getImplicitOp(unsigned i) {
246a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    assert(i < numImplicitRefs && "implicit ref# out of range!");
247a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return operands[i + operands.size() - numImplicitRefs];
248a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  }
249a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  const MachineOperand& getImplicitOp(unsigned i) const {
250a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    assert(i < numImplicitRefs && "implicit ref# out of range!");
251a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return operands[i + operands.size() - numImplicitRefs];
252a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  }
25327a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner
25427a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  // regsUsed - all machine registers used for this instruction, including regs
25527a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  // used to save values across the instruction.  This is a bitset of registers.
25627a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  std::vector<bool> regsUsed;
257413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
258413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  // OperandComplete - Return true if it's illegal to add a new operand
259413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  bool OperandsComplete() const;
260a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
2616a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Advepublic:
2627279122e668816bed0d4f38d3392bbab0140fad0Chris Lattner  MachineInstr(MachineOpCode Opcode);
2637279122e668816bed0d4f38d3392bbab0140fad0Chris Lattner  MachineInstr(MachineOpCode Opcode, unsigned numOperands);
264413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
265413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// MachineInstr ctor - This constructor only does a _reserve_ of the
266413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// operands, not a resize for them.  It is expected that if you use this that
267413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// you call add* methods below to fill up the operands, instead of the Set
2687db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// methods.  Eventually, the "resizing" ctors will be phased out.
269413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
270413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  MachineInstr(MachineOpCode Opcode, unsigned numOperands, bool XX, bool YY);
271e8b57ef2603ed522083dc18e559ca4e20abf22aeVikram S. Adve
2727db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// MachineInstr ctor - Work exactly the same as the ctor above, except that
2737db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// the MachineInstr is created and added to the end of the specified basic
2747db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// block.
2757db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  ///
2767db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode, unsigned numOps);
2777db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner
2787db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner
2797db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// replace - Support to rewrite a machine instruction in place: for now,
2807db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// simply replace() and then set new operands with Set.*Operand methods
2817db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  /// below.
2827db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  ///
283b98a53f201fd3652f4b7e37d1f2cb3b9b0775d45Chris Lattner  void replace(MachineOpCode Opcode, unsigned numOperands);
284e8b57ef2603ed522083dc18e559ca4e20abf22aeVikram S. Adve
285572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  // The opcode.
286e8b57ef2603ed522083dc18e559ca4e20abf22aeVikram S. Adve  //
2877db458fb0768059f050d3a0f1a26818fa8e22712Chris Lattner  const MachineOpCode getOpcode() const { return opCode; }
288572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  const MachineOpCode getOpCode() const { return opCode; }
289da47526737c111128e34b9627a3beea1a68bd93eChris Lattner
290a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  //
291a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  // Information about explicit operands of the instruction
292a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  //
293a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  unsigned getNumOperands() const { return operands.size() - numImplicitRefs; }
294a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve
295572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  const MachineOperand& getOperand(unsigned i) const {
296a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    assert(i < getNumOperands() && "getOperand() out of range!");
297572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner    return operands[i];
298572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
299572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  MachineOperand& getOperand(unsigned i) {
300a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    assert(i < getNumOperands() && "getOperand() out of range!");
301572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner    return operands[i];
302572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
3036d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner
3046d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  MachineOperand::MachineOperandType getOperandType(unsigned i) const {
305133f079c8cf966d2222c2dda2de56d2cc600497eChris Lattner    return getOperand(i).getType();
3066d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  }
3076d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner
3086d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  bool operandIsDefined(unsigned i) const {
3096d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner    return getOperand(i).opIsDef();
3106d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  }
3116d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner
3126d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  bool operandIsDefinedAndUsed(unsigned i) const {
3136d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner    return getOperand(i).opIsDefAndUse();
3146d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner  }
315a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
316a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  //
317a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  // Information about implicit operands of the instruction
318a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  //
319a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
320a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve
321572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  const Value* getImplicitRef(unsigned i) const {
322a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return getImplicitOp(i).getVRegValue();
323572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
324572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  Value* getImplicitRef(unsigned i) {
325a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return getImplicitOp(i).getVRegValue();
326572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
327572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner
328572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool implicitRefIsDefined(unsigned i) const {
329a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return getImplicitOp(i).opIsDef();
330572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
331572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  bool implicitRefIsDefinedAndUsed(unsigned i) const {
332a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve    return getImplicitOp(i).opIsDefAndUse();
333572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  }
334a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  inline void addImplicitRef    (Value* V,
335a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 bool isDef=false,bool isDefAndUse=false);
336a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  inline void setImplicitRef    (unsigned i, Value* V,
337a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 bool isDef=false, bool isDefAndUse=false);
338a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
339a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  //
3407a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  // Information about registers used in this instruction
3417a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
342572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  const std::vector<bool> &getRegsUsed() const { return regsUsed; }
3437a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve
34427a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  // insertUsedReg - Add a register to the Used registers set...
34527a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  void insertUsedReg(unsigned Reg) {
34627a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner    if (Reg >= regsUsed.size())
34727a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner      regsUsed.resize(Reg+1);
34827a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner    regsUsed[Reg] = true;
34927a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner  }
35027a08935ca4ccf2121c2cf4bfbf148e2382c7762Chris Lattner
3517a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
352a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve  // Debugging support
353fa78fbf446b505767e838f9c188707183c57fc9cChris Lattner  //
354af55be15dfa5321f470ce9734fabd858f5af7a88Chris Lattner  void print(std::ostream &OS, const TargetMachine &TM) const;
355572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void dump() const;
356572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
3572f898d207466bf233b55607e404baca302bc7b5eChris Lattner
3582f898d207466bf233b55607e404baca302bc7b5eChris Lattner  //
3592f898d207466bf233b55607e404baca302bc7b5eChris Lattner  // Define iterators to access the Value operands of the Machine Instruction.
360a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  // Note that these iterators only enumerate the explicit operands.
3612f898d207466bf233b55607e404baca302bc7b5eChris Lattner  // begin() and end() are defined to produce these iterators...
3622f898d207466bf233b55607e404baca302bc7b5eChris Lattner  //
3632f898d207466bf233b55607e404baca302bc7b5eChris Lattner  template<class _MI, class _V> class ValOpIterator;
3642f898d207466bf233b55607e404baca302bc7b5eChris Lattner  typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
3652f898d207466bf233b55607e404baca302bc7b5eChris Lattner  typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
3662f898d207466bf233b55607e404baca302bc7b5eChris Lattner
36723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve  // Access to set the operands when building the machine instruction
3687a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
369a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  void SetMachineOperandVal     (unsigned i,
370a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 MachineOperand::MachineOperandType operandType,
371a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 Value* V,
372a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 bool isDef=false,
373a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 bool isDefAndUse=false);
374a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
375a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  void SetMachineOperandConst   (unsigned i,
376a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 MachineOperand::MachineOperandType operandType,
377a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 int64_t intValue);
378a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
379a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  void SetMachineOperandReg     (unsigned i,
380a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 int regNum,
381a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 bool isDef=false);
382572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner
383413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  //===--------------------------------------------------------------------===//
384413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  // Accessors to add operands when building up machine instructions
385413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  //
386413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
387413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
388413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// operands list...
389413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
39023e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
391413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
392413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
393413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
39432f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner             !isDef ? MOTy::Use : (isDefAndUse ? MOTy::UseAndDef : MOTy::Def)));
39532f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  }
39632f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner
39732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner  void addRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use) {
39832f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    assert(!OperandsComplete() &&
39932f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner           "Trying to add an operand to a machine instr that is already done!");
40032f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
40132f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner                                      UTy));
402413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
403413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
404413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addRegOperand - Add a symbolic virtual register reference...
405413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
40623e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  void addRegOperand(int reg, bool isDef) {
407413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
408413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
4099cc361579b6a0aad9a71dc617eedd4d909d48acfChris Lattner    operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
41032f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner                                      isDef ? MOTy::Def : MOTy::Use));
411413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
412413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
41323e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  /// addRegOperand - Add a symbolic virtual register reference...
41423e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  ///
41523e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  void addRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
41623e6bba592af68ba991ee6900978e81eb21a08afChris Lattner    assert(!OperandsComplete() &&
41723e6bba592af68ba991ee6900978e81eb21a08afChris Lattner           "Trying to add an operand to a machine instr that is already done!");
41823e6bba592af68ba991ee6900978e81eb21a08afChris Lattner    operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
41923e6bba592af68ba991ee6900978e81eb21a08afChris Lattner                                      UTy));
42023e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  }
42123e6bba592af68ba991ee6900978e81eb21a08afChris Lattner
422413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addPCDispOperand - Add a PC relative displacement operand to the MI
423413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
424413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  void addPCDispOperand(Value *V) {
425413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
426413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
42732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner    operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp,
42832f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner                                      MOTy::Use));
429413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
430413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
431413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
432413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
43323e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  void addMachineRegOperand(int reg, bool isDef) {
434413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
435413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
436413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
43732f3d08cdebd8941f2149a8f32acd45bd224ca8dChris Lattner                                      isDef ? MOTy::Def : MOTy::Use));
438413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    insertUsedReg(reg);
439413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
440413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
44123e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
44223e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  ///
44323e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  void addMachineRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
44423e6bba592af68ba991ee6900978e81eb21a08afChris Lattner    assert(!OperandsComplete() &&
44523e6bba592af68ba991ee6900978e81eb21a08afChris Lattner           "Trying to add an operand to a machine instr that is already done!");
44623e6bba592af68ba991ee6900978e81eb21a08afChris Lattner    operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
44723e6bba592af68ba991ee6900978e81eb21a08afChris Lattner                                      UTy));
44823e6bba592af68ba991ee6900978e81eb21a08afChris Lattner    insertUsedReg(reg);
44923e6bba592af68ba991ee6900978e81eb21a08afChris Lattner  }
45023e6bba592af68ba991ee6900978e81eb21a08afChris Lattner
451413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addZeroExtImmOperand - Add a zero extended constant argument to the
452413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// machine instruction.
453413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
454413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  void addZeroExtImmOperand(int64_t intValue) {
455413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
456413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
457413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    operands.push_back(MachineOperand(intValue,
458413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner                                      MachineOperand::MO_UnextendedImmed));
459413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
460413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
461413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// addSignExtImmOperand - Add a zero extended constant argument to the
462413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  /// machine instruction.
463413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  ///
464413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  void addSignExtImmOperand(int64_t intValue) {
465413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    assert(!OperandsComplete() &&
466413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner           "Trying to add an operand to a machine instr that is already done!");
467413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner    operands.push_back(MachineOperand(intValue,
468413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner                                      MachineOperand::MO_SignExtendedImmed));
469413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner  }
470413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
471413746e9833d97a8b463ef6a788aa326cf3829a2Chris Lattner
472572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  unsigned substituteValue(const Value* oldVal, Value* newVal,
473572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner                           bool defsOnly = true);
474572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner
475572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void setOperandHi32(unsigned i) { operands[i].markHi32(); }
476572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void setOperandLo32(unsigned i) { operands[i].markLo32(); }
477572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void setOperandHi64(unsigned i) { operands[i].markHi64(); }
478572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void setOperandLo64(unsigned i) { operands[i].markLo64(); }
479a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve
48069cacd471093e38a51e0e637fca1a1768b935136Vikram S. Adve
481572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  // SetRegForOperand - Replaces the Value for the operand with its allocated
4827a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  // physical register after register allocation is complete.
4837a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
484572f5c8c0cf66cd6f53dda255cd8c4d8f27d8505Chris Lattner  void SetRegForOperand(unsigned i, int regNum);
4856d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner
4867a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
4877a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  // Iterator to enumerate machine operands.
4887a4be9580e095ca4bffd16ec6ec4882f6270fb09Vikram S. Adve  //
4892f898d207466bf233b55607e404baca302bc7b5eChris Lattner  template<class MITy, class VTy>
49039d69009d015a5177303c9d8865143531e099314Chris Lattner  class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
4912f898d207466bf233b55607e404baca302bc7b5eChris Lattner    unsigned i;
4922f898d207466bf233b55607e404baca302bc7b5eChris Lattner    MITy MI;
4932f898d207466bf233b55607e404baca302bc7b5eChris Lattner
4946d6c3f86186333037f2fd3fb001e8b2998c080d9Chris Lattner    void skipToNextVal() {
4952f898d207466bf233b55607e404baca302bc7b5eChris Lattner      while (i < MI->getNumOperands() &&
496a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve             !( (MI->getOperandType(i) == MachineOperand::MO_VirtualRegister ||
497a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                 MI->getOperandType(i) == MachineOperand::MO_CCRegister)
498a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                && MI->getOperand(i).getVRegValue() != 0))
4992f898d207466bf233b55607e404baca302bc7b5eChris Lattner        ++i;
5002f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5012f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5022f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
5032f898d207466bf233b55607e404baca302bc7b5eChris Lattner      skipToNextVal();
5042f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5052f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5062f898d207466bf233b55607e404baca302bc7b5eChris Lattner  public:
5072f898d207466bf233b55607e404baca302bc7b5eChris Lattner    typedef ValOpIterator<MITy, VTy> _Self;
5082f898d207466bf233b55607e404baca302bc7b5eChris Lattner
509a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve    inline VTy operator*() const {
510a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve      return MI->getOperand(i).getVRegValue();
5112f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5122f898d207466bf233b55607e404baca302bc7b5eChris Lattner
513a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve    const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
514a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve          MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
515a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve
5162f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline VTy operator->() const { return operator*(); }
517a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve
518a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve    inline bool isDef()       const { return MI->getOperand(i).opIsDef(); }
519a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve    inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
520a7710518dacb30dabf0c2f057b546dc7bcf37071Vikram S. Adve
5212f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline _Self& operator++() { i++; skipToNextVal(); return *this; }
5222f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
5232f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5242f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline bool operator==(const _Self &y) const {
5252f898d207466bf233b55607e404baca302bc7b5eChris Lattner      return i == y.i;
5262f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5272f898d207466bf233b55607e404baca302bc7b5eChris Lattner    inline bool operator!=(const _Self &y) const {
5282f898d207466bf233b55607e404baca302bc7b5eChris Lattner      return !operator==(y);
5292f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5302f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5312f898d207466bf233b55607e404baca302bc7b5eChris Lattner    static _Self begin(MITy MI) {
5322f898d207466bf233b55607e404baca302bc7b5eChris Lattner      return _Self(MI, 0);
5332f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5342f898d207466bf233b55607e404baca302bc7b5eChris Lattner    static _Self end(MITy MI) {
5352f898d207466bf233b55607e404baca302bc7b5eChris Lattner      return _Self(MI, MI->getNumOperands());
5362f898d207466bf233b55607e404baca302bc7b5eChris Lattner    }
5372f898d207466bf233b55607e404baca302bc7b5eChris Lattner  };
5382f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5392f898d207466bf233b55607e404baca302bc7b5eChris Lattner  // define begin() and end()
5402f898d207466bf233b55607e404baca302bc7b5eChris Lattner  val_op_iterator begin() { return val_op_iterator::begin(this); }
5412f898d207466bf233b55607e404baca302bc7b5eChris Lattner  val_op_iterator end()   { return val_op_iterator::end(this); }
5422f898d207466bf233b55607e404baca302bc7b5eChris Lattner
5432f898d207466bf233b55607e404baca302bc7b5eChris Lattner  const_val_op_iterator begin() const {
5442f898d207466bf233b55607e404baca302bc7b5eChris Lattner    return const_val_op_iterator::begin(this);
5452f898d207466bf233b55607e404baca302bc7b5eChris Lattner  }
5462f898d207466bf233b55607e404baca302bc7b5eChris Lattner  const_val_op_iterator end() const {
5472f898d207466bf233b55607e404baca302bc7b5eChris Lattner    return const_val_op_iterator::end(this);
5482f898d207466bf233b55607e404baca302bc7b5eChris Lattner  }
549a995e6086deca9cbd9aab9d6e1e94b36964b66daVikram S. Adve};
55023ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
551a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
552a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve// Define here to enable inlining of the functions used.
553a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve//
554a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Advevoid MachineInstr::addImplicitRef(Value* V,
555a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                  bool isDef,
556a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                  bool isDefAndUse)
557a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve{
558a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  ++numImplicitRefs;
559a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  addRegOperand(V, isDef, isDefAndUse);
560a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve}
561a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
562a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Advevoid MachineInstr::setImplicitRef(unsigned i,
563a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                  Value* V,
564a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                  bool isDef,
565a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                  bool isDefAndUse)
566a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve{
567a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve  assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
5688f211a4ab013d4a3324162b972a5b4d075cbd20eVikram S. Adve  SetMachineOperandVal(i + getNumOperands(),
569a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                       MachineOperand::MO_VirtualRegister,
570a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                       V, isDef, isDefAndUse);
571a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve}
572a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
573a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve
57423ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
575593da4acc56d4c591ad688e6605b04d0825c867eVikram S. Adve// Debugging Support
57623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve//---------------------------------------------------------------------------
57723ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
578a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Advestd::ostream& operator<<        (std::ostream& os,
579a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 const MachineInstr& minstr);
580593da4acc56d4c591ad688e6605b04d0825c867eVikram S. Adve
581a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Advestd::ostream& operator<<        (std::ostream& os,
582a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Adve                                 const MachineOperand& mop);
58323ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve
584a2bae305fb5a870c4ef753ed290a7ddea73ec82bVikram S. Advevoid PrintMachineInstructions   (const Function *F);
585136c9f4062b0fe6d864ebc2bc2b0cbada931a28eVikram S. Adve
58623ee550765232e22d0daf6407141ecef4c55c06fVikram S. Adve#endif
587