blob: 43081ccb329b1372256d3720d2f01a310fad1624 [file] [log] [blame]
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001//===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00006//
7//===----------------------------------------------------------------------===//
8//
Francis Visoiu Mistrih3aa8eaa2017-11-28 19:23:39 +00009/// \file Methods common to all machine operands.
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/MachineOperand.h"
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +000014#include "llvm/ADT/StringExtras.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000015#include "llvm/Analysis/Loads.h"
Krzysztof Parzyszekcc3f6302018-08-20 20:37:57 +000016#include "llvm/Analysis/MemoryLocation.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000017#include "llvm/CodeGen/MIRPrinter.h"
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Francis Visoiu Mistrihb41dbbe2017-12-13 10:30:59 +000019#include "llvm/CodeGen/MachineJumpTableInfo.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +000021#include "llvm/CodeGen/TargetInstrInfo.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000022#include "llvm/CodeGen/TargetRegisterInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000023#include "llvm/Config/llvm-config.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000024#include "llvm/IR/Constants.h"
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +000025#include "llvm/IR/IRPrintingPasses.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000026#include "llvm/IR/ModuleSlotTracker.h"
Eric Christopherb6c190d2019-04-12 06:16:33 +000027#include "llvm/MC/MCDwarf.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000028#include "llvm/Target/TargetIntrinsicInfo.h"
29#include "llvm/Target/TargetMachine.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000030
31using namespace llvm;
32
33static cl::opt<int>
34 PrintRegMaskNumRegs("print-regmask-num-regs",
35 cl::desc("Number of registers to limit to when "
36 "printing regmask operands in IR dumps. "
37 "unlimited = -1"),
38 cl::init(32), cl::Hidden);
39
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000040static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
41 if (const MachineInstr *MI = MO.getParent())
42 if (const MachineBasicBlock *MBB = MI->getParent())
43 if (const MachineFunction *MF = MBB->getParent())
44 return MF;
45 return nullptr;
46}
47static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
48 return const_cast<MachineFunction *>(
49 getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
50}
51
Matt Arsenaultf4d31132019-08-06 03:59:31 +000052void MachineOperand::setReg(Register Reg) {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000053 if (getReg() == Reg)
54 return; // No change.
55
Geoff Berryf8bf2ec2018-02-23 18:25:08 +000056 // Clear the IsRenamable bit to keep it conservatively correct.
57 IsRenamable = false;
58
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000059 // Otherwise, we have to change the register. If this operand is embedded
60 // into a machine function, we need to update the old and new register's
61 // use/def lists.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000062 if (MachineFunction *MF = getMFIfAvailable(*this)) {
63 MachineRegisterInfo &MRI = MF->getRegInfo();
64 MRI.removeRegOperandFromUseList(this);
65 SmallContents.RegNo = Reg;
66 MRI.addRegOperandToUseList(this);
67 return;
Francis Visoiu Mistrihc2641d62017-12-06 11:57:53 +000068 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000069
70 // Otherwise, just change the register, no problem. :)
71 SmallContents.RegNo = Reg;
72}
73
Matt Arsenaultf4d31132019-08-06 03:59:31 +000074void MachineOperand::substVirtReg(Register Reg, unsigned SubIdx,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000075 const TargetRegisterInfo &TRI) {
Matt Arsenaultf4d31132019-08-06 03:59:31 +000076 assert(Reg.isVirtual());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000077 if (SubIdx && getSubReg())
78 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
79 setReg(Reg);
80 if (SubIdx)
81 setSubReg(SubIdx);
82}
83
Matt Arsenaultf4d31132019-08-06 03:59:31 +000084void MachineOperand::substPhysReg(MCRegister Reg, const TargetRegisterInfo &TRI) {
85 assert(Reg.isPhysical());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000086 if (getSubReg()) {
87 Reg = TRI.getSubReg(Reg, getSubReg());
88 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
89 // That won't happen in legal code.
90 setSubReg(0);
91 if (isDef())
92 setIsUndef(false);
93 }
94 setReg(Reg);
95}
96
97/// Change a def to a use, or a use to a def.
98void MachineOperand::setIsDef(bool Val) {
99 assert(isReg() && "Wrong MachineOperand accessor");
100 assert((!Val || !isDebug()) && "Marking a debug operation as def");
101 if (IsDef == Val)
102 return;
Geoff Berry60c43102017-12-12 17:53:59 +0000103 assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000104 // MRI may keep uses and defs in different list positions.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000105 if (MachineFunction *MF = getMFIfAvailable(*this)) {
106 MachineRegisterInfo &MRI = MF->getRegInfo();
107 MRI.removeRegOperandFromUseList(this);
108 IsDef = Val;
109 MRI.addRegOperandToUseList(this);
110 return;
111 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000112 IsDef = Val;
113}
114
Geoff Berry60c43102017-12-12 17:53:59 +0000115bool MachineOperand::isRenamable() const {
116 assert(isReg() && "Wrong MachineOperand accessor");
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000117 assert(Register::isPhysicalRegister(getReg()) &&
Geoff Berry60c43102017-12-12 17:53:59 +0000118 "isRenamable should only be checked on physical registers");
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000119 if (!IsRenamable)
120 return false;
121
122 const MachineInstr *MI = getParent();
123 if (!MI)
124 return true;
125
126 if (isDef())
127 return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle);
128
129 assert(isUse() && "Reg is not def or use");
130 return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle);
Geoff Berry60c43102017-12-12 17:53:59 +0000131}
132
133void MachineOperand::setIsRenamable(bool Val) {
134 assert(isReg() && "Wrong MachineOperand accessor");
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000135 assert(Register::isPhysicalRegister(getReg()) &&
Geoff Berry60c43102017-12-12 17:53:59 +0000136 "setIsRenamable should only be called on physical registers");
Geoff Berry60c43102017-12-12 17:53:59 +0000137 IsRenamable = Val;
138}
139
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000140// If this operand is currently a register operand, and if this is in a
141// function, deregister the operand from the register's use/def list.
142void MachineOperand::removeRegFromUses() {
143 if (!isReg() || !isOnRegUseList())
144 return;
145
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000146 if (MachineFunction *MF = getMFIfAvailable(*this))
147 MF->getRegInfo().removeRegOperandFromUseList(this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000148}
149
150/// ChangeToImmediate - Replace this operand with a new immediate operand of
151/// the specified value. If an operand is known to be an immediate already,
152/// the setImm method should be used.
153void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
154 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
155
156 removeRegFromUses();
157
158 OpKind = MO_Immediate;
159 Contents.ImmVal = ImmVal;
160}
161
162void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
163 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
164
165 removeRegFromUses();
166
167 OpKind = MO_FPImmediate;
168 Contents.CFP = FPImm;
169}
170
171void MachineOperand::ChangeToES(const char *SymName,
Peter Collingbourne33773d52019-07-31 20:14:09 +0000172 unsigned TargetFlags) {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000173 assert((!isReg() || !isTied()) &&
174 "Cannot change a tied operand into an external symbol");
175
176 removeRegFromUses();
177
178 OpKind = MO_ExternalSymbol;
179 Contents.OffsetedInfo.Val.SymbolName = SymName;
180 setOffset(0); // Offset is always 0.
181 setTargetFlags(TargetFlags);
182}
183
Nicolai Haehnlef672b612019-05-15 17:48:10 +0000184void MachineOperand::ChangeToGA(const GlobalValue *GV, int64_t Offset,
Peter Collingbourne33773d52019-07-31 20:14:09 +0000185 unsigned TargetFlags) {
Nicolai Haehnlef672b612019-05-15 17:48:10 +0000186 assert((!isReg() || !isTied()) &&
187 "Cannot change a tied operand into a global address");
188
189 removeRegFromUses();
190
191 OpKind = MO_GlobalAddress;
192 Contents.OffsetedInfo.Val.GV = GV;
193 setOffset(Offset);
194 setTargetFlags(TargetFlags);
195}
196
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000197void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
198 assert((!isReg() || !isTied()) &&
199 "Cannot change a tied operand into an MCSymbol");
200
201 removeRegFromUses();
202
203 OpKind = MO_MCSymbol;
204 Contents.Sym = Sym;
205}
206
207void MachineOperand::ChangeToFrameIndex(int Idx) {
208 assert((!isReg() || !isTied()) &&
209 "Cannot change a tied operand into a FrameIndex");
210
211 removeRegFromUses();
212
213 OpKind = MO_FrameIndex;
214 setIndex(Idx);
215}
216
217void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
Peter Collingbourne33773d52019-07-31 20:14:09 +0000218 unsigned TargetFlags) {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000219 assert((!isReg() || !isTied()) &&
220 "Cannot change a tied operand into a FrameIndex");
221
222 removeRegFromUses();
223
224 OpKind = MO_TargetIndex;
225 setIndex(Idx);
226 setOffset(Offset);
227 setTargetFlags(TargetFlags);
228}
229
230/// ChangeToRegister - Replace this operand with a new register operand of
231/// the specified value. If an operand is known to be an register already,
232/// the setReg method should be used.
Matt Arsenaultf4d31132019-08-06 03:59:31 +0000233void MachineOperand::ChangeToRegister(Register Reg, bool isDef, bool isImp,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000234 bool isKill, bool isDead, bool isUndef,
235 bool isDebug) {
236 MachineRegisterInfo *RegInfo = nullptr;
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000237 if (MachineFunction *MF = getMFIfAvailable(*this))
238 RegInfo = &MF->getRegInfo();
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000239 // If this operand is already a register operand, remove it from the
240 // register's use/def lists.
241 bool WasReg = isReg();
242 if (RegInfo && WasReg)
243 RegInfo->removeRegOperandFromUseList(this);
244
245 // Change this to a register and set the reg#.
Geoff Berry60c43102017-12-12 17:53:59 +0000246 assert(!(isDead && !isDef) && "Dead flag on non-def");
247 assert(!(isKill && isDef) && "Kill flag on def");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000248 OpKind = MO_Register;
249 SmallContents.RegNo = Reg;
250 SubReg_TargetFlags = 0;
251 IsDef = isDef;
252 IsImp = isImp;
Geoff Berry60c43102017-12-12 17:53:59 +0000253 IsDeadOrKill = isKill | isDead;
254 IsRenamable = false;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000255 IsUndef = isUndef;
256 IsInternalRead = false;
257 IsEarlyClobber = false;
258 IsDebug = isDebug;
259 // Ensure isOnRegUseList() returns false.
260 Contents.Reg.Prev = nullptr;
261 // Preserve the tie when the operand was already a register.
262 if (!WasReg)
263 TiedTo = 0;
264
265 // If this operand is embedded in a function, add the operand to the
266 // register's use/def list.
267 if (RegInfo)
268 RegInfo->addRegOperandToUseList(this);
269}
270
271/// isIdenticalTo - Return true if this operand is identical to the specified
272/// operand. Note that this should stay in sync with the hash_value overload
273/// below.
274bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
275 if (getType() != Other.getType() ||
276 getTargetFlags() != Other.getTargetFlags())
277 return false;
278
279 switch (getType()) {
280 case MachineOperand::MO_Register:
281 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
282 getSubReg() == Other.getSubReg();
283 case MachineOperand::MO_Immediate:
284 return getImm() == Other.getImm();
285 case MachineOperand::MO_CImmediate:
286 return getCImm() == Other.getCImm();
287 case MachineOperand::MO_FPImmediate:
288 return getFPImm() == Other.getFPImm();
289 case MachineOperand::MO_MachineBasicBlock:
290 return getMBB() == Other.getMBB();
291 case MachineOperand::MO_FrameIndex:
292 return getIndex() == Other.getIndex();
293 case MachineOperand::MO_ConstantPoolIndex:
294 case MachineOperand::MO_TargetIndex:
295 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
296 case MachineOperand::MO_JumpTableIndex:
297 return getIndex() == Other.getIndex();
298 case MachineOperand::MO_GlobalAddress:
299 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
300 case MachineOperand::MO_ExternalSymbol:
301 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
302 getOffset() == Other.getOffset();
303 case MachineOperand::MO_BlockAddress:
304 return getBlockAddress() == Other.getBlockAddress() &&
305 getOffset() == Other.getOffset();
306 case MachineOperand::MO_RegisterMask:
307 case MachineOperand::MO_RegisterLiveOut: {
308 // Shallow compare of the two RegMasks
309 const uint32_t *RegMask = getRegMask();
310 const uint32_t *OtherRegMask = Other.getRegMask();
311 if (RegMask == OtherRegMask)
312 return true;
313
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000314 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
315 // Calculate the size of the RegMask
316 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
317 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000318
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000319 // Deep compare of the two RegMasks
320 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
321 }
322 // We don't know the size of the RegMask, so we can't deep compare the two
323 // reg masks.
324 return false;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000325 }
326 case MachineOperand::MO_MCSymbol:
327 return getMCSymbol() == Other.getMCSymbol();
328 case MachineOperand::MO_CFIIndex:
329 return getCFIIndex() == Other.getCFIIndex();
330 case MachineOperand::MO_Metadata:
331 return getMetadata() == Other.getMetadata();
332 case MachineOperand::MO_IntrinsicID:
333 return getIntrinsicID() == Other.getIntrinsicID();
334 case MachineOperand::MO_Predicate:
335 return getPredicate() == Other.getPredicate();
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000336 case MachineOperand::MO_ShuffleMask:
337 return getShuffleMask() == Other.getShuffleMask();
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000338 }
339 llvm_unreachable("Invalid machine operand type");
340}
341
342// Note: this must stay exactly in sync with isIdenticalTo above.
343hash_code llvm::hash_value(const MachineOperand &MO) {
344 switch (MO.getType()) {
345 case MachineOperand::MO_Register:
346 // Register operands don't have target flags.
Matt Arsenaulte3a676e2019-06-24 15:50:29 +0000347 return hash_combine(MO.getType(), (unsigned)MO.getReg(), MO.getSubReg(), MO.isDef());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000348 case MachineOperand::MO_Immediate:
349 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
350 case MachineOperand::MO_CImmediate:
351 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
352 case MachineOperand::MO_FPImmediate:
353 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
354 case MachineOperand::MO_MachineBasicBlock:
355 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
356 case MachineOperand::MO_FrameIndex:
357 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
358 case MachineOperand::MO_ConstantPoolIndex:
359 case MachineOperand::MO_TargetIndex:
360 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
361 MO.getOffset());
362 case MachineOperand::MO_JumpTableIndex:
363 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
364 case MachineOperand::MO_ExternalSymbol:
365 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
Eli Friedmand8e87222019-06-01 00:08:54 +0000366 StringRef(MO.getSymbolName()));
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000367 case MachineOperand::MO_GlobalAddress:
368 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
369 MO.getOffset());
370 case MachineOperand::MO_BlockAddress:
371 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
372 MO.getOffset());
373 case MachineOperand::MO_RegisterMask:
374 case MachineOperand::MO_RegisterLiveOut:
375 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
376 case MachineOperand::MO_Metadata:
377 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
378 case MachineOperand::MO_MCSymbol:
379 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
380 case MachineOperand::MO_CFIIndex:
381 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
382 case MachineOperand::MO_IntrinsicID:
383 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
384 case MachineOperand::MO_Predicate:
385 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000386 case MachineOperand::MO_ShuffleMask:
387 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getShuffleMask());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000388 }
389 llvm_unreachable("Invalid machine operand type");
390}
391
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000392// Try to crawl up to the machine function and get TRI and IntrinsicInfo from
393// it.
394static void tryToGetTargetInfo(const MachineOperand &MO,
395 const TargetRegisterInfo *&TRI,
396 const TargetIntrinsicInfo *&IntrinsicInfo) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000397 if (const MachineFunction *MF = getMFIfAvailable(MO)) {
398 TRI = MF->getSubtarget().getRegisterInfo();
399 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000400 }
401}
402
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000403static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
404 const auto *TII = MF.getSubtarget().getInstrInfo();
405 assert(TII && "expected instruction info");
406 auto Indices = TII->getSerializableTargetIndices();
407 auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
408 return I.first == Index;
409 });
410 if (Found != Indices.end())
411 return Found->second;
412 return nullptr;
413}
414
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000415static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
416 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
417 for (const auto &I : Flags) {
418 if (I.first == TF) {
419 return I.second;
420 }
421 }
422 return nullptr;
423}
424
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000425static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
426 const TargetRegisterInfo *TRI) {
427 if (!TRI) {
428 OS << "%dwarfreg." << DwarfReg;
429 return;
430 }
431
432 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
433 if (Reg == -1) {
434 OS << "<badreg>";
435 return;
436 }
437 OS << printReg(Reg, TRI);
438}
439
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000440static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
441 ModuleSlotTracker &MST) {
442 OS << "%ir-block.";
443 if (BB.hasName()) {
444 printLLVMNameWithoutPrefix(OS, BB.getName());
445 return;
446 }
447 Optional<int> Slot;
448 if (const Function *F = BB.getParent()) {
449 if (F == MST.getCurrentFunction()) {
450 Slot = MST.getLocalSlot(&BB);
451 } else if (const Module *M = F->getParent()) {
452 ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
453 CustomMST.incorporateFunction(*F);
454 Slot = CustomMST.getLocalSlot(&BB);
455 }
456 }
457 if (Slot)
458 MachineOperand::printIRSlotNumber(OS, *Slot);
459 else
460 OS << "<unknown>";
461}
462
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000463static void printIRValueReference(raw_ostream &OS, const Value &V,
464 ModuleSlotTracker &MST) {
465 if (isa<GlobalValue>(V)) {
466 V.printAsOperand(OS, /*PrintType=*/false, MST);
467 return;
468 }
469 if (isa<Constant>(V)) {
470 // Machine memory operands can load/store to/from constant value pointers.
471 OS << '`';
472 V.printAsOperand(OS, /*PrintType=*/true, MST);
473 OS << '`';
474 return;
475 }
476 OS << "%ir.";
477 if (V.hasName()) {
478 printLLVMNameWithoutPrefix(OS, V.getName());
479 return;
480 }
Jonas Paulssonf213f812018-10-25 23:39:07 +0000481 int Slot = MST.getCurrentFunction() ? MST.getLocalSlot(&V) : -1;
482 MachineOperand::printIRSlotNumber(OS, Slot);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000483}
484
485static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
486 SyncScope::ID SSID,
487 SmallVectorImpl<StringRef> &SSNs) {
488 switch (SSID) {
489 case SyncScope::System:
490 break;
491 default:
492 if (SSNs.empty())
493 Context.getSyncScopeNames(SSNs);
494
495 OS << "syncscope(\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +0000496 printEscapedString(SSNs[SSID], OS);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000497 OS << "\") ";
498 break;
499 }
500}
501
502static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
503 unsigned TMMOFlag) {
504 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
505 for (const auto &I : Flags) {
506 if (I.first == TMMOFlag) {
507 return I.second;
508 }
509 }
510 return nullptr;
511}
512
513static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
514 const MachineFrameInfo *MFI) {
515 StringRef Name;
516 if (MFI) {
517 IsFixed = MFI->isFixedObjectIndex(FrameIndex);
518 if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
519 if (Alloca->hasName())
520 Name = Alloca->getName();
521 if (IsFixed)
522 FrameIndex -= MFI->getObjectIndexBegin();
523 }
524 MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
525}
526
Francis Visoiu Mistrihecd0b832018-01-16 10:53:11 +0000527void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +0000528 const TargetRegisterInfo *TRI) {
529 OS << "%subreg.";
530 if (TRI)
531 OS << TRI->getSubRegIndexName(Index);
532 else
533 OS << Index;
534}
535
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000536void MachineOperand::printTargetFlags(raw_ostream &OS,
537 const MachineOperand &Op) {
538 if (!Op.getTargetFlags())
539 return;
540 const MachineFunction *MF = getMFIfAvailable(Op);
541 if (!MF)
542 return;
543
544 const auto *TII = MF->getSubtarget().getInstrInfo();
545 assert(TII && "expected instruction info");
546 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
547 OS << "target-flags(";
548 const bool HasDirectFlags = Flags.first;
549 const bool HasBitmaskFlags = Flags.second;
550 if (!HasDirectFlags && !HasBitmaskFlags) {
551 OS << "<unknown>) ";
552 return;
553 }
554 if (HasDirectFlags) {
555 if (const auto *Name = getTargetFlagName(TII, Flags.first))
556 OS << Name;
557 else
558 OS << "<unknown target flag>";
559 }
560 if (!HasBitmaskFlags) {
561 OS << ") ";
562 return;
563 }
564 bool IsCommaNeeded = HasDirectFlags;
565 unsigned BitMask = Flags.second;
566 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
567 for (const auto &Mask : BitMasks) {
568 // Check if the flag's bitmask has the bits of the current mask set.
569 if ((BitMask & Mask.first) == Mask.first) {
570 if (IsCommaNeeded)
571 OS << ", ";
572 IsCommaNeeded = true;
573 OS << Mask.second;
574 // Clear the bits which were serialized from the flag's bitmask.
575 BitMask &= ~(Mask.first);
576 }
577 }
578 if (BitMask) {
579 // When the resulting flag's bitmask isn't zero, we know that we didn't
580 // serialize all of the bit flags.
581 if (IsCommaNeeded)
582 OS << ", ";
583 OS << "<unknown bitmask target flag>";
584 }
585 OS << ") ";
586}
587
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000588void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
589 OS << "<mcsymbol " << Sym << ">";
590}
591
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000592void MachineOperand::printStackObjectReference(raw_ostream &OS,
593 unsigned FrameIndex,
594 bool IsFixed, StringRef Name) {
595 if (IsFixed) {
596 OS << "%fixed-stack." << FrameIndex;
597 return;
598 }
599
600 OS << "%stack." << FrameIndex;
601 if (!Name.empty())
602 OS << '.' << Name;
603}
604
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000605void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
606 if (Offset == 0)
607 return;
608 if (Offset < 0) {
609 OS << " - " << -Offset;
610 return;
611 }
612 OS << " + " << Offset;
613}
614
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000615void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
616 if (Slot == -1)
617 OS << "<badref>";
618 else
619 OS << Slot;
620}
621
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000622static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
623 const TargetRegisterInfo *TRI) {
624 switch (CFI.getOperation()) {
625 case MCCFIInstruction::OpSameValue:
626 OS << "same_value ";
627 if (MCSymbol *Label = CFI.getLabel())
628 MachineOperand::printSymbol(OS, *Label);
629 printCFIRegister(CFI.getRegister(), OS, TRI);
630 break;
631 case MCCFIInstruction::OpRememberState:
632 OS << "remember_state ";
633 if (MCSymbol *Label = CFI.getLabel())
634 MachineOperand::printSymbol(OS, *Label);
635 break;
636 case MCCFIInstruction::OpRestoreState:
637 OS << "restore_state ";
638 if (MCSymbol *Label = CFI.getLabel())
639 MachineOperand::printSymbol(OS, *Label);
640 break;
641 case MCCFIInstruction::OpOffset:
642 OS << "offset ";
643 if (MCSymbol *Label = CFI.getLabel())
644 MachineOperand::printSymbol(OS, *Label);
645 printCFIRegister(CFI.getRegister(), OS, TRI);
646 OS << ", " << CFI.getOffset();
647 break;
648 case MCCFIInstruction::OpDefCfaRegister:
649 OS << "def_cfa_register ";
650 if (MCSymbol *Label = CFI.getLabel())
651 MachineOperand::printSymbol(OS, *Label);
652 printCFIRegister(CFI.getRegister(), OS, TRI);
653 break;
654 case MCCFIInstruction::OpDefCfaOffset:
655 OS << "def_cfa_offset ";
656 if (MCSymbol *Label = CFI.getLabel())
657 MachineOperand::printSymbol(OS, *Label);
658 OS << CFI.getOffset();
659 break;
660 case MCCFIInstruction::OpDefCfa:
661 OS << "def_cfa ";
662 if (MCSymbol *Label = CFI.getLabel())
663 MachineOperand::printSymbol(OS, *Label);
664 printCFIRegister(CFI.getRegister(), OS, TRI);
665 OS << ", " << CFI.getOffset();
666 break;
667 case MCCFIInstruction::OpRelOffset:
668 OS << "rel_offset ";
669 if (MCSymbol *Label = CFI.getLabel())
670 MachineOperand::printSymbol(OS, *Label);
671 printCFIRegister(CFI.getRegister(), OS, TRI);
672 OS << ", " << CFI.getOffset();
673 break;
674 case MCCFIInstruction::OpAdjustCfaOffset:
675 OS << "adjust_cfa_offset ";
676 if (MCSymbol *Label = CFI.getLabel())
677 MachineOperand::printSymbol(OS, *Label);
678 OS << CFI.getOffset();
679 break;
680 case MCCFIInstruction::OpRestore:
681 OS << "restore ";
682 if (MCSymbol *Label = CFI.getLabel())
683 MachineOperand::printSymbol(OS, *Label);
684 printCFIRegister(CFI.getRegister(), OS, TRI);
685 break;
686 case MCCFIInstruction::OpEscape: {
687 OS << "escape ";
688 if (MCSymbol *Label = CFI.getLabel())
689 MachineOperand::printSymbol(OS, *Label);
690 if (!CFI.getValues().empty()) {
691 size_t e = CFI.getValues().size() - 1;
692 for (size_t i = 0; i < e; ++i)
693 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
694 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
695 }
696 break;
697 }
698 case MCCFIInstruction::OpUndefined:
699 OS << "undefined ";
700 if (MCSymbol *Label = CFI.getLabel())
701 MachineOperand::printSymbol(OS, *Label);
702 printCFIRegister(CFI.getRegister(), OS, TRI);
703 break;
704 case MCCFIInstruction::OpRegister:
705 OS << "register ";
706 if (MCSymbol *Label = CFI.getLabel())
707 MachineOperand::printSymbol(OS, *Label);
708 printCFIRegister(CFI.getRegister(), OS, TRI);
709 OS << ", ";
710 printCFIRegister(CFI.getRegister2(), OS, TRI);
711 break;
712 case MCCFIInstruction::OpWindowSave:
713 OS << "window_save ";
714 if (MCSymbol *Label = CFI.getLabel())
715 MachineOperand::printSymbol(OS, *Label);
716 break;
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000717 case MCCFIInstruction::OpNegateRAState:
718 OS << "negate_ra_sign_state ";
719 if (MCSymbol *Label = CFI.getLabel())
720 MachineOperand::printSymbol(OS, *Label);
721 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000722 default:
723 // TODO: Print the other CFI Operations.
724 OS << "<unserializable cfi directive>";
725 break;
726 }
727}
728
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000729void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
730 const TargetIntrinsicInfo *IntrinsicInfo) const {
Roman Tereshinf487eda2018-05-07 22:31:12 +0000731 print(OS, LLT{}, TRI, IntrinsicInfo);
732}
733
734void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint,
735 const TargetRegisterInfo *TRI,
736 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000737 tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000738 ModuleSlotTracker DummyMST(nullptr);
Roman Tereshinf487eda2018-05-07 22:31:12 +0000739 print(OS, DummyMST, TypeToPrint, /*PrintDef=*/false, /*IsStandalone=*/true,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000740 /*ShouldPrintRegisterTies=*/true,
741 /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000742}
743
744void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000745 LLT TypeToPrint, bool PrintDef, bool IsStandalone,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000746 bool ShouldPrintRegisterTies,
747 unsigned TiedOperandIdx,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000748 const TargetRegisterInfo *TRI,
749 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000750 printTargetFlags(OS, *this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000751 switch (getType()) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000752 case MachineOperand::MO_Register: {
753 unsigned Reg = getReg();
754 if (isImplicit())
755 OS << (isDef() ? "implicit-def " : "implicit ");
756 else if (PrintDef && isDef())
757 // Print the 'def' flag only when the operand is defined after '='.
758 OS << "def ";
759 if (isInternalRead())
760 OS << "internal ";
761 if (isDead())
762 OS << "dead ";
763 if (isKill())
764 OS << "killed ";
765 if (isUndef())
766 OS << "undef ";
767 if (isEarlyClobber())
768 OS << "early-clobber ";
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000769 if (Register::isPhysicalRegister(getReg()) && isRenamable())
Geoff Berry60c43102017-12-12 17:53:59 +0000770 OS << "renamable ";
Matthias Brauna8340382018-10-30 23:28:27 +0000771 // isDebug() is exactly true for register operands of a DBG_VALUE. So we
772 // simply infer it when parsing and do not need to print it.
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000773
774 const MachineRegisterInfo *MRI = nullptr;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000775 if (Register::isVirtualRegister(Reg)) {
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000776 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
777 MRI = &MF->getRegInfo();
778 }
779 }
780
781 OS << printReg(Reg, TRI, 0, MRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000782 // Print the sub register.
783 if (unsigned SubReg = getSubReg()) {
784 if (TRI)
785 OS << '.' << TRI->getSubRegIndexName(SubReg);
786 else
787 OS << ".subreg" << SubReg;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000788 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000789 // Print the register class / bank.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000790 if (Register::isVirtualRegister(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000791 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
792 const MachineRegisterInfo &MRI = MF->getRegInfo();
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000793 if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000794 OS << ':';
795 OS << printRegClassOrBank(Reg, MRI, TRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000796 }
797 }
798 }
799 // Print ties.
800 if (ShouldPrintRegisterTies && isTied() && !isDef())
801 OS << "(tied-def " << TiedOperandIdx << ")";
802 // Print types.
803 if (TypeToPrint.isValid())
804 OS << '(' << TypeToPrint << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000805 break;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000806 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000807 case MachineOperand::MO_Immediate:
808 OS << getImm();
809 break;
810 case MachineOperand::MO_CImmediate:
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +0000811 getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000812 break;
813 case MachineOperand::MO_FPImmediate:
Francis Visoiu Mistrih3b265c82017-12-19 21:47:00 +0000814 getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000815 break;
816 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000817 OS << printMBBReference(*getMBB());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000818 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000819 case MachineOperand::MO_FrameIndex: {
820 int FrameIndex = getIndex();
821 bool IsFixed = false;
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000822 const MachineFrameInfo *MFI = nullptr;
823 if (const MachineFunction *MF = getMFIfAvailable(*this))
824 MFI = &MF->getFrameInfo();
825 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000826 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000827 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000828 case MachineOperand::MO_ConstantPoolIndex:
Francis Visoiu Mistrih26ae8a62017-12-13 10:30:45 +0000829 OS << "%const." << getIndex();
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000830 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000831 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000832 case MachineOperand::MO_TargetIndex: {
833 OS << "target-index(";
834 const char *Name = "<unknown>";
835 if (const MachineFunction *MF = getMFIfAvailable(*this))
836 if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
837 Name = TargetIndexName;
838 OS << Name << ')';
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000839 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000840 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000841 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000842 case MachineOperand::MO_JumpTableIndex:
Francis Visoiu Mistrihb41dbbe2017-12-13 10:30:59 +0000843 OS << printJumpTableEntryReference(getIndex());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000844 break;
845 case MachineOperand::MO_GlobalAddress:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000846 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000847 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000848 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000849 case MachineOperand::MO_ExternalSymbol: {
850 StringRef Name = getSymbolName();
Puyan Lotfife6c9cb2018-01-10 00:56:48 +0000851 OS << '&';
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000852 if (Name.empty()) {
853 OS << "\"\"";
854 } else {
855 printLLVMNameWithoutPrefix(OS, Name);
856 }
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000857 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000858 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000859 }
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000860 case MachineOperand::MO_BlockAddress: {
861 OS << "blockaddress(";
862 getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
863 MST);
864 OS << ", ";
865 printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
866 OS << ')';
867 MachineOperand::printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000868 break;
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000869 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000870 case MachineOperand::MO_RegisterMask: {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000871 OS << "<regmask";
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000872 if (TRI) {
873 unsigned NumRegsInMask = 0;
874 unsigned NumRegsEmitted = 0;
875 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
876 unsigned MaskWord = i / 32;
877 unsigned MaskBit = i % 32;
878 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
879 if (PrintRegMaskNumRegs < 0 ||
880 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
881 OS << " " << printReg(i, TRI);
882 NumRegsEmitted++;
883 }
884 NumRegsInMask++;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000885 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000886 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000887 if (NumRegsEmitted != NumRegsInMask)
888 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
889 } else {
890 OS << " ...";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000891 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000892 OS << ">";
893 break;
894 }
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000895 case MachineOperand::MO_RegisterLiveOut: {
896 const uint32_t *RegMask = getRegLiveOut();
897 OS << "liveout(";
898 if (!TRI) {
899 OS << "<unknown>";
900 } else {
901 bool IsCommaNeeded = false;
902 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
903 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
904 if (IsCommaNeeded)
905 OS << ", ";
906 OS << printReg(Reg, TRI);
907 IsCommaNeeded = true;
908 }
909 }
910 }
911 OS << ")";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000912 break;
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000913 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000914 case MachineOperand::MO_Metadata:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000915 getMetadata()->printAsOperand(OS, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000916 break;
917 case MachineOperand::MO_MCSymbol:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000918 printSymbol(OS, *getMCSymbol());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000919 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000920 case MachineOperand::MO_CFIIndex: {
921 if (const MachineFunction *MF = getMFIfAvailable(*this))
922 printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
923 else
924 OS << "<cfi directive>";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000925 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000926 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000927 case MachineOperand::MO_IntrinsicID: {
928 Intrinsic::ID ID = getIntrinsicID();
929 if (ID < Intrinsic::num_intrinsics)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000930 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000931 else if (IntrinsicInfo)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000932 OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000933 else
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000934 OS << "intrinsic(" << ID << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000935 break;
936 }
937 case MachineOperand::MO_Predicate: {
938 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
Francis Visoiu Mistrihcb2683d2017-12-19 21:47:10 +0000939 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
940 << CmpInst::getPredicateName(Pred) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000941 break;
942 }
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000943 case MachineOperand::MO_ShuffleMask:
944 OS << "shufflemask(";
945 const Constant* C = getShuffleMask();
946 const int NumElts = C->getType()->getVectorNumElements();
947
948 StringRef Separator;
949 for (int I = 0; I != NumElts; ++I) {
950 OS << Separator;
951 C->getAggregateElement(I)->printAsOperand(OS, false, MST);
952 Separator = ", ";
953 }
954
955 OS << ')';
956 break;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000957 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000958}
959
960#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
961LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
962#endif
963
964//===----------------------------------------------------------------------===//
965// MachineMemOperand Implementation
966//===----------------------------------------------------------------------===//
967
968/// getAddrSpace - Return the LLVM IR address space number that this pointer
969/// points into.
Yaxun Liu49477042017-12-02 22:13:22 +0000970unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000971
972/// isDereferenceable - Return true if V is always dereferenceable for
973/// Offset + Size byte.
974bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
975 const DataLayout &DL) const {
976 if (!V.is<const Value *>())
977 return false;
978
979 const Value *BasePtr = V.get<const Value *>();
980 if (BasePtr == nullptr)
981 return false;
982
983 return isDereferenceableAndAlignedPointer(
984 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
985}
986
987/// getConstantPool - Return a MachinePointerInfo record that refers to the
988/// constant pool.
989MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
990 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
991}
992
993/// getFixedStack - Return a MachinePointerInfo record that refers to the
994/// the specified FrameIndex.
995MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
996 int FI, int64_t Offset) {
997 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
998}
999
1000MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
1001 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
1002}
1003
1004MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
1005 return MachinePointerInfo(MF.getPSVManager().getGOT());
1006}
1007
1008MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
1009 int64_t Offset, uint8_t ID) {
1010 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
1011}
1012
Yaxun Liu49477042017-12-02 22:13:22 +00001013MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
1014 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
1015}
1016
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001017MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Daniel Sandersc01efe62018-04-09 18:42:19 +00001018 uint64_t s, uint64_t a,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001019 const AAMDNodes &AAInfo,
1020 const MDNode *Ranges, SyncScope::ID SSID,
1021 AtomicOrdering Ordering,
1022 AtomicOrdering FailureOrdering)
1023 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
1024 AAInfo(AAInfo), Ranges(Ranges) {
1025 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
1026 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
1027 "invalid pointer value");
Matt Arsenault2a645982019-01-31 01:38:47 +00001028 assert(getBaseAlignment() == a && a != 0 && "Alignment is not a power of 2!");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001029 assert((isLoad() || isStore()) && "Not a load/store!");
1030
1031 AtomicInfo.SSID = static_cast<unsigned>(SSID);
1032 assert(getSyncScopeID() == SSID && "Value truncated");
1033 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
1034 assert(getOrdering() == Ordering && "Value truncated");
1035 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
1036 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
1037}
1038
1039/// Profile - Gather unique data for the object.
1040///
1041void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
1042 ID.AddInteger(getOffset());
1043 ID.AddInteger(Size);
1044 ID.AddPointer(getOpaqueValue());
1045 ID.AddInteger(getFlags());
1046 ID.AddInteger(getBaseAlignment());
1047}
1048
1049void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
1050 // The Value and Offset may differ due to CSE. But the flags and size
1051 // should be the same.
1052 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
1053 assert(MMO->getSize() == getSize() && "Size mismatch!");
1054
1055 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
1056 // Update the alignment value.
1057 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
1058 // Also update the base and offset, because the new alignment may
1059 // not be applicable with the old ones.
1060 PtrInfo = MMO->PtrInfo;
1061 }
1062}
1063
1064/// getAlignment - Return the minimum known alignment in bytes of the
1065/// actual memory reference.
1066uint64_t MachineMemOperand::getAlignment() const {
1067 return MinAlign(getBaseAlignment(), getOffset());
1068}
1069
1070void MachineMemOperand::print(raw_ostream &OS) const {
1071 ModuleSlotTracker DummyMST(nullptr);
1072 print(OS, DummyMST);
1073}
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001074
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001075void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001076 SmallVector<StringRef, 0> SSNs;
1077 LLVMContext Ctx;
1078 print(OS, MST, SSNs, Ctx, nullptr, nullptr);
1079}
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001080
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001081void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
1082 SmallVectorImpl<StringRef> &SSNs,
1083 const LLVMContext &Context,
1084 const MachineFrameInfo *MFI,
1085 const TargetInstrInfo *TII) const {
1086 OS << '(';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001087 if (isVolatile())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001088 OS << "volatile ";
1089 if (isNonTemporal())
1090 OS << "non-temporal ";
1091 if (isDereferenceable())
1092 OS << "dereferenceable ";
1093 if (isInvariant())
1094 OS << "invariant ";
1095 if (getFlags() & MachineMemOperand::MOTargetFlag1)
1096 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
1097 << "\" ";
1098 if (getFlags() & MachineMemOperand::MOTargetFlag2)
1099 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
1100 << "\" ";
1101 if (getFlags() & MachineMemOperand::MOTargetFlag3)
1102 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
1103 << "\" ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001104
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001105 assert((isLoad() || isStore()) &&
1106 "machine memory operand must be a load or store (or both)");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001107 if (isLoad())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001108 OS << "load ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001109 if (isStore())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001110 OS << "store ";
1111
1112 printSyncScope(OS, Context, getSyncScopeID(), SSNs);
1113
1114 if (getOrdering() != AtomicOrdering::NotAtomic)
1115 OS << toIRString(getOrdering()) << ' ';
1116 if (getFailureOrdering() != AtomicOrdering::NotAtomic)
1117 OS << toIRString(getFailureOrdering()) << ' ';
1118
Krzysztof Parzyszekcc3f6302018-08-20 20:37:57 +00001119 if (getSize() == MemoryLocation::UnknownSize)
1120 OS << "unknown-size";
1121 else
1122 OS << getSize();
1123
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001124 if (const Value *Val = getValue()) {
1125 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1126 printIRValueReference(OS, *Val, MST);
1127 } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
1128 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1129 assert(PVal && "Expected a pseudo source value");
1130 switch (PVal->kind()) {
1131 case PseudoSourceValue::Stack:
1132 OS << "stack";
1133 break;
1134 case PseudoSourceValue::GOT:
1135 OS << "got";
1136 break;
1137 case PseudoSourceValue::JumpTable:
1138 OS << "jump-table";
1139 break;
1140 case PseudoSourceValue::ConstantPool:
1141 OS << "constant-pool";
1142 break;
1143 case PseudoSourceValue::FixedStack: {
1144 int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1145 bool IsFixed = true;
1146 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
1147 break;
1148 }
1149 case PseudoSourceValue::GlobalValueCallEntry:
1150 OS << "call-entry ";
1151 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1152 OS, /*PrintType=*/false, MST);
1153 break;
1154 case PseudoSourceValue::ExternalSymbolCallEntry:
1155 OS << "call-entry &";
1156 printLLVMNameWithoutPrefix(
1157 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
1158 break;
Matt Arsenault3062e872019-06-14 13:26:34 +00001159 default:
Tim Renouf4db09602018-03-27 21:14:04 +00001160 // FIXME: This is not necessarily the correct MIR serialization format for
1161 // a custom pseudo source value, but at least it allows
1162 // -print-machineinstrs to work on a target with custom pseudo source
1163 // values.
1164 OS << "custom ";
1165 PVal->printCustom(OS);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001166 break;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001167 }
1168 }
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001169 MachineOperand::printOperandOffset(OS, getOffset());
1170 if (getBaseAlignment() != getSize())
1171 OS << ", align " << getBaseAlignment();
1172 auto AAInfo = getAAInfo();
1173 if (AAInfo.TBAA) {
1174 OS << ", !tbaa ";
1175 AAInfo.TBAA->printAsOperand(OS, MST);
1176 }
1177 if (AAInfo.Scope) {
1178 OS << ", !alias.scope ";
1179 AAInfo.Scope->printAsOperand(OS, MST);
1180 }
1181 if (AAInfo.NoAlias) {
1182 OS << ", !noalias ";
1183 AAInfo.NoAlias->printAsOperand(OS, MST);
1184 }
1185 if (getRanges()) {
1186 OS << ", !range ";
1187 getRanges()->printAsOperand(OS, MST);
1188 }
1189 // FIXME: Implement addrspace printing/parsing in MIR.
1190 // For now, print this even though parsing it is not available in MIR.
1191 if (unsigned AS = getAddrSpace())
1192 OS << ", addrspace " << AS;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001193
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001194 OS << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001195}