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