Chris Lattner | e138b3d | 2008-01-01 20:36:19 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // Methods common to all machine instructions. |
| 11 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 822b4fb | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 15 | #include "llvm/Value.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 19 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | f14cf85 | 2008-01-07 07:42:25 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrDesc.h" |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 23 | #include "llvm/Target/MRegisterInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LeakDetector.h" |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Streams.h" |
Jeff Cohen | c21c5ee | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 26 | #include <ostream> |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // MachineOperand Implementation |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 33 | /// AddRegOperandToRegInfo - Add this register operand to the specified |
| 34 | /// MachineRegisterInfo. If it is null, then the next/prev fields should be |
| 35 | /// explicitly nulled out. |
| 36 | void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { |
| 37 | assert(isReg() && "Can only add reg operand to use lists"); |
| 38 | |
| 39 | // If the reginfo pointer is null, just explicitly null out or next/prev |
| 40 | // pointers, to ensure they are not garbage. |
| 41 | if (RegInfo == 0) { |
| 42 | Contents.Reg.Prev = 0; |
| 43 | Contents.Reg.Next = 0; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // Otherwise, add this operand to the head of the registers use/def list. |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 48 | MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 50 | // For SSA values, we prefer to keep the definition at the start of the list. |
| 51 | // we do this by skipping over the definition if it is at the head of the |
| 52 | // list. |
| 53 | if (*Head && (*Head)->isDef()) |
| 54 | Head = &(*Head)->Contents.Reg.Next; |
| 55 | |
| 56 | Contents.Reg.Next = *Head; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 57 | if (Contents.Reg.Next) { |
| 58 | assert(getReg() == Contents.Reg.Next->getReg() && |
| 59 | "Different regs on the same list!"); |
| 60 | Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; |
| 61 | } |
| 62 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 63 | Contents.Reg.Prev = Head; |
| 64 | *Head = this; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void MachineOperand::setReg(unsigned Reg) { |
| 68 | if (getReg() == Reg) return; // No change. |
| 69 | |
| 70 | // Otherwise, we have to change the register. If this operand is embedded |
| 71 | // into a machine function, we need to update the old and new register's |
| 72 | // use/def lists. |
| 73 | if (MachineInstr *MI = getParent()) |
| 74 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 75 | if (MachineFunction *MF = MBB->getParent()) { |
| 76 | RemoveRegOperandFromRegInfo(); |
| 77 | Contents.Reg.RegNo = Reg; |
| 78 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Otherwise, just change the register, no problem. :) |
| 83 | Contents.Reg.RegNo = Reg; |
| 84 | } |
| 85 | |
| 86 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 87 | /// the specified value. If an operand is known to be an immediate already, |
| 88 | /// the setImm method should be used. |
| 89 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
| 90 | // If this operand is currently a register operand, and if this is in a |
| 91 | // function, deregister the operand from the register's use/def list. |
| 92 | if (isReg() && getParent() && getParent()->getParent() && |
| 93 | getParent()->getParent()->getParent()) |
| 94 | RemoveRegOperandFromRegInfo(); |
| 95 | |
| 96 | OpKind = MO_Immediate; |
| 97 | Contents.ImmVal = ImmVal; |
| 98 | } |
| 99 | |
| 100 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 101 | /// the specified value. If an operand is known to be an register already, |
| 102 | /// the setReg method should be used. |
| 103 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
| 104 | bool isKill, bool isDead) { |
| 105 | // If this operand is already a register operand, use setReg to update the |
| 106 | // register's use/def lists. |
| 107 | if (isReg()) { |
| 108 | setReg(Reg); |
| 109 | } else { |
| 110 | // Otherwise, change this to a register and set the reg#. |
| 111 | OpKind = MO_Register; |
| 112 | Contents.Reg.RegNo = Reg; |
| 113 | |
| 114 | // If this operand is embedded in a function, add the operand to the |
| 115 | // register's use/def list. |
| 116 | if (MachineInstr *MI = getParent()) |
| 117 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 118 | if (MachineFunction *MF = MBB->getParent()) |
| 119 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 120 | } |
| 121 | |
| 122 | IsDef = isDef; |
| 123 | IsImp = isImp; |
| 124 | IsKill = isKill; |
| 125 | IsDead = isDead; |
| 126 | SubReg = 0; |
| 127 | } |
| 128 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 129 | /// isIdenticalTo - Return true if this operand is identical to the specified |
| 130 | /// operand. |
| 131 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
| 132 | if (getType() != Other.getType()) return false; |
| 133 | |
| 134 | switch (getType()) { |
| 135 | default: assert(0 && "Unrecognized operand type"); |
| 136 | case MachineOperand::MO_Register: |
| 137 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 138 | getSubReg() == Other.getSubReg(); |
| 139 | case MachineOperand::MO_Immediate: |
| 140 | return getImm() == Other.getImm(); |
| 141 | case MachineOperand::MO_MachineBasicBlock: |
| 142 | return getMBB() == Other.getMBB(); |
| 143 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 144 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 145 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 146 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 147 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 148 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 149 | case MachineOperand::MO_GlobalAddress: |
| 150 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 151 | case MachineOperand::MO_ExternalSymbol: |
| 152 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 153 | getOffset() == Other.getOffset(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// print - Print the specified machine operand. |
| 158 | /// |
| 159 | void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const { |
| 160 | switch (getType()) { |
| 161 | case MachineOperand::MO_Register: |
| 162 | if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) { |
| 163 | OS << "%reg" << getReg(); |
| 164 | } else { |
| 165 | // If the instruction is embedded into a basic block, we can find the |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 166 | // target info for the instruction. |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 167 | if (TM == 0) |
| 168 | if (const MachineInstr *MI = getParent()) |
| 169 | if (const MachineBasicBlock *MBB = MI->getParent()) |
| 170 | if (const MachineFunction *MF = MBB->getParent()) |
| 171 | TM = &MF->getTarget(); |
| 172 | |
| 173 | if (TM) |
| 174 | OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; |
| 175 | else |
| 176 | OS << "%mreg" << getReg(); |
| 177 | } |
| 178 | |
| 179 | if (isDef() || isKill() || isDead() || isImplicit()) { |
| 180 | OS << "<"; |
| 181 | bool NeedComma = false; |
| 182 | if (isImplicit()) { |
| 183 | OS << (isDef() ? "imp-def" : "imp-use"); |
| 184 | NeedComma = true; |
| 185 | } else if (isDef()) { |
| 186 | OS << "def"; |
| 187 | NeedComma = true; |
| 188 | } |
| 189 | if (isKill() || isDead()) { |
| 190 | if (NeedComma) OS << ","; |
| 191 | if (isKill()) OS << "kill"; |
| 192 | if (isDead()) OS << "dead"; |
| 193 | } |
| 194 | OS << ">"; |
| 195 | } |
| 196 | break; |
| 197 | case MachineOperand::MO_Immediate: |
| 198 | OS << getImm(); |
| 199 | break; |
| 200 | case MachineOperand::MO_MachineBasicBlock: |
| 201 | OS << "mbb<" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 202 | << ((Value*)getMBB()->getBasicBlock())->getName() |
| 203 | << "," << (void*)getMBB() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 204 | break; |
| 205 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 206 | OS << "<fi#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 207 | break; |
| 208 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 209 | OS << "<cp#" << getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 210 | if (getOffset()) OS << "+" << getOffset(); |
| 211 | OS << ">"; |
| 212 | break; |
| 213 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 214 | OS << "<jt#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 215 | break; |
| 216 | case MachineOperand::MO_GlobalAddress: |
| 217 | OS << "<ga:" << ((Value*)getGlobal())->getName(); |
| 218 | if (getOffset()) OS << "+" << getOffset(); |
| 219 | OS << ">"; |
| 220 | break; |
| 221 | case MachineOperand::MO_ExternalSymbol: |
| 222 | OS << "<es:" << getSymbolName(); |
| 223 | if (getOffset()) OS << "+" << getOffset(); |
| 224 | OS << ">"; |
| 225 | break; |
| 226 | default: |
| 227 | assert(0 && "Unrecognized operand type"); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | // MachineInstr Implementation |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 235 | /// MachineInstr ctor - This constructor creates a dummy MachineInstr with |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 236 | /// TID NULL and no operands. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 237 | MachineInstr::MachineInstr() |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 238 | : TID(0), NumImplicitOps(0), Parent(0) { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 239 | // Make sure that we get added to a machine basicblock |
| 240 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 243 | void MachineInstr::addImplicitDefUseOperands() { |
| 244 | if (TID->ImplicitDefs) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 245 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 246 | addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 247 | if (TID->ImplicitUses) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 248 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 249 | addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /// MachineInstr ctor - This constructor create a MachineInstr and add the |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 253 | /// implicit operands. It reserves space for number of operands specified by |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 254 | /// TargetInstrDesc or the numOperands if it is not zero. (for |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 255 | /// instructions with variable number of operands). |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 256 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 257 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 258 | if (!NoImp && TID->getImplicitDefs()) |
| 259 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 260 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 261 | if (!NoImp && TID->getImplicitUses()) |
| 262 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 263 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 264 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 265 | if (!NoImp) |
| 266 | addImplicitDefUseOperands(); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 267 | // Make sure that we get added to a machine basicblock |
| 268 | LeakDetector::addGarbageObject(this); |
| 269 | } |
| 270 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 271 | /// MachineInstr ctor - Work exactly the same as the ctor above, except that the |
| 272 | /// MachineInstr is created and added to the end of the specified basic block. |
| 273 | /// |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 274 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 275 | const TargetInstrDesc &tid) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 276 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 277 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 278 | if (TID->ImplicitDefs) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 279 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 280 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 281 | if (TID->ImplicitUses) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 282 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 283 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 284 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 285 | addImplicitDefUseOperands(); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 286 | // Make sure that we get added to a machine basicblock |
| 287 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 288 | MBB->push_back(this); // Add instruction to end of basic block! |
| 289 | } |
| 290 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 291 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 292 | /// |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 293 | MachineInstr::MachineInstr(const MachineInstr &MI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 294 | TID = &MI.getDesc(); |
Evan Cheng | 6b2c05f | 2006-11-15 20:54:29 +0000 | [diff] [blame] | 295 | NumImplicitOps = MI.NumImplicitOps; |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 296 | Operands.reserve(MI.getNumOperands()); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 297 | MemOperands = MI.MemOperands; |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 298 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 299 | // Add operands |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 300 | for (unsigned i = 0; i != MI.getNumOperands(); ++i) { |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 301 | Operands.push_back(MI.getOperand(i)); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 302 | Operands.back().ParentMI = this; |
| 303 | } |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 304 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 305 | // Set parent, next, and prev to null |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 306 | Parent = 0; |
| 307 | Prev = 0; |
| 308 | Next = 0; |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 312 | MachineInstr::~MachineInstr() { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 313 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 314 | #ifndef NDEBUG |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 315 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 316 | assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 317 | assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && |
| 318 | "Reg operand def/use list corrupted"); |
| 319 | } |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 320 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 323 | /// getOpcode - Returns the opcode of this MachineInstr. |
| 324 | /// |
Dan Gohman | cb648f9 | 2007-09-14 20:08:19 +0000 | [diff] [blame] | 325 | int MachineInstr::getOpcode() const { |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 326 | return TID->Opcode; |
| 327 | } |
| 328 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 329 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 330 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 331 | /// return null. |
| 332 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 333 | if (MachineBasicBlock *MBB = getParent()) |
| 334 | if (MachineFunction *MF = MBB->getParent()) |
| 335 | return &MF->getRegInfo(); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 340 | /// this instruction from their respective use lists. This requires that the |
| 341 | /// operands already be on their use lists. |
| 342 | void MachineInstr::RemoveRegOperandsFromUseLists() { |
| 343 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 344 | if (Operands[i].isReg()) |
| 345 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 350 | /// this instruction from their respective use lists. This requires that the |
| 351 | /// operands not be on their use lists yet. |
| 352 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { |
| 353 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 354 | if (Operands[i].isReg()) |
| 355 | Operands[i].AddRegOperandToRegInfo(&RegInfo); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 361 | /// implicit operand, it is added to the end of the operand list. If it is |
| 362 | /// an explicit operand it is added at the end of the explicit operand list |
| 363 | /// (before the first implicit operand). |
| 364 | void MachineInstr::addOperand(const MachineOperand &Op) { |
| 365 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
| 366 | assert((isImpReg || !OperandsComplete()) && |
| 367 | "Trying to add an operand to a machine instr that is already done!"); |
| 368 | |
| 369 | // If we are adding the operand to the end of the list, our job is simpler. |
| 370 | // This is true most of the time, so this is a reasonable optimization. |
| 371 | if (isImpReg || NumImplicitOps == 0) { |
| 372 | // We can only do this optimization if we know that the operand list won't |
| 373 | // reallocate. |
| 374 | if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { |
| 375 | Operands.push_back(Op); |
| 376 | |
| 377 | // Set the parent of the operand. |
| 378 | Operands.back().ParentMI = this; |
| 379 | |
| 380 | // If the operand is a register, update the operand's use list. |
| 381 | if (Op.isReg()) |
| 382 | Operands.back().AddRegOperandToRegInfo(getRegInfo()); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Otherwise, we have to insert a real operand before any implicit ones. |
| 388 | unsigned OpNo = Operands.size()-NumImplicitOps; |
| 389 | |
| 390 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 391 | |
| 392 | // If this instruction isn't embedded into a function, then we don't need to |
| 393 | // update any operand lists. |
| 394 | if (RegInfo == 0) { |
| 395 | // Simple insertion, no reginfo update needed for other register operands. |
| 396 | Operands.insert(Operands.begin()+OpNo, Op); |
| 397 | Operands[OpNo].ParentMI = this; |
| 398 | |
| 399 | // Do explicitly set the reginfo for this operand though, to ensure the |
| 400 | // next/prev fields are properly nulled out. |
| 401 | if (Operands[OpNo].isReg()) |
| 402 | Operands[OpNo].AddRegOperandToRegInfo(0); |
| 403 | |
| 404 | } else if (Operands.size()+1 <= Operands.capacity()) { |
| 405 | // Otherwise, we have to remove register operands from their register use |
| 406 | // list, add the operand, then add the register operands back to their use |
| 407 | // list. This also must handle the case when the operand list reallocates |
| 408 | // to somewhere else. |
| 409 | |
| 410 | // If insertion of this operand won't cause reallocation of the operand |
| 411 | // list, just remove the implicit operands, add the operand, then re-add all |
| 412 | // the rest of the operands. |
| 413 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 414 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 415 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 416 | } |
| 417 | |
| 418 | // Add the operand. If it is a register, add it to the reg list. |
| 419 | Operands.insert(Operands.begin()+OpNo, Op); |
| 420 | Operands[OpNo].ParentMI = this; |
| 421 | |
| 422 | if (Operands[OpNo].isReg()) |
| 423 | Operands[OpNo].AddRegOperandToRegInfo(RegInfo); |
| 424 | |
| 425 | // Re-add all the implicit ops. |
| 426 | for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { |
| 427 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 428 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 429 | } |
| 430 | } else { |
| 431 | // Otherwise, we will be reallocating the operand list. Remove all reg |
| 432 | // operands from their list, then readd them after the operand list is |
| 433 | // reallocated. |
| 434 | RemoveRegOperandsFromUseLists(); |
| 435 | |
| 436 | Operands.insert(Operands.begin()+OpNo, Op); |
| 437 | Operands[OpNo].ParentMI = this; |
| 438 | |
| 439 | // Re-add all the operands. |
| 440 | AddRegOperandsToUseLists(*RegInfo); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 445 | /// fewer operand than it started with. |
| 446 | /// |
| 447 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
| 448 | assert(OpNo < Operands.size() && "Invalid operand number"); |
| 449 | |
| 450 | // Special case removing the last one. |
| 451 | if (OpNo == Operands.size()-1) { |
| 452 | // If needed, remove from the reg def/use list. |
| 453 | if (Operands.back().isReg() && Operands.back().isOnRegUseList()) |
| 454 | Operands.back().RemoveRegOperandFromRegInfo(); |
| 455 | |
| 456 | Operands.pop_back(); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // Otherwise, we are removing an interior operand. If we have reginfo to |
| 461 | // update, remove all operands that will be shifted down from their reg lists, |
| 462 | // move everything down, then re-add them. |
| 463 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 464 | if (RegInfo) { |
| 465 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 466 | if (Operands[i].isReg()) |
| 467 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | Operands.erase(Operands.begin()+OpNo); |
| 472 | |
| 473 | if (RegInfo) { |
| 474 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 475 | if (Operands[i].isReg()) |
| 476 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 482 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 483 | /// block, and returns it, but does not delete it. |
| 484 | MachineInstr *MachineInstr::removeFromParent() { |
| 485 | assert(getParent() && "Not embedded in a basic block!"); |
| 486 | getParent()->remove(this); |
| 487 | return this; |
| 488 | } |
| 489 | |
| 490 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 491 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 492 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 493 | bool MachineInstr::OperandsComplete() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 494 | unsigned short NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 495 | if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 496 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 500 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 501 | /// |
| 502 | unsigned MachineInstr::getNumExplicitOperands() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 503 | unsigned NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 504 | if (!TID->isVariadic()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 505 | return NumOperands; |
| 506 | |
| 507 | for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) { |
| 508 | const MachineOperand &MO = getOperand(NumOperands); |
| 509 | if (!MO.isRegister() || !MO.isImplicit()) |
| 510 | NumOperands++; |
| 511 | } |
| 512 | return NumOperands; |
| 513 | } |
| 514 | |
Chris Lattner | 8ace2cd | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 515 | |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 516 | /// isDebugLabel - Returns true if the MachineInstr represents a debug label. |
| 517 | /// |
| 518 | bool MachineInstr::isDebugLabel() const { |
| 519 | return getOpcode() == TargetInstrInfo::LABEL && getOperand(1).getImm() == 0; |
| 520 | } |
| 521 | |
Evan Cheng | faa5107 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 522 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 523 | /// the specific register or -1 if it is not found. It further tightening |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 524 | /// the search criteria to a use that kills the register if isKill is true. |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 525 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const { |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 527 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 528 | if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg) |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 529 | if (!isKill || MO.isKill()) |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 530 | return i; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 531 | } |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 532 | return -1; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 535 | /// findRegisterDefOperand() - Returns the MachineOperand that is a def of |
| 536 | /// the specific register or NULL if it is not found. |
| 537 | MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) { |
| 538 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 539 | MachineOperand &MO = getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 540 | if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg) |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 541 | return &MO; |
| 542 | } |
| 543 | return NULL; |
| 544 | } |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 545 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 546 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 547 | /// operand list that is used to represent the predicate. It returns -1 if |
| 548 | /// none is found. |
| 549 | int MachineInstr::findFirstPredOperandIdx() const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 550 | const TargetInstrDesc &TID = getDesc(); |
| 551 | if (TID.isPredicable()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 552 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 553 | if (TID.OpInfo[i].isPredicate()) |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 554 | return i; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 557 | return -1; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 558 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 559 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 560 | /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due |
| 561 | /// to two addr elimination. |
| 562 | bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 563 | const TargetInstrDesc &TID = getDesc(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 564 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 565 | const MachineOperand &MO1 = getOperand(i); |
| 566 | if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { |
| 567 | for (unsigned j = i+1; j < e; ++j) { |
| 568 | const MachineOperand &MO2 = getOperand(j); |
| 569 | if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 570 | TID.getOperandConstraint(j, TOI::TIED_TO) == (int)i) |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | return false; |
| 576 | } |
| 577 | |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 578 | /// copyKillDeadInfo - Copies kill / dead operand properties from MI. |
| 579 | /// |
| 580 | void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { |
| 581 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 582 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 583 | if (!MO.isRegister() || (!MO.isKill() && !MO.isDead())) |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 584 | continue; |
| 585 | for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { |
| 586 | MachineOperand &MOp = getOperand(j); |
| 587 | if (!MOp.isIdenticalTo(MO)) |
| 588 | continue; |
| 589 | if (MO.isKill()) |
| 590 | MOp.setIsKill(); |
| 591 | else |
| 592 | MOp.setIsDead(); |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 598 | /// copyPredicates - Copies predicate operand(s) from MI. |
| 599 | void MachineInstr::copyPredicates(const MachineInstr *MI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 600 | const TargetInstrDesc &TID = MI->getDesc(); |
| 601 | if (TID.isPredicable()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 602 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 603 | if (TID.OpInfo[i].isPredicate()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 604 | // Predicated operands must be last operands. |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 605 | addOperand(MI->getOperand(i)); |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 611 | void MachineInstr::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 612 | cerr << " " << *this; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 615 | void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | e308789 | 2007-12-30 21:31:53 +0000 | [diff] [blame] | 616 | // Specialize printing if op#0 is definition |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 617 | unsigned StartOp = 0; |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 618 | if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 619 | getOperand(0).print(OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 620 | OS << " = "; |
| 621 | ++StartOp; // Don't print this operand again! |
| 622 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 624 | OS << getDesc().getName(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 626 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
| 627 | if (i != StartOp) |
| 628 | OS << ","; |
| 629 | OS << " "; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 630 | getOperand(i).print(OS, TM); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 631 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 632 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 633 | if (getNumMemOperands() > 0) { |
| 634 | OS << ", SV:"; |
| 635 | for (unsigned i = 0; i < getNumMemOperands(); i++) { |
| 636 | const MemOperand &MRO = getMemOperand(i); |
| 637 | const Value *V = MRO.getValue(); |
| 638 | |
| 639 | assert(V && "SV missing."); |
| 640 | assert((MRO.isLoad() || MRO.isStore()) && |
| 641 | "SV has to be a load, store or both."); |
| 642 | |
| 643 | if (MRO.isVolatile()) |
| 644 | OS << "Volatile "; |
| 645 | if (MRO.isLoad()) |
| 646 | OS << "LD "; |
| 647 | if (MRO.isStore()) |
| 648 | OS << "ST "; |
| 649 | |
| 650 | OS << MRO.getSize(); |
| 651 | |
| 652 | if (!V->getName().empty()) |
| 653 | OS << "[" << V->getName() << " + " << MRO.getOffset() << "]"; |
| 654 | else if (isa<PseudoSourceValue>(V)) |
| 655 | OS << "[" << *V << " + " << MRO.getOffset() << "]"; |
| 656 | else |
| 657 | OS << "[" << V << " + " << MRO.getOffset() << "]"; |
| 658 | } |
| 659 | } |
| 660 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 661 | OS << "\n"; |
| 662 | } |
| 663 | |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 664 | bool MachineInstr::addRegisterKilled(unsigned IncomingReg, |
| 665 | const MRegisterInfo *RegInfo, |
| 666 | bool AddIfNotFound) { |
| 667 | bool Found = false; |
| 668 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 669 | MachineOperand &MO = getOperand(i); |
| 670 | if (MO.isRegister() && MO.isUse()) { |
| 671 | unsigned Reg = MO.getReg(); |
| 672 | if (!Reg) |
| 673 | continue; |
| 674 | if (Reg == IncomingReg) { |
| 675 | MO.setIsKill(); |
| 676 | Found = true; |
| 677 | break; |
| 678 | } else if (MRegisterInfo::isPhysicalRegister(Reg) && |
| 679 | MRegisterInfo::isPhysicalRegister(IncomingReg) && |
| 680 | RegInfo->isSuperRegister(IncomingReg, Reg) && |
| 681 | MO.isKill()) |
| 682 | // A super-register kill already exists. |
| 683 | Found = true; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // If not found, this means an alias of one of the operand is killed. Add a |
| 688 | // new implicit operand if required. |
| 689 | if (!Found && AddIfNotFound) { |
| 690 | addOperand(MachineOperand::CreateReg(IncomingReg, false/*IsDef*/, |
| 691 | true/*IsImp*/,true/*IsKill*/)); |
| 692 | return true; |
| 693 | } |
| 694 | return Found; |
| 695 | } |
| 696 | |
| 697 | bool MachineInstr::addRegisterDead(unsigned IncomingReg, |
| 698 | const MRegisterInfo *RegInfo, |
| 699 | bool AddIfNotFound) { |
| 700 | bool Found = false; |
| 701 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 702 | MachineOperand &MO = getOperand(i); |
| 703 | if (MO.isRegister() && MO.isDef()) { |
| 704 | unsigned Reg = MO.getReg(); |
| 705 | if (!Reg) |
| 706 | continue; |
| 707 | if (Reg == IncomingReg) { |
| 708 | MO.setIsDead(); |
| 709 | Found = true; |
| 710 | break; |
| 711 | } else if (MRegisterInfo::isPhysicalRegister(Reg) && |
| 712 | MRegisterInfo::isPhysicalRegister(IncomingReg) && |
| 713 | RegInfo->isSuperRegister(IncomingReg, Reg) && |
| 714 | MO.isDead()) |
| 715 | // There exists a super-register that's marked dead. |
| 716 | return true; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // If not found, this means an alias of one of the operand is dead. Add a |
| 721 | // new implicit operand. |
| 722 | if (!Found && AddIfNotFound) { |
| 723 | addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/, |
| 724 | true/*IsImp*/,false/*IsKill*/, |
| 725 | true/*IsDead*/)); |
| 726 | return true; |
| 727 | } |
| 728 | return Found; |
| 729 | } |
| 730 | |
| 731 | /// copyKillDeadInfo - copies killed/dead information from one instr to another |
| 732 | void MachineInstr::copyKillDeadInfo(MachineInstr *OldMI, |
| 733 | const MRegisterInfo *RegInfo) { |
| 734 | // If the instruction defines any virtual registers, update the VarInfo, |
| 735 | // kill and dead information for the instruction. |
| 736 | for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) { |
| 737 | MachineOperand &MO = OldMI->getOperand(i); |
| 738 | if (MO.isRegister() && MO.getReg() && |
| 739 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 740 | unsigned Reg = MO.getReg(); |
| 741 | if (MO.isDef()) { |
| 742 | if (MO.isDead()) { |
| 743 | MO.setIsDead(false); |
| 744 | addRegisterDead(Reg, RegInfo); |
| 745 | } |
| 746 | } |
| 747 | if (MO.isKill()) { |
| 748 | MO.setIsKill(false); |
| 749 | addRegisterKilled(Reg, RegInfo); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |