From 943b5e117fe9a087f9aa529a2632c2d32cc22374 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 4 May 2006 19:14:44 +0000 Subject: [PATCH] Remove redundancy and a level of indirection when creating machine operands git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28107 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/MachineInstr.h | 127 +++++++++++++--------------- lib/CodeGen/MachineInstr.cpp | 26 ++---- 2 files changed, 64 insertions(+), 89 deletions(-) diff --git a/include/llvm/CodeGen/MachineInstr.h b/include/llvm/CodeGen/MachineInstr.h index 75ce64220d4..48db3201210 100644 --- a/include/llvm/CodeGen/MachineInstr.h +++ b/include/llvm/CodeGen/MachineInstr.h @@ -85,40 +85,7 @@ private: /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex int offset; - MachineOperand(int64_t ImmVal) : flags(0), opType(MO_Immediate) { - contents.immedVal = ImmVal; - offset = 0; - } - - MachineOperand(unsigned Idx, MachineOperandType OpTy): flags(0), opType(OpTy){ - contents.immedVal = Idx; - offset = 0; - } - - MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy) - : flags(UseTy), opType(OpTy) { - contents.RegNo = Reg; - offset = 0; - } - - MachineOperand(GlobalValue *V, int Offset = 0) - : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) { - contents.GV = V; - offset = Offset; - } - - MachineOperand(MachineBasicBlock *mbb) - : flags(0), opType(MO_MachineBasicBlock) { - contents.MBB = mbb; - offset = 0; - } - - MachineOperand(const char *SymName, int Offset) - : flags(0), opType(MO_ExternalSymbol) { - contents.SymbolName = SymName; - offset = Offset; - } - + MachineOperand() {} public: MachineOperand(const MachineOperand &M) { *this = M; @@ -252,7 +219,7 @@ public: /// class MachineInstr { short Opcode; // the opcode - std::vector operands; // the operands + std::vector Operands; // the operands MachineInstr* prev, *next; // links for our intrusive list MachineBasicBlock* parent; // pointer to the owning basic block @@ -288,21 +255,21 @@ public: /// Access to explicit operands of the instruction. /// - unsigned getNumOperands() const { return operands.size(); } + unsigned getNumOperands() const { return Operands.size(); } const MachineOperand& getOperand(unsigned i) const { assert(i < getNumOperands() && "getOperand() out of range!"); - return operands[i]; + return Operands[i]; } MachineOperand& getOperand(unsigned i) { assert(i < getNumOperands() && "getOperand() out of range!"); - return operands[i]; + return Operands[i]; } /// clone - Create a copy of 'this' instruction that is identical in /// all ways except the the instruction has no parent, prev, or next. - MachineInstr* clone() const; + MachineInstr* clone() const { return new MachineInstr(*this); } /// removeFromParent - This method unlinks 'this' from the containing basic /// block, and returns it, but does not delete it. @@ -322,70 +289,87 @@ public: friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr); //===--------------------------------------------------------------------===// - // Accessors to add operands when building up machine instructions + // Accessors to add operands when building up machine instructions. // - /// addRegOperand - Add a symbolic virtual register reference... + /// addRegOperand - Add a register operand. /// - void addRegOperand(int reg, + void addRegOperand(unsigned 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_Register, UTy)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_Register; + Op.flags = UTy; + Op.contents.RegNo = Reg; + Op.offset = 0; } /// addImmOperand - Add a zero extended constant argument to the /// machine instruction. /// void addImmOperand(int64_t Val) { - assert(!OperandsComplete() && - "Trying to add an operand to a machine instr that is already done!"); - operands.push_back(MachineOperand(Val)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_Immediate; + Op.flags = 0; + Op.contents.immedVal = Val; + Op.offset = 0; } void addMachineBasicBlockOperand(MachineBasicBlock *MBB) { - assert(!OperandsComplete() && - "Trying to add an operand to a machine instr that is already done!"); - operands.push_back(MachineOperand(MBB)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_MachineBasicBlock; + Op.flags = 0; + Op.contents.MBB = MBB; + Op.offset = 0; } /// addFrameIndexOperand - Add an abstract frame index to the instruction /// void addFrameIndexOperand(unsigned Idx) { - assert(!OperandsComplete() && - "Trying to add an operand to a machine instr that is already done!"); - operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_FrameIndex; + Op.flags = 0; + Op.contents.immedVal = Idx; + Op.offset = 0; } /// addConstantPoolndexOperand - Add a constant pool object index to the /// instruction. /// - void addConstantPoolIndexOperand(unsigned I, int Offset=0) { - assert(!OperandsComplete() && - "Trying to add an operand to a machine instr that is already done!"); - operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex)); + void addConstantPoolIndexOperand(unsigned Idx, int Offset) { + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_ConstantPoolIndex; + Op.flags = 0; + Op.contents.immedVal = Idx; + Op.offset = Offset; } /// addJumpTableIndexOperand - Add a jump table object index to the /// instruction. /// - void addJumpTableIndexOperand(unsigned I) { - assert(!OperandsComplete() && - "Trying to add an operand to a machine instr that is already done!"); - operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex)); + void addJumpTableIndexOperand(unsigned Idx) { + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_JumpTableIndex; + Op.flags = 0; + Op.contents.immedVal = Idx; + Op.offset = 0; } 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, Offset)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_GlobalAddress; + Op.flags = 0; + Op.contents.GV = GV; + Op.offset = Offset; } /// addExternalSymbolOperand - Add an external symbol operand to this instr /// void addExternalSymbolOperand(const char *SymName) { - operands.push_back(MachineOperand(SymName, 0)); + MachineOperand &Op = AddNewOperand(); + Op.opType = MachineOperand::MO_ExternalSymbol; + Op.flags = 0; + Op.contents.SymbolName = SymName; + Op.offset = 0; } //===--------------------------------------------------------------------===// @@ -400,7 +384,14 @@ public: /// fewer operand than it started with. /// void RemoveOperand(unsigned i) { - operands.erase(operands.begin()+i); + Operands.erase(Operands.begin()+i); + } +private: + MachineOperand &AddNewOperand() { + assert(!OperandsComplete() && + "Trying to add an operand to a machine instr that is already done!"); + Operands.push_back(MachineOperand()); + return Operands.back(); } }; diff --git a/lib/CodeGen/MachineInstr.cpp b/lib/CodeGen/MachineInstr.cpp index 2cb5119e49d..495da5edfcf 100644 --- a/lib/CodeGen/MachineInstr.cpp +++ b/lib/CodeGen/MachineInstr.cpp @@ -43,7 +43,7 @@ namespace llvm { /// MachineInstr::MachineInstr(short opcode, unsigned numOperands) : Opcode(opcode), parent(0) { - operands.reserve(numOperands); + Operands.reserve(numOperands); // Make sure that we get added to a machine basicblock LeakDetector::addGarbageObject(this); } @@ -55,7 +55,7 @@ MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode, unsigned numOperands) : Opcode(opcode), parent(0) { assert(MBB && "Cannot use inserting ctor with null basic block!"); - operands.reserve(numOperands); + Operands.reserve(numOperands); // Make sure that we get added to a machine basicblock LeakDetector::addGarbageObject(this); MBB->push_back(this); // Add instruction to end of basic block! @@ -65,11 +65,11 @@ MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode, /// MachineInstr::MachineInstr(const MachineInstr &MI) { Opcode = MI.getOpcode(); - operands.reserve(MI.getNumOperands()); + Operands.reserve(MI.getNumOperands()); // Add operands - for (unsigned i = 0; i < MI.getNumOperands(); ++i) - operands.push_back(MachineOperand(MI.getOperand(i))); + for (unsigned i = 0; i != MI.getNumOperands(); ++i) + Operands.push_back(MI.getOperand(i)); // Set parent, next, and prev to null parent = 0; @@ -82,13 +82,6 @@ MachineInstr::~MachineInstr() { LeakDetector::removeGarbageObject(this); } -/// clone - Create a copy of 'this' instruction that is identical in all ways -/// except the following: the new instruction has no parent and it has no name -/// -MachineInstr* MachineInstr::clone() const { - return new MachineInstr(*this); -} - /// removeFromParent - This method unlinks 'this' from the containing basic /// block, and returns it, but does not delete it. MachineInstr *MachineInstr::removeFromParent() { @@ -111,15 +104,6 @@ void MachineInstr::dump() const { std::cerr << " " << *this; } -static inline std::ostream& OutputValue(std::ostream &os, const Value* val) { - os << "(val "; - os << (void*) val; // print address always - if (val && val->hasName()) - os << " " << val->getName(); // print name also, if available - os << ")"; - return os; -} - static inline void OutputReg(std::ostream &os, unsigned RegNo, const MRegisterInfo *MRI = 0) { if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {