Chris Lattner | 0cb9dd7 | 2008-01-01 20:36:19 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | e8f7c2f | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // Methods common to all machine instructions. |
| 11 | // |
Chris Lattner | 959a5fb | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | ab9e557 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 23fcc08 | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/FoldingSet.h" |
| 16 | #include "llvm/ADT/Hashing.h" |
| 17 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | e9c46c2 | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 63f41ab | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | 25a404e | 2011-07-02 03:53:34 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
| 27 | #include "llvm/IR/InlineAsm.h" |
| 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Metadata.h" |
| 30 | #include "llvm/IR/Module.h" |
| 31 | #include "llvm/IR/Type.h" |
| 32 | #include "llvm/IR/Value.h" |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrDesc.h" |
Chris Lattner | 6c604e3 | 2010-03-13 08:14:18 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSymbol.h" |
David Greene | 29388d6 | 2010-01-04 23:48:20 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | aedb4a6 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | a078d83 | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetMachine.h" |
| 41 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | 43df6c2 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // MachineOperand Implementation |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 49 | void MachineOperand::setReg(unsigned Reg) { |
| 50 | if (getReg() == Reg) return; // No change. |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 52 | // Otherwise, we have to change the register. If this operand is embedded |
| 53 | // into a machine function, we need to update the old and new register's |
| 54 | // use/def lists. |
| 55 | if (MachineInstr *MI = getParent()) |
| 56 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 57 | if (MachineFunction *MF = MBB->getParent()) { |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 58 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 59 | MRI.removeRegOperandFromUseList(this); |
Jakob Stoklund Olesen | a494169 | 2010-10-19 20:56:32 +0000 | [diff] [blame] | 60 | SmallContents.RegNo = Reg; |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 61 | MRI.addRegOperandToUseList(this); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 62 | return; |
| 63 | } |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 65 | // Otherwise, just change the register, no problem. :) |
Jakob Stoklund Olesen | a494169 | 2010-10-19 20:56:32 +0000 | [diff] [blame] | 66 | SmallContents.RegNo = Reg; |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Jakob Stoklund Olesen | 64824ea | 2010-05-28 18:18:53 +0000 | [diff] [blame] | 69 | void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, |
| 70 | const TargetRegisterInfo &TRI) { |
| 71 | assert(TargetRegisterInfo::isVirtualRegister(Reg)); |
| 72 | if (SubIdx && getSubReg()) |
| 73 | SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); |
| 74 | setReg(Reg); |
Jakob Stoklund Olesen | 7b0ac86 | 2010-06-01 22:39:25 +0000 | [diff] [blame] | 75 | if (SubIdx) |
| 76 | setSubReg(SubIdx); |
Jakob Stoklund Olesen | 64824ea | 2010-05-28 18:18:53 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { |
| 80 | assert(TargetRegisterInfo::isPhysicalRegister(Reg)); |
| 81 | if (getSubReg()) { |
| 82 | Reg = TRI.getSubReg(Reg, getSubReg()); |
Jakob Stoklund Olesen | 89bd2ae | 2011-05-08 19:21:08 +0000 | [diff] [blame] | 83 | // Note that getSubReg() may return 0 if the sub-register doesn't exist. |
| 84 | // That won't happen in legal code. |
Jakob Stoklund Olesen | 64824ea | 2010-05-28 18:18:53 +0000 | [diff] [blame] | 85 | setSubReg(0); |
| 86 | } |
| 87 | setReg(Reg); |
| 88 | } |
| 89 | |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 90 | /// Change a def to a use, or a use to a def. |
| 91 | void MachineOperand::setIsDef(bool Val) { |
| 92 | assert(isReg() && "Wrong MachineOperand accessor"); |
| 93 | assert((!Val || !isDebug()) && "Marking a debug operation as def"); |
| 94 | if (IsDef == Val) |
| 95 | return; |
| 96 | // MRI may keep uses and defs in different list positions. |
| 97 | if (MachineInstr *MI = getParent()) |
| 98 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 99 | if (MachineFunction *MF = MBB->getParent()) { |
| 100 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 101 | MRI.removeRegOperandFromUseList(this); |
| 102 | IsDef = Val; |
| 103 | MRI.addRegOperandToUseList(this); |
| 104 | return; |
| 105 | } |
| 106 | IsDef = Val; |
| 107 | } |
| 108 | |
Matt Arsenault | 93ffe58 | 2014-09-28 19:24:59 +0000 | [diff] [blame] | 109 | // If this operand is currently a register operand, and if this is in a |
| 110 | // function, deregister the operand from the register's use/def list. |
| 111 | void MachineOperand::removeRegFromUses() { |
| 112 | if (!isReg() || !isOnRegUseList()) |
| 113 | return; |
| 114 | |
| 115 | if (MachineInstr *MI = getParent()) { |
| 116 | if (MachineBasicBlock *MBB = MI->getParent()) { |
| 117 | if (MachineFunction *MF = MBB->getParent()) |
| 118 | MF->getRegInfo().removeRegOperandFromUseList(this); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 123 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 124 | /// the specified value. If an operand is known to be an immediate already, |
| 125 | /// the setImm method should be used. |
| 126 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 127 | assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); |
Matt Arsenault | 93ffe58 | 2014-09-28 19:24:59 +0000 | [diff] [blame] | 128 | |
| 129 | removeRegFromUses(); |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 131 | OpKind = MO_Immediate; |
| 132 | Contents.ImmVal = ImmVal; |
| 133 | } |
| 134 | |
Matt Arsenault | 93ffe58 | 2014-09-28 19:24:59 +0000 | [diff] [blame] | 135 | void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) { |
| 136 | assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); |
| 137 | |
| 138 | removeRegFromUses(); |
| 139 | |
| 140 | OpKind = MO_FPImmediate; |
| 141 | Contents.CFP = FPImm; |
| 142 | } |
| 143 | |
Matt Arsenault | 633dba4 | 2015-05-06 17:05:54 +0000 | [diff] [blame] | 144 | void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) { |
| 145 | assert((!isReg() || !isTied()) && |
| 146 | "Cannot change a tied operand into an external symbol"); |
| 147 | |
| 148 | removeRegFromUses(); |
| 149 | |
| 150 | OpKind = MO_ExternalSymbol; |
| 151 | Contents.OffsetedInfo.Val.SymbolName = SymName; |
| 152 | setOffset(0); // Offset is always 0. |
| 153 | setTargetFlags(TargetFlags); |
| 154 | } |
| 155 | |
| 156 | void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) { |
| 157 | assert((!isReg() || !isTied()) && |
| 158 | "Cannot change a tied operand into an MCSymbol"); |
| 159 | |
| 160 | removeRegFromUses(); |
| 161 | |
| 162 | OpKind = MO_MCSymbol; |
| 163 | Contents.Sym = Sym; |
| 164 | } |
| 165 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 166 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 167 | /// the specified value. If an operand is known to be an register already, |
| 168 | /// the setReg method should be used. |
| 169 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
Dale Johannesen | d40d42c | 2010-02-10 00:41:49 +0000 | [diff] [blame] | 170 | bool isKill, bool isDead, bool isUndef, |
| 171 | bool isDebug) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 172 | MachineRegisterInfo *RegInfo = nullptr; |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 173 | if (MachineInstr *MI = getParent()) |
| 174 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 175 | if (MachineFunction *MF = MBB->getParent()) |
| 176 | RegInfo = &MF->getRegInfo(); |
| 177 | // If this operand is already a register operand, remove it from the |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 178 | // register's use/def lists. |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 179 | bool WasReg = isReg(); |
| 180 | if (RegInfo && WasReg) |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 181 | RegInfo->removeRegOperandFromUseList(this); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 182 | |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 183 | // Change this to a register and set the reg#. |
| 184 | OpKind = MO_Register; |
| 185 | SmallContents.RegNo = Reg; |
Jakob Stoklund Olesen | a1b246d | 2013-01-07 23:21:44 +0000 | [diff] [blame] | 186 | SubReg_TargetFlags = 0; |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 187 | IsDef = isDef; |
| 188 | IsImp = isImp; |
| 189 | IsKill = isKill; |
| 190 | IsDead = isDead; |
Evan Cheng | 0dc101b | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 191 | IsUndef = isUndef; |
Jakob Stoklund Olesen | b0d91ab | 2011-12-07 00:22:07 +0000 | [diff] [blame] | 192 | IsInternalRead = false; |
Dale Johannesen | c0d712d | 2008-09-14 01:44:36 +0000 | [diff] [blame] | 193 | IsEarlyClobber = false; |
Dale Johannesen | d40d42c | 2010-02-10 00:41:49 +0000 | [diff] [blame] | 194 | IsDebug = isDebug; |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 195 | // Ensure isOnRegUseList() returns false. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 196 | Contents.Reg.Prev = nullptr; |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 197 | // Preserve the tie when the operand was already a register. |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 198 | if (!WasReg) |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 199 | TiedTo = 0; |
Jakob Stoklund Olesen | ae7b971 | 2012-08-10 00:21:26 +0000 | [diff] [blame] | 200 | |
| 201 | // If this operand is embedded in a function, add the operand to the |
| 202 | // register's use/def list. |
| 203 | if (RegInfo) |
| 204 | RegInfo->addRegOperandToUseList(this); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 207 | /// isIdenticalTo - Return true if this operand is identical to the specified |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 208 | /// operand. Note that this should stay in sync with the hash_value overload |
| 209 | /// below. |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 210 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 211 | if (getType() != Other.getType() || |
| 212 | getTargetFlags() != Other.getTargetFlags()) |
| 213 | return false; |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 215 | switch (getType()) { |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 216 | case MachineOperand::MO_Register: |
| 217 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 218 | getSubReg() == Other.getSubReg(); |
| 219 | case MachineOperand::MO_Immediate: |
| 220 | return getImm() == Other.getImm(); |
Cameron Zwarich | 7da0f9a | 2011-07-01 23:45:21 +0000 | [diff] [blame] | 221 | case MachineOperand::MO_CImmediate: |
| 222 | return getCImm() == Other.getCImm(); |
Nate Begeman | 26b76b6 | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 223 | case MachineOperand::MO_FPImmediate: |
| 224 | return getFPImm() == Other.getFPImm(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 225 | case MachineOperand::MO_MachineBasicBlock: |
| 226 | return getMBB() == Other.getMBB(); |
| 227 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 228 | return getIndex() == Other.getIndex(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 229 | case MachineOperand::MO_ConstantPoolIndex: |
Jakob Stoklund Olesen | 84689b0 | 2012-08-07 18:56:39 +0000 | [diff] [blame] | 230 | case MachineOperand::MO_TargetIndex: |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 231 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 232 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 233 | return getIndex() == Other.getIndex(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 234 | case MachineOperand::MO_GlobalAddress: |
| 235 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 236 | case MachineOperand::MO_ExternalSymbol: |
| 237 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 238 | getOffset() == Other.getOffset(); |
Dan Gohman | 6c93880 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 239 | case MachineOperand::MO_BlockAddress: |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 240 | return getBlockAddress() == Other.getBlockAddress() && |
| 241 | getOffset() == Other.getOffset(); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 242 | case MachineOperand::MO_RegisterMask: |
| 243 | case MachineOperand::MO_RegisterLiveOut: |
Jakob Stoklund Olesen | 374ed32 | 2012-01-16 19:22:00 +0000 | [diff] [blame] | 244 | return getRegMask() == Other.getRegMask(); |
Chris Lattner | 6c604e3 | 2010-03-13 08:14:18 +0000 | [diff] [blame] | 245 | case MachineOperand::MO_MCSymbol: |
| 246 | return getMCSymbol() == Other.getMCSymbol(); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 247 | case MachineOperand::MO_CFIIndex: |
| 248 | return getCFIIndex() == Other.getCFIIndex(); |
Chris Lattner | f839ee0 | 2010-04-07 18:03:19 +0000 | [diff] [blame] | 249 | case MachineOperand::MO_Metadata: |
| 250 | return getMetadata() == Other.getMetadata(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 251 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 252 | llvm_unreachable("Invalid machine operand type"); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 255 | // Note: this must stay exactly in sync with isIdenticalTo above. |
| 256 | hash_code llvm::hash_value(const MachineOperand &MO) { |
| 257 | switch (MO.getType()) { |
| 258 | case MachineOperand::MO_Register: |
Jakob Stoklund Olesen | dba99d0 | 2012-08-28 18:05:48 +0000 | [diff] [blame] | 259 | // Register operands don't have target flags. |
| 260 | return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 261 | case MachineOperand::MO_Immediate: |
| 262 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); |
| 263 | case MachineOperand::MO_CImmediate: |
| 264 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); |
| 265 | case MachineOperand::MO_FPImmediate: |
| 266 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); |
| 267 | case MachineOperand::MO_MachineBasicBlock: |
| 268 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); |
| 269 | case MachineOperand::MO_FrameIndex: |
| 270 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); |
| 271 | case MachineOperand::MO_ConstantPoolIndex: |
Jakob Stoklund Olesen | 84689b0 | 2012-08-07 18:56:39 +0000 | [diff] [blame] | 272 | case MachineOperand::MO_TargetIndex: |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 273 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), |
| 274 | MO.getOffset()); |
| 275 | case MachineOperand::MO_JumpTableIndex: |
| 276 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); |
| 277 | case MachineOperand::MO_ExternalSymbol: |
| 278 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), |
| 279 | MO.getSymbolName()); |
| 280 | case MachineOperand::MO_GlobalAddress: |
| 281 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), |
| 282 | MO.getOffset()); |
| 283 | case MachineOperand::MO_BlockAddress: |
| 284 | return hash_combine(MO.getType(), MO.getTargetFlags(), |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 285 | MO.getBlockAddress(), MO.getOffset()); |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 286 | case MachineOperand::MO_RegisterMask: |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 287 | case MachineOperand::MO_RegisterLiveOut: |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 288 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); |
| 289 | case MachineOperand::MO_Metadata: |
| 290 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); |
| 291 | case MachineOperand::MO_MCSymbol: |
| 292 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 293 | case MachineOperand::MO_CFIIndex: |
| 294 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 295 | } |
| 296 | llvm_unreachable("Invalid machine operand type"); |
| 297 | } |
| 298 | |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 299 | /// print - Print the specified machine operand. |
| 300 | /// |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 301 | void MachineOperand::print(raw_ostream &OS, |
| 302 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 303 | switch (getType()) { |
| 304 | case MachineOperand::MO_Register: |
Jakob Stoklund Olesen | 1331a15 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 305 | OS << PrintReg(getReg(), TRI, getSubReg()); |
Dan Gohman | 0ab1144 | 2008-12-18 21:51:27 +0000 | [diff] [blame] | 306 | |
Evan Cheng | 0dc101b | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 307 | if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || |
Jakob Stoklund Olesen | e56c60c | 2012-08-28 18:34:41 +0000 | [diff] [blame] | 308 | isInternalRead() || isEarlyClobber() || isTied()) { |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 309 | OS << '<'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 310 | bool NeedComma = false; |
Evan Cheng | 70b1fa5 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 311 | if (isDef()) { |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 312 | if (NeedComma) OS << ','; |
Dale Johannesen | 1f3ab86 | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 313 | if (isEarlyClobber()) |
| 314 | OS << "earlyclobber,"; |
Evan Cheng | 70b1fa5 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 315 | if (isImplicit()) |
| 316 | OS << "imp-"; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 317 | OS << "def"; |
| 318 | NeedComma = true; |
Jakob Stoklund Olesen | 7111a63 | 2012-04-20 21:45:33 +0000 | [diff] [blame] | 319 | // <def,read-undef> only makes sense when getSubReg() is set. |
| 320 | // Don't clutter the output otherwise. |
| 321 | if (isUndef() && getSubReg()) |
| 322 | OS << ",read-undef"; |
Evan Cheng | f781bd8 | 2009-10-21 07:56:02 +0000 | [diff] [blame] | 323 | } else if (isImplicit()) { |
Evan Cheng | 70b1fa5 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 324 | OS << "imp-use"; |
Evan Cheng | f781bd8 | 2009-10-21 07:56:02 +0000 | [diff] [blame] | 325 | NeedComma = true; |
| 326 | } |
Evan Cheng | 70b1fa5 | 2009-10-14 23:37:31 +0000 | [diff] [blame] | 327 | |
Jakob Stoklund Olesen | e56c60c | 2012-08-28 18:34:41 +0000 | [diff] [blame] | 328 | if (isKill()) { |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 329 | if (NeedComma) OS << ','; |
Jakob Stoklund Olesen | e56c60c | 2012-08-28 18:34:41 +0000 | [diff] [blame] | 330 | OS << "kill"; |
| 331 | NeedComma = true; |
| 332 | } |
| 333 | if (isDead()) { |
| 334 | if (NeedComma) OS << ','; |
| 335 | OS << "dead"; |
| 336 | NeedComma = true; |
| 337 | } |
| 338 | if (isUndef() && isUse()) { |
| 339 | if (NeedComma) OS << ','; |
| 340 | OS << "undef"; |
| 341 | NeedComma = true; |
| 342 | } |
| 343 | if (isInternalRead()) { |
| 344 | if (NeedComma) OS << ','; |
| 345 | OS << "internal"; |
| 346 | NeedComma = true; |
| 347 | } |
| 348 | if (isTied()) { |
| 349 | if (NeedComma) OS << ','; |
| 350 | OS << "tied"; |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 351 | if (TiedTo != 15) |
| 352 | OS << unsigned(TiedTo - 1); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 353 | } |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 354 | OS << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 355 | } |
| 356 | break; |
| 357 | case MachineOperand::MO_Immediate: |
| 358 | OS << getImm(); |
| 359 | break; |
Devang Patel | f071d72 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 360 | case MachineOperand::MO_CImmediate: |
| 361 | getCImm()->getValue().print(OS, false); |
| 362 | break; |
Nate Begeman | 26b76b6 | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 363 | case MachineOperand::MO_FPImmediate: |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 364 | if (getFPImm()->getType()->isFloatTy()) |
Nate Begeman | 26b76b6 | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 365 | OS << getFPImm()->getValueAPF().convertToFloat(); |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 366 | else |
Nate Begeman | 26b76b6 | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 367 | OS << getFPImm()->getValueAPF().convertToDouble(); |
Nate Begeman | 26b76b6 | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 368 | break; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 369 | case MachineOperand::MO_MachineBasicBlock: |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 370 | OS << "<BB#" << getMBB()->getNumber() << ">"; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 371 | break; |
| 372 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 373 | OS << "<fi#" << getIndex() << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 374 | break; |
| 375 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 376 | OS << "<cp#" << getIndex(); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 377 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 378 | OS << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 379 | break; |
Jakob Stoklund Olesen | 84689b0 | 2012-08-07 18:56:39 +0000 | [diff] [blame] | 380 | case MachineOperand::MO_TargetIndex: |
| 381 | OS << "<ti#" << getIndex(); |
| 382 | if (getOffset()) OS << "+" << getOffset(); |
| 383 | OS << '>'; |
| 384 | break; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 385 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 386 | OS << "<jt#" << getIndex() << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 387 | break; |
| 388 | case MachineOperand::MO_GlobalAddress: |
Dan Gohman | 0080ee2 | 2009-11-06 18:03:10 +0000 | [diff] [blame] | 389 | OS << "<ga:"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 390 | getGlobal()->printAsOperand(OS, /*PrintType=*/false); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 391 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 392 | OS << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 393 | break; |
| 394 | case MachineOperand::MO_ExternalSymbol: |
| 395 | OS << "<es:" << getSymbolName(); |
| 396 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 397 | OS << '>'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 398 | break; |
Dan Gohman | 6c93880 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 399 | case MachineOperand::MO_BlockAddress: |
Dale Johannesen | 7b1a7ed | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 400 | OS << '<'; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 401 | getBlockAddress()->printAsOperand(OS, /*PrintType=*/false); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 402 | if (getOffset()) OS << "+" << getOffset(); |
Dan Gohman | 6c93880 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 403 | OS << '>'; |
| 404 | break; |
Jakob Stoklund Olesen | 374ed32 | 2012-01-16 19:22:00 +0000 | [diff] [blame] | 405 | case MachineOperand::MO_RegisterMask: |
Jakob Stoklund Olesen | 5e1ac45 | 2012-02-02 23:52:57 +0000 | [diff] [blame] | 406 | OS << "<regmask>"; |
Jakob Stoklund Olesen | 374ed32 | 2012-01-16 19:22:00 +0000 | [diff] [blame] | 407 | break; |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 408 | case MachineOperand::MO_RegisterLiveOut: |
| 409 | OS << "<regliveout>"; |
| 410 | break; |
Dale Johannesen | 7b1a7ed | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 411 | case MachineOperand::MO_Metadata: |
| 412 | OS << '<'; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 413 | getMetadata()->printAsOperand(OS); |
Dale Johannesen | 7b1a7ed | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 414 | OS << '>'; |
| 415 | break; |
Chris Lattner | 6c604e3 | 2010-03-13 08:14:18 +0000 | [diff] [blame] | 416 | case MachineOperand::MO_MCSymbol: |
| 417 | OS << "<MCSym=" << *getMCSymbol() << '>'; |
| 418 | break; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 419 | case MachineOperand::MO_CFIIndex: |
| 420 | OS << "<call frame instruction>"; |
| 421 | break; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 422 | } |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 423 | |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 424 | if (unsigned TF = getTargetFlags()) |
| 425 | OS << "[TF=" << TF << ']'; |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | //===----------------------------------------------------------------------===// |
Dan Gohman | aedb4a6 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 429 | // MachineMemOperand Implementation |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
Chris Lattner | de93bb0 | 2010-09-21 05:39:30 +0000 | [diff] [blame] | 432 | /// getAddrSpace - Return the LLVM IR address space number that this pointer |
| 433 | /// points into. |
| 434 | unsigned MachinePointerInfo::getAddrSpace() const { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 435 | if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0; |
| 436 | return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace(); |
Chris Lattner | de93bb0 | 2010-09-21 05:39:30 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 82fd06d | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 439 | /// getConstantPool - Return a MachinePointerInfo record that refers to the |
| 440 | /// constant pool. |
| 441 | MachinePointerInfo MachinePointerInfo::getConstantPool() { |
| 442 | return MachinePointerInfo(PseudoSourceValue::getConstantPool()); |
| 443 | } |
| 444 | |
| 445 | /// getFixedStack - Return a MachinePointerInfo record that refers to the |
| 446 | /// the specified FrameIndex. |
| 447 | MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) { |
| 448 | return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset); |
| 449 | } |
| 450 | |
Chris Lattner | 50287ea | 2010-09-21 06:43:24 +0000 | [diff] [blame] | 451 | MachinePointerInfo MachinePointerInfo::getJumpTable() { |
| 452 | return MachinePointerInfo(PseudoSourceValue::getJumpTable()); |
| 453 | } |
| 454 | |
| 455 | MachinePointerInfo MachinePointerInfo::getGOT() { |
| 456 | return MachinePointerInfo(PseudoSourceValue::getGOT()); |
| 457 | } |
Chris Lattner | de93bb0 | 2010-09-21 05:39:30 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 886250c | 2010-09-21 18:51:21 +0000 | [diff] [blame] | 459 | MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) { |
| 460 | return MachinePointerInfo(PseudoSourceValue::getStack(), Offset); |
| 461 | } |
| 462 | |
Chris Lattner | 00ca0b8 | 2010-09-21 04:32:08 +0000 | [diff] [blame] | 463 | MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f, |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 464 | uint64_t s, unsigned int a, |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 465 | const AAMDNodes &AAInfo, |
Rafael Espindola | 80c540e | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 466 | const MDNode *Ranges) |
Chris Lattner | 00ca0b8 | 2010-09-21 04:32:08 +0000 | [diff] [blame] | 467 | : PtrInfo(ptrinfo), Size(s), |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 468 | Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 469 | AAInfo(AAInfo), Ranges(Ranges) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 470 | assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() || |
| 471 | isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) && |
Chris Lattner | 00ca0b8 | 2010-09-21 04:32:08 +0000 | [diff] [blame] | 472 | "invalid pointer value"); |
Dan Gohman | e7c8242 | 2009-09-21 19:47:04 +0000 | [diff] [blame] | 473 | assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); |
Dan Gohman | bf98f68 | 2008-07-16 15:56:42 +0000 | [diff] [blame] | 474 | assert((isLoad() || isStore()) && "Not a load/store!"); |
Dan Gohman | aedb4a6 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Dan Gohman | 2da2bed | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 477 | /// Profile - Gather unique data for the object. |
| 478 | /// |
| 479 | void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { |
Chris Lattner | 187f653 | 2010-09-21 04:23:39 +0000 | [diff] [blame] | 480 | ID.AddInteger(getOffset()); |
Dan Gohman | 2da2bed | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 481 | ID.AddInteger(Size); |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 482 | ID.AddPointer(getOpaqueValue()); |
Dan Gohman | 2da2bed | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 483 | ID.AddInteger(Flags); |
| 484 | } |
| 485 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 486 | void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { |
| 487 | // The Value and Offset may differ due to CSE. But the flags and size |
| 488 | // should be the same. |
| 489 | assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); |
| 490 | assert(MMO->getSize() == getSize() && "Size mismatch!"); |
| 491 | |
| 492 | if (MMO->getBaseAlignment() >= getBaseAlignment()) { |
| 493 | // Update the alignment value. |
David Greene | 3a0412f | 2010-02-15 16:48:31 +0000 | [diff] [blame] | 494 | Flags = (Flags & ((1 << MOMaxBits) - 1)) | |
| 495 | ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits); |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 496 | // Also update the base and offset, because the new alignment may |
| 497 | // not be applicable with the old ones. |
Chris Lattner | 187f653 | 2010-09-21 04:23:39 +0000 | [diff] [blame] | 498 | PtrInfo = MMO->PtrInfo; |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Dan Gohman | 5a6b11c | 2009-09-25 23:33:20 +0000 | [diff] [blame] | 502 | /// getAlignment - Return the minimum known alignment in bytes of the |
| 503 | /// actual memory reference. |
| 504 | uint64_t MachineMemOperand::getAlignment() const { |
| 505 | return MinAlign(getBaseAlignment(), getOffset()); |
| 506 | } |
| 507 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 508 | raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { |
| 509 | assert((MMO.isLoad() || MMO.isStore()) && |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 510 | "SV has to be a load, store or both."); |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 511 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 512 | if (MMO.isVolatile()) |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 513 | OS << "Volatile "; |
| 514 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 515 | if (MMO.isLoad()) |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 516 | OS << "LD"; |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 517 | if (MMO.isStore()) |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 518 | OS << "ST"; |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 519 | OS << MMO.getSize(); |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 520 | |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 521 | // Print the address information. |
| 522 | OS << "["; |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 523 | if (const Value *V = MMO.getValue()) |
| 524 | V->printAsOperand(OS, /*PrintType=*/false); |
| 525 | else if (const PseudoSourceValue *PSV = MMO.getPseudoValue()) |
| 526 | PSV->printCustom(OS); |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 527 | else |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 528 | OS << "<unknown>"; |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 529 | |
Matt Arsenault | 68c38fd | 2013-12-14 00:24:02 +0000 | [diff] [blame] | 530 | unsigned AS = MMO.getAddrSpace(); |
| 531 | if (AS != 0) |
| 532 | OS << "(addrspace=" << AS << ')'; |
| 533 | |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 534 | // If the alignment of the memory reference itself differs from the alignment |
| 535 | // of the base pointer, print the base alignment explicitly, next to the base |
| 536 | // pointer. |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 537 | if (MMO.getBaseAlignment() != MMO.getAlignment()) |
| 538 | OS << "(align=" << MMO.getBaseAlignment() << ")"; |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 539 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 540 | if (MMO.getOffset() != 0) |
| 541 | OS << "+" << MMO.getOffset(); |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 542 | OS << "]"; |
| 543 | |
| 544 | // Print the alignment of the reference. |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 545 | if (MMO.getBaseAlignment() != MMO.getAlignment() || |
| 546 | MMO.getBaseAlignment() != MMO.getSize()) |
| 547 | OS << "(align=" << MMO.getAlignment() << ")"; |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 548 | |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 549 | // Print TBAA info. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 550 | if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) { |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 551 | OS << "(tbaa="; |
| 552 | if (TBAAInfo->getNumOperands() > 0) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 553 | TBAAInfo->getOperand(0)->printAsOperand(OS); |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 554 | else |
| 555 | OS << "<unknown>"; |
| 556 | OS << ")"; |
| 557 | } |
| 558 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 559 | // Print AA scope info. |
| 560 | if (const MDNode *ScopeInfo = MMO.getAAInfo().Scope) { |
| 561 | OS << "(alias.scope="; |
| 562 | if (ScopeInfo->getNumOperands() > 0) |
| 563 | for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 564 | ScopeInfo->getOperand(i)->printAsOperand(OS); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 565 | if (i != ie-1) |
| 566 | OS << ","; |
| 567 | } |
| 568 | else |
| 569 | OS << "<unknown>"; |
| 570 | OS << ")"; |
| 571 | } |
| 572 | |
| 573 | // Print AA noalias scope info. |
| 574 | if (const MDNode *NoAliasInfo = MMO.getAAInfo().NoAlias) { |
| 575 | OS << "(noalias="; |
| 576 | if (NoAliasInfo->getNumOperands() > 0) |
| 577 | for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 578 | NoAliasInfo->getOperand(i)->printAsOperand(OS); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 579 | if (i != ie-1) |
| 580 | OS << ","; |
| 581 | } |
| 582 | else |
| 583 | OS << "<unknown>"; |
| 584 | OS << ")"; |
| 585 | } |
| 586 | |
Bill Wendling | 9f638ab | 2011-04-29 23:45:22 +0000 | [diff] [blame] | 587 | // Print nontemporal info. |
| 588 | if (MMO.isNonTemporal()) |
| 589 | OS << "(nontemporal)"; |
| 590 | |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 591 | return OS; |
| 592 | } |
| 593 | |
Dan Gohman | aedb4a6 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 594 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 595 | // MachineInstr Implementation |
| 596 | //===----------------------------------------------------------------------===// |
| 597 | |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 598 | void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 599 | if (MCID->ImplicitDefs) |
Craig Topper | 5a4bcc7 | 2012-03-08 08:22:45 +0000 | [diff] [blame] | 600 | for (const uint16_t *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 601 | addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 602 | if (MCID->ImplicitUses) |
Craig Topper | 5a4bcc7 | 2012-03-08 08:22:45 +0000 | [diff] [blame] | 603 | for (const uint16_t *ImpUses = MCID->getImplicitUses(); *ImpUses; ++ImpUses) |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 604 | addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | 77af6ac | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Bob Wilson | 406f270 | 2010-04-09 04:34:03 +0000 | [diff] [blame] | 607 | /// MachineInstr ctor - This constructor creates a MachineInstr and adds the |
| 608 | /// implicit operands. It reserves space for the number of operands specified by |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 609 | /// the MCInstrDesc. |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 610 | MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid, |
Benjamin Kramer | a9591b5 | 2015-02-07 12:28:15 +0000 | [diff] [blame] | 611 | DebugLoc dl, bool NoImp) |
| 612 | : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0), |
| 613 | AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr), |
| 614 | debugLoc(std::move(dl)) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 615 | assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); |
| 616 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 617 | // Reserve space for the expected number of operands. |
| 618 | if (unsigned NumOps = MCID->getNumOperands() + |
| 619 | MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) { |
| 620 | CapOperands = OperandCapacity::get(NumOps); |
| 621 | Operands = MF.allocateOperandArray(CapOperands); |
| 622 | } |
| 623 | |
Dale Johannesen | 4e04ef3 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 624 | if (!NoImp) |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 625 | addImplicitDefUseOperands(MF); |
Dale Johannesen | 4e04ef3 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Misha Brukman | b47ab7a | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 628 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 629 | /// |
Evan Cheng | a7a20c4 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 630 | MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 631 | : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 632 | Flags(0), AsmPrinterFlags(0), |
Benjamin Kramer | d03878b | 2012-03-16 16:39:27 +0000 | [diff] [blame] | 633 | NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 634 | debugLoc(MI.getDebugLoc()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 635 | assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); |
| 636 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 637 | CapOperands = OperandCapacity::get(MI.getNumOperands()); |
| 638 | Operands = MF.allocateOperandArray(CapOperands); |
Tanya Lattner | 9953d86 | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 639 | |
Jakob Stoklund Olesen | dc5285f | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 640 | // Copy operands. |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 641 | for (const MachineOperand &MO : MI.operands()) |
| 642 | addOperand(MF, MO); |
Tanya Lattner | bcee21b | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 643 | |
Jakob Stoklund Olesen | a33f504 | 2012-12-18 21:36:05 +0000 | [diff] [blame] | 644 | // Copy all the sensible flags. |
| 645 | setFlags(MI.Flags); |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 648 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 649 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 650 | /// return null. |
| 651 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 652 | if (MachineBasicBlock *MBB = getParent()) |
Dan Gohman | f188fa4 | 2008-07-08 23:59:09 +0000 | [diff] [blame] | 653 | return &MBB->getParent()->getRegInfo(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 654 | return nullptr; |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 658 | /// this instruction from their respective use lists. This requires that the |
| 659 | /// operands already be on their use lists. |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 660 | void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 661 | for (MachineOperand &MO : operands()) |
| 662 | if (MO.isReg()) |
| 663 | MRI.removeRegOperandFromUseList(&MO); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 667 | /// this instruction from their respective use lists. This requires that the |
| 668 | /// operands not be on their use lists yet. |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 669 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 670 | for (MachineOperand &MO : operands()) |
| 671 | if (MO.isReg()) |
| 672 | MRI.addRegOperandToUseList(&MO); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Jakob Stoklund Olesen | 2455b585 | 2012-12-20 22:54:05 +0000 | [diff] [blame] | 675 | void MachineInstr::addOperand(const MachineOperand &Op) { |
| 676 | MachineBasicBlock *MBB = getParent(); |
| 677 | assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs"); |
| 678 | MachineFunction *MF = MBB->getParent(); |
| 679 | assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs"); |
| 680 | addOperand(*MF, Op); |
| 681 | } |
| 682 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 683 | /// Move NumOps MachineOperands from Src to Dst, with support for overlapping |
| 684 | /// ranges. If MRI is non-null also update use-def chains. |
| 685 | static void moveOperands(MachineOperand *Dst, MachineOperand *Src, |
| 686 | unsigned NumOps, MachineRegisterInfo *MRI) { |
| 687 | if (MRI) |
| 688 | return MRI->moveOperands(Dst, Src, NumOps); |
| 689 | |
Benjamin Kramer | 5c0e64f | 2015-02-21 16:22:48 +0000 | [diff] [blame] | 690 | // MachineOperand is a trivially copyable type so we can just use memmove. |
| 691 | std::memmove(Dst, Src, NumOps * sizeof(MachineOperand)); |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 694 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 695 | /// implicit operand, it is added to the end of the operand list. If it is |
| 696 | /// an explicit operand it is added at the end of the explicit operand list |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 697 | /// (before the first implicit operand). |
Jakob Stoklund Olesen | 2455b585 | 2012-12-20 22:54:05 +0000 | [diff] [blame] | 698 | void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) { |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 699 | assert(MCID && "Cannot add operands before providing an instr descriptor"); |
Dan Gohman | 9356d8f | 2008-12-09 22:45:08 +0000 | [diff] [blame] | 700 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 701 | // Check if we're adding one of our existing operands. |
| 702 | if (&Op >= Operands && &Op < Operands + NumOperands) { |
| 703 | // This is unusual: MI->addOperand(MI->getOperand(i)). |
| 704 | // If adding Op requires reallocating or moving existing operands around, |
| 705 | // the Op reference could go stale. Support it by copying Op. |
| 706 | MachineOperand CopyOp(Op); |
| 707 | return addOperand(MF, CopyOp); |
| 708 | } |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 709 | |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 710 | // Find the insert location for the new operand. Implicit registers go at |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 711 | // the end, everything else goes before the implicit regs. |
| 712 | // |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 713 | // FIXME: Allow mixed explicit and implicit operands on inline asm. |
| 714 | // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as |
| 715 | // implicit-defs, but they must not be moved around. See the FIXME in |
| 716 | // InstrEmitter.cpp. |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 717 | unsigned OpNo = getNumOperands(); |
| 718 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 719 | if (!isImpReg && !isInlineAsm()) { |
| 720 | while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) { |
| 721 | --OpNo; |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 722 | assert(!Operands[OpNo].isTied() && "Cannot move tied operands"); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 725 | |
Pekka Jaaskelainen | eb4a6e7 | 2013-10-15 14:40:46 +0000 | [diff] [blame] | 726 | #ifndef NDEBUG |
Pekka Jaaskelainen | eb08e2e | 2013-10-15 14:18:10 +0000 | [diff] [blame] | 727 | bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata; |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 728 | // OpNo now points as the desired insertion point. Unless this is a variadic |
| 729 | // instruction, only implicit regs are allowed beyond MCID->getNumOperands(). |
Jakob Stoklund Olesen | c300ef0 | 2012-07-04 23:53:23 +0000 | [diff] [blame] | 730 | // RegMask operands go between the explicit and implicit operands. |
| 731 | assert((isImpReg || Op.isRegMask() || MCID->isVariadic() || |
Pekka Jaaskelainen | eb08e2e | 2013-10-15 14:18:10 +0000 | [diff] [blame] | 732 | OpNo < MCID->getNumOperands() || isMetaDataOp) && |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 733 | "Trying to add an operand to a machine instr that is already done!"); |
Pekka Jaaskelainen | eb4a6e7 | 2013-10-15 14:40:46 +0000 | [diff] [blame] | 734 | #endif |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 735 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 736 | MachineRegisterInfo *MRI = getRegInfo(); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 737 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 738 | // Determine if the Operands array needs to be reallocated. |
| 739 | // Save the old capacity and operand array. |
| 740 | OperandCapacity OldCap = CapOperands; |
| 741 | MachineOperand *OldOperands = Operands; |
| 742 | if (!OldOperands || OldCap.getSize() == getNumOperands()) { |
| 743 | CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1); |
| 744 | Operands = MF.allocateOperandArray(CapOperands); |
| 745 | // Move the operands before the insertion point. |
| 746 | if (OpNo) |
| 747 | moveOperands(Operands, OldOperands, OpNo, MRI); |
| 748 | } |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 749 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 750 | // Move the operands following the insertion point. |
| 751 | if (OpNo != NumOperands) |
| 752 | moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo, |
| 753 | MRI); |
| 754 | ++NumOperands; |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 755 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 756 | // Deallocate the old operand array. |
| 757 | if (OldOperands != Operands && OldOperands) |
| 758 | MF.deallocateOperandArray(OldCap, OldOperands); |
| 759 | |
| 760 | // Copy Op into place. It still needs to be inserted into the MRI use lists. |
| 761 | MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op); |
| 762 | NewMO->ParentMI = this; |
| 763 | |
| 764 | // When adding a register operand, tell MRI about it. |
| 765 | if (NewMO->isReg()) { |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 766 | // Ensure isOnRegUseList() returns false, regardless of Op's status. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 767 | NewMO->Contents.Reg.Prev = nullptr; |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 768 | // Ignore existing ties. This is not a property that can be copied. |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 769 | NewMO->TiedTo = 0; |
| 770 | // Add the new operand to MRI, but only for instructions in an MBB. |
| 771 | if (MRI) |
| 772 | MRI->addRegOperandToUseList(NewMO); |
Jakob Stoklund Olesen | 0eecbbe | 2012-08-30 14:39:06 +0000 | [diff] [blame] | 773 | // The MCID operand information isn't accurate until we start adding |
| 774 | // explicit operands. The implicit operands are added first, then the |
| 775 | // explicits are inserted before them. |
| 776 | if (!isImpReg) { |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 777 | // Tie uses to defs as indicated in MCInstrDesc. |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 778 | if (NewMO->isUse()) { |
Jakob Stoklund Olesen | 0eecbbe | 2012-08-30 14:39:06 +0000 | [diff] [blame] | 779 | int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO); |
Jakob Stoklund Olesen | 5c8eda0 | 2012-08-31 20:50:53 +0000 | [diff] [blame] | 780 | if (DefIdx != -1) |
| 781 | tieOperands(DefIdx, OpNo); |
Jakob Stoklund Olesen | e56c60c | 2012-08-28 18:34:41 +0000 | [diff] [blame] | 782 | } |
Jakob Stoklund Olesen | 0eecbbe | 2012-08-30 14:39:06 +0000 | [diff] [blame] | 783 | // If the register operand is flagged as early, mark the operand as such. |
| 784 | if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1) |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 785 | NewMO->setIsEarlyClobber(true); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 786 | } |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
| 790 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 791 | /// fewer operand than it started with. |
| 792 | /// |
| 793 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
Jakob Stoklund Olesen | b089483 | 2012-12-22 17:13:06 +0000 | [diff] [blame] | 794 | assert(OpNo < getNumOperands() && "Invalid operand number"); |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 795 | untieRegOperand(OpNo); |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 796 | |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 797 | #ifndef NDEBUG |
| 798 | // Moving tied operands would break the ties. |
Jakob Stoklund Olesen | b089483 | 2012-12-22 17:13:06 +0000 | [diff] [blame] | 799 | for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i) |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 800 | if (Operands[i].isReg()) |
| 801 | assert(!Operands[i].isTied() && "Cannot move tied operands"); |
| 802 | #endif |
| 803 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 804 | MachineRegisterInfo *MRI = getRegInfo(); |
| 805 | if (MRI && Operands[OpNo].isReg()) |
| 806 | MRI->removeRegOperandFromUseList(Operands + OpNo); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 807 | |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 808 | // Don't call the MachineOperand destructor. A lot of this code depends on |
| 809 | // MachineOperand having a trivial destructor anyway, and adding a call here |
| 810 | // wouldn't make it 'destructor-correct'. |
| 811 | |
| 812 | if (unsigned N = NumOperands - 1 - OpNo) |
| 813 | moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI); |
| 814 | --NumOperands; |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 817 | /// addMemOperand - Add a MachineMemOperand to the machine instruction. |
| 818 | /// This function should be used only occasionally. The setMemRefs function |
| 819 | /// is the primary method for setting up a MachineInstr's MemRefs list. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 820 | void MachineInstr::addMemOperand(MachineFunction &MF, |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 821 | MachineMemOperand *MO) { |
| 822 | mmo_iterator OldMemRefs = MemRefs; |
Jakob Stoklund Olesen | 5adc4a1 | 2013-01-07 23:21:41 +0000 | [diff] [blame] | 823 | unsigned OldNumMemRefs = NumMemRefs; |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 824 | |
Jakob Stoklund Olesen | 5adc4a1 | 2013-01-07 23:21:41 +0000 | [diff] [blame] | 825 | unsigned NewNum = NumMemRefs + 1; |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 826 | mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 827 | |
Benjamin Kramer | d03878b | 2012-03-16 16:39:27 +0000 | [diff] [blame] | 828 | std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs); |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 829 | NewMemRefs[NewNum - 1] = MO; |
Jakob Stoklund Olesen | 5adc4a1 | 2013-01-07 23:21:41 +0000 | [diff] [blame] | 830 | setMemRefs(NewMemRefs, NewMemRefs + NewNum); |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 831 | } |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 832 | |
Benjamin Kramer | 97f889f | 2012-03-17 17:03:45 +0000 | [diff] [blame] | 833 | bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const { |
Jakob Stoklund Olesen | f0615c7 | 2013-01-10 18:42:44 +0000 | [diff] [blame] | 834 | assert(!isBundledWithPred() && "Must be called on bundle header"); |
Jakob Stoklund Olesen | 55a7be2 | 2013-01-10 01:29:42 +0000 | [diff] [blame] | 835 | for (MachineBasicBlock::const_instr_iterator MII = this;; ++MII) { |
Benjamin Kramer | 97f889f | 2012-03-17 17:03:45 +0000 | [diff] [blame] | 836 | if (MII->getDesc().getFlags() & Mask) { |
Evan Cheng | cdf89fd | 2011-12-08 19:23:10 +0000 | [diff] [blame] | 837 | if (Type == AnyInBundle) |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 838 | return true; |
| 839 | } else { |
Jakob Stoklund Olesen | 55a7be2 | 2013-01-10 01:29:42 +0000 | [diff] [blame] | 840 | if (Type == AllInBundle && !MII->isBundle()) |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 841 | return false; |
| 842 | } |
Jakob Stoklund Olesen | 55a7be2 | 2013-01-10 01:29:42 +0000 | [diff] [blame] | 843 | // This was the last instruction in the bundle. |
| 844 | if (!MII->isBundledWithSucc()) |
| 845 | return Type == AllInBundle; |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 846 | } |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Evan Cheng | e9c46c2 | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 849 | bool MachineInstr::isIdenticalTo(const MachineInstr *Other, |
| 850 | MICheckType Check) const { |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 851 | // If opcodes or number of operands are not the same then the two |
| 852 | // instructions are obviously not identical. |
| 853 | if (Other->getOpcode() != getOpcode() || |
| 854 | Other->getNumOperands() != getNumOperands()) |
| 855 | return false; |
| 856 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 857 | if (isBundle()) { |
| 858 | // Both instructions are bundles, compare MIs inside the bundle. |
| 859 | MachineBasicBlock::const_instr_iterator I1 = *this; |
| 860 | MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end(); |
| 861 | MachineBasicBlock::const_instr_iterator I2 = *Other; |
| 862 | MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end(); |
| 863 | while (++I1 != E1 && I1->isInsideBundle()) { |
| 864 | ++I2; |
| 865 | if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check)) |
| 866 | return false; |
| 867 | } |
| 868 | } |
| 869 | |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 870 | // Check operands to make sure they match. |
| 871 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 872 | const MachineOperand &MO = getOperand(i); |
| 873 | const MachineOperand &OMO = Other->getOperand(i); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 874 | if (!MO.isReg()) { |
| 875 | if (!MO.isIdenticalTo(OMO)) |
| 876 | return false; |
| 877 | continue; |
| 878 | } |
| 879 | |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 880 | // Clients may or may not want to ignore defs when testing for equality. |
| 881 | // For example, machine CSE pass only cares about finding common |
| 882 | // subexpressions, so it's safe to ignore virtual register defs. |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 883 | if (MO.isDef()) { |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 884 | if (Check == IgnoreDefs) |
| 885 | continue; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 886 | else if (Check == IgnoreVRegDefs) { |
| 887 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 888 | TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) |
| 889 | if (MO.getReg() != OMO.getReg()) |
| 890 | return false; |
| 891 | } else { |
| 892 | if (!MO.isIdenticalTo(OMO)) |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 893 | return false; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 894 | if (Check == CheckKillDead && MO.isDead() != OMO.isDead()) |
| 895 | return false; |
| 896 | } |
| 897 | } else { |
| 898 | if (!MO.isIdenticalTo(OMO)) |
| 899 | return false; |
| 900 | if (Check == CheckKillDead && MO.isKill() != OMO.isKill()) |
| 901 | return false; |
| 902 | } |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 903 | } |
Devang Patel | bf8cc60 | 2011-07-07 17:45:33 +0000 | [diff] [blame] | 904 | // If DebugLoc does not match then two dbg.values are not identical. |
| 905 | if (isDebugValue()) |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 906 | if (getDebugLoc() && Other->getDebugLoc() && |
| 907 | getDebugLoc() != Other->getDebugLoc()) |
Devang Patel | bf8cc60 | 2011-07-07 17:45:33 +0000 | [diff] [blame] | 908 | return false; |
Evan Cheng | 0f260e1 | 2010-03-03 21:54:14 +0000 | [diff] [blame] | 909 | return true; |
Evan Cheng | e9c46c2 | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Chris Lattner | bec79b4 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 912 | MachineInstr *MachineInstr::removeFromParent() { |
| 913 | assert(getParent() && "Not embedded in a basic block!"); |
Jakob Stoklund Olesen | ccfb5fb | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 914 | return getParent()->remove(this); |
Chris Lattner | bec79b4 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Jakob Stoklund Olesen | ccfb5fb | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 917 | MachineInstr *MachineInstr::removeFromBundle() { |
| 918 | assert(getParent() && "Not embedded in a basic block!"); |
| 919 | return getParent()->remove_instr(this); |
| 920 | } |
Chris Lattner | bec79b4 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 921 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 922 | void MachineInstr::eraseFromParent() { |
| 923 | assert(getParent() && "Not embedded in a basic block!"); |
Jakob Stoklund Olesen | ccfb5fb | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 924 | getParent()->erase(this); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Gerolf Hoflehner | caa8bfd | 2014-08-13 21:15:23 +0000 | [diff] [blame] | 927 | void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() { |
| 928 | assert(getParent() && "Not embedded in a basic block!"); |
| 929 | MachineBasicBlock *MBB = getParent(); |
| 930 | MachineFunction *MF = MBB->getParent(); |
| 931 | assert(MF && "Not embedded in a function!"); |
| 932 | |
| 933 | MachineInstr *MI = (MachineInstr *)this; |
| 934 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 935 | |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 936 | for (const MachineOperand &MO : MI->operands()) { |
Gerolf Hoflehner | caa8bfd | 2014-08-13 21:15:23 +0000 | [diff] [blame] | 937 | if (!MO.isReg() || !MO.isDef()) |
| 938 | continue; |
| 939 | unsigned Reg = MO.getReg(); |
| 940 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 941 | continue; |
| 942 | MRI.markUsesInDebugValueAsUndef(Reg); |
| 943 | } |
| 944 | MI->eraseFromParent(); |
| 945 | } |
| 946 | |
Jakob Stoklund Olesen | ccfb5fb | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 947 | void MachineInstr::eraseFromBundle() { |
| 948 | assert(getParent() && "Not embedded in a basic block!"); |
| 949 | getParent()->erase_instr(this); |
| 950 | } |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 951 | |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 952 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 953 | /// |
| 954 | unsigned MachineInstr::getNumExplicitOperands() const { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 955 | unsigned NumOperands = MCID->getNumOperands(); |
| 956 | if (!MCID->isVariadic()) |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 957 | return NumOperands; |
| 958 | |
Dan Gohman | 3760853 | 2009-04-15 17:59:11 +0000 | [diff] [blame] | 959 | for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { |
| 960 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 961 | if (!MO.isReg() || !MO.isImplicit()) |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 962 | NumOperands++; |
| 963 | } |
| 964 | return NumOperands; |
| 965 | } |
| 966 | |
Jakob Stoklund Olesen | fead62d | 2012-12-07 04:23:29 +0000 | [diff] [blame] | 967 | void MachineInstr::bundleWithPred() { |
| 968 | assert(!isBundledWithPred() && "MI is already bundled with its predecessor"); |
| 969 | setFlag(BundledPred); |
| 970 | MachineBasicBlock::instr_iterator Pred = this; |
| 971 | --Pred; |
Jakob Stoklund Olesen | 00f6c77 | 2012-12-18 23:00:28 +0000 | [diff] [blame] | 972 | assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags"); |
Jakob Stoklund Olesen | fead62d | 2012-12-07 04:23:29 +0000 | [diff] [blame] | 973 | Pred->setFlag(BundledSucc); |
| 974 | } |
| 975 | |
| 976 | void MachineInstr::bundleWithSucc() { |
| 977 | assert(!isBundledWithSucc() && "MI is already bundled with its successor"); |
| 978 | setFlag(BundledSucc); |
| 979 | MachineBasicBlock::instr_iterator Succ = this; |
| 980 | ++Succ; |
Jakob Stoklund Olesen | 00f6c77 | 2012-12-18 23:00:28 +0000 | [diff] [blame] | 981 | assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags"); |
Jakob Stoklund Olesen | fead62d | 2012-12-07 04:23:29 +0000 | [diff] [blame] | 982 | Succ->setFlag(BundledPred); |
| 983 | } |
| 984 | |
| 985 | void MachineInstr::unbundleFromPred() { |
| 986 | assert(isBundledWithPred() && "MI isn't bundled with its predecessor"); |
| 987 | clearFlag(BundledPred); |
| 988 | MachineBasicBlock::instr_iterator Pred = this; |
| 989 | --Pred; |
Jakob Stoklund Olesen | 00f6c77 | 2012-12-18 23:00:28 +0000 | [diff] [blame] | 990 | assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags"); |
Jakob Stoklund Olesen | fead62d | 2012-12-07 04:23:29 +0000 | [diff] [blame] | 991 | Pred->clearFlag(BundledSucc); |
| 992 | } |
| 993 | |
| 994 | void MachineInstr::unbundleFromSucc() { |
| 995 | assert(isBundledWithSucc() && "MI isn't bundled with its successor"); |
| 996 | clearFlag(BundledSucc); |
| 997 | MachineBasicBlock::instr_iterator Succ = this; |
Sergei Larin | 3b46d7e | 2013-01-09 17:54:33 +0000 | [diff] [blame] | 998 | ++Succ; |
Jakob Stoklund Olesen | 00f6c77 | 2012-12-18 23:00:28 +0000 | [diff] [blame] | 999 | assert(Succ->isBundledWithPred() && "Inconsistent bundle flags"); |
Jakob Stoklund Olesen | fead62d | 2012-12-07 04:23:29 +0000 | [diff] [blame] | 1000 | Succ->clearFlag(BundledPred); |
| 1001 | } |
| 1002 | |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1003 | bool MachineInstr::isStackAligningInlineAsm() const { |
| 1004 | if (isInlineAsm()) { |
| 1005 | unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); |
| 1006 | if (ExtraInfo & InlineAsm::Extra_IsAlignStack) |
| 1007 | return true; |
| 1008 | } |
| 1009 | return false; |
| 1010 | } |
Chris Lattner | 33f5af0 | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 1011 | |
Chad Rosier | 994f404 | 2012-09-05 21:00:58 +0000 | [diff] [blame] | 1012 | InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const { |
| 1013 | assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!"); |
| 1014 | unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); |
Chad Rosier | e53314f | 2012-09-05 22:40:13 +0000 | [diff] [blame] | 1015 | return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0); |
Chad Rosier | 994f404 | 2012-09-05 21:00:58 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Jakob Stoklund Olesen | 1e73716 | 2011-10-12 23:37:33 +0000 | [diff] [blame] | 1018 | int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx, |
| 1019 | unsigned *GroupNo) const { |
| 1020 | assert(isInlineAsm() && "Expected an inline asm instruction"); |
| 1021 | assert(OpIdx < getNumOperands() && "OpIdx out of range"); |
| 1022 | |
| 1023 | // Ignore queries about the initial operands. |
| 1024 | if (OpIdx < InlineAsm::MIOp_FirstOperand) |
| 1025 | return -1; |
| 1026 | |
| 1027 | unsigned Group = 0; |
| 1028 | unsigned NumOps; |
| 1029 | for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; |
| 1030 | i += NumOps) { |
| 1031 | const MachineOperand &FlagMO = getOperand(i); |
| 1032 | // If we reach the implicit register operands, stop looking. |
| 1033 | if (!FlagMO.isImm()) |
| 1034 | return -1; |
| 1035 | NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); |
| 1036 | if (i + NumOps > OpIdx) { |
| 1037 | if (GroupNo) |
| 1038 | *GroupNo = Group; |
| 1039 | return i; |
| 1040 | } |
| 1041 | ++Group; |
| 1042 | } |
| 1043 | return -1; |
| 1044 | } |
| 1045 | |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1046 | const TargetRegisterClass* |
| 1047 | MachineInstr::getRegClassConstraint(unsigned OpIdx, |
| 1048 | const TargetInstrInfo *TII, |
| 1049 | const TargetRegisterInfo *TRI) const { |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1050 | assert(getParent() && "Can't have an MBB reference here!"); |
| 1051 | assert(getParent()->getParent() && "Can't have an MF reference here!"); |
| 1052 | const MachineFunction &MF = *getParent()->getParent(); |
| 1053 | |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1054 | // Most opcodes have fixed constraints in their MCInstrDesc. |
| 1055 | if (!isInlineAsm()) |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1056 | return TII->getRegClass(getDesc(), OpIdx, TRI, MF); |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (!getOperand(OpIdx).isReg()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1059 | return nullptr; |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1060 | |
| 1061 | // For tied uses on inline asm, get the constraint from the def. |
| 1062 | unsigned DefIdx; |
| 1063 | if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx)) |
| 1064 | OpIdx = DefIdx; |
| 1065 | |
| 1066 | // Inline asm stores register class constraints in the flag word. |
| 1067 | int FlagIdx = findInlineAsmFlagIdx(OpIdx); |
| 1068 | if (FlagIdx < 0) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1069 | return nullptr; |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1070 | |
| 1071 | unsigned Flag = getOperand(FlagIdx).getImm(); |
| 1072 | unsigned RCID; |
| 1073 | if (InlineAsm::hasRegClassConstraint(Flag, RCID)) |
| 1074 | return TRI->getRegClass(RCID); |
| 1075 | |
| 1076 | // Assume that all registers in a memory operand are pointers. |
| 1077 | if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem) |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1078 | return TRI->getPointerRegClass(MF); |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1079 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1080 | return nullptr; |
Jakob Stoklund Olesen | 35b362f | 2011-10-12 23:37:36 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Quentin Colombet | 1fb3362a | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 1083 | const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg( |
| 1084 | unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, |
| 1085 | const TargetRegisterInfo *TRI, bool ExploreBundle) const { |
| 1086 | // Check every operands inside the bundle if we have |
| 1087 | // been asked to. |
| 1088 | if (ExploreBundle) |
| 1089 | for (ConstMIBundleOperands OpndIt(this); OpndIt.isValid() && CurRC; |
| 1090 | ++OpndIt) |
| 1091 | CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl( |
| 1092 | OpndIt.getOperandNo(), Reg, CurRC, TII, TRI); |
| 1093 | else |
| 1094 | // Otherwise, just check the current operands. |
| 1095 | for (ConstMIOperands OpndIt(this); OpndIt.isValid() && CurRC; ++OpndIt) |
| 1096 | CurRC = getRegClassConstraintEffectForVRegImpl(OpndIt.getOperandNo(), Reg, |
| 1097 | CurRC, TII, TRI); |
| 1098 | return CurRC; |
| 1099 | } |
| 1100 | |
| 1101 | const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl( |
| 1102 | unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC, |
| 1103 | const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { |
| 1104 | assert(CurRC && "Invalid initial register class"); |
| 1105 | // Check if Reg is constrained by some of its use/def from MI. |
| 1106 | const MachineOperand &MO = getOperand(OpIdx); |
| 1107 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1108 | return CurRC; |
| 1109 | // If yes, accumulate the constraints through the operand. |
| 1110 | return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI); |
| 1111 | } |
| 1112 | |
| 1113 | const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect( |
| 1114 | unsigned OpIdx, const TargetRegisterClass *CurRC, |
| 1115 | const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { |
| 1116 | const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI); |
| 1117 | const MachineOperand &MO = getOperand(OpIdx); |
| 1118 | assert(MO.isReg() && |
| 1119 | "Cannot get register constraints for non-register operand"); |
| 1120 | assert(CurRC && "Invalid initial register class"); |
| 1121 | if (unsigned SubIdx = MO.getSubReg()) { |
| 1122 | if (OpRC) |
| 1123 | CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx); |
| 1124 | else |
| 1125 | CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx); |
| 1126 | } else if (OpRC) |
| 1127 | CurRC = TRI->getCommonSubClass(CurRC, OpRC); |
| 1128 | return CurRC; |
| 1129 | } |
| 1130 | |
Jakob Stoklund Olesen | 68d752b | 2013-01-09 18:28:16 +0000 | [diff] [blame] | 1131 | /// Return the number of instructions inside the MI bundle, not counting the |
| 1132 | /// header instruction. |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1133 | unsigned MachineInstr::getBundleSize() const { |
Jakob Stoklund Olesen | 68d752b | 2013-01-09 18:28:16 +0000 | [diff] [blame] | 1134 | MachineBasicBlock::const_instr_iterator I = this; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1135 | unsigned Size = 0; |
Jakob Stoklund Olesen | 68d752b | 2013-01-09 18:28:16 +0000 | [diff] [blame] | 1136 | while (I->isBundledWithSucc()) |
| 1137 | ++Size, ++I; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1138 | return Size; |
| 1139 | } |
| 1140 | |
Evan Cheng | 910c808 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 1141 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Jim Grosbach | 9632c14 | 2009-09-17 17:57:26 +0000 | [diff] [blame] | 1142 | /// the specific register or -1 if it is not found. It further tightens |
Evan Cheng | 9965aeb | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 1143 | /// the search criteria to a use that kills the register if isKill is true. |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1144 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, |
| 1145 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 75c2194 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 1146 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | 5983bdb | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 1147 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1148 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1149 | continue; |
| 1150 | unsigned MOReg = MO.getReg(); |
| 1151 | if (!MOReg) |
| 1152 | continue; |
| 1153 | if (MOReg == Reg || |
| 1154 | (TRI && |
| 1155 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 1156 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 1157 | TRI->isSubRegister(MOReg, Reg))) |
Evan Cheng | 9965aeb | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 1158 | if (!isKill || MO.isKill()) |
Evan Cheng | ec3ac31 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 1159 | return i; |
Evan Cheng | 75c2194 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 1160 | } |
Evan Cheng | ec3ac31 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 1161 | return -1; |
Evan Cheng | 75c2194 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 1162 | } |
Jakob Stoklund Olesen | 5d4c134 | 2010-05-19 20:36:22 +0000 | [diff] [blame] | 1163 | |
Jakob Stoklund Olesen | 7d7f604 | 2010-05-21 20:02:01 +0000 | [diff] [blame] | 1164 | /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) |
| 1165 | /// indicating if this instruction reads or writes Reg. This also considers |
| 1166 | /// partial defines. |
| 1167 | std::pair<bool,bool> |
| 1168 | MachineInstr::readsWritesVirtualRegister(unsigned Reg, |
| 1169 | SmallVectorImpl<unsigned> *Ops) const { |
| 1170 | bool PartDef = false; // Partial redefine. |
| 1171 | bool FullDef = false; // Full define. |
| 1172 | bool Use = false; |
Jakob Stoklund Olesen | 5d4c134 | 2010-05-19 20:36:22 +0000 | [diff] [blame] | 1173 | |
| 1174 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1175 | const MachineOperand &MO = getOperand(i); |
| 1176 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1177 | continue; |
Jakob Stoklund Olesen | 7d7f604 | 2010-05-21 20:02:01 +0000 | [diff] [blame] | 1178 | if (Ops) |
| 1179 | Ops->push_back(i); |
Jakob Stoklund Olesen | 5d4c134 | 2010-05-19 20:36:22 +0000 | [diff] [blame] | 1180 | if (MO.isUse()) |
Jakob Stoklund Olesen | 7d7f604 | 2010-05-21 20:02:01 +0000 | [diff] [blame] | 1181 | Use |= !MO.isUndef(); |
Jakob Stoklund Olesen | 9eb77bf | 2011-08-19 00:30:17 +0000 | [diff] [blame] | 1182 | else if (MO.getSubReg() && !MO.isUndef()) |
| 1183 | // A partial <def,undef> doesn't count as reading the register. |
Jakob Stoklund Olesen | 5d4c134 | 2010-05-19 20:36:22 +0000 | [diff] [blame] | 1184 | PartDef = true; |
| 1185 | else |
| 1186 | FullDef = true; |
| 1187 | } |
Jakob Stoklund Olesen | 7d7f604 | 2010-05-21 20:02:01 +0000 | [diff] [blame] | 1188 | // A partial redefine uses Reg unless there is also a full define. |
| 1189 | return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); |
Jakob Stoklund Olesen | 5d4c134 | 2010-05-19 20:36:22 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1192 | /// findRegisterDefOperandIdx() - Returns the operand index that is a def of |
Dan Gohman | 72a0bc1 | 2008-05-06 00:20:10 +0000 | [diff] [blame] | 1193 | /// the specified register or -1 if it is not found. If isDead is true, defs |
| 1194 | /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it |
| 1195 | /// also checks if there is a def of a super-register. |
Evan Cheng | 3858451 | 2010-05-21 20:53:24 +0000 | [diff] [blame] | 1196 | int |
| 1197 | MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, |
| 1198 | const TargetRegisterInfo *TRI) const { |
| 1199 | bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); |
Evan Cheng | f7ed82d | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1200 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1201 | const MachineOperand &MO = getOperand(i); |
Jakob Stoklund Olesen | e7d3f44 | 2012-02-14 23:49:37 +0000 | [diff] [blame] | 1202 | // Accept regmask operands when Overlap is set. |
| 1203 | // Ignore them when looking for a specific def operand (Overlap == false). |
| 1204 | if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg)) |
| 1205 | return i; |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1206 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1207 | continue; |
| 1208 | unsigned MOReg = MO.getReg(); |
Evan Cheng | 3858451 | 2010-05-21 20:53:24 +0000 | [diff] [blame] | 1209 | bool Found = (MOReg == Reg); |
| 1210 | if (!Found && TRI && isPhys && |
| 1211 | TargetRegisterInfo::isPhysicalRegister(MOReg)) { |
| 1212 | if (Overlap) |
| 1213 | Found = TRI->regsOverlap(MOReg, Reg); |
| 1214 | else |
| 1215 | Found = TRI->isSubRegister(MOReg, Reg); |
| 1216 | } |
| 1217 | if (Found && (!isDead || MO.isDead())) |
| 1218 | return i; |
Evan Cheng | f7ed82d | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1219 | } |
Evan Cheng | 6325446 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1220 | return -1; |
Evan Cheng | f7ed82d | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1221 | } |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 1222 | |
Evan Cheng | 5983bdb | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 1223 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 1224 | /// operand list that is used to represent the predicate. It returns -1 if |
| 1225 | /// none is found. |
| 1226 | int MachineInstr::findFirstPredOperandIdx() const { |
Jim Grosbach | ed16ec4 | 2011-08-29 22:24:09 +0000 | [diff] [blame] | 1227 | // Don't call MCID.findFirstPredOperandIdx() because this variant |
| 1228 | // is sometimes called on an instruction that's not yet complete, and |
| 1229 | // so the number of operands is less than the MCID indicates. In |
| 1230 | // particular, the PTX target does this. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1231 | const MCInstrDesc &MCID = getDesc(); |
| 1232 | if (MCID.isPredicable()) { |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 1233 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1234 | if (MCID.OpInfo[i].isPredicate()) |
Evan Cheng | 5983bdb | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 1235 | return i; |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Evan Cheng | 5983bdb | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 1238 | return -1; |
Evan Cheng | 4d728b0 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 1239 | } |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 1240 | |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 1241 | // MachineOperand::TiedTo is 4 bits wide. |
| 1242 | const unsigned TiedMax = 15; |
| 1243 | |
| 1244 | /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other. |
| 1245 | /// |
| 1246 | /// Use and def operands can be tied together, indicated by a non-zero TiedTo |
| 1247 | /// field. TiedTo can have these values: |
| 1248 | /// |
| 1249 | /// 0: Operand is not tied to anything. |
| 1250 | /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1). |
| 1251 | /// TiedMax: Tied to an operand >= TiedMax-1. |
| 1252 | /// |
| 1253 | /// The tied def must be one of the first TiedMax operands on a normal |
| 1254 | /// instruction. INLINEASM instructions allow more tied defs. |
| 1255 | /// |
Jakob Stoklund Olesen | 5c8eda0 | 2012-08-31 20:50:53 +0000 | [diff] [blame] | 1256 | void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) { |
Jakob Stoklund Olesen | 5c8eda0 | 2012-08-31 20:50:53 +0000 | [diff] [blame] | 1257 | MachineOperand &DefMO = getOperand(DefIdx); |
| 1258 | MachineOperand &UseMO = getOperand(UseIdx); |
| 1259 | assert(DefMO.isDef() && "DefIdx must be a def operand"); |
| 1260 | assert(UseMO.isUse() && "UseIdx must be a use operand"); |
| 1261 | assert(!DefMO.isTied() && "Def is already tied to another use"); |
| 1262 | assert(!UseMO.isTied() && "Use is already tied to another def"); |
| 1263 | |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 1264 | if (DefIdx < TiedMax) |
| 1265 | UseMO.TiedTo = DefIdx + 1; |
| 1266 | else { |
| 1267 | // Inline asm can use the group descriptors to find tied operands, but on |
| 1268 | // normal instruction, the tied def must be within the first TiedMax |
| 1269 | // operands. |
| 1270 | assert(isInlineAsm() && "DefIdx out of range"); |
| 1271 | UseMO.TiedTo = TiedMax; |
| 1272 | } |
| 1273 | |
| 1274 | // UseIdx can be out of range, we'll search for it in findTiedOperandIdx(). |
| 1275 | DefMO.TiedTo = std::min(UseIdx + 1, TiedMax); |
Jakob Stoklund Olesen | 5c8eda0 | 2012-08-31 20:50:53 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 1278 | /// Given the index of a tied register operand, find the operand it is tied to. |
| 1279 | /// Defs are tied to uses and vice versa. Returns the index of the tied operand |
| 1280 | /// which must exist. |
| 1281 | unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const { |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 1282 | const MachineOperand &MO = getOperand(OpIdx); |
| 1283 | assert(MO.isTied() && "Operand isn't tied"); |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 1284 | |
Jakob Stoklund Olesen | 0a09da8 | 2012-09-04 18:36:28 +0000 | [diff] [blame] | 1285 | // Normally TiedTo is in range. |
| 1286 | if (MO.TiedTo < TiedMax) |
| 1287 | return MO.TiedTo - 1; |
| 1288 | |
| 1289 | // Uses on normal instructions can be out of range. |
| 1290 | if (!isInlineAsm()) { |
| 1291 | // Normal tied defs must be in the 0..TiedMax-1 range. |
| 1292 | if (MO.isUse()) |
| 1293 | return TiedMax - 1; |
| 1294 | // MO is a def. Search for the tied use. |
| 1295 | for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) { |
| 1296 | const MachineOperand &UseMO = getOperand(i); |
| 1297 | if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1) |
| 1298 | return i; |
| 1299 | } |
| 1300 | llvm_unreachable("Can't find tied use"); |
| 1301 | } |
| 1302 | |
| 1303 | // Now deal with inline asm by parsing the operand group descriptor flags. |
| 1304 | // Find the beginning of each operand group. |
| 1305 | SmallVector<unsigned, 8> GroupIdx; |
| 1306 | unsigned OpIdxGroup = ~0u; |
| 1307 | unsigned NumOps; |
| 1308 | for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; |
| 1309 | i += NumOps) { |
| 1310 | const MachineOperand &FlagMO = getOperand(i); |
| 1311 | assert(FlagMO.isImm() && "Invalid tied operand on inline asm"); |
| 1312 | unsigned CurGroup = GroupIdx.size(); |
| 1313 | GroupIdx.push_back(i); |
| 1314 | NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); |
| 1315 | // OpIdx belongs to this operand group. |
| 1316 | if (OpIdx > i && OpIdx < i + NumOps) |
| 1317 | OpIdxGroup = CurGroup; |
| 1318 | unsigned TiedGroup; |
| 1319 | if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup)) |
| 1320 | continue; |
| 1321 | // Operands in this group are tied to operands in TiedGroup which must be |
| 1322 | // earlier. Find the number of operands between the two groups. |
| 1323 | unsigned Delta = i - GroupIdx[TiedGroup]; |
| 1324 | |
| 1325 | // OpIdx is a use tied to TiedGroup. |
| 1326 | if (OpIdxGroup == CurGroup) |
| 1327 | return OpIdx - Delta; |
| 1328 | |
| 1329 | // OpIdx is a def tied to this use group. |
| 1330 | if (OpIdxGroup == TiedGroup) |
| 1331 | return OpIdx + Delta; |
| 1332 | } |
| 1333 | llvm_unreachable("Invalid tied operand on inline asm"); |
Jakob Stoklund Olesen | 2b16664 | 2012-08-29 00:37:58 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1336 | /// clearKillInfo - Clears kill flags on all operands. |
| 1337 | /// |
| 1338 | void MachineInstr::clearKillInfo() { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1339 | for (MachineOperand &MO : operands()) { |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1340 | if (MO.isReg() && MO.isUse()) |
| 1341 | MO.setIsKill(false); |
| 1342 | } |
| 1343 | } |
| 1344 | |
Jakob Stoklund Olesen | a8ad977 | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 1345 | void MachineInstr::substituteRegister(unsigned FromReg, |
| 1346 | unsigned ToReg, |
| 1347 | unsigned SubIdx, |
| 1348 | const TargetRegisterInfo &RegInfo) { |
| 1349 | if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { |
| 1350 | if (SubIdx) |
| 1351 | ToReg = RegInfo.getSubReg(ToReg, SubIdx); |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1352 | for (MachineOperand &MO : operands()) { |
Jakob Stoklund Olesen | a8ad977 | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 1353 | if (!MO.isReg() || MO.getReg() != FromReg) |
| 1354 | continue; |
| 1355 | MO.substPhysReg(ToReg, RegInfo); |
| 1356 | } |
| 1357 | } else { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1358 | for (MachineOperand &MO : operands()) { |
Jakob Stoklund Olesen | a8ad977 | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 1359 | if (!MO.isReg() || MO.getReg() != FromReg) |
| 1360 | continue; |
| 1361 | MO.substVirtReg(ToReg, SubIdx, RegInfo); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
Evan Cheng | 7d98a48 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 1366 | /// isSafeToMove - Return true if it is safe to move this instruction. If |
| 1367 | /// SawStore is set to true, it means that there is a store (or call) between |
| 1368 | /// the instruction's location and its intended destination. |
Dan Gohman | 0d9d8ae | 2008-11-18 19:04:29 +0000 | [diff] [blame] | 1369 | bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, |
Evan Cheng | 62e795a | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 1370 | AliasAnalysis *AA, |
| 1371 | bool &SawStore) const { |
Evan Cheng | 399e110 | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 1372 | // Ignore stuff that we obviously can't move. |
Jakob Stoklund Olesen | 813a109 | 2012-08-29 20:48:45 +0000 | [diff] [blame] | 1373 | // |
| 1374 | // Treat volatile loads as stores. This is not strictly necessary for |
Jakob Stoklund Olesen | d92e2bc | 2012-09-04 18:44:43 +0000 | [diff] [blame] | 1375 | // volatiles, but it is required for atomic loads. It is not allowed to move |
Jakob Stoklund Olesen | 813a109 | 2012-08-29 20:48:45 +0000 | [diff] [blame] | 1376 | // a load across an atomic load with Ordering > Monotonic. |
| 1377 | if (mayStore() || isCall() || |
Jakob Stoklund Olesen | cea3e77 | 2012-08-29 21:19:21 +0000 | [diff] [blame] | 1378 | (mayLoad() && hasOrderedMemoryRef())) { |
Evan Cheng | 399e110 | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 1379 | SawStore = true; |
| 1380 | return false; |
| 1381 | } |
Evan Cheng | 0638c20 | 2011-01-07 21:08:26 +0000 | [diff] [blame] | 1382 | |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1383 | if (isPosition() || isDebugValue() || isTerminator() || |
| 1384 | hasUnmodeledSideEffects()) |
Evan Cheng | 399e110 | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 1385 | return false; |
| 1386 | |
| 1387 | // See if this instruction does a load. If so, we have to guarantee that the |
| 1388 | // loaded value doesn't change between the load and the its intended |
| 1389 | // destination. The check for isInvariantLoad gives the targe the chance to |
| 1390 | // classify the load as always returning a constant, e.g. a constant pool |
| 1391 | // load. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1392 | if (mayLoad() && !isInvariantLoad(AA)) |
Evan Cheng | 399e110 | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 1393 | // Otherwise, this is a real load. If there is a store between the load and |
Jakob Stoklund Olesen | 813a109 | 2012-08-29 20:48:45 +0000 | [diff] [blame] | 1394 | // end of block, we can't move it. |
| 1395 | return !SawStore; |
Dan Gohman | 7c59ed6 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1396 | |
Evan Cheng | 399e110 | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 1397 | return true; |
| 1398 | } |
| 1399 | |
Jakob Stoklund Olesen | cea3e77 | 2012-08-29 21:19:21 +0000 | [diff] [blame] | 1400 | /// hasOrderedMemoryRef - Return true if this instruction may have an ordered |
| 1401 | /// or volatile memory reference, or if the information describing the memory |
| 1402 | /// reference is not available. Return false if it is known to have no ordered |
| 1403 | /// memory references. |
| 1404 | bool MachineInstr::hasOrderedMemoryRef() const { |
Dan Gohman | 7c59ed6 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1405 | // An instruction known never to access memory won't have a volatile access. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1406 | if (!mayStore() && |
| 1407 | !mayLoad() && |
| 1408 | !isCall() && |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1409 | !hasUnmodeledSideEffects()) |
Dan Gohman | 7c59ed6 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1410 | return false; |
| 1411 | |
| 1412 | // Otherwise, if the instruction has no memory reference information, |
| 1413 | // conservatively assume it wasn't preserved. |
| 1414 | if (memoperands_empty()) |
| 1415 | return true; |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 1416 | |
Jakob Stoklund Olesen | cea3e77 | 2012-08-29 21:19:21 +0000 | [diff] [blame] | 1417 | // Check the memory reference information for ordered references. |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1418 | for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) |
Jakob Stoklund Olesen | cea3e77 | 2012-08-29 21:19:21 +0000 | [diff] [blame] | 1419 | if (!(*I)->isUnordered()) |
Dan Gohman | 7c59ed6 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 1420 | return true; |
| 1421 | |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1425 | /// isInvariantLoad - Return true if this instruction is loading from a |
| 1426 | /// location whose value is invariant across the function. For example, |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1427 | /// loading a value from the constant pool or from the argument area |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1428 | /// of a function if it does not change. This should only return true of |
| 1429 | /// *all* loads the instruction does are invariant (if it does multiple loads). |
| 1430 | bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { |
| 1431 | // If the instruction doesn't load at all, it isn't an invariant load. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1432 | if (!mayLoad()) |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1433 | return false; |
| 1434 | |
| 1435 | // If the instruction has lost its memoperands, conservatively assume that |
| 1436 | // it may not be an invariant load. |
| 1437 | if (memoperands_empty()) |
| 1438 | return false; |
| 1439 | |
| 1440 | const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); |
| 1441 | |
| 1442 | for (mmo_iterator I = memoperands_begin(), |
| 1443 | E = memoperands_end(); I != E; ++I) { |
| 1444 | if ((*I)->isVolatile()) return false; |
| 1445 | if ((*I)->isStore()) return false; |
Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1446 | if ((*I)->isInvariant()) return true; |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1447 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 1448 | |
| 1449 | // A load from a constant PseudoSourceValue is invariant. |
| 1450 | if (const PseudoSourceValue *PSV = (*I)->getPseudoValue()) |
| 1451 | if (PSV->isConstant(MFI)) |
| 1452 | continue; |
| 1453 | |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1454 | if (const Value *V = (*I)->getValue()) { |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1455 | // If we have an AliasAnalysis, ask it whether the memory is constant. |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 1456 | if (AA && AA->pointsToConstantMemory( |
| 1457 | AliasAnalysis::Location(V, (*I)->getSize(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1458 | (*I)->getAAInfo()))) |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 1459 | continue; |
| 1460 | } |
| 1461 | |
| 1462 | // Otherwise assume conservatively. |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
| 1466 | // Everything checks out. |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
Evan Cheng | 7145382 | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1470 | /// isConstantValuePHI - If the specified instruction is a PHI that always |
| 1471 | /// merges together the same virtual register, return the register, otherwise |
| 1472 | /// return 0. |
| 1473 | unsigned MachineInstr::isConstantValuePHI() const { |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1474 | if (!isPHI()) |
Evan Cheng | 7145382 | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1475 | return 0; |
Evan Cheng | 5c668a2 | 2009-12-07 23:10:34 +0000 | [diff] [blame] | 1476 | assert(getNumOperands() >= 3 && |
| 1477 | "It's illegal to have a PHI without source operands"); |
Evan Cheng | 7145382 | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 1478 | |
| 1479 | unsigned Reg = getOperand(1).getReg(); |
| 1480 | for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) |
| 1481 | if (getOperand(i).getReg() != Reg) |
| 1482 | return 0; |
| 1483 | return Reg; |
| 1484 | } |
| 1485 | |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1486 | bool MachineInstr::hasUnmodeledSideEffects() const { |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1487 | if (hasProperty(MCID::UnmodeledSideEffects)) |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1488 | return true; |
| 1489 | if (isInlineAsm()) { |
| 1490 | unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); |
| 1491 | if (ExtraInfo & InlineAsm::Extra_HasSideEffects) |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
Evan Cheng | b083c47 | 2010-04-08 20:02:37 +0000 | [diff] [blame] | 1498 | /// allDefsAreDead - Return true if all the defs of this instruction are dead. |
| 1499 | /// |
| 1500 | bool MachineInstr::allDefsAreDead() const { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1501 | for (const MachineOperand &MO : operands()) { |
Evan Cheng | b083c47 | 2010-04-08 20:02:37 +0000 | [diff] [blame] | 1502 | if (!MO.isReg() || MO.isUse()) |
| 1503 | continue; |
| 1504 | if (!MO.isDead()) |
| 1505 | return false; |
| 1506 | } |
| 1507 | return true; |
| 1508 | } |
| 1509 | |
Evan Cheng | 21eedfb | 2010-10-22 21:49:09 +0000 | [diff] [blame] | 1510 | /// copyImplicitOps - Copy implicit register operands from specified |
| 1511 | /// instruction to this instruction. |
Jakob Stoklund Olesen | 33f5d14 | 2012-12-20 22:54:02 +0000 | [diff] [blame] | 1512 | void MachineInstr::copyImplicitOps(MachineFunction &MF, |
| 1513 | const MachineInstr *MI) { |
Evan Cheng | 21eedfb | 2010-10-22 21:49:09 +0000 | [diff] [blame] | 1514 | for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands(); |
| 1515 | i != e; ++i) { |
| 1516 | const MachineOperand &MO = MI->getOperand(i); |
Lang Hames | 7c8189c | 2014-03-17 01:22:54 +0000 | [diff] [blame] | 1517 | if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask()) |
Jakob Stoklund Olesen | 33f5d14 | 2012-12-20 22:54:02 +0000 | [diff] [blame] | 1518 | addOperand(MF, MO); |
Evan Cheng | 21eedfb | 2010-10-22 21:49:09 +0000 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | |
Brian Gaeke | e8f7c2f | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 1522 | void MachineInstr::dump() const { |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 1523 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
David Greene | 29388d6 | 2010-01-04 23:48:20 +0000 | [diff] [blame] | 1524 | dbgs() << " " << *this; |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 1525 | #endif |
Mon P Wang | dfcc1ff | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1528 | void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const { |
| 1529 | // We can be a bit tidier if we know the MachineFunction. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1530 | const MachineFunction *MF = nullptr; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1531 | const TargetRegisterInfo *TRI = nullptr; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1532 | const MachineRegisterInfo *MRI = nullptr; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1533 | const TargetInstrInfo *TII = nullptr; |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1534 | if (const MachineBasicBlock *MBB = getParent()) { |
| 1535 | MF = MBB->getParent(); |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1536 | if (MF) { |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1537 | MRI = &MF->getRegInfo(); |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1538 | TRI = MF->getSubtarget().getRegisterInfo(); |
| 1539 | TII = MF->getSubtarget().getInstrInfo(); |
| 1540 | } |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1541 | } |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1542 | |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1543 | // Save a list of virtual registers. |
| 1544 | SmallVector<unsigned, 8> VirtRegs; |
| 1545 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1546 | // Print explicitly defined operands on the left of an assignment syntax. |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1547 | unsigned StartOp = 0, e = getNumOperands(); |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1548 | for (; StartOp < e && getOperand(StartOp).isReg() && |
| 1549 | getOperand(StartOp).isDef() && |
| 1550 | !getOperand(StartOp).isImplicit(); |
| 1551 | ++StartOp) { |
| 1552 | if (StartOp != 0) OS << ", "; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1553 | getOperand(StartOp).print(OS, TRI); |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1554 | unsigned Reg = getOperand(StartOp).getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1555 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1556 | VirtRegs.push_back(Reg); |
Chris Lattner | ac6e974 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1557 | } |
Tanya Lattner | 23dbc81 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 1558 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1559 | if (StartOp != 0) |
| 1560 | OS << " = "; |
| 1561 | |
| 1562 | // Print the opcode name. |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1563 | if (TII) |
| 1564 | OS << TII->getName(getOpcode()); |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 1565 | else |
| 1566 | OS << "UNKNOWN"; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1567 | |
Andrew Trick | b36388a | 2013-01-25 07:45:25 +0000 | [diff] [blame] | 1568 | if (SkipOpers) |
| 1569 | return; |
| 1570 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1571 | // Print the rest of the operands. |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1572 | bool OmittedAnyCallClobbers = false; |
| 1573 | bool FirstOp = true; |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1574 | unsigned AsmDescOp = ~0u; |
| 1575 | unsigned AsmOpCount = 0; |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1576 | |
Jakob Stoklund Olesen | 2318d1e | 2011-09-29 00:40:51 +0000 | [diff] [blame] | 1577 | if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) { |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1578 | // Print asm string. |
| 1579 | OS << " "; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1580 | getOperand(InlineAsm::MIOp_AsmString).print(OS, TRI); |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1581 | |
Eric Christopher | 0cb6fd9 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 1582 | // Print HasSideEffects, MayLoad, MayStore, IsAlignStack |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1583 | unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); |
| 1584 | if (ExtraInfo & InlineAsm::Extra_HasSideEffects) |
| 1585 | OS << " [sideeffect]"; |
Eric Christopher | 0cb6fd9 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 1586 | if (ExtraInfo & InlineAsm::Extra_MayLoad) |
| 1587 | OS << " [mayload]"; |
| 1588 | if (ExtraInfo & InlineAsm::Extra_MayStore) |
| 1589 | OS << " [maystore]"; |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1590 | if (ExtraInfo & InlineAsm::Extra_IsAlignStack) |
| 1591 | OS << " [alignstack]"; |
Chad Rosier | cbd2a19 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 1592 | if (getInlineAsmDialect() == InlineAsm::AD_ATT) |
Chad Rosier | 994f404 | 2012-09-05 21:00:58 +0000 | [diff] [blame] | 1593 | OS << " [attdialect]"; |
Chad Rosier | cbd2a19 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 1594 | if (getInlineAsmDialect() == InlineAsm::AD_Intel) |
Chad Rosier | 994f404 | 2012-09-05 21:00:58 +0000 | [diff] [blame] | 1595 | OS << " [inteldialect]"; |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1596 | |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1597 | StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand; |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 1598 | FirstOp = false; |
| 1599 | } |
| 1600 | |
| 1601 | |
Chris Lattner | ac6e974 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1602 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1603 | const MachineOperand &MO = getOperand(i); |
| 1604 | |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1605 | if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1606 | VirtRegs.push_back(MO.getReg()); |
| 1607 | |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1608 | // Omit call-clobbered registers which aren't used anywhere. This makes |
| 1609 | // call instructions much less noisy on targets where calls clobber lots |
| 1610 | // of registers. Don't rely on MO.isDead() because we may be called before |
| 1611 | // LiveVariables is run, or we may be looking at a non-allocatable reg. |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1612 | if (MRI && isCall() && |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1613 | MO.isReg() && MO.isImplicit() && MO.isDef()) { |
| 1614 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1615 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1616 | if (MRI->use_empty(Reg)) { |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1617 | bool HasAliasLive = false; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1618 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1619 | unsigned AliasReg = *AI; |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1620 | if (!MRI->use_empty(AliasReg)) { |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1621 | HasAliasLive = true; |
| 1622 | break; |
| 1623 | } |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1624 | } |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1625 | if (!HasAliasLive) { |
| 1626 | OmittedAnyCallClobbers = true; |
| 1627 | continue; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | if (FirstOp) FirstOp = false; else OS << ","; |
Chris Lattner | ac6e974 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 1634 | OS << " "; |
Jakob Stoklund Olesen | e8800b8 | 2010-01-19 22:08:34 +0000 | [diff] [blame] | 1635 | if (i < getDesc().NumOperands) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1636 | const MCOperandInfo &MCOI = getDesc().OpInfo[i]; |
| 1637 | if (MCOI.isPredicate()) |
Jakob Stoklund Olesen | e8800b8 | 2010-01-19 22:08:34 +0000 | [diff] [blame] | 1638 | OS << "pred:"; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1639 | if (MCOI.isOptionalDef()) |
Jakob Stoklund Olesen | e8800b8 | 2010-01-19 22:08:34 +0000 | [diff] [blame] | 1640 | OS << "opt:"; |
| 1641 | } |
Evan Cheng | d4d1a51 | 2010-04-28 20:03:13 +0000 | [diff] [blame] | 1642 | if (isDebugValue() && MO.isMetadata()) { |
| 1643 | // Pretty print DBG_VALUE instructions. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1644 | auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata()); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 1645 | if (DIV && !DIV->getName().empty()) |
| 1646 | OS << "!\"" << DIV->getName() << '\"'; |
Evan Cheng | d4d1a51 | 2010-04-28 20:03:13 +0000 | [diff] [blame] | 1647 | else |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1648 | MO.print(OS, TRI); |
| 1649 | } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) { |
| 1650 | OS << TRI->getSubRegIndexName(MO.getImm()); |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1651 | } else if (i == AsmDescOp && MO.isImm()) { |
| 1652 | // Pretty print the inline asm operand descriptor. |
| 1653 | OS << '$' << AsmOpCount++; |
| 1654 | unsigned Flag = MO.getImm(); |
| 1655 | switch (InlineAsm::getKind(Flag)) { |
Jakob Stoklund Olesen | 24abd9d | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 1656 | case InlineAsm::Kind_RegUse: OS << ":[reguse"; break; |
| 1657 | case InlineAsm::Kind_RegDef: OS << ":[regdef"; break; |
| 1658 | case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break; |
| 1659 | case InlineAsm::Kind_Clobber: OS << ":[clobber"; break; |
| 1660 | case InlineAsm::Kind_Imm: OS << ":[imm"; break; |
| 1661 | case InlineAsm::Kind_Mem: OS << ":[mem"; break; |
| 1662 | default: OS << ":[??" << InlineAsm::getKind(Flag); break; |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Jakob Stoklund Olesen | 24abd9d | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 1665 | unsigned RCID = 0; |
Nick Lewycky | 8488225 | 2011-10-13 00:54:59 +0000 | [diff] [blame] | 1666 | if (InlineAsm::hasRegClassConstraint(Flag, RCID)) { |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1667 | if (TRI) { |
| 1668 | OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID)); |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1669 | } else |
Jakob Stoklund Olesen | 24abd9d | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 1670 | OS << ":RC" << RCID; |
Nick Lewycky | 8488225 | 2011-10-13 00:54:59 +0000 | [diff] [blame] | 1671 | } |
Jakob Stoklund Olesen | 24abd9d | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 1672 | |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1673 | unsigned TiedTo = 0; |
| 1674 | if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo)) |
Jakob Stoklund Olesen | 24abd9d | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 1675 | OS << " tiedto:$" << TiedTo; |
| 1676 | |
| 1677 | OS << ']'; |
Jakob Stoklund Olesen | 6b356b1 | 2011-06-27 04:08:29 +0000 | [diff] [blame] | 1678 | |
| 1679 | // Compute the index of the next operand descriptor. |
| 1680 | AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag); |
Evan Cheng | d4d1a51 | 2010-04-28 20:03:13 +0000 | [diff] [blame] | 1681 | } else |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1682 | MO.print(OS, TRI); |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | // Briefly indicate whether any call clobbers were omitted. |
| 1686 | if (OmittedAnyCallClobbers) { |
Bill Wendling | ec030f2 | 2009-12-25 13:45:50 +0000 | [diff] [blame] | 1687 | if (!FirstOp) OS << ","; |
Dan Gohman | 2745d19 | 2009-11-09 19:38:45 +0000 | [diff] [blame] | 1688 | OS << " ..."; |
Chris Lattner | 214808f | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 1689 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1690 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1691 | bool HaveSemi = false; |
Jakob Stoklund Olesen | 6922e9c | 2013-01-09 18:35:09 +0000 | [diff] [blame] | 1692 | const unsigned PrintableFlags = FrameSetup; |
| 1693 | if (Flags & PrintableFlags) { |
Anton Korobeynikov | 65cff414 | 2011-03-05 18:43:04 +0000 | [diff] [blame] | 1694 | if (!HaveSemi) OS << ";"; HaveSemi = true; |
| 1695 | OS << " flags: "; |
| 1696 | |
| 1697 | if (Flags & FrameSetup) |
| 1698 | OS << "FrameSetup"; |
| 1699 | } |
| 1700 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1701 | if (!memoperands_empty()) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1702 | if (!HaveSemi) OS << ";"; HaveSemi = true; |
| 1703 | |
| 1704 | OS << " mem:"; |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1705 | for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); |
| 1706 | i != e; ++i) { |
| 1707 | OS << **i; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1708 | if (std::next(i) != e) |
Dan Gohman | c0353bf | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 1709 | OS << " "; |
Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1713 | // Print the regclass of any virtual registers encountered. |
| 1714 | if (MRI && !VirtRegs.empty()) { |
| 1715 | if (!HaveSemi) OS << ";"; HaveSemi = true; |
| 1716 | for (unsigned i = 0; i != VirtRegs.size(); ++i) { |
| 1717 | const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]); |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1718 | OS << " " << TRI->getRegClassName(RC) |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1719 | << ':' << PrintReg(VirtRegs[i]); |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1720 | for (unsigned j = i+1; j != VirtRegs.size();) { |
| 1721 | if (MRI->getRegClass(VirtRegs[j]) != RC) { |
| 1722 | ++j; |
| 1723 | continue; |
| 1724 | } |
| 1725 | if (VirtRegs[i] != VirtRegs[j]) |
Jakob Stoklund Olesen | 1331a15 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 1726 | OS << "," << PrintReg(VirtRegs[j]); |
Jakob Stoklund Olesen | 0ff2c11 | 2010-07-28 18:35:46 +0000 | [diff] [blame] | 1727 | VirtRegs.erase(VirtRegs.begin()+j); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | |
Anton Korobeynikov | 65cff414 | 2011-03-05 18:43:04 +0000 | [diff] [blame] | 1732 | // Print debug location information. |
Duncan P. N. Exon Smith | c5bd3e0 | 2015-04-03 16:23:04 +0000 | [diff] [blame] | 1733 | if (isDebugValue() && getOperand(e - 2).isMetadata()) { |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1734 | if (!HaveSemi) OS << ";"; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1735 | auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata()); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 1736 | OS << " line no:" << DV->getLine(); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1737 | if (auto *InlinedAt = debugLoc->getInlinedAt()) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 1738 | DebugLoc InlinedAtDL(InlinedAt); |
| 1739 | if (InlinedAtDL && MF) { |
Devang Patel | d61b1d5 | 2011-08-04 20:44:26 +0000 | [diff] [blame] | 1740 | OS << " inlined @[ "; |
Eric Christopher | b9f0009 | 2015-02-26 23:32:17 +0000 | [diff] [blame] | 1741 | InlinedAtDL.print(OS); |
Devang Patel | d61b1d5 | 2011-08-04 20:44:26 +0000 | [diff] [blame] | 1742 | OS << " ]"; |
| 1743 | } |
| 1744 | } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1745 | if (isIndirectDebugValue()) |
| 1746 | OS << " indirect"; |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 1747 | } else if (debugLoc && MF) { |
Arnaud A. de Grandmaison | 75c9e6d | 2014-03-15 22:13:15 +0000 | [diff] [blame] | 1748 | if (!HaveSemi) OS << ";"; |
Dan Gohman | 2e3f187 | 2009-11-23 21:29:08 +0000 | [diff] [blame] | 1749 | OS << " dbg:"; |
Eric Christopher | b9f0009 | 2015-02-26 23:32:17 +0000 | [diff] [blame] | 1750 | debugLoc.print(OS); |
Bill Wendling | 1a0a3d0 | 2009-02-19 21:44:55 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Anton Korobeynikov | 65cff414 | 2011-03-05 18:43:04 +0000 | [diff] [blame] | 1753 | OS << '\n'; |
Chris Lattner | 214808f | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1756 | bool MachineInstr::addRegisterKilled(unsigned IncomingReg, |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1757 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1758 | bool AddIfNotFound) { |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1759 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1760 | bool hasAliases = isPhysReg && |
| 1761 | MCRegAliasIterator(IncomingReg, RegInfo, false).isValid(); |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1762 | bool Found = false; |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1763 | SmallVector<unsigned,4> DeadOps; |
Bill Wendling | 7921ad0 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1764 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1765 | MachineOperand &MO = getOperand(i); |
Jakob Stoklund Olesen | f465f06 | 2009-08-04 20:09:25 +0000 | [diff] [blame] | 1766 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1767 | continue; |
| 1768 | unsigned Reg = MO.getReg(); |
| 1769 | if (!Reg) |
| 1770 | continue; |
Bill Wendling | 7921ad0 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1771 | |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1772 | if (Reg == IncomingReg) { |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1773 | if (!Found) { |
| 1774 | if (MO.isKill()) |
| 1775 | // The register is already marked kill. |
| 1776 | return true; |
Jakob Stoklund Olesen | c59cd9b | 2009-08-02 19:13:03 +0000 | [diff] [blame] | 1777 | if (isPhysReg && isRegTiedToDefOperand(i)) |
| 1778 | // Two-address uses of physregs must not be marked kill. |
| 1779 | return true; |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1780 | MO.setIsKill(); |
| 1781 | Found = true; |
| 1782 | } |
| 1783 | } else if (hasAliases && MO.isKill() && |
| 1784 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1785 | // A super-register kill already exists. |
| 1786 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | b261292 | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1787 | return true; |
| 1788 | if (RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1789 | DeadOps.push_back(i); |
Bill Wendling | 7921ad0 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1793 | // Trim unneeded kill operands. |
| 1794 | while (!DeadOps.empty()) { |
| 1795 | unsigned OpIdx = DeadOps.back(); |
| 1796 | if (getOperand(OpIdx).isImplicit()) |
| 1797 | RemoveOperand(OpIdx); |
| 1798 | else |
| 1799 | getOperand(OpIdx).setIsKill(false); |
| 1800 | DeadOps.pop_back(); |
| 1801 | } |
| 1802 | |
Bill Wendling | 7921ad0 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1803 | // If not found, this means an alias of one of the operands is killed. Add a |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1804 | // new implicit operand if required. |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1805 | if (!Found && AddIfNotFound) { |
Bill Wendling | 7921ad0 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1806 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1807 | false /*IsDef*/, |
| 1808 | true /*IsImp*/, |
| 1809 | true /*IsKill*/)); |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1810 | return true; |
| 1811 | } |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1812 | return Found; |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Jakob Stoklund Olesen | 8c139a5 | 2012-01-26 17:52:15 +0000 | [diff] [blame] | 1815 | void MachineInstr::clearRegisterKills(unsigned Reg, |
| 1816 | const TargetRegisterInfo *RegInfo) { |
| 1817 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1818 | RegInfo = nullptr; |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1819 | for (MachineOperand &MO : operands()) { |
Jakob Stoklund Olesen | 8c139a5 | 2012-01-26 17:52:15 +0000 | [diff] [blame] | 1820 | if (!MO.isReg() || !MO.isUse() || !MO.isKill()) |
| 1821 | continue; |
| 1822 | unsigned OpReg = MO.getReg(); |
| 1823 | if (OpReg == Reg || (RegInfo && RegInfo->isSuperRegister(Reg, OpReg))) |
| 1824 | MO.setIsKill(false); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1828 | bool MachineInstr::addRegisterDead(unsigned Reg, |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1829 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1830 | bool AddIfNotFound) { |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1831 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg); |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1832 | bool hasAliases = isPhysReg && |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1833 | MCRegAliasIterator(Reg, RegInfo, false).isValid(); |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1834 | bool Found = false; |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1835 | SmallVector<unsigned,4> DeadOps; |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1836 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1837 | MachineOperand &MO = getOperand(i); |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1838 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1839 | continue; |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1840 | unsigned MOReg = MO.getReg(); |
| 1841 | if (!MOReg) |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1842 | continue; |
| 1843 | |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1844 | if (MOReg == Reg) { |
Jakob Stoklund Olesen | 76ad3de | 2011-04-05 16:53:50 +0000 | [diff] [blame] | 1845 | MO.setIsDead(); |
| 1846 | Found = true; |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1847 | } else if (hasAliases && MO.isDead() && |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1848 | TargetRegisterInfo::isPhysicalRegister(MOReg)) { |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1849 | // There exists a super-register that's marked dead. |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1850 | if (RegInfo->isSuperRegister(Reg, MOReg)) |
Dan Gohman | b261292 | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1851 | return true; |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1852 | if (RegInfo->isSubRegister(Reg, MOReg)) |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1853 | DeadOps.push_back(i); |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
Evan Cheng | 6c17773 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1857 | // Trim unneeded dead operands. |
| 1858 | while (!DeadOps.empty()) { |
| 1859 | unsigned OpIdx = DeadOps.back(); |
| 1860 | if (getOperand(OpIdx).isImplicit()) |
| 1861 | RemoveOperand(OpIdx); |
| 1862 | else |
| 1863 | getOperand(OpIdx).setIsDead(false); |
| 1864 | DeadOps.pop_back(); |
| 1865 | } |
| 1866 | |
Dan Gohman | c7367b4 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1867 | // If not found, this means an alias of one of the operands is dead. Add a |
| 1868 | // new implicit operand if required. |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 1869 | if (Found || !AddIfNotFound) |
| 1870 | return Found; |
Jim Grosbach | dee9e8a | 2011-08-24 16:44:17 +0000 | [diff] [blame] | 1871 | |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1872 | addOperand(MachineOperand::CreateReg(Reg, |
Chris Lattner | fd68280 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 1873 | true /*IsDef*/, |
| 1874 | true /*IsImp*/, |
| 1875 | false /*IsKill*/, |
| 1876 | true /*IsDead*/)); |
| 1877 | return true; |
Owen Anderson | 2a8a485 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1878 | } |
Jakob Stoklund Olesen | 7725526 | 2010-01-06 00:29:28 +0000 | [diff] [blame] | 1879 | |
Matthias Braun | 26e7ea6 | 2015-02-04 19:35:16 +0000 | [diff] [blame] | 1880 | void MachineInstr::clearRegisterDeads(unsigned Reg) { |
| 1881 | for (MachineOperand &MO : operands()) { |
| 1882 | if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg) |
| 1883 | continue; |
| 1884 | MO.setIsDead(false); |
| 1885 | } |
| 1886 | } |
| 1887 | |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 1888 | void MachineInstr::addRegisterDefReadUndef(unsigned Reg) { |
| 1889 | for (MachineOperand &MO : operands()) { |
| 1890 | if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0) |
| 1891 | continue; |
| 1892 | MO.setIsUndef(); |
| 1893 | } |
| 1894 | } |
| 1895 | |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1896 | void MachineInstr::addRegisterDefined(unsigned Reg, |
Jakob Stoklund Olesen | 7725526 | 2010-01-06 00:29:28 +0000 | [diff] [blame] | 1897 | const TargetRegisterInfo *RegInfo) { |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1898 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1899 | MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo); |
Jakob Stoklund Olesen | 1f38010 | 2010-05-21 16:32:16 +0000 | [diff] [blame] | 1900 | if (MO) |
| 1901 | return; |
| 1902 | } else { |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1903 | for (const MachineOperand &MO : operands()) { |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1904 | if (MO.isReg() && MO.getReg() == Reg && MO.isDef() && |
Jakob Stoklund Olesen | 1f38010 | 2010-05-21 16:32:16 +0000 | [diff] [blame] | 1905 | MO.getSubReg() == 0) |
| 1906 | return; |
| 1907 | } |
| 1908 | } |
Matthias Braun | 1965bfa | 2013-10-10 21:28:38 +0000 | [diff] [blame] | 1909 | addOperand(MachineOperand::CreateReg(Reg, |
Jakob Stoklund Olesen | 1f38010 | 2010-05-21 16:32:16 +0000 | [diff] [blame] | 1910 | true /*IsDef*/, |
| 1911 | true /*IsImp*/)); |
Jakob Stoklund Olesen | 7725526 | 2010-01-06 00:29:28 +0000 | [diff] [blame] | 1912 | } |
Evan Cheng | 59d27fe | 2010-03-03 23:37:30 +0000 | [diff] [blame] | 1913 | |
Jakob Stoklund Olesen | 4290be4 | 2012-02-03 20:43:39 +0000 | [diff] [blame] | 1914 | void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs, |
Dan Gohman | 8693650 | 2010-06-18 23:28:01 +0000 | [diff] [blame] | 1915 | const TargetRegisterInfo &TRI) { |
Jakob Stoklund Olesen | 56fe2ed | 2012-02-03 21:23:14 +0000 | [diff] [blame] | 1916 | bool HasRegMask = false; |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1917 | for (MachineOperand &MO : operands()) { |
Jakob Stoklund Olesen | 56fe2ed | 2012-02-03 21:23:14 +0000 | [diff] [blame] | 1918 | if (MO.isRegMask()) { |
| 1919 | HasRegMask = true; |
| 1920 | continue; |
| 1921 | } |
Dan Gohman | 8693650 | 2010-06-18 23:28:01 +0000 | [diff] [blame] | 1922 | if (!MO.isReg() || !MO.isDef()) continue; |
| 1923 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | f650732 | 2012-02-03 20:43:35 +0000 | [diff] [blame] | 1924 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Dan Gohman | 8693650 | 2010-06-18 23:28:01 +0000 | [diff] [blame] | 1925 | // If there are no uses, including partial uses, the def is dead. |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1926 | if (std::none_of(UsedRegs.begin(), UsedRegs.end(), |
| 1927 | [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); })) |
| 1928 | MO.setIsDead(); |
Dan Gohman | 8693650 | 2010-06-18 23:28:01 +0000 | [diff] [blame] | 1929 | } |
Jakob Stoklund Olesen | 56fe2ed | 2012-02-03 21:23:14 +0000 | [diff] [blame] | 1930 | |
| 1931 | // This is a call with a register mask operand. |
| 1932 | // Mask clobbers are always dead, so add defs for the non-dead defines. |
| 1933 | if (HasRegMask) |
| 1934 | for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); |
| 1935 | I != E; ++I) |
| 1936 | addRegisterDefined(*I, &TRI); |
Dan Gohman | 8693650 | 2010-06-18 23:28:01 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Evan Cheng | 59d27fe | 2010-03-03 23:37:30 +0000 | [diff] [blame] | 1939 | unsigned |
| 1940 | MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { |
Chandler Carruth | 962152c | 2012-03-07 09:39:46 +0000 | [diff] [blame] | 1941 | // Build up a buffer of hash code components. |
Chandler Carruth | 962152c | 2012-03-07 09:39:46 +0000 | [diff] [blame] | 1942 | SmallVector<size_t, 8> HashComponents; |
| 1943 | HashComponents.reserve(MI->getNumOperands() + 1); |
| 1944 | HashComponents.push_back(MI->getOpcode()); |
Benjamin Kramer | 60c5bbf | 2015-02-21 17:08:08 +0000 | [diff] [blame] | 1945 | for (const MachineOperand &MO : MI->operands()) { |
Chandler Carruth | 264854f | 2012-07-05 11:06:22 +0000 | [diff] [blame] | 1946 | if (MO.isReg() && MO.isDef() && |
| 1947 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
| 1948 | continue; // Skip virtual register defs. |
| 1949 | |
| 1950 | HashComponents.push_back(hash_value(MO)); |
Evan Cheng | 59d27fe | 2010-03-03 23:37:30 +0000 | [diff] [blame] | 1951 | } |
Chandler Carruth | 962152c | 2012-03-07 09:39:46 +0000 | [diff] [blame] | 1952 | return hash_combine_range(HashComponents.begin(), HashComponents.end()); |
Evan Cheng | 59d27fe | 2010-03-03 23:37:30 +0000 | [diff] [blame] | 1953 | } |
Jakob Stoklund Olesen | 25a404e | 2011-07-02 03:53:34 +0000 | [diff] [blame] | 1954 | |
| 1955 | void MachineInstr::emitError(StringRef Msg) const { |
| 1956 | // Find the source location cookie. |
| 1957 | unsigned LocCookie = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1958 | const MDNode *LocMD = nullptr; |
Jakob Stoklund Olesen | 25a404e | 2011-07-02 03:53:34 +0000 | [diff] [blame] | 1959 | for (unsigned i = getNumOperands(); i != 0; --i) { |
| 1960 | if (getOperand(i-1).isMetadata() && |
| 1961 | (LocMD = getOperand(i-1).getMetadata()) && |
| 1962 | LocMD->getNumOperands() != 0) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1963 | if (const ConstantInt *CI = |
| 1964 | mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) { |
Jakob Stoklund Olesen | 25a404e | 2011-07-02 03:53:34 +0000 | [diff] [blame] | 1965 | LocCookie = CI->getZExtValue(); |
| 1966 | break; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | if (const MachineBasicBlock *MBB = getParent()) |
| 1972 | if (const MachineFunction *MF = MBB->getParent()) |
| 1973 | return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg); |
| 1974 | report_fatal_error(Msg); |
| 1975 | } |