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