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