[llvm-commits] CVS: llvm/include/llvm/CodeGen/MachineInstr.h MachineInstrBuilder.h

Chris Lattner lattner at cs.uiuc.edu
Thu Oct 14 21:38:49 PDT 2004



Changes in directory llvm/include/llvm/CodeGen:

MachineInstr.h updated: 1.154 -> 1.155
MachineInstrBuilder.h updated: 1.23 -> 1.24
---
Log message:

Allow machine operands to represent global variables with offsets.  This is
useful when you have a reference like:

int A[100];

void foo() { A[10] = 1; }

In this case, &A[10] is a single constant and should be treated as such.

Only MO_GlobalAddress and MO_ExternalSymbol are allowed to use this field, no
other operand type is.

This is another fine patch contributed by Jeff Cohen!!


---
Diffs of the changes:  (+44 -21)

Index: llvm/include/llvm/CodeGen/MachineInstr.h
diff -u llvm/include/llvm/CodeGen/MachineInstr.h:1.154 llvm/include/llvm/CodeGen/MachineInstr.h:1.155
--- llvm/include/llvm/CodeGen/MachineInstr.h:1.154	Wed Sep  1 17:55:34 2004
+++ llvm/include/llvm/CodeGen/MachineInstr.h	Thu Oct 14 23:38:36 2004
@@ -126,48 +126,60 @@
 
   char flags;                   // see bit field definitions above
   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
-  int regNum;	                // register number for an explicit register
+  union {
+    int regNum;	                // register number for an explicit register
                                 // will be set for a value after reg allocation
-private:
+
+    int offset;                 // Offset to address of global or external, only
+                                // valid for MO_GlobalAddress and MO_ExternalSym
+  } extra;
+
   void zeroContents () { 
     memset (&contents, 0, sizeof (contents));
+    memset (&extra, 0, sizeof (extra));
   }
 
   MachineOperand(int ImmVal = 0, MachineOperandType OpTy = MO_VirtualRegister)
-    : flags(0), opType(OpTy), regNum(-1) {
+    : flags(0), opType(OpTy) {
     zeroContents ();
     contents.immedVal = ImmVal;
+    extra.regNum = -1;
   }
 
   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
-    : flags(UseTy), opType(OpTy), regNum(Reg) {
+    : 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), regNum(-1) {
+		 bool isPCRelative = false, int Offset = 0)
+    : flags(UseTy | (isPCRelative?PCRELATIVE:0)), opType(OpTy) {
     zeroContents ();
     contents.value = V;
+    extra.offset = Offset;
   }
 
   MachineOperand(MachineBasicBlock *mbb)
-    : flags(0), opType(MO_MachineBasicBlock), regNum(-1) {
+    : flags(0), opType(MO_MachineBasicBlock) {
     zeroContents ();
     contents.MBB = mbb;
+    extra.regNum = -1;
   }
 
-  MachineOperand(const std::string &SymName, bool isPCRelative)
-    : flags(isPCRelative?PCRELATIVE:0), opType(MO_ExternalSymbol), regNum(-1) {
+  MachineOperand(const std::string &SymName, bool isPCRelative, int Offset)
+    : flags(isPCRelative?PCRELATIVE:0), opType(MO_ExternalSymbol) {
     zeroContents ();
     contents.SymbolName = new std::string (SymName);
+    extra.offset = Offset;
   }
 
 public:
   MachineOperand(const MachineOperand &M)
-    : flags(M.flags), opType(M.opType), regNum(M.regNum) {
+    : flags(M.flags), opType(M.opType) {
     zeroContents ();
     contents = M.contents;
+    extra = M.extra;
     if (isExternalSymbol())
       contents.SymbolName = new std::string(M.getSymbolName());
   }
@@ -184,7 +196,7 @@
     contents = MO.contents;
     flags    = MO.flags;
     opType   = MO.opType;
-    regNum   = MO.regNum;
+    extra    = MO.extra;
     if (isExternalSymbol())
       contents.SymbolName = new std::string(MO.getSymbolName());
     return *this;
@@ -245,7 +257,7 @@
   }
   int getMachineRegNum() const {
     assert(opType == MO_MachineRegister && "Wrong MachineOperand accessor");
-    return regNum;
+    return extra.regNum;
   }
   int getImmedValue() const {
     assert(isImmediate() && "Wrong MachineOperand accessor");
@@ -271,6 +283,11 @@
     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
     return (GlobalValue*)contents.value;
   }
+  int getOffset() const {
+    assert((isGlobalAddress() || isExternalSymbol()) &&
+        "Wrong MachineOperand accessor");
+    return extra.offset;
+  }
   const std::string &getSymbolName() const {
     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
     return *contents.SymbolName;
@@ -292,7 +309,7 @@
   /// allocated to this operand.
   ///
   bool hasAllocatedReg() const {
-    return (regNum >= 0 &&
+    return (extra.regNum >= 0 &&
             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
              opType == MO_MachineRegister));
   }
@@ -302,7 +319,7 @@
   ///
   unsigned getReg() const {
     assert(hasAllocatedReg());
-    return regNum;
+    return extra.regNum;
   }
 
   /// MachineOperand mutators...
@@ -311,7 +328,7 @@
     // 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!");
-    regNum = Reg;
+    extra.regNum = Reg;
   }  
 
   void setValueReg(Value *val) {
@@ -324,6 +341,12 @@
     contents.immedVal = immVal;
   }
 
+  void setOffset(int Offset) {
+    assert((isGlobalAddress() || isExternalSymbol()) &&
+        "Wrong MachineOperand accessor");
+    extra.offset = Offset;
+  }
+
   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
 
   /// markHi32, markLo32, etc. - These methods are deprecated and only used by
@@ -342,7 +365,7 @@
   void setRegForValue(int reg) {
     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
 	   opType == MO_MachineRegister);
-    regNum = reg;
+    extra.regNum = reg;
   }
   
   friend class MachineInstr;
@@ -615,18 +638,18 @@
     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
   }
 
-  void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative) {
+  void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative, int Offset) {
     assert(!OperandsComplete() &&
            "Trying to add an operand to a machine instr that is already done!");
     operands.push_back(
       MachineOperand((Value*)GV, MachineOperand::MO_GlobalAddress,
-                     MachineOperand::Use, isPCRelative));
+                     MachineOperand::Use, isPCRelative, Offset));
   }
 
   /// addExternalSymbolOperand - Add an external symbol operand to this instr
   ///
   void addExternalSymbolOperand(const std::string &SymName, bool isPCRelative) {
-    operands.push_back(MachineOperand(SymName, isPCRelative));
+    operands.push_back(MachineOperand(SymName, isPCRelative, 0));
   }
 
   //===--------------------------------------------------------------------===//


Index: llvm/include/llvm/CodeGen/MachineInstrBuilder.h
diff -u llvm/include/llvm/CodeGen/MachineInstrBuilder.h:1.23 llvm/include/llvm/CodeGen/MachineInstrBuilder.h:1.24
--- llvm/include/llvm/CodeGen/MachineInstrBuilder.h:1.23	Thu Oct 14 13:47:56 2004
+++ llvm/include/llvm/CodeGen/MachineInstrBuilder.h	Thu Oct 14 23:38:36 2004
@@ -124,8 +124,8 @@
   }
 
   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
-                                              bool isPCRelative = false) const {
-    MI->addGlobalAddressOperand(GV, isPCRelative);
+                                              bool isPCRelative = false, int Offset = 0) const {
+    MI->addGlobalAddressOperand(GV, isPCRelative, Offset);
     return *this;
   }
 






More information about the llvm-commits mailing list