Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- MachineInstr.cpp --------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 3 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 4 | |
Chris Lattner | 822b4fb | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 5 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 6 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 3801f6d | 2002-02-03 07:46:01 +0000 | [diff] [blame] | 7 | #include "llvm/Value.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 8 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 9 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 10 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 11 | |
Chris Lattner | f1757c4 | 2002-10-29 17:40:30 +0000 | [diff] [blame] | 12 | // Global variable holding an array of descriptors for machine instructions. |
| 13 | // The actual object needs to be created separately for each target machine. |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 14 | // This variable is initialized and reset by class TargetInstrInfo. |
Chris Lattner | f1757c4 | 2002-10-29 17:40:30 +0000 | [diff] [blame] | 15 | // |
| 16 | // FIXME: This should be a property of the target so that more than one target |
| 17 | // at a time can be active... |
| 18 | // |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 19 | extern const TargetInstrDescriptor *TargetInstrDescriptors; |
Ruchira Sasanka | 69917e2 | 2001-10-18 22:40:02 +0000 | [diff] [blame] | 20 | |
Vikram S. Adve | 1885da4 | 2001-07-31 21:49:28 +0000 | [diff] [blame] | 21 | // Constructor for instructions with variable #operands |
Chris Lattner | b98a53f | 2002-10-28 21:02:40 +0000 | [diff] [blame] | 22 | MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands) |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 23 | : opCode(OpCode), |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 24 | opCodeFlags(0), |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 25 | operands(numOperands, MachineOperand()), |
| 26 | numImplicitRefs(0) |
| 27 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 30 | /// MachineInstr ctor - This constructor only does a _reserve_ of the operands, |
| 31 | /// not a resize for them. It is expected that if you use this that you call |
| 32 | /// add* methods below to fill up the operands, instead of the Set methods. |
| 33 | /// Eventually, the "resizing" ctors will be phased out. |
| 34 | /// |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 35 | MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands, |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 36 | bool XX, bool YY) |
| 37 | : opCode(Opcode), |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 38 | opCodeFlags(0), |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 39 | numImplicitRefs(0) |
| 40 | { |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 41 | operands.reserve(numOperands); |
| 42 | } |
| 43 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 44 | /// MachineInstr ctor - Work exactly the same as the ctor above, except that the |
| 45 | /// MachineInstr is created and added to the end of the specified basic block. |
| 46 | /// |
| 47 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode, |
| 48 | unsigned numOperands) |
| 49 | : opCode(Opcode), |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 50 | opCodeFlags(0), |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 51 | numImplicitRefs(0) |
| 52 | { |
| 53 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
| 54 | operands.reserve(numOperands); |
| 55 | MBB->push_back(this); // Add instruction to end of basic block! |
| 56 | } |
| 57 | |
| 58 | |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 59 | // OperandComplete - Return true if it's illegal to add a new operand |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 60 | bool MachineInstr::OperandsComplete() const |
| 61 | { |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 62 | int NumOperands = TargetInstrDescriptors[opCode].numOperands; |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 63 | if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 64 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
| 68 | |
Vikram S. Adve | e8b57ef | 2002-09-20 00:47:49 +0000 | [diff] [blame] | 69 | // |
| 70 | // Support for replacing opcode and operands of a MachineInstr in place. |
| 71 | // This only resets the size of the operand vector and initializes it. |
| 72 | // The new operands must be set explicitly later. |
| 73 | // |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 74 | void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands) |
| 75 | { |
| 76 | assert(getNumImplicitRefs() == 0 && |
| 77 | "This is probably broken because implicit refs are going to be lost."); |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 78 | opCode = Opcode; |
Vikram S. Adve | e8b57ef | 2002-09-20 00:47:49 +0000 | [diff] [blame] | 79 | operands.clear(); |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 80 | operands.resize(numOperands, MachineOperand()); |
Vikram S. Adve | e8b57ef | 2002-09-20 00:47:49 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame^] | 83 | void MachineInstr::SetMachineOperandVal(unsigned i, |
| 84 | MachineOperand::MachineOperandType opTy, |
| 85 | Value* V) { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 86 | assert(i < operands.size()); // may be explicit or implicit op |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame^] | 87 | operands[i].opType = opTy; |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 88 | operands[i].value = V; |
| 89 | operands[i].regNum = -1; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 93 | MachineInstr::SetMachineOperandConst(unsigned i, |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 94 | MachineOperand::MachineOperandType operandType, |
Vikram S. Adve | c356e56 | 2002-03-18 03:35:24 +0000 | [diff] [blame] | 95 | int64_t intValue) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 96 | { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 97 | assert(i < getNumOperands()); // must be explicit op |
Vikram S. Adve | c356e56 | 2002-03-18 03:35:24 +0000 | [diff] [blame] | 98 | assert(TargetInstrDescriptors[opCode].resultPos != (int) i && |
| 99 | "immed. constant cannot be defined"); |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 100 | |
| 101 | operands[i].opType = operandType; |
| 102 | operands[i].value = NULL; |
| 103 | operands[i].immedVal = intValue; |
| 104 | operands[i].regNum = -1; |
| 105 | operands[i].flags = 0; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame^] | 108 | void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 109 | assert(i < getNumOperands()); // must be explicit op |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 2f30598 | 2002-10-28 19:46:59 +0000 | [diff] [blame] | 111 | operands[i].opType = MachineOperand::MO_MachineRegister; |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 112 | operands[i].value = NULL; |
| 113 | operands[i].regNum = regNum; |
Chris Lattner | 27a0893 | 2002-10-22 23:16:21 +0000 | [diff] [blame] | 114 | insertUsedReg(regNum); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
Vikram S. Adve | 7a4be95 | 2002-07-08 22:38:45 +0000 | [diff] [blame] | 118 | MachineInstr::SetRegForOperand(unsigned i, int regNum) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 119 | { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 120 | assert(i < getNumOperands()); // must be explicit op |
Vikram S. Adve | 7a4be95 | 2002-07-08 22:38:45 +0000 | [diff] [blame] | 121 | operands[i].setRegForValue(regNum); |
Chris Lattner | 27a0893 | 2002-10-22 23:16:21 +0000 | [diff] [blame] | 122 | insertUsedReg(regNum); |
Vikram S. Adve | 7a4be95 | 2002-07-08 22:38:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 125 | void |
| 126 | MachineInstr::SetRegForImplicitRef(unsigned i, int regNum) |
| 127 | { |
| 128 | getImplicitOp(i).setRegForValue(regNum); |
| 129 | insertUsedReg(regNum); |
| 130 | } |
| 131 | |
Vikram S. Adve | 7a4be95 | 2002-07-08 22:38:45 +0000 | [diff] [blame] | 132 | |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 133 | // Subsitute all occurrences of Value* oldVal with newVal in all operands |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 134 | // and all implicit refs. |
| 135 | // If defsOnly == true, substitute defs only. |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 136 | unsigned |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 137 | MachineInstr::substituteValue(const Value* oldVal, Value* newVal, |
| 138 | bool defsOnly, bool notDefsAndUses, |
| 139 | bool& someArgsWereIgnored) |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 140 | { |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 141 | assert((defsOnly || !notDefsAndUses) && |
| 142 | "notDefsAndUses is irrelevant if defsOnly == false."); |
| 143 | |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 144 | unsigned numSubst = 0; |
| 145 | |
| 146 | // Subsitute operands |
| 147 | for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O) |
| 148 | if (*O == oldVal) |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 149 | if (!defsOnly || |
| 150 | notDefsAndUses && O.isDefOnly() || |
| 151 | !notDefsAndUses && !O.isUseOnly()) |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 152 | { |
| 153 | O.getMachineOperand().value = newVal; |
| 154 | ++numSubst; |
| 155 | } |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 156 | else |
| 157 | someArgsWereIgnored = true; |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 158 | |
| 159 | // Subsitute implicit refs |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 160 | for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i) |
Chris Lattner | 27a0893 | 2002-10-22 23:16:21 +0000 | [diff] [blame] | 161 | if (getImplicitRef(i) == oldVal) |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 162 | if (!defsOnly || |
| 163 | notDefsAndUses && getImplicitOp(i).opIsDefOnly() || |
| 164 | !notDefsAndUses && !getImplicitOp(i).opIsUse()) |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 165 | { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 166 | getImplicitOp(i).value = newVal; |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 167 | ++numSubst; |
| 168 | } |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 169 | else |
| 170 | someArgsWereIgnored = true; |
Vikram S. Adve | e2a78e3 | 2002-08-14 16:52:58 +0000 | [diff] [blame] | 171 | |
| 172 | return numSubst; |
| 173 | } |
| 174 | |
| 175 | |
Vikram S. Adve | 7a4be95 | 2002-07-08 22:38:45 +0000 | [diff] [blame] | 176 | void |
| 177 | MachineInstr::dump() const |
| 178 | { |
Chris Lattner | 925b771 | 2003-08-03 20:24:29 +0000 | [diff] [blame] | 179 | std::cerr << " " << *this; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Vikram S. Adve | 8c6936a | 2002-09-16 15:18:53 +0000 | [diff] [blame] | 182 | static inline std::ostream& |
| 183 | OutputValue(std::ostream &os, const Value* val) |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 184 | { |
| 185 | os << "(val "; |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 186 | os << (void*) val; // print address always |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 187 | if (val && val->hasName()) |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 188 | os << " " << val->getName() << ")"; // print name also, if available |
| 189 | return os; |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 192 | static inline void OutputReg(std::ostream &os, unsigned RegNo, |
| 193 | const MRegisterInfo *MRI = 0) { |
| 194 | if (MRI) { |
| 195 | if (RegNo < MRegisterInfo::FirstVirtualRegister) |
| 196 | os << "%" << MRI->get(RegNo).Name; |
| 197 | else |
| 198 | os << "%reg" << RegNo; |
| 199 | } else |
| 200 | os << "%mreg(" << RegNo << ")"; |
Vikram S. Adve | 8c6936a | 2002-09-16 15:18:53 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 203 | static void print(const MachineOperand &MO, std::ostream &OS, |
| 204 | const TargetMachine &TM) { |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 205 | const MRegisterInfo *MRI = TM.getRegisterInfo(); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 206 | bool CloseParen = true; |
| 207 | if (MO.opHiBits32()) |
| 208 | OS << "%lm("; |
| 209 | else if (MO.opLoBits32()) |
| 210 | OS << "%lo("; |
| 211 | else if (MO.opHiBits64()) |
| 212 | OS << "%hh("; |
| 213 | else if (MO.opLoBits64()) |
| 214 | OS << "%hm("; |
| 215 | else |
| 216 | CloseParen = false; |
| 217 | |
| 218 | switch (MO.getType()) { |
| 219 | case MachineOperand::MO_VirtualRegister: |
| 220 | if (MO.getVRegValue()) { |
| 221 | OS << "%reg"; |
| 222 | OutputValue(OS, MO.getVRegValue()); |
| 223 | if (MO.hasAllocatedReg()) |
| 224 | OS << "=="; |
| 225 | } |
| 226 | if (MO.hasAllocatedReg()) |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 227 | OutputReg(OS, MO.getAllocatedRegNum(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 228 | break; |
| 229 | case MachineOperand::MO_CCRegister: |
| 230 | OS << "%ccreg"; |
| 231 | OutputValue(OS, MO.getVRegValue()); |
| 232 | if (MO.hasAllocatedReg()) { |
| 233 | OS << "=="; |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 234 | OutputReg(OS, MO.getAllocatedRegNum(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | case MachineOperand::MO_MachineRegister: |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 238 | OutputReg(OS, MO.getMachineRegNum(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 239 | break; |
| 240 | case MachineOperand::MO_SignExtendedImmed: |
| 241 | OS << (long)MO.getImmedValue(); |
| 242 | break; |
| 243 | case MachineOperand::MO_UnextendedImmed: |
| 244 | OS << (long)MO.getImmedValue(); |
| 245 | break; |
| 246 | case MachineOperand::MO_PCRelativeDisp: { |
| 247 | const Value* opVal = MO.getVRegValue(); |
| 248 | bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); |
| 249 | OS << "%disp(" << (isLabel? "label " : "addr-of-val "); |
| 250 | if (opVal->hasName()) |
| 251 | OS << opVal->getName(); |
| 252 | else |
| 253 | OS << (const void*) opVal; |
| 254 | OS << ")"; |
| 255 | break; |
| 256 | } |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 257 | case MachineOperand::MO_MachineBasicBlock: |
| 258 | OS << "bb<" |
| 259 | << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() |
| 260 | << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">"; |
| 261 | break; |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 262 | case MachineOperand::MO_FrameIndex: |
| 263 | OS << "<fi#" << MO.getFrameIndex() << ">"; |
| 264 | break; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 265 | case MachineOperand::MO_ConstantPoolIndex: |
| 266 | OS << "<cp#" << MO.getConstantPoolIndex() << ">"; |
| 267 | break; |
| 268 | case MachineOperand::MO_GlobalAddress: |
| 269 | OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">"; |
| 270 | break; |
| 271 | case MachineOperand::MO_ExternalSymbol: |
| 272 | OS << "<es:" << MO.getSymbolName() << ">"; |
| 273 | break; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 274 | default: |
| 275 | assert(0 && "Unrecognized operand type"); |
| 276 | } |
| 277 | |
| 278 | if (CloseParen) |
| 279 | OS << ")"; |
| 280 | } |
| 281 | |
Chris Lattner | af55be1 | 2002-11-17 23:22:13 +0000 | [diff] [blame] | 282 | void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const { |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 283 | unsigned StartOp = 0; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 285 | // Specialize printing if op#0 is definition |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 286 | if (getNumOperands() && |
| 287 | (getOperand(0).opIsDefOnly() || getOperand(0).opIsDefAndUse())) { |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 288 | ::print(getOperand(0), OS, TM); |
| 289 | OS << " = "; |
| 290 | ++StartOp; // Don't print this operand again! |
| 291 | } |
| 292 | OS << TM.getInstrInfo().getName(getOpcode()); |
| 293 | |
| 294 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 295 | const MachineOperand& mop = getOperand(i); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 296 | if (i != StartOp) |
| 297 | OS << ","; |
| 298 | OS << " "; |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 299 | ::print(mop, OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 300 | |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 301 | if (mop.opIsDefAndUse()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 302 | OS << "<def&use>"; |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 303 | else if (mop.opIsDefOnly()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 304 | OS << "<def>"; |
| 305 | } |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 307 | // code for printing implict references |
| 308 | if (getNumImplicitRefs()) { |
| 309 | OS << "\tImplicitRefs: "; |
| 310 | for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) { |
| 311 | OS << "\t"; |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 312 | OutputValue(OS, getImplicitRef(i)); |
| 313 | if (getImplicitOp(i).opIsDefAndUse()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 314 | OS << "<def&use>"; |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 315 | else if (getImplicitOp(i).opIsDefOnly()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 316 | OS << "<def>"; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | OS << "\n"; |
| 321 | } |
| 322 | |
| 323 | |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 324 | std::ostream &operator<<(std::ostream& os, const MachineInstr& MI) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 325 | { |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 326 | os << TargetInstrDescriptors[MI.opCode].Name; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 328 | for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) { |
| 329 | os << "\t" << MI.getOperand(i); |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 330 | if (MI.getOperand(i).opIsDefOnly()) |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 331 | os << "<d>"; |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 332 | if (MI.getOperand(i).opIsDefAndUse()) |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 333 | os << "<d&u>"; |
Ruchira Sasanka | 8d24337 | 2001-11-14 20:05:23 +0000 | [diff] [blame] | 334 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 335 | |
Ruchira Sasanka | 69917e2 | 2001-10-18 22:40:02 +0000 | [diff] [blame] | 336 | // code for printing implict references |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 337 | unsigned NumOfImpRefs = MI.getNumImplicitRefs(); |
| 338 | if (NumOfImpRefs > 0) { |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 339 | os << "\tImplicit: "; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 340 | for (unsigned z=0; z < NumOfImpRefs; z++) { |
| 341 | OutputValue(os, MI.getImplicitRef(z)); |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 342 | if (MI.getImplicitOp(z).opIsDefOnly()) os << "<d>"; |
| 343 | if (MI.getImplicitOp(z).opIsDefAndUse()) os << "<d&u>"; |
Ruchira Sasanka | 07c7086 | 2001-11-15 20:46:40 +0000 | [diff] [blame] | 344 | os << "\t"; |
Ruchira Sasanka | 69917e2 | 2001-10-18 22:40:02 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 348 | return os << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 351 | std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 352 | { |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 353 | if (MO.opHiBits32()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 354 | OS << "%lm("; |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 355 | else if (MO.opLoBits32()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 356 | OS << "%lo("; |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 357 | else if (MO.opHiBits64()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 358 | OS << "%hh("; |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 359 | else if (MO.opLoBits64()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 360 | OS << "%hm("; |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 362 | switch (MO.getType()) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 363 | { |
| 364 | case MachineOperand::MO_VirtualRegister: |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 365 | if (MO.hasAllocatedReg()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 366 | OutputReg(OS, MO.getAllocatedRegNum()); |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 367 | |
| 368 | if (MO.getVRegValue()) { |
| 369 | if (MO.hasAllocatedReg()) OS << "=="; |
| 370 | OS << "%vreg"; |
| 371 | OutputValue(OS, MO.getVRegValue()); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 372 | } |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 373 | break; |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 374 | case MachineOperand::MO_CCRegister: |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 375 | OS << "%ccreg"; |
| 376 | OutputValue(OS, MO.getVRegValue()); |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 377 | if (MO.hasAllocatedReg()) { |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 378 | OS << "=="; |
| 379 | OutputReg(OS, MO.getAllocatedRegNum()); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 380 | } |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 381 | break; |
| 382 | case MachineOperand::MO_MachineRegister: |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 383 | OutputReg(OS, MO.getMachineRegNum()); |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 384 | break; |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 385 | case MachineOperand::MO_SignExtendedImmed: |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 386 | OS << (long)MO.getImmedValue(); |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 387 | break; |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 388 | case MachineOperand::MO_UnextendedImmed: |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 389 | OS << (long)MO.getImmedValue(); |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 390 | break; |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 391 | case MachineOperand::MO_PCRelativeDisp: |
Vikram S. Adve | e949da5 | 2001-09-30 23:44:19 +0000 | [diff] [blame] | 392 | { |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 393 | const Value* opVal = MO.getVRegValue(); |
Chris Lattner | 4d669b5 | 2002-04-08 22:01:15 +0000 | [diff] [blame] | 394 | bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 395 | OS << "%disp(" << (isLabel? "label " : "addr-of-val "); |
Vikram S. Adve | d9beb97 | 2001-11-12 14:19:47 +0000 | [diff] [blame] | 396 | if (opVal->hasName()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 397 | OS << opVal->getName(); |
Vikram S. Adve | d9beb97 | 2001-11-12 14:19:47 +0000 | [diff] [blame] | 398 | else |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 399 | OS << (const void*) opVal; |
| 400 | OS << ")"; |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 401 | break; |
Vikram S. Adve | e949da5 | 2001-09-30 23:44:19 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 403 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 404 | OS << "bb<" |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 405 | << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() |
| 406 | << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">"; |
| 407 | break; |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 408 | case MachineOperand::MO_FrameIndex: |
| 409 | OS << "<fi#" << MO.getFrameIndex() << ">"; |
| 410 | break; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 411 | case MachineOperand::MO_ConstantPoolIndex: |
| 412 | OS << "<cp#" << MO.getConstantPoolIndex() << ">"; |
| 413 | break; |
| 414 | case MachineOperand::MO_GlobalAddress: |
| 415 | OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">"; |
| 416 | break; |
| 417 | case MachineOperand::MO_ExternalSymbol: |
| 418 | OS << "<es:" << MO.getSymbolName() << ">"; |
| 419 | break; |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 420 | default: |
| 421 | assert(0 && "Unrecognized operand type"); |
| 422 | break; |
| 423 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 425 | if (MO.flags & |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 426 | (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 | |
| 427 | MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64)) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 428 | OS << ")"; |
Vikram S. Adve | 3bc9ef9 | 2002-07-10 21:45:04 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 430 | return OS; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 431 | } |