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" |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 17 | #include "llvm/InlineAsm.h" |
Chris Lattner | 5e9cd43 | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 19 | #include "llvm/Value.h" |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineMemOperand.h" |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | f14cf85 | 2008-01-07 07:42:25 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrDesc.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/AliasAnalysis.h" |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/DebugInfo.h" |
David Greene | 3b32533 | 2010-01-04 23:48:20 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 33 | #include "llvm/Support/LeakDetector.h" |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/FoldingSet.h" |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 37 | #include "llvm/Metadata.h" |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // MachineOperand Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 44 | /// AddRegOperandToRegInfo - Add this register operand to the specified |
| 45 | /// MachineRegisterInfo. If it is null, then the next/prev fields should be |
| 46 | /// explicitly nulled out. |
| 47 | void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 48 | assert(isReg() && "Can only add reg operand to use lists"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 49 | |
| 50 | // If the reginfo pointer is null, just explicitly null out or next/prev |
| 51 | // pointers, to ensure they are not garbage. |
| 52 | if (RegInfo == 0) { |
| 53 | Contents.Reg.Prev = 0; |
| 54 | Contents.Reg.Next = 0; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // 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] | 59 | MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 61 | // For SSA values, we prefer to keep the definition at the start of the list. |
| 62 | // we do this by skipping over the definition if it is at the head of the |
| 63 | // list. |
| 64 | if (*Head && (*Head)->isDef()) |
| 65 | Head = &(*Head)->Contents.Reg.Next; |
| 66 | |
| 67 | Contents.Reg.Next = *Head; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 68 | if (Contents.Reg.Next) { |
| 69 | assert(getReg() == Contents.Reg.Next->getReg() && |
| 70 | "Different regs on the same list!"); |
| 71 | Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; |
| 72 | } |
| 73 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 74 | Contents.Reg.Prev = Head; |
| 75 | *Head = this; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Dan Gohman | 3bc1a37 | 2009-04-15 01:17:37 +0000 | [diff] [blame] | 78 | /// RemoveRegOperandFromRegInfo - Remove this register operand from the |
| 79 | /// MachineRegisterInfo it is linked with. |
| 80 | void MachineOperand::RemoveRegOperandFromRegInfo() { |
| 81 | assert(isOnRegUseList() && "Reg operand is not on a use list"); |
| 82 | // Unlink this from the doubly linked list of operands. |
| 83 | MachineOperand *NextOp = Contents.Reg.Next; |
| 84 | *Contents.Reg.Prev = NextOp; |
| 85 | if (NextOp) { |
| 86 | assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); |
| 87 | NextOp->Contents.Reg.Prev = Contents.Reg.Prev; |
| 88 | } |
| 89 | Contents.Reg.Prev = 0; |
| 90 | Contents.Reg.Next = 0; |
| 91 | } |
| 92 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 93 | void MachineOperand::setReg(unsigned Reg) { |
| 94 | if (getReg() == Reg) return; // No change. |
| 95 | |
| 96 | // Otherwise, we have to change the register. If this operand is embedded |
| 97 | // into a machine function, we need to update the old and new register's |
| 98 | // use/def lists. |
| 99 | if (MachineInstr *MI = getParent()) |
| 100 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 101 | if (MachineFunction *MF = MBB->getParent()) { |
| 102 | RemoveRegOperandFromRegInfo(); |
| 103 | Contents.Reg.RegNo = Reg; |
| 104 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Otherwise, just change the register, no problem. :) |
| 109 | Contents.Reg.RegNo = Reg; |
| 110 | } |
| 111 | |
| 112 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 113 | /// the specified value. If an operand is known to be an immediate already, |
| 114 | /// the setImm method should be used. |
| 115 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
| 116 | // If this operand is currently a register operand, and if this is in a |
| 117 | // function, deregister the operand from the register's use/def list. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 118 | if (isReg() && getParent() && getParent()->getParent() && |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 119 | getParent()->getParent()->getParent()) |
| 120 | RemoveRegOperandFromRegInfo(); |
| 121 | |
| 122 | OpKind = MO_Immediate; |
| 123 | Contents.ImmVal = ImmVal; |
| 124 | } |
| 125 | |
| 126 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 127 | /// the specified value. If an operand is known to be an register already, |
| 128 | /// the setReg method should be used. |
| 129 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
Dale Johannesen | 9653f9e | 2010-02-10 00:41:49 +0000 | [diff] [blame] | 130 | bool isKill, bool isDead, bool isUndef, |
| 131 | bool isDebug) { |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 132 | // If this operand is already a register operand, use setReg to update the |
| 133 | // register's use/def lists. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 134 | if (isReg()) { |
Dale Johannesen | e009180 | 2008-09-14 01:44:36 +0000 | [diff] [blame] | 135 | assert(!isEarlyClobber()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 136 | setReg(Reg); |
| 137 | } else { |
| 138 | // Otherwise, change this to a register and set the reg#. |
| 139 | OpKind = MO_Register; |
| 140 | Contents.Reg.RegNo = Reg; |
| 141 | |
| 142 | // If this operand is embedded in a function, add the operand to the |
| 143 | // register's use/def list. |
| 144 | if (MachineInstr *MI = getParent()) |
| 145 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 146 | if (MachineFunction *MF = MBB->getParent()) |
| 147 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 148 | } |
| 149 | |
| 150 | IsDef = isDef; |
| 151 | IsImp = isImp; |
| 152 | IsKill = isKill; |
| 153 | IsDead = isDead; |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 154 | IsUndef = isUndef; |
Dale Johannesen | e009180 | 2008-09-14 01:44:36 +0000 | [diff] [blame] | 155 | IsEarlyClobber = false; |
Dale Johannesen | 9653f9e | 2010-02-10 00:41:49 +0000 | [diff] [blame] | 156 | IsDebug = isDebug; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 157 | SubReg = 0; |
| 158 | } |
| 159 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 160 | /// isIdenticalTo - Return true if this operand is identical to the specified |
| 161 | /// operand. |
| 162 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 163 | if (getType() != Other.getType() || |
| 164 | getTargetFlags() != Other.getTargetFlags()) |
| 165 | return false; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 166 | |
| 167 | switch (getType()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 168 | default: llvm_unreachable("Unrecognized operand type"); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 169 | case MachineOperand::MO_Register: |
| 170 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 171 | getSubReg() == Other.getSubReg(); |
| 172 | case MachineOperand::MO_Immediate: |
| 173 | return getImm() == Other.getImm(); |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 174 | case MachineOperand::MO_FPImmediate: |
| 175 | return getFPImm() == Other.getFPImm(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 176 | case MachineOperand::MO_MachineBasicBlock: |
| 177 | return getMBB() == Other.getMBB(); |
| 178 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 179 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 180 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 181 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 182 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 183 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 184 | case MachineOperand::MO_GlobalAddress: |
| 185 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 186 | case MachineOperand::MO_ExternalSymbol: |
| 187 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 188 | getOffset() == Other.getOffset(); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 189 | case MachineOperand::MO_BlockAddress: |
| 190 | return getBlockAddress() == Other.getBlockAddress(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /// print - Print the specified machine operand. |
| 195 | /// |
Mon P Wang | 5ca6bd1 | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 196 | void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 197 | // If the instruction is embedded into a basic block, we can find the |
| 198 | // target info for the instruction. |
| 199 | if (!TM) |
| 200 | if (const MachineInstr *MI = getParent()) |
| 201 | if (const MachineBasicBlock *MBB = MI->getParent()) |
| 202 | if (const MachineFunction *MF = MBB->getParent()) |
| 203 | TM = &MF->getTarget(); |
| 204 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 205 | switch (getType()) { |
| 206 | case MachineOperand::MO_Register: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 207 | if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 208 | OS << "%reg" << getReg(); |
| 209 | } else { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 210 | if (TM) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 211 | OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 212 | else |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 213 | OS << "%physreg" << getReg(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 214 | } |
Dan Gohman | 2ccc839 | 2008-12-18 21:51:27 +0000 | [diff] [blame] | 215 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 216 | if (getSubReg() != 0) |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 217 | OS << ':' << getSubReg(); |
Dan Gohman | 2ccc839 | 2008-12-18 21:51:27 +0000 | [diff] [blame] | 218 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 219 | if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || |
| 220 | isEarlyClobber()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 221 | OS << '<'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 222 | bool NeedComma = false; |
Evan Cheng | 0789707 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 223 | if (isDef()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 224 | if (NeedComma) OS << ','; |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 225 | if (isEarlyClobber()) |
| 226 | OS << "earlyclobber,"; |
Evan Cheng | 0789707 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 227 | if (isImplicit()) |
| 228 | OS << "imp-"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 229 | OS << "def"; |
| 230 | NeedComma = true; |
Evan Cheng | 5affca0 | 2009-10-21 07:56:02 +0000 | [diff] [blame] | 231 | } else if (isImplicit()) { |
Evan Cheng | 0789707 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 232 | OS << "imp-use"; |
Evan Cheng | 5affca0 | 2009-10-21 07:56:02 +0000 | [diff] [blame] | 233 | NeedComma = true; |
| 234 | } |
Evan Cheng | 0789707 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 235 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 236 | if (isKill() || isDead() || isUndef()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 237 | if (NeedComma) OS << ','; |
Bill Wendling | 181eb73 | 2008-02-24 00:56:13 +0000 | [diff] [blame] | 238 | if (isKill()) OS << "kill"; |
| 239 | if (isDead()) OS << "dead"; |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 240 | if (isUndef()) { |
| 241 | if (isKill() || isDead()) |
| 242 | OS << ','; |
| 243 | OS << "undef"; |
| 244 | } |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 246 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 247 | } |
| 248 | break; |
| 249 | case MachineOperand::MO_Immediate: |
| 250 | OS << getImm(); |
| 251 | break; |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 252 | case MachineOperand::MO_FPImmediate: |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 253 | if (getFPImm()->getType()->isFloatTy()) |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 254 | OS << getFPImm()->getValueAPF().convertToFloat(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 255 | else |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 256 | OS << getFPImm()->getValueAPF().convertToDouble(); |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 257 | break; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 258 | case MachineOperand::MO_MachineBasicBlock: |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 259 | OS << "<BB#" << getMBB()->getNumber() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 260 | break; |
| 261 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 262 | OS << "<fi#" << getIndex() << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 263 | break; |
| 264 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 265 | OS << "<cp#" << getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 266 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 267 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 268 | break; |
| 269 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 270 | OS << "<jt#" << getIndex() << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 271 | break; |
| 272 | case MachineOperand::MO_GlobalAddress: |
Dan Gohman | 8d4e3b5 | 2009-11-06 18:03:10 +0000 | [diff] [blame] | 273 | OS << "<ga:"; |
| 274 | WriteAsOperand(OS, getGlobal(), /*PrintType=*/false); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 275 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 276 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 277 | break; |
| 278 | case MachineOperand::MO_ExternalSymbol: |
| 279 | OS << "<es:" << getSymbolName(); |
| 280 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 281 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 282 | break; |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 283 | case MachineOperand::MO_BlockAddress: |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 284 | OS << '<'; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 285 | WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 286 | OS << '>'; |
| 287 | break; |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 288 | case MachineOperand::MO_Metadata: |
| 289 | OS << '<'; |
| 290 | WriteAsOperand(OS, getMetadata(), /*PrintType=*/false); |
| 291 | OS << '>'; |
| 292 | break; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 293 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 294 | llvm_unreachable("Unrecognized operand type"); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 295 | } |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 296 | |
| 297 | if (unsigned TF = getTargetFlags()) |
| 298 | OS << "[TF=" << TF << ']'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | //===----------------------------------------------------------------------===// |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 302 | // MachineMemOperand Implementation |
| 303 | //===----------------------------------------------------------------------===// |
| 304 | |
| 305 | MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, |
| 306 | int64_t o, uint64_t s, unsigned int a) |
| 307 | : Offset(o), Size(s), V(v), |
| 308 | Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { |
Dan Gohman | 28f02fd | 2009-09-21 19:47:04 +0000 | [diff] [blame] | 309 | assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); |
Dan Gohman | c5e1f98 | 2008-07-16 15:56:42 +0000 | [diff] [blame] | 310 | assert((isLoad() || isStore()) && "Not a load/store!"); |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 313 | /// Profile - Gather unique data for the object. |
| 314 | /// |
| 315 | void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { |
| 316 | ID.AddInteger(Offset); |
| 317 | ID.AddInteger(Size); |
| 318 | ID.AddPointer(V); |
| 319 | ID.AddInteger(Flags); |
| 320 | } |
| 321 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 322 | void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { |
| 323 | // The Value and Offset may differ due to CSE. But the flags and size |
| 324 | // should be the same. |
| 325 | assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); |
| 326 | assert(MMO->getSize() == getSize() && "Size mismatch!"); |
| 327 | |
| 328 | if (MMO->getBaseAlignment() >= getBaseAlignment()) { |
| 329 | // Update the alignment value. |
| 330 | Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3); |
| 331 | // Also update the base and offset, because the new alignment may |
| 332 | // not be applicable with the old ones. |
| 333 | V = MMO->getValue(); |
| 334 | Offset = MMO->getOffset(); |
| 335 | } |
| 336 | } |
| 337 | |
Dan Gohman | 4b2ebc1 | 2009-09-25 23:33:20 +0000 | [diff] [blame] | 338 | /// getAlignment - Return the minimum known alignment in bytes of the |
| 339 | /// actual memory reference. |
| 340 | uint64_t MachineMemOperand::getAlignment() const { |
| 341 | return MinAlign(getBaseAlignment(), getOffset()); |
| 342 | } |
| 343 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 344 | raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { |
| 345 | assert((MMO.isLoad() || MMO.isStore()) && |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 346 | "SV has to be a load, store or both."); |
| 347 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 348 | if (MMO.isVolatile()) |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 349 | OS << "Volatile "; |
| 350 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 351 | if (MMO.isLoad()) |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 352 | OS << "LD"; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 353 | if (MMO.isStore()) |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 354 | OS << "ST"; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 355 | OS << MMO.getSize(); |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 356 | |
| 357 | // Print the address information. |
| 358 | OS << "["; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 359 | if (!MMO.getValue()) |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 360 | OS << "<unknown>"; |
| 361 | else |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 362 | WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false); |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 363 | |
| 364 | // If the alignment of the memory reference itself differs from the alignment |
| 365 | // of the base pointer, print the base alignment explicitly, next to the base |
| 366 | // pointer. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 367 | if (MMO.getBaseAlignment() != MMO.getAlignment()) |
| 368 | OS << "(align=" << MMO.getBaseAlignment() << ")"; |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 369 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 370 | if (MMO.getOffset() != 0) |
| 371 | OS << "+" << MMO.getOffset(); |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 372 | OS << "]"; |
| 373 | |
| 374 | // Print the alignment of the reference. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 375 | if (MMO.getBaseAlignment() != MMO.getAlignment() || |
| 376 | MMO.getBaseAlignment() != MMO.getSize()) |
| 377 | OS << "(align=" << MMO.getAlignment() << ")"; |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 378 | |
| 379 | return OS; |
| 380 | } |
| 381 | |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 382 | //===----------------------------------------------------------------------===// |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 383 | // MachineInstr Implementation |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 386 | /// MachineInstr ctor - This constructor creates a dummy MachineInstr with |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 387 | /// TID NULL and no operands. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 388 | MachineInstr::MachineInstr() |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 389 | : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 390 | Parent(0), debugLoc(DebugLoc::getUnknownLoc()) { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 391 | // Make sure that we get added to a machine basicblock |
| 392 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 395 | void MachineInstr::addImplicitDefUseOperands() { |
| 396 | if (TID->ImplicitDefs) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 397 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 398 | addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 399 | if (TID->ImplicitUses) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 400 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 401 | addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /// MachineInstr ctor - This constructor create a MachineInstr and add the |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 405 | /// implicit operands. It reserves space for number of operands specified by |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 406 | /// TargetInstrDesc or the numOperands if it is not zero. (for |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 407 | /// instructions with variable number of operands). |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 408 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 409 | : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), |
| 410 | MemRefs(0), MemRefsEnd(0), Parent(0), |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 411 | debugLoc(DebugLoc::getUnknownLoc()) { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 412 | if (!NoImp && TID->getImplicitDefs()) |
| 413 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 414 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 415 | if (!NoImp && TID->getImplicitUses()) |
| 416 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 417 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 418 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 419 | if (!NoImp) |
| 420 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 421 | // Make sure that we get added to a machine basicblock |
| 422 | LeakDetector::addGarbageObject(this); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 425 | /// MachineInstr ctor - As above, but with a DebugLoc. |
| 426 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, |
| 427 | bool NoImp) |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 428 | : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 429 | Parent(0), debugLoc(dl) { |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 430 | if (!NoImp && TID->getImplicitDefs()) |
| 431 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
| 432 | NumImplicitOps++; |
| 433 | if (!NoImp && TID->getImplicitUses()) |
| 434 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
| 435 | NumImplicitOps++; |
| 436 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
| 437 | if (!NoImp) |
| 438 | addImplicitDefUseOperands(); |
| 439 | // Make sure that we get added to a machine basicblock |
| 440 | LeakDetector::addGarbageObject(this); |
| 441 | } |
| 442 | |
| 443 | /// MachineInstr ctor - Work exactly the same as the ctor two above, except |
| 444 | /// that the MachineInstr is created and added to the end of the specified |
| 445 | /// basic block. |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 446 | /// |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 447 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 448 | : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), |
| 449 | MemRefs(0), MemRefsEnd(0), Parent(0), |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 450 | debugLoc(DebugLoc::getUnknownLoc()) { |
| 451 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
| 452 | if (TID->ImplicitDefs) |
| 453 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
| 454 | NumImplicitOps++; |
| 455 | if (TID->ImplicitUses) |
| 456 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
| 457 | NumImplicitOps++; |
| 458 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
| 459 | addImplicitDefUseOperands(); |
| 460 | // Make sure that we get added to a machine basicblock |
| 461 | LeakDetector::addGarbageObject(this); |
| 462 | MBB->push_back(this); // Add instruction to end of basic block! |
| 463 | } |
| 464 | |
| 465 | /// MachineInstr ctor - As above, but with a DebugLoc. |
| 466 | /// |
| 467 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 468 | const TargetInstrDesc &tid) |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 469 | : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 470 | Parent(0), debugLoc(dl) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 471 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 472 | if (TID->ImplicitDefs) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 473 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 474 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 475 | if (TID->ImplicitUses) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 476 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 477 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 478 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 479 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 480 | // Make sure that we get added to a machine basicblock |
| 481 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 482 | MBB->push_back(this); // Add instruction to end of basic block! |
| 483 | } |
| 484 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 485 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 486 | /// |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 487 | MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) |
Dan Gohman | 834651c | 2009-11-16 22:49:38 +0000 | [diff] [blame] | 488 | : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0), |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 489 | MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd), |
| 490 | Parent(0), debugLoc(MI.getDebugLoc()) { |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 491 | Operands.reserve(MI.getNumOperands()); |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 492 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 493 | // Add operands |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 494 | for (unsigned i = 0; i != MI.getNumOperands(); ++i) |
| 495 | addOperand(MI.getOperand(i)); |
| 496 | NumImplicitOps = MI.NumImplicitOps; |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 497 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 498 | // Set parent to null. |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 499 | Parent = 0; |
Dan Gohman | 6116a73 | 2008-07-21 18:47:29 +0000 | [diff] [blame] | 500 | |
| 501 | LeakDetector::addGarbageObject(this); |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 504 | MachineInstr::~MachineInstr() { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 505 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 506 | #ifndef NDEBUG |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 507 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 508 | assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 509 | assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 510 | "Reg operand def/use list corrupted"); |
| 511 | } |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 512 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 515 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 516 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 517 | /// return null. |
| 518 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 519 | if (MachineBasicBlock *MBB = getParent()) |
Dan Gohman | 4e526b9 | 2008-07-08 23:59:09 +0000 | [diff] [blame] | 520 | return &MBB->getParent()->getRegInfo(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 525 | /// this instruction from their respective use lists. This requires that the |
| 526 | /// operands already be on their use lists. |
| 527 | void MachineInstr::RemoveRegOperandsFromUseLists() { |
| 528 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 529 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 530 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 535 | /// this instruction from their respective use lists. This requires that the |
| 536 | /// operands not be on their use lists yet. |
| 537 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { |
| 538 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 539 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 540 | Operands[i].AddRegOperandToRegInfo(&RegInfo); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 546 | /// implicit operand, it is added to the end of the operand list. If it is |
| 547 | /// an explicit operand it is added at the end of the explicit operand list |
| 548 | /// (before the first implicit operand). |
| 549 | void MachineInstr::addOperand(const MachineOperand &Op) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 550 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 551 | assert((isImpReg || !OperandsComplete()) && |
| 552 | "Trying to add an operand to a machine instr that is already done!"); |
| 553 | |
Dan Gohman | bcf28c0 | 2008-12-09 22:45:08 +0000 | [diff] [blame] | 554 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 555 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 556 | // If we are adding the operand to the end of the list, our job is simpler. |
| 557 | // This is true most of the time, so this is a reasonable optimization. |
| 558 | if (isImpReg || NumImplicitOps == 0) { |
| 559 | // We can only do this optimization if we know that the operand list won't |
| 560 | // reallocate. |
| 561 | if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { |
| 562 | Operands.push_back(Op); |
| 563 | |
| 564 | // Set the parent of the operand. |
| 565 | Operands.back().ParentMI = this; |
| 566 | |
| 567 | // If the operand is a register, update the operand's use list. |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 568 | if (Op.isReg()) { |
Dan Gohman | bcf28c0 | 2008-12-09 22:45:08 +0000 | [diff] [blame] | 569 | Operands.back().AddRegOperandToRegInfo(RegInfo); |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 570 | // If the register operand is flagged as early, mark the operand as such |
| 571 | unsigned OpNo = Operands.size() - 1; |
| 572 | if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) |
| 573 | Operands[OpNo].setIsEarlyClobber(true); |
| 574 | } |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // Otherwise, we have to insert a real operand before any implicit ones. |
| 580 | unsigned OpNo = Operands.size()-NumImplicitOps; |
| 581 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 582 | // If this instruction isn't embedded into a function, then we don't need to |
| 583 | // update any operand lists. |
| 584 | if (RegInfo == 0) { |
| 585 | // Simple insertion, no reginfo update needed for other register operands. |
| 586 | Operands.insert(Operands.begin()+OpNo, Op); |
| 587 | Operands[OpNo].ParentMI = this; |
| 588 | |
| 589 | // Do explicitly set the reginfo for this operand though, to ensure the |
| 590 | // next/prev fields are properly nulled out. |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 591 | if (Operands[OpNo].isReg()) { |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 592 | Operands[OpNo].AddRegOperandToRegInfo(0); |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 593 | // If the register operand is flagged as early, mark the operand as such |
| 594 | if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) |
| 595 | Operands[OpNo].setIsEarlyClobber(true); |
| 596 | } |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 597 | |
| 598 | } else if (Operands.size()+1 <= Operands.capacity()) { |
| 599 | // Otherwise, we have to remove register operands from their register use |
| 600 | // list, add the operand, then add the register operands back to their use |
| 601 | // list. This also must handle the case when the operand list reallocates |
| 602 | // to somewhere else. |
| 603 | |
| 604 | // If insertion of this operand won't cause reallocation of the operand |
| 605 | // list, just remove the implicit operands, add the operand, then re-add all |
| 606 | // the rest of the operands. |
| 607 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 608 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 609 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 610 | } |
| 611 | |
| 612 | // Add the operand. If it is a register, add it to the reg list. |
| 613 | Operands.insert(Operands.begin()+OpNo, Op); |
| 614 | Operands[OpNo].ParentMI = this; |
| 615 | |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 616 | if (Operands[OpNo].isReg()) { |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 617 | Operands[OpNo].AddRegOperandToRegInfo(RegInfo); |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 618 | // If the register operand is flagged as early, mark the operand as such |
| 619 | if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) |
| 620 | Operands[OpNo].setIsEarlyClobber(true); |
| 621 | } |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 622 | |
| 623 | // Re-add all the implicit ops. |
| 624 | for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 625 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 626 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 627 | } |
| 628 | } else { |
| 629 | // Otherwise, we will be reallocating the operand list. Remove all reg |
| 630 | // operands from their list, then readd them after the operand list is |
| 631 | // reallocated. |
| 632 | RemoveRegOperandsFromUseLists(); |
| 633 | |
| 634 | Operands.insert(Operands.begin()+OpNo, Op); |
| 635 | Operands[OpNo].ParentMI = this; |
| 636 | |
| 637 | // Re-add all the operands. |
| 638 | AddRegOperandsToUseLists(*RegInfo); |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 639 | |
| 640 | // If the register operand is flagged as early, mark the operand as such |
| 641 | if (Operands[OpNo].isReg() |
| 642 | && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) |
| 643 | Operands[OpNo].setIsEarlyClobber(true); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 648 | /// fewer operand than it started with. |
| 649 | /// |
| 650 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
| 651 | assert(OpNo < Operands.size() && "Invalid operand number"); |
| 652 | |
| 653 | // Special case removing the last one. |
| 654 | if (OpNo == Operands.size()-1) { |
| 655 | // If needed, remove from the reg def/use list. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 656 | if (Operands.back().isReg() && Operands.back().isOnRegUseList()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 657 | Operands.back().RemoveRegOperandFromRegInfo(); |
| 658 | |
| 659 | Operands.pop_back(); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | // Otherwise, we are removing an interior operand. If we have reginfo to |
| 664 | // update, remove all operands that will be shifted down from their reg lists, |
| 665 | // move everything down, then re-add them. |
| 666 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 667 | if (RegInfo) { |
| 668 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 669 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 670 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | Operands.erase(Operands.begin()+OpNo); |
| 675 | |
| 676 | if (RegInfo) { |
| 677 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 678 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 679 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 684 | /// addMemOperand - Add a MachineMemOperand to the machine instruction. |
| 685 | /// This function should be used only occasionally. The setMemRefs function |
| 686 | /// is the primary method for setting up a MachineInstr's MemRefs list. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 687 | void MachineInstr::addMemOperand(MachineFunction &MF, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 688 | MachineMemOperand *MO) { |
| 689 | mmo_iterator OldMemRefs = MemRefs; |
| 690 | mmo_iterator OldMemRefsEnd = MemRefsEnd; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 691 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 692 | size_t NewNum = (MemRefsEnd - MemRefs) + 1; |
| 693 | mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); |
| 694 | mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 695 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 696 | std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs); |
| 697 | NewMemRefs[NewNum - 1] = MO; |
| 698 | |
| 699 | MemRefs = NewMemRefs; |
| 700 | MemRefsEnd = NewMemRefsEnd; |
| 701 | } |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 703 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 704 | /// block, and returns it, but does not delete it. |
| 705 | MachineInstr *MachineInstr::removeFromParent() { |
| 706 | assert(getParent() && "Not embedded in a basic block!"); |
| 707 | getParent()->remove(this); |
| 708 | return this; |
| 709 | } |
| 710 | |
| 711 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 712 | /// eraseFromParent - This method unlinks 'this' from the containing basic |
| 713 | /// block, and deletes it. |
| 714 | void MachineInstr::eraseFromParent() { |
| 715 | assert(getParent() && "Not embedded in a basic block!"); |
| 716 | getParent()->erase(this); |
| 717 | } |
| 718 | |
| 719 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 720 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 721 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 722 | bool MachineInstr::OperandsComplete() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 723 | unsigned short NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 724 | if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 725 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 726 | return false; |
| 727 | } |
| 728 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 729 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 730 | /// |
| 731 | unsigned MachineInstr::getNumExplicitOperands() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 732 | unsigned NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 733 | if (!TID->isVariadic()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 734 | return NumOperands; |
| 735 | |
Dan Gohman | 9407cd4 | 2009-04-15 17:59:11 +0000 | [diff] [blame] | 736 | for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { |
| 737 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 738 | if (!MO.isReg() || !MO.isImplicit()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 739 | NumOperands++; |
| 740 | } |
| 741 | return NumOperands; |
| 742 | } |
| 743 | |
Chris Lattner | 8ace2cd | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 744 | |
Evan Cheng | faa5107 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 745 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Jim Grosbach | f9ca50e | 2009-09-17 17:57:26 +0000 | [diff] [blame] | 746 | /// the specific register or -1 if it is not found. It further tightens |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 747 | /// the search criteria to a use that kills the register if isKill is true. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 748 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, |
| 749 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 750 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 751 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 752 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 753 | continue; |
| 754 | unsigned MOReg = MO.getReg(); |
| 755 | if (!MOReg) |
| 756 | continue; |
| 757 | if (MOReg == Reg || |
| 758 | (TRI && |
| 759 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 760 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 761 | TRI->isSubRegister(MOReg, Reg))) |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 762 | if (!isKill || MO.isKill()) |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 763 | return i; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 764 | } |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 765 | return -1; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 768 | /// findRegisterDefOperandIdx() - Returns the operand index that is a def of |
Dan Gohman | 703bfe6 | 2008-05-06 00:20:10 +0000 | [diff] [blame] | 769 | /// the specified register or -1 if it is not found. If isDead is true, defs |
| 770 | /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it |
| 771 | /// also checks if there is a def of a super-register. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 772 | int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, |
| 773 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 774 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 775 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 776 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 777 | continue; |
| 778 | unsigned MOReg = MO.getReg(); |
| 779 | if (MOReg == Reg || |
| 780 | (TRI && |
| 781 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 782 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 783 | TRI->isSubRegister(MOReg, Reg))) |
| 784 | if (!isDead || MO.isDead()) |
| 785 | return i; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 786 | } |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 787 | return -1; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 788 | } |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 789 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 790 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 791 | /// operand list that is used to represent the predicate. It returns -1 if |
| 792 | /// none is found. |
| 793 | int MachineInstr::findFirstPredOperandIdx() const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 794 | const TargetInstrDesc &TID = getDesc(); |
| 795 | if (TID.isPredicable()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 796 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 797 | if (TID.OpInfo[i].isPredicate()) |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 798 | return i; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 801 | return -1; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 802 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 803 | |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 804 | /// isRegTiedToUseOperand - Given the index of a register def operand, |
| 805 | /// check if the register def is tied to a source operand, due to either |
| 806 | /// two-address elimination or inline assembly constraints. Returns the |
| 807 | /// first tied use operand index by reference is UseOpIdx is not null. |
Jakob Stoklund Olesen | ce9be2c | 2009-04-29 20:57:16 +0000 | [diff] [blame] | 808 | bool MachineInstr:: |
| 809 | isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 810 | if (isInlineAsm()) { |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 811 | assert(DefOpIdx >= 2); |
| 812 | const MachineOperand &MO = getOperand(DefOpIdx); |
Chris Lattner | c30aa7b | 2009-04-09 23:33:34 +0000 | [diff] [blame] | 813 | if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 814 | return false; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 815 | // Determine the actual operand index that corresponds to this index. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 816 | unsigned DefNo = 0; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 817 | unsigned DefPart = 0; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 818 | for (unsigned i = 1, e = getNumOperands(); i < e; ) { |
| 819 | const MachineOperand &FMO = getOperand(i); |
Jakob Stoklund Olesen | 45d34fe | 2009-07-19 19:09:59 +0000 | [diff] [blame] | 820 | // After the normal asm operands there may be additional imp-def regs. |
| 821 | if (!FMO.isImm()) |
| 822 | return false; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 823 | // Skip over this def. |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 824 | unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); |
| 825 | unsigned PrevDef = i + 1; |
| 826 | i = PrevDef + NumOps; |
| 827 | if (i > DefOpIdx) { |
| 828 | DefPart = DefOpIdx - PrevDef; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 829 | break; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 830 | } |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 831 | ++DefNo; |
| 832 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 833 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 834 | const MachineOperand &FMO = getOperand(i); |
| 835 | if (!FMO.isImm()) |
| 836 | continue; |
| 837 | if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) |
| 838 | continue; |
| 839 | unsigned Idx; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 840 | if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 841 | Idx == DefNo) { |
| 842 | if (UseOpIdx) |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 843 | *UseOpIdx = (unsigned)i + 1 + DefPart; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 844 | return true; |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 845 | } |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 846 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 847 | return false; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 850 | assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 851 | const TargetInstrDesc &TID = getDesc(); |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 852 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { |
| 853 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 2ce7f20 | 2008-12-05 05:45:42 +0000 | [diff] [blame] | 854 | if (MO.isReg() && MO.isUse() && |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 855 | TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { |
| 856 | if (UseOpIdx) |
| 857 | *UseOpIdx = (unsigned)i; |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 858 | return true; |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 859 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 860 | } |
| 861 | return false; |
| 862 | } |
| 863 | |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 864 | /// isRegTiedToDefOperand - Return true if the operand of the specified index |
| 865 | /// is a register use and it is tied to an def operand. It also returns the def |
| 866 | /// operand index by reference. |
Jakob Stoklund Olesen | ce9be2c | 2009-04-29 20:57:16 +0000 | [diff] [blame] | 867 | bool MachineInstr:: |
| 868 | isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 869 | if (isInlineAsm()) { |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 870 | const MachineOperand &MO = getOperand(UseOpIdx); |
Chris Lattner | 0c8382c | 2009-04-09 16:50:43 +0000 | [diff] [blame] | 871 | if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 872 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 873 | |
| 874 | // Find the flag operand corresponding to UseOpIdx |
| 875 | unsigned FlagIdx, NumOps=0; |
| 876 | for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { |
| 877 | const MachineOperand &UFMO = getOperand(FlagIdx); |
Jakob Stoklund Olesen | 45d34fe | 2009-07-19 19:09:59 +0000 | [diff] [blame] | 878 | // After the normal asm operands there may be additional imp-def regs. |
| 879 | if (!UFMO.isImm()) |
| 880 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 881 | NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); |
| 882 | assert(NumOps < getNumOperands() && "Invalid inline asm flag"); |
| 883 | if (UseOpIdx < FlagIdx+NumOps+1) |
| 884 | break; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 885 | } |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 886 | if (FlagIdx >= UseOpIdx) |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 887 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 888 | const MachineOperand &UFMO = getOperand(FlagIdx); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 889 | unsigned DefNo; |
| 890 | if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { |
| 891 | if (!DefOpIdx) |
| 892 | return true; |
| 893 | |
| 894 | unsigned DefIdx = 1; |
| 895 | // Remember to adjust the index. First operand is asm string, then there |
| 896 | // is a flag for each. |
| 897 | while (DefNo) { |
| 898 | const MachineOperand &FMO = getOperand(DefIdx); |
| 899 | assert(FMO.isImm()); |
| 900 | // Skip over this def. |
| 901 | DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; |
| 902 | --DefNo; |
| 903 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 904 | *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 910 | const TargetInstrDesc &TID = getDesc(); |
| 911 | if (UseOpIdx >= TID.getNumOperands()) |
| 912 | return false; |
| 913 | const MachineOperand &MO = getOperand(UseOpIdx); |
| 914 | if (!MO.isReg() || !MO.isUse()) |
| 915 | return false; |
| 916 | int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); |
| 917 | if (DefIdx == -1) |
| 918 | return false; |
| 919 | if (DefOpIdx) |
| 920 | *DefOpIdx = (unsigned)DefIdx; |
| 921 | return true; |
| 922 | } |
| 923 | |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 924 | /// copyKillDeadInfo - Copies kill / dead operand properties from MI. |
| 925 | /// |
| 926 | void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { |
| 927 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 928 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 929 | if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 930 | continue; |
| 931 | for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { |
| 932 | MachineOperand &MOp = getOperand(j); |
| 933 | if (!MOp.isIdenticalTo(MO)) |
| 934 | continue; |
| 935 | if (MO.isKill()) |
| 936 | MOp.setIsKill(); |
| 937 | else |
| 938 | MOp.setIsDead(); |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 944 | /// copyPredicates - Copies predicate operand(s) from MI. |
| 945 | void MachineInstr::copyPredicates(const MachineInstr *MI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 946 | const TargetInstrDesc &TID = MI->getDesc(); |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 947 | if (!TID.isPredicable()) |
| 948 | return; |
| 949 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 950 | if (TID.OpInfo[i].isPredicate()) { |
| 951 | // Predicated operands must be last operands. |
| 952 | addOperand(MI->getOperand(i)); |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 957 | /// isSafeToMove - Return true if it is safe to move this instruction. If |
| 958 | /// SawStore is set to true, it means that there is a store (or call) between |
| 959 | /// the instruction's location and its intended destination. |
Dan Gohman | b3b930a | 2008-11-18 19:04:29 +0000 | [diff] [blame] | 960 | bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 961 | bool &SawStore, |
| 962 | AliasAnalysis *AA) const { |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 963 | // Ignore stuff that we obviously can't move. |
| 964 | if (TID->mayStore() || TID->isCall()) { |
| 965 | SawStore = true; |
| 966 | return false; |
| 967 | } |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 968 | if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 969 | return false; |
| 970 | |
| 971 | // See if this instruction does a load. If so, we have to guarantee that the |
| 972 | // loaded value doesn't change between the load and the its intended |
| 973 | // destination. The check for isInvariantLoad gives the targe the chance to |
| 974 | // classify the load as always returning a constant, e.g. a constant pool |
| 975 | // load. |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 976 | if (TID->mayLoad() && !isInvariantLoad(AA)) |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 977 | // Otherwise, this is a real load. If there is a store between the load and |
Evan Cheng | 7cc2c40 | 2009-07-28 21:49:18 +0000 | [diff] [blame] | 978 | // end of block, or if the load is volatile, we can't move it. |
Dan Gohman | d790a5c | 2008-10-02 15:04:30 +0000 | [diff] [blame] | 979 | return !SawStore && !hasVolatileMemoryRef(); |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 980 | |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 981 | return true; |
| 982 | } |
| 983 | |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 984 | /// isSafeToReMat - Return true if it's safe to rematerialize the specified |
| 985 | /// instruction which defined the specified register instead of copying it. |
Dan Gohman | b3b930a | 2008-11-18 19:04:29 +0000 | [diff] [blame] | 986 | bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 987 | unsigned DstReg, |
| 988 | AliasAnalysis *AA) const { |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 989 | bool SawStore = false; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 990 | if (!TII->isTriviallyReMaterializable(this, AA) || |
| 991 | !isSafeToMove(TII, SawStore, AA)) |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 992 | return false; |
| 993 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | cbad42c | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 994 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 995 | if (!MO.isReg()) |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 996 | continue; |
| 997 | // FIXME: For now, do not remat any instruction with register operands. |
| 998 | // Later on, we can loosen the restriction is the register operands have |
| 999 | // not been modified between the def and use. Note, this is different from |
Evan Cheng | 8763c1c | 2008-08-27 20:58:54 +0000 | [diff] [blame] | 1000 | // MachineSink because the code is no longer in two-address form (at least |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 1001 | // partially). |
| 1002 | if (MO.isUse()) |
| 1003 | return false; |
| 1004 | else if (!MO.isDead() && MO.getReg() != DstReg) |
| 1005 | return false; |
| 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1010 | /// hasVolatileMemoryRef - Return true if this instruction may have a |
| 1011 | /// volatile memory reference, or if the information describing the |
| 1012 | /// memory reference is not available. Return false if it is known to |
| 1013 | /// have no volatile memory references. |
| 1014 | bool MachineInstr::hasVolatileMemoryRef() const { |
| 1015 | // An instruction known never to access memory won't have a volatile access. |
| 1016 | if (!TID->mayStore() && |
| 1017 | !TID->mayLoad() && |
| 1018 | !TID->isCall() && |
| 1019 | !TID->hasUnmodeledSideEffects()) |
| 1020 | return false; |
| 1021 | |
| 1022 | // Otherwise, if the instruction has no memory reference information, |
| 1023 | // conservatively assume it wasn't preserved. |
| 1024 | if (memoperands_empty()) |
| 1025 | return true; |
| 1026 | |
| 1027 | // Check the memory reference information for volatile references. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1028 | for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) |
| 1029 | if ((*I)->isVolatile()) |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1030 | return true; |
| 1031 | |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1035 | /// isInvariantLoad - Return true if this instruction is loading from a |
| 1036 | /// location whose value is invariant across the function. For example, |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame^] | 1037 | /// loading a value from the constant pool or from the argument area |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1038 | /// of a function if it does not change. This should only return true of |
| 1039 | /// *all* loads the instruction does are invariant (if it does multiple loads). |
| 1040 | bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { |
| 1041 | // If the instruction doesn't load at all, it isn't an invariant load. |
| 1042 | if (!TID->mayLoad()) |
| 1043 | return false; |
| 1044 | |
| 1045 | // If the instruction has lost its memoperands, conservatively assume that |
| 1046 | // it may not be an invariant load. |
| 1047 | if (memoperands_empty()) |
| 1048 | return false; |
| 1049 | |
| 1050 | const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); |
| 1051 | |
| 1052 | for (mmo_iterator I = memoperands_begin(), |
| 1053 | E = memoperands_end(); I != E; ++I) { |
| 1054 | if ((*I)->isVolatile()) return false; |
| 1055 | if ((*I)->isStore()) return false; |
| 1056 | |
| 1057 | if (const Value *V = (*I)->getValue()) { |
| 1058 | // A load from a constant PseudoSourceValue is invariant. |
| 1059 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) |
| 1060 | if (PSV->isConstant(MFI)) |
| 1061 | continue; |
| 1062 | // If we have an AliasAnalysis, ask it whether the memory is constant. |
| 1063 | if (AA && AA->pointsToConstantMemory(V)) |
| 1064 | continue; |
| 1065 | } |
| 1066 | |
| 1067 | // Otherwise assume conservatively. |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | // Everything checks out. |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1075 | /// isConstantValuePHI - If the specified instruction is a PHI that always |
| 1076 | /// merges together the same virtual register, return the register, otherwise |
| 1077 | /// return 0. |
| 1078 | unsigned MachineInstr::isConstantValuePHI() const { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1079 | if (!isPHI()) |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1080 | return 0; |
Evan Cheng | d8f079c | 2009-12-07 23:10:34 +0000 | [diff] [blame] | 1081 | assert(getNumOperands() >= 3 && |
| 1082 | "It's illegal to have a PHI without source operands"); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1083 | |
| 1084 | unsigned Reg = getOperand(1).getReg(); |
| 1085 | for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) |
| 1086 | if (getOperand(i).getReg() != Reg) |
| 1087 | return 0; |
| 1088 | return Reg; |
| 1089 | } |
| 1090 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 1091 | void MachineInstr::dump() const { |
David Greene | 3b32533 | 2010-01-04 23:48:20 +0000 | [diff] [blame] | 1092 | dbgs() << " " << *this; |
Mon P Wang | 5ca6bd1 | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1096 | // We can be a bit tidier if we know the TargetMachine and/or MachineFunction. |
| 1097 | const MachineFunction *MF = 0; |
| 1098 | if (const MachineBasicBlock *MBB = getParent()) { |
| 1099 | MF = MBB->getParent(); |
| 1100 | if (!TM && MF) |
| 1101 | TM = &MF->getTarget(); |
| 1102 | } |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Print explicitly defined operands on the left of an assignment syntax. |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1105 | unsigned StartOp = 0, e = getNumOperands(); |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1106 | for (; StartOp < e && getOperand(StartOp).isReg() && |
| 1107 | getOperand(StartOp).isDef() && |
| 1108 | !getOperand(StartOp).isImplicit(); |
| 1109 | ++StartOp) { |
| 1110 | if (StartOp != 0) OS << ", "; |
| 1111 | getOperand(StartOp).print(OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1112 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 1113 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1114 | if (StartOp != 0) |
| 1115 | OS << " = "; |
| 1116 | |
| 1117 | // Print the opcode name. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1118 | OS << getDesc().getName(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1119 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1120 | // Print the rest of the operands. |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1121 | bool OmittedAnyCallClobbers = false; |
| 1122 | bool FirstOp = true; |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1123 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1124 | const MachineOperand &MO = getOperand(i); |
| 1125 | |
| 1126 | // Omit call-clobbered registers which aren't used anywhere. This makes |
| 1127 | // call instructions much less noisy on targets where calls clobber lots |
| 1128 | // of registers. Don't rely on MO.isDead() because we may be called before |
| 1129 | // LiveVariables is run, or we may be looking at a non-allocatable reg. |
| 1130 | if (MF && getDesc().isCall() && |
| 1131 | MO.isReg() && MO.isImplicit() && MO.isDef()) { |
| 1132 | unsigned Reg = MO.getReg(); |
| 1133 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1134 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 1135 | if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) { |
| 1136 | bool HasAliasLive = false; |
| 1137 | for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg); |
| 1138 | unsigned AliasReg = *Alias; ++Alias) |
| 1139 | if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) { |
| 1140 | HasAliasLive = true; |
| 1141 | break; |
| 1142 | } |
| 1143 | if (!HasAliasLive) { |
| 1144 | OmittedAnyCallClobbers = true; |
| 1145 | continue; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | if (FirstOp) FirstOp = false; else OS << ","; |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1152 | OS << " "; |
Jakob Stoklund Olesen | b1bb4af | 2010-01-19 22:08:34 +0000 | [diff] [blame] | 1153 | if (i < getDesc().NumOperands) { |
| 1154 | const TargetOperandInfo &TOI = getDesc().OpInfo[i]; |
| 1155 | if (TOI.isPredicate()) |
| 1156 | OS << "pred:"; |
| 1157 | if (TOI.isOptionalDef()) |
| 1158 | OS << "opt:"; |
| 1159 | } |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1160 | MO.print(OS, TM); |
| 1161 | } |
| 1162 | |
| 1163 | // Briefly indicate whether any call clobbers were omitted. |
| 1164 | if (OmittedAnyCallClobbers) { |
Bill Wendling | 164558e | 2009-12-25 13:45:50 +0000 | [diff] [blame] | 1165 | if (!FirstOp) OS << ","; |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1166 | OS << " ..."; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 1167 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1168 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1169 | bool HaveSemi = false; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1170 | if (!memoperands_empty()) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1171 | if (!HaveSemi) OS << ";"; HaveSemi = true; |
| 1172 | |
| 1173 | OS << " mem:"; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1174 | for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); |
| 1175 | i != e; ++i) { |
| 1176 | OS << **i; |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 1177 | if (next(i) != e) |
| 1178 | OS << " "; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Dan Gohman | 80f6c58 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1182 | if (!debugLoc.isUnknown() && MF) { |
Bill Wendling | ad2cf9d | 2009-12-25 13:44:36 +0000 | [diff] [blame] | 1183 | if (!HaveSemi) OS << ";"; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1184 | |
| 1185 | // TODO: print InlinedAtLoc information |
| 1186 | |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1187 | DILocation DLT = MF->getDILocation(debugLoc); |
| 1188 | DIScope Scope = DLT.getScope(); |
Dan Gohman | 75ae593 | 2009-11-23 21:29:08 +0000 | [diff] [blame] | 1189 | OS << " dbg:"; |
Dan Gohman | 4b808b0 | 2009-12-05 00:20:51 +0000 | [diff] [blame] | 1190 | // Omit the directory, since it's usually long and uninteresting. |
Dan Gohman | 261a7d9 | 2009-12-01 00:45:56 +0000 | [diff] [blame] | 1191 | if (!Scope.isNull()) |
Dan Gohman | 4b808b0 | 2009-12-05 00:20:51 +0000 | [diff] [blame] | 1192 | OS << Scope.getFilename(); |
| 1193 | else |
| 1194 | OS << "<unknown>"; |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1195 | OS << ':' << DLT.getLineNumber(); |
| 1196 | if (DLT.getColumnNumber() != 0) |
| 1197 | OS << ':' << DLT.getColumnNumber(); |
Bill Wendling | b5ef273 | 2009-02-19 21:44:55 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 1200 | OS << "\n"; |
| 1201 | } |
| 1202 | |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1203 | bool MachineInstr::addRegisterKilled(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1204 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1205 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1206 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1207 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1208 | bool Found = false; |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1209 | SmallVector<unsigned,4> DeadOps; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1210 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1211 | MachineOperand &MO = getOperand(i); |
Jakob Stoklund Olesen | efb8e3e | 2009-08-04 20:09:25 +0000 | [diff] [blame] | 1212 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1213 | continue; |
| 1214 | unsigned Reg = MO.getReg(); |
| 1215 | if (!Reg) |
| 1216 | continue; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1217 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1218 | if (Reg == IncomingReg) { |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1219 | if (!Found) { |
| 1220 | if (MO.isKill()) |
| 1221 | // The register is already marked kill. |
| 1222 | return true; |
Jakob Stoklund Olesen | ece4818 | 2009-08-02 19:13:03 +0000 | [diff] [blame] | 1223 | if (isPhysReg && isRegTiedToDefOperand(i)) |
| 1224 | // Two-address uses of physregs must not be marked kill. |
| 1225 | return true; |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1226 | MO.setIsKill(); |
| 1227 | Found = true; |
| 1228 | } |
| 1229 | } else if (hasAliases && MO.isKill() && |
| 1230 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1231 | // A super-register kill already exists. |
| 1232 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1233 | return true; |
| 1234 | if (RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1235 | DeadOps.push_back(i); |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1239 | // Trim unneeded kill operands. |
| 1240 | while (!DeadOps.empty()) { |
| 1241 | unsigned OpIdx = DeadOps.back(); |
| 1242 | if (getOperand(OpIdx).isImplicit()) |
| 1243 | RemoveOperand(OpIdx); |
| 1244 | else |
| 1245 | getOperand(OpIdx).setIsKill(false); |
| 1246 | DeadOps.pop_back(); |
| 1247 | } |
| 1248 | |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1249 | // If not found, this means an alias of one of the operands is killed. Add a |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1250 | // new implicit operand if required. |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1251 | if (!Found && AddIfNotFound) { |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1252 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1253 | false /*IsDef*/, |
| 1254 | true /*IsImp*/, |
| 1255 | true /*IsKill*/)); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1256 | return true; |
| 1257 | } |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1258 | return Found; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | bool MachineInstr::addRegisterDead(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1262 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1263 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1264 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Evan Cheng | 01b2e23 | 2008-06-27 22:11:49 +0000 | [diff] [blame] | 1265 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1266 | bool Found = false; |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1267 | SmallVector<unsigned,4> DeadOps; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1268 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1269 | MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1270 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1271 | continue; |
| 1272 | unsigned Reg = MO.getReg(); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1273 | if (!Reg) |
| 1274 | continue; |
| 1275 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1276 | if (Reg == IncomingReg) { |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1277 | if (!Found) { |
| 1278 | if (MO.isDead()) |
| 1279 | // The register is already marked dead. |
| 1280 | return true; |
| 1281 | MO.setIsDead(); |
| 1282 | Found = true; |
| 1283 | } |
| 1284 | } else if (hasAliases && MO.isDead() && |
| 1285 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1286 | // There exists a super-register that's marked dead. |
| 1287 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1288 | return true; |
Owen Anderson | 22ae999 | 2008-08-14 18:34:18 +0000 | [diff] [blame] | 1289 | if (RegInfo->getSubRegisters(IncomingReg) && |
| 1290 | RegInfo->getSuperRegisters(Reg) && |
| 1291 | RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1292 | DeadOps.push_back(i); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1296 | // Trim unneeded dead operands. |
| 1297 | while (!DeadOps.empty()) { |
| 1298 | unsigned OpIdx = DeadOps.back(); |
| 1299 | if (getOperand(OpIdx).isImplicit()) |
| 1300 | RemoveOperand(OpIdx); |
| 1301 | else |
| 1302 | getOperand(OpIdx).setIsDead(false); |
| 1303 | DeadOps.pop_back(); |
| 1304 | } |
| 1305 | |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1306 | // If not found, this means an alias of one of the operands is dead. Add a |
| 1307 | // new implicit operand if required. |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 1308 | if (Found || !AddIfNotFound) |
| 1309 | return Found; |
| 1310 | |
| 1311 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1312 | true /*IsDef*/, |
| 1313 | true /*IsImp*/, |
| 1314 | false /*IsKill*/, |
| 1315 | true /*IsDead*/)); |
| 1316 | return true; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1317 | } |
Jakob Stoklund Olesen | 8efadf9 | 2010-01-06 00:29:28 +0000 | [diff] [blame] | 1318 | |
| 1319 | void MachineInstr::addRegisterDefined(unsigned IncomingReg, |
| 1320 | const TargetRegisterInfo *RegInfo) { |
| 1321 | MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo); |
| 1322 | if (!MO || MO->getSubReg()) |
| 1323 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1324 | true /*IsDef*/, |
| 1325 | true /*IsImp*/)); |
| 1326 | } |