Move some methods out of MachineInstr into MachineOperand
[oota-llvm.git] / include / llvm / CodeGen / MachineInstr.h
index 818331d2521792c849d6f88fbfb0db193f3ecd9b..239f879727e258daf5a394c8d2996bc019525d96 100644 (file)
@@ -37,45 +37,14 @@ typedef short MachineOpCode;
 //===----------------------------------------------------------------------===//
 // class MachineOperand
 //
-// Purpose:
 //   Representation of each machine instruction operand.
-//   This class is designed so that you can allocate a vector of operands
-//   first and initialize each one later.
-//
-//   E.g, for this VM instruction:
-//     ptr = alloca type, numElements
-//   we generate 2 machine instructions on the SPARC:
-//
-//    mul Constant, Numelements -> Reg
-//    add %sp, Reg -> Ptr
-//
-//   Each instruction has 3 operands, listed above.  Of those:
-//   - Reg, NumElements, and Ptr are of operand type MO_Register.
-//   - Constant is of operand type MO_SignExtendedImmed on the SPARC.
-//
-//   For the register operands, the virtual register type is as follows:
 //
-//   - Reg will be of virtual register type MO_MInstrVirtualReg.  The field
-//     MachineInstr* minstr will point to the instruction that computes reg.
-//
-//   - %sp will be of virtual register type MO_MachineReg.
-//     The field regNum identifies the machine register.
-//
-//   - NumElements will be of virtual register type MO_VirtualReg.
-//     The field Value* value identifies the value.
-//
-//   - Ptr will also be of virtual register type MO_VirtualReg.
-//     Again, the field Value* value identifies the value.
-//
-//===----------------------------------------------------------------------===//
-
 struct MachineOperand {
 private:
   // Bit fields of the flags variable used for different operand properties
   enum {
     DEFFLAG     = 0x01,       // this is a def of the operand
     USEFLAG     = 0x02,       // this is a use of the operand
-    PCRELATIVE  = 0x40        // Operand is relative to PC, not a global address
   };
 
 public:
@@ -93,9 +62,7 @@ public:
 
   enum MachineOperandType {
     MO_VirtualRegister,         // virtual register for *value
-    MO_MachineRegister,         // pre-assigned machine register `regNum'
-    MO_SignExtendedImmed,
-    MO_UnextendedImmed,
+    MO_Immediate,               // Immediate Operand
     MO_MachineBasicBlock,       // MachineBasicBlock reference
     MO_FrameIndex,              // Abstract Stack Frame Index
     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
@@ -106,15 +73,8 @@ public:
 
 private:
   union {
-    Value*  value;      // BasicBlockVal for a label operand.
-                        // ConstantVal for a non-address immediate.
-                        // Virtual register for an SSA operand,
-                        //   including hidden operands required for
-                        //   the generated machine code.
-                        // LLVM global for MO_GlobalAddress.
-
+    GlobalValue *GV;    // LLVM global for MO_GlobalAddress.
     int64_t immedVal;   // Constant value for an explicit constant
-
     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
     const char *SymbolName;     // For MO_ExternalSymbol type
   } contents;
@@ -122,51 +82,32 @@ private:
   char flags;                   // see bit field definitions above
   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
   union {
-    int regNum;                 // register number for an explicit register
-                                // will be set for a value after reg allocation
-
-    int offset;                 // Offset to address of global or external, only
-                                // valid for MO_GlobalAddress, MO_ExternalSym
-                                // and MO_ConstantPoolIndex
+    int regNum;     // register number for an explicit register
+    int offset;     // Offset to address of global or external, only
+                    // valid for MO_GlobalAddress, MO_ExternalSym
+                    // and MO_ConstantPoolIndex
   } extra;
 
-  void zeroContents () {
-    memset (&contents, 0, sizeof (contents));
-    memset (&extra, 0, sizeof (extra));
+  void zeroContents() {
+    contents.immedVal = 0;
+    extra.offset = 0;
   }
 
-  MachineOperand(int64_t ImmVal = 0,
-        MachineOperandType OpTy = MO_VirtualRegister, int Offset = 0)
+  MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
     : flags(0), opType(OpTy) {
-    zeroContents ();
     contents.immedVal = ImmVal;
-    if (OpTy == MachineOperand::MO_ConstantPoolIndex)
-      extra.offset = Offset;
-    else
-      extra.regNum = -1;
+    extra.offset = Offset;
   }
 
   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
     : flags(UseTy), opType(OpTy) {
-    zeroContents ();
-    extra.regNum = Reg;
-  }
-
-  MachineOperand(Value *V, MachineOperandType OpTy, UseType UseTy,
-                 bool isPCRelative = false)
-    : flags(UseTy | (isPCRelative?PCRELATIVE:0)), opType(OpTy) {
-    assert(OpTy != MachineOperand::MO_GlobalAddress);
     zeroContents();
-    contents.value = V;
-    extra.regNum = -1;
+    extra.regNum = Reg;
   }
 
-  MachineOperand(GlobalValue *V, MachineOperandType OpTy, UseType UseTy,
-                 bool isPCRelative = false, int Offset = 0)
-    : flags(UseTy | (isPCRelative?PCRELATIVE:0)), opType(OpTy) {
-    assert(OpTy == MachineOperand::MO_GlobalAddress);
-    zeroContents ();
-    contents.value = (Value*)V;
+  MachineOperand(GlobalValue *V, int Offset = 0)
+    : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
+    contents.GV = V;
     extra.offset = Offset;
   }
 
@@ -174,11 +115,10 @@ private:
     : flags(0), opType(MO_MachineBasicBlock) {
     zeroContents ();
     contents.MBB = mbb;
-    extra.regNum = -1;
   }
 
-  MachineOperand(const char *SymName, bool isPCRelative, int Offset)
-    : flags(isPCRelative?PCRELATIVE:0), opType(MO_ExternalSymbol) {
+  MachineOperand(const char *SymName, int Offset)
+    : flags(0), opType(MO_ExternalSymbol) {
     zeroContents ();
     contents.SymbolName = SymName;
     extra.offset = Offset;
@@ -192,7 +132,6 @@ public:
     extra = M.extra;
   }
 
-
   ~MachineOperand() {}
 
   const MachineOperand &operator=(const MachineOperand &MO) {
@@ -211,46 +150,22 @@ public:
   ///
   UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
 
-  /// isRegister - Return true if this operand is a register operand.  The X86
-  /// backend currently can't decide whether to use MO_MR or MO_VR to represent
-  /// them, so we accept both.
-  ///
-  /// Note: The sparc backend should not use this method.
+  /// isRegister - Return true if this operand is a register operand.
   ///
   bool isRegister() const {
-    return opType == MO_MachineRegister || opType == MO_VirtualRegister;
+    return opType == MO_VirtualRegister;
   }
 
   /// Accessors that tell you what kind of MachineOperand you're looking at.
   ///
   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
-  bool isImmediate() const {
-    return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
-  }
+  bool isImmediate() const { return opType == MO_Immediate; }
   bool isFrameIndex() const { return opType == MO_FrameIndex; }
   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
 
-  /// getVRegValueOrNull - Get the Value* out of a MachineOperand if it
-  /// has one. This is deprecated and only used by the SPARC v9 backend.
-  ///
-  Value* getVRegValueOrNull() const {
-    return opType == MO_VirtualRegister ? contents.value : NULL;
-  }
-
-  /// MachineOperand accessors that only work on certain types of
-  /// MachineOperand...
-  ///
-  Value* getVRegValue() const {
-    assert(opType == MO_VirtualRegister && "Wrong MachineOperand accessor");
-    return contents.value;
-  }
-  int getMachineRegNum() const {
-    assert(opType == MO_MachineRegister && "Wrong MachineOperand accessor");
-    return extra.regNum;
-  }
   int64_t getImmedValue() const {
     assert(isImmediate() && "Wrong MachineOperand accessor");
     return contents.immedVal;
@@ -277,7 +192,7 @@ public:
   }
   GlobalValue *getGlobal() const {
     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
-    return (GlobalValue*)contents.value;
+    return contents.GV;
   }
   int getOffset() const {
     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
@@ -301,8 +216,7 @@ public:
   /// allocated to this operand.
   ///
   bool hasAllocatedReg() const {
-    return (extra.regNum >= 0 &&
-            (opType == MO_VirtualRegister || opType == MO_MachineRegister));
+    return extra.regNum >= 0 && opType == MO_VirtualRegister;
   }
 
   /// getReg - Returns the register number. It is a runtime error to call this
@@ -313,21 +227,14 @@ public:
     return extra.regNum;
   }
 
-  /// MachineOperand mutators...
+  /// MachineOperand mutators.
   ///
   void setReg(unsigned Reg) {
-    // This method's comment used to say: 'TODO: get rid of this duplicate
-    // code.' It's not clear where the duplication is.
     assert(hasAllocatedReg() && "This operand cannot have a register number!");
     extra.regNum = Reg;
   }
 
-  void setValueReg(Value *val) {
-    assert(getVRegValueOrNull() != 0 && "Original operand must of type Value*");
-    contents.value = val;
-  }
-
-  void setImmedValue(int immVal) {
+  void setImmedValue(int64_t immVal) {
     assert(isImmediate() && "Wrong MachineOperand mutator");
     contents.immedVal = immVal;
   }
@@ -338,6 +245,22 @@ public:
         "Wrong MachineOperand accessor");
     extra.offset = Offset;
   }
+  
+  /// ChangeToImmediate - Replace this operand with a new immediate operand of
+  /// the specified value.  If an operand is known to be an immediate already,
+  /// the setImmedValue method should be used.
+  void ChangeToImmediate(int64_t ImmVal) {
+    opType = MO_Immediate;
+    contents.immedVal = ImmVal;
+  }
+
+  /// ChangeToRegister - Replace this operand with a new register operand of
+  /// the specified value.  If an operand is known to be an register already,
+  /// the setReg method should be used.
+  void ChangeToRegister(unsigned Reg) {
+    opType = MO_VirtualRegister;
+    extra.regNum = Reg;
+  }
 
   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
 
@@ -382,8 +305,6 @@ class MachineInstr {
   friend struct ilist_traits<MachineInstr>;
 
 public:
-  MachineInstr(short Opcode, unsigned numOperands);
-
   /// MachineInstr ctor - This constructor only does a _reserve_ of the
   /// operands, not a resize for them.  It is expected that if you use this that
   /// you call add* methods below to fill up the operands, instead of the Set
@@ -445,28 +366,6 @@ public:
   // Accessors to add operands when building up machine instructions
   //
 
-  /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
-  /// operands list...
-  ///
-  void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
-    assert(!OperandsComplete() &&
-           "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(V, MachineOperand::MO_VirtualRegister,
-                     !isDef ? MachineOperand::Use :
-                     (isDefAndUse ? MachineOperand::UseAndDef :
-                      MachineOperand::Def)));
-  }
-
-  void addRegOperand(Value *V,
-                     MachineOperand::UseType UTy = MachineOperand::Use,
-                     bool isPCRelative = false) {
-    assert(!OperandsComplete() &&
-           "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
-                                      UTy, isPCRelative));
-  }
-
   /// addRegOperand - Add a symbolic virtual register reference...
   ///
   void addRegOperand(int reg, bool isDef) {
@@ -487,26 +386,6 @@ public:
       MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
   }
 
-  /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
-  ///
-  void addMachineRegOperand(int reg, bool isDef) {
-    assert(!OperandsComplete() &&
-           "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(reg, MachineOperand::MO_MachineRegister,
-                     isDef ? MachineOperand::Def : MachineOperand::Use));
-  }
-
-  /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
-  ///
-  void addMachineRegOperand(int reg,
-                            MachineOperand::UseType UTy = MachineOperand::Use) {
-    assert(!OperandsComplete() &&
-           "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(reg, MachineOperand::MO_MachineRegister, UTy));
-  }
-
   /// addZeroExtImmOperand - Add a zero extended constant argument to the
   /// machine instruction.
   ///
@@ -514,7 +393,7 @@ public:
     assert(!OperandsComplete() &&
            "Trying to add an operand to a machine instr that is already done!");
     operands.push_back(
-      MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
+      MachineOperand(intValue, MachineOperand::MO_Immediate));
   }
 
   /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
@@ -523,18 +402,7 @@ public:
   void addZeroExtImm64Operand(uint64_t intValue) {
     assert(!OperandsComplete() &&
            "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
-  }
-
-  /// addSignExtImmOperand - Add a zero extended constant argument to the
-  /// machine instruction.
-  ///
-  void addSignExtImmOperand(int intValue) {
-    assert(!OperandsComplete() &&
-           "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
+    operands.push_back(MachineOperand(intValue, MachineOperand::MO_Immediate));
   }
 
   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
@@ -569,24 +437,21 @@ public:
     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
   }
   
-  void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative, int Offset) {
+  void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
     assert(!OperandsComplete() &&
            "Trying to add an operand to a machine instr that is already done!");
-    operands.push_back(
-      MachineOperand(GV, MachineOperand::MO_GlobalAddress,
-                     MachineOperand::Use, isPCRelative, Offset));
+    operands.push_back(MachineOperand(GV, Offset));
   }
 
   /// addExternalSymbolOperand - Add an external symbol operand to this instr
   ///
-  void addExternalSymbolOperand(const char *SymName, bool isPCRelative) {
-    operands.push_back(MachineOperand(SymName, isPCRelative, 0));
+  void addExternalSymbolOperand(const char *SymName) {
+    operands.push_back(MachineOperand(SymName, 0));
   }
 
   //===--------------------------------------------------------------------===//
   // Accessors used to modify instructions in place.
   //
-  // FIXME: Move this stuff to MachineOperand itself!
 
   /// setOpcode - Replace the opcode of the current instruction with a new one.
   ///
@@ -598,18 +463,6 @@ public:
   void RemoveOperand(unsigned i) {
     operands.erase(operands.begin()+i);
   }
-
-  // Access to set the operands when building the machine instruction
-  //
-  void SetMachineOperandVal(unsigned i,
-                            MachineOperand::MachineOperandType operandType,
-                            Value* V);
-
-  void SetMachineOperandConst(unsigned i,
-                              MachineOperand::MachineOperandType operandType,
-                              int intValue);
-
-  void SetMachineOperandReg(unsigned i, int regNum);
 };
 
 //===----------------------------------------------------------------------===//