blob: 8eccfb85a9461869fcd808a7294f0e1fb5c1ed60 [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
Pavel Labathaaff1a62019-09-24 09:31:02 +0000432 if (Optional<unsigned> Reg = TRI->getLLVMRegNum(DwarfReg, true))
433 OS << printReg(*Reg, TRI);
434 else
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000435 OS << "<badreg>";
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000436}
437
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000438static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
439 ModuleSlotTracker &MST) {
440 OS << "%ir-block.";
441 if (BB.hasName()) {
442 printLLVMNameWithoutPrefix(OS, BB.getName());
443 return;
444 }
445 Optional<int> Slot;
446 if (const Function *F = BB.getParent()) {
447 if (F == MST.getCurrentFunction()) {
448 Slot = MST.getLocalSlot(&BB);
449 } else if (const Module *M = F->getParent()) {
450 ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
451 CustomMST.incorporateFunction(*F);
452 Slot = CustomMST.getLocalSlot(&BB);
453 }
454 }
455 if (Slot)
456 MachineOperand::printIRSlotNumber(OS, *Slot);
457 else
458 OS << "<unknown>";
459}
460
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000461static void printIRValueReference(raw_ostream &OS, const Value &V,
462 ModuleSlotTracker &MST) {
463 if (isa<GlobalValue>(V)) {
464 V.printAsOperand(OS, /*PrintType=*/false, MST);
465 return;
466 }
467 if (isa<Constant>(V)) {
468 // Machine memory operands can load/store to/from constant value pointers.
469 OS << '`';
470 V.printAsOperand(OS, /*PrintType=*/true, MST);
471 OS << '`';
472 return;
473 }
474 OS << "%ir.";
475 if (V.hasName()) {
476 printLLVMNameWithoutPrefix(OS, V.getName());
477 return;
478 }
Jonas Paulssonf213f812018-10-25 23:39:07 +0000479 int Slot = MST.getCurrentFunction() ? MST.getLocalSlot(&V) : -1;
480 MachineOperand::printIRSlotNumber(OS, Slot);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000481}
482
483static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
484 SyncScope::ID SSID,
485 SmallVectorImpl<StringRef> &SSNs) {
486 switch (SSID) {
487 case SyncScope::System:
488 break;
489 default:
490 if (SSNs.empty())
491 Context.getSyncScopeNames(SSNs);
492
493 OS << "syncscope(\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +0000494 printEscapedString(SSNs[SSID], OS);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000495 OS << "\") ";
496 break;
497 }
498}
499
500static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
501 unsigned TMMOFlag) {
502 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
503 for (const auto &I : Flags) {
504 if (I.first == TMMOFlag) {
505 return I.second;
506 }
507 }
508 return nullptr;
509}
510
511static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
512 const MachineFrameInfo *MFI) {
513 StringRef Name;
514 if (MFI) {
515 IsFixed = MFI->isFixedObjectIndex(FrameIndex);
516 if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
517 if (Alloca->hasName())
518 Name = Alloca->getName();
519 if (IsFixed)
520 FrameIndex -= MFI->getObjectIndexBegin();
521 }
522 MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
523}
524
Francis Visoiu Mistrihecd0b832018-01-16 10:53:11 +0000525void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +0000526 const TargetRegisterInfo *TRI) {
527 OS << "%subreg.";
528 if (TRI)
529 OS << TRI->getSubRegIndexName(Index);
530 else
531 OS << Index;
532}
533
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000534void MachineOperand::printTargetFlags(raw_ostream &OS,
535 const MachineOperand &Op) {
536 if (!Op.getTargetFlags())
537 return;
538 const MachineFunction *MF = getMFIfAvailable(Op);
539 if (!MF)
540 return;
541
542 const auto *TII = MF->getSubtarget().getInstrInfo();
543 assert(TII && "expected instruction info");
544 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
545 OS << "target-flags(";
546 const bool HasDirectFlags = Flags.first;
547 const bool HasBitmaskFlags = Flags.second;
548 if (!HasDirectFlags && !HasBitmaskFlags) {
549 OS << "<unknown>) ";
550 return;
551 }
552 if (HasDirectFlags) {
553 if (const auto *Name = getTargetFlagName(TII, Flags.first))
554 OS << Name;
555 else
556 OS << "<unknown target flag>";
557 }
558 if (!HasBitmaskFlags) {
559 OS << ") ";
560 return;
561 }
562 bool IsCommaNeeded = HasDirectFlags;
563 unsigned BitMask = Flags.second;
564 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
565 for (const auto &Mask : BitMasks) {
566 // Check if the flag's bitmask has the bits of the current mask set.
567 if ((BitMask & Mask.first) == Mask.first) {
568 if (IsCommaNeeded)
569 OS << ", ";
570 IsCommaNeeded = true;
571 OS << Mask.second;
572 // Clear the bits which were serialized from the flag's bitmask.
573 BitMask &= ~(Mask.first);
574 }
575 }
576 if (BitMask) {
577 // When the resulting flag's bitmask isn't zero, we know that we didn't
578 // serialize all of the bit flags.
579 if (IsCommaNeeded)
580 OS << ", ";
581 OS << "<unknown bitmask target flag>";
582 }
583 OS << ") ";
584}
585
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000586void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
587 OS << "<mcsymbol " << Sym << ">";
588}
589
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000590void MachineOperand::printStackObjectReference(raw_ostream &OS,
591 unsigned FrameIndex,
592 bool IsFixed, StringRef Name) {
593 if (IsFixed) {
594 OS << "%fixed-stack." << FrameIndex;
595 return;
596 }
597
598 OS << "%stack." << FrameIndex;
599 if (!Name.empty())
600 OS << '.' << Name;
601}
602
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000603void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
604 if (Offset == 0)
605 return;
606 if (Offset < 0) {
607 OS << " - " << -Offset;
608 return;
609 }
610 OS << " + " << Offset;
611}
612
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000613void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
614 if (Slot == -1)
615 OS << "<badref>";
616 else
617 OS << Slot;
618}
619
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000620static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
621 const TargetRegisterInfo *TRI) {
622 switch (CFI.getOperation()) {
623 case MCCFIInstruction::OpSameValue:
624 OS << "same_value ";
625 if (MCSymbol *Label = CFI.getLabel())
626 MachineOperand::printSymbol(OS, *Label);
627 printCFIRegister(CFI.getRegister(), OS, TRI);
628 break;
629 case MCCFIInstruction::OpRememberState:
630 OS << "remember_state ";
631 if (MCSymbol *Label = CFI.getLabel())
632 MachineOperand::printSymbol(OS, *Label);
633 break;
634 case MCCFIInstruction::OpRestoreState:
635 OS << "restore_state ";
636 if (MCSymbol *Label = CFI.getLabel())
637 MachineOperand::printSymbol(OS, *Label);
638 break;
639 case MCCFIInstruction::OpOffset:
640 OS << "offset ";
641 if (MCSymbol *Label = CFI.getLabel())
642 MachineOperand::printSymbol(OS, *Label);
643 printCFIRegister(CFI.getRegister(), OS, TRI);
644 OS << ", " << CFI.getOffset();
645 break;
646 case MCCFIInstruction::OpDefCfaRegister:
647 OS << "def_cfa_register ";
648 if (MCSymbol *Label = CFI.getLabel())
649 MachineOperand::printSymbol(OS, *Label);
650 printCFIRegister(CFI.getRegister(), OS, TRI);
651 break;
652 case MCCFIInstruction::OpDefCfaOffset:
653 OS << "def_cfa_offset ";
654 if (MCSymbol *Label = CFI.getLabel())
655 MachineOperand::printSymbol(OS, *Label);
656 OS << CFI.getOffset();
657 break;
658 case MCCFIInstruction::OpDefCfa:
659 OS << "def_cfa ";
660 if (MCSymbol *Label = CFI.getLabel())
661 MachineOperand::printSymbol(OS, *Label);
662 printCFIRegister(CFI.getRegister(), OS, TRI);
663 OS << ", " << CFI.getOffset();
664 break;
665 case MCCFIInstruction::OpRelOffset:
666 OS << "rel_offset ";
667 if (MCSymbol *Label = CFI.getLabel())
668 MachineOperand::printSymbol(OS, *Label);
669 printCFIRegister(CFI.getRegister(), OS, TRI);
670 OS << ", " << CFI.getOffset();
671 break;
672 case MCCFIInstruction::OpAdjustCfaOffset:
673 OS << "adjust_cfa_offset ";
674 if (MCSymbol *Label = CFI.getLabel())
675 MachineOperand::printSymbol(OS, *Label);
676 OS << CFI.getOffset();
677 break;
678 case MCCFIInstruction::OpRestore:
679 OS << "restore ";
680 if (MCSymbol *Label = CFI.getLabel())
681 MachineOperand::printSymbol(OS, *Label);
682 printCFIRegister(CFI.getRegister(), OS, TRI);
683 break;
684 case MCCFIInstruction::OpEscape: {
685 OS << "escape ";
686 if (MCSymbol *Label = CFI.getLabel())
687 MachineOperand::printSymbol(OS, *Label);
688 if (!CFI.getValues().empty()) {
689 size_t e = CFI.getValues().size() - 1;
690 for (size_t i = 0; i < e; ++i)
691 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
692 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
693 }
694 break;
695 }
696 case MCCFIInstruction::OpUndefined:
697 OS << "undefined ";
698 if (MCSymbol *Label = CFI.getLabel())
699 MachineOperand::printSymbol(OS, *Label);
700 printCFIRegister(CFI.getRegister(), OS, TRI);
701 break;
702 case MCCFIInstruction::OpRegister:
703 OS << "register ";
704 if (MCSymbol *Label = CFI.getLabel())
705 MachineOperand::printSymbol(OS, *Label);
706 printCFIRegister(CFI.getRegister(), OS, TRI);
707 OS << ", ";
708 printCFIRegister(CFI.getRegister2(), OS, TRI);
709 break;
710 case MCCFIInstruction::OpWindowSave:
711 OS << "window_save ";
712 if (MCSymbol *Label = CFI.getLabel())
713 MachineOperand::printSymbol(OS, *Label);
714 break;
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000715 case MCCFIInstruction::OpNegateRAState:
716 OS << "negate_ra_sign_state ";
717 if (MCSymbol *Label = CFI.getLabel())
718 MachineOperand::printSymbol(OS, *Label);
719 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000720 default:
721 // TODO: Print the other CFI Operations.
722 OS << "<unserializable cfi directive>";
723 break;
724 }
725}
726
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000727void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
728 const TargetIntrinsicInfo *IntrinsicInfo) const {
Roman Tereshinf487eda2018-05-07 22:31:12 +0000729 print(OS, LLT{}, TRI, IntrinsicInfo);
730}
731
732void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint,
733 const TargetRegisterInfo *TRI,
734 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000735 tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000736 ModuleSlotTracker DummyMST(nullptr);
Roman Tereshinf487eda2018-05-07 22:31:12 +0000737 print(OS, DummyMST, TypeToPrint, /*PrintDef=*/false, /*IsStandalone=*/true,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000738 /*ShouldPrintRegisterTies=*/true,
739 /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000740}
741
742void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000743 LLT TypeToPrint, bool PrintDef, bool IsStandalone,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000744 bool ShouldPrintRegisterTies,
745 unsigned TiedOperandIdx,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000746 const TargetRegisterInfo *TRI,
747 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000748 printTargetFlags(OS, *this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000749 switch (getType()) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000750 case MachineOperand::MO_Register: {
Daniel Sanders0c476112019-08-15 19:22:08 +0000751 Register Reg = getReg();
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000752 if (isImplicit())
753 OS << (isDef() ? "implicit-def " : "implicit ");
754 else if (PrintDef && isDef())
755 // Print the 'def' flag only when the operand is defined after '='.
756 OS << "def ";
757 if (isInternalRead())
758 OS << "internal ";
759 if (isDead())
760 OS << "dead ";
761 if (isKill())
762 OS << "killed ";
763 if (isUndef())
764 OS << "undef ";
765 if (isEarlyClobber())
766 OS << "early-clobber ";
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000767 if (Register::isPhysicalRegister(getReg()) && isRenamable())
Geoff Berry60c43102017-12-12 17:53:59 +0000768 OS << "renamable ";
Matthias Brauna8340382018-10-30 23:28:27 +0000769 // isDebug() is exactly true for register operands of a DBG_VALUE. So we
770 // simply infer it when parsing and do not need to print it.
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000771
772 const MachineRegisterInfo *MRI = nullptr;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000773 if (Register::isVirtualRegister(Reg)) {
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000774 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
775 MRI = &MF->getRegInfo();
776 }
777 }
778
779 OS << printReg(Reg, TRI, 0, MRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000780 // Print the sub register.
781 if (unsigned SubReg = getSubReg()) {
782 if (TRI)
783 OS << '.' << TRI->getSubRegIndexName(SubReg);
784 else
785 OS << ".subreg" << SubReg;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000786 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000787 // Print the register class / bank.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000788 if (Register::isVirtualRegister(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000789 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
790 const MachineRegisterInfo &MRI = MF->getRegInfo();
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000791 if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000792 OS << ':';
793 OS << printRegClassOrBank(Reg, MRI, TRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000794 }
795 }
796 }
797 // Print ties.
798 if (ShouldPrintRegisterTies && isTied() && !isDef())
799 OS << "(tied-def " << TiedOperandIdx << ")";
800 // Print types.
801 if (TypeToPrint.isValid())
802 OS << '(' << TypeToPrint << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000803 break;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000804 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000805 case MachineOperand::MO_Immediate:
806 OS << getImm();
807 break;
808 case MachineOperand::MO_CImmediate:
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +0000809 getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000810 break;
811 case MachineOperand::MO_FPImmediate:
Francis Visoiu Mistrih3b265c82017-12-19 21:47:00 +0000812 getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000813 break;
814 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000815 OS << printMBBReference(*getMBB());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000816 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000817 case MachineOperand::MO_FrameIndex: {
818 int FrameIndex = getIndex();
819 bool IsFixed = false;
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000820 const MachineFrameInfo *MFI = nullptr;
821 if (const MachineFunction *MF = getMFIfAvailable(*this))
822 MFI = &MF->getFrameInfo();
823 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000824 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000825 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000826 case MachineOperand::MO_ConstantPoolIndex:
Francis Visoiu Mistrih26ae8a62017-12-13 10:30:45 +0000827 OS << "%const." << getIndex();
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000828 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000829 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000830 case MachineOperand::MO_TargetIndex: {
831 OS << "target-index(";
832 const char *Name = "<unknown>";
833 if (const MachineFunction *MF = getMFIfAvailable(*this))
834 if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
835 Name = TargetIndexName;
836 OS << Name << ')';
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000837 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000838 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000839 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000840 case MachineOperand::MO_JumpTableIndex:
Francis Visoiu Mistrihb41dbbe2017-12-13 10:30:59 +0000841 OS << printJumpTableEntryReference(getIndex());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000842 break;
843 case MachineOperand::MO_GlobalAddress:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000844 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000845 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000846 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000847 case MachineOperand::MO_ExternalSymbol: {
848 StringRef Name = getSymbolName();
Puyan Lotfife6c9cb2018-01-10 00:56:48 +0000849 OS << '&';
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000850 if (Name.empty()) {
851 OS << "\"\"";
852 } else {
853 printLLVMNameWithoutPrefix(OS, Name);
854 }
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000855 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000856 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000857 }
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000858 case MachineOperand::MO_BlockAddress: {
859 OS << "blockaddress(";
860 getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
861 MST);
862 OS << ", ";
863 printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
864 OS << ')';
865 MachineOperand::printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000866 break;
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000867 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000868 case MachineOperand::MO_RegisterMask: {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000869 OS << "<regmask";
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000870 if (TRI) {
871 unsigned NumRegsInMask = 0;
872 unsigned NumRegsEmitted = 0;
873 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
874 unsigned MaskWord = i / 32;
875 unsigned MaskBit = i % 32;
876 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
877 if (PrintRegMaskNumRegs < 0 ||
878 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
879 OS << " " << printReg(i, TRI);
880 NumRegsEmitted++;
881 }
882 NumRegsInMask++;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000883 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000884 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000885 if (NumRegsEmitted != NumRegsInMask)
886 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
887 } else {
888 OS << " ...";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000889 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000890 OS << ">";
891 break;
892 }
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000893 case MachineOperand::MO_RegisterLiveOut: {
894 const uint32_t *RegMask = getRegLiveOut();
895 OS << "liveout(";
896 if (!TRI) {
897 OS << "<unknown>";
898 } else {
899 bool IsCommaNeeded = false;
900 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
901 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
902 if (IsCommaNeeded)
903 OS << ", ";
904 OS << printReg(Reg, TRI);
905 IsCommaNeeded = true;
906 }
907 }
908 }
909 OS << ")";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000910 break;
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000911 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000912 case MachineOperand::MO_Metadata:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000913 getMetadata()->printAsOperand(OS, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000914 break;
915 case MachineOperand::MO_MCSymbol:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000916 printSymbol(OS, *getMCSymbol());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000917 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000918 case MachineOperand::MO_CFIIndex: {
919 if (const MachineFunction *MF = getMFIfAvailable(*this))
920 printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
921 else
922 OS << "<cfi directive>";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000923 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000924 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000925 case MachineOperand::MO_IntrinsicID: {
926 Intrinsic::ID ID = getIntrinsicID();
927 if (ID < Intrinsic::num_intrinsics)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000928 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000929 else if (IntrinsicInfo)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000930 OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000931 else
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000932 OS << "intrinsic(" << ID << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000933 break;
934 }
935 case MachineOperand::MO_Predicate: {
936 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
Francis Visoiu Mistrihcb2683d2017-12-19 21:47:10 +0000937 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
938 << CmpInst::getPredicateName(Pred) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000939 break;
940 }
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000941 case MachineOperand::MO_ShuffleMask:
942 OS << "shufflemask(";
943 const Constant* C = getShuffleMask();
944 const int NumElts = C->getType()->getVectorNumElements();
945
946 StringRef Separator;
947 for (int I = 0; I != NumElts; ++I) {
948 OS << Separator;
949 C->getAggregateElement(I)->printAsOperand(OS, false, MST);
950 Separator = ", ";
951 }
952
953 OS << ')';
954 break;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000955 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000956}
957
958#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
959LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
960#endif
961
962//===----------------------------------------------------------------------===//
963// MachineMemOperand Implementation
964//===----------------------------------------------------------------------===//
965
966/// getAddrSpace - Return the LLVM IR address space number that this pointer
967/// points into.
Yaxun Liu49477042017-12-02 22:13:22 +0000968unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000969
970/// isDereferenceable - Return true if V is always dereferenceable for
971/// Offset + Size byte.
972bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
973 const DataLayout &DL) const {
974 if (!V.is<const Value *>())
975 return false;
976
977 const Value *BasePtr = V.get<const Value *>();
978 if (BasePtr == nullptr)
979 return false;
980
981 return isDereferenceableAndAlignedPointer(
982 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
983}
984
985/// getConstantPool - Return a MachinePointerInfo record that refers to the
986/// constant pool.
987MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
988 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
989}
990
991/// getFixedStack - Return a MachinePointerInfo record that refers to the
992/// the specified FrameIndex.
993MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
994 int FI, int64_t Offset) {
995 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
996}
997
998MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
999 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
1000}
1001
1002MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
1003 return MachinePointerInfo(MF.getPSVManager().getGOT());
1004}
1005
1006MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
1007 int64_t Offset, uint8_t ID) {
1008 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
1009}
1010
Yaxun Liu49477042017-12-02 22:13:22 +00001011MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
1012 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
1013}
1014
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001015MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Daniel Sandersc01efe62018-04-09 18:42:19 +00001016 uint64_t s, uint64_t a,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001017 const AAMDNodes &AAInfo,
1018 const MDNode *Ranges, SyncScope::ID SSID,
1019 AtomicOrdering Ordering,
1020 AtomicOrdering FailureOrdering)
1021 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
1022 AAInfo(AAInfo), Ranges(Ranges) {
1023 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
1024 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
1025 "invalid pointer value");
Matt Arsenault2a645982019-01-31 01:38:47 +00001026 assert(getBaseAlignment() == a && a != 0 && "Alignment is not a power of 2!");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001027 assert((isLoad() || isStore()) && "Not a load/store!");
1028
1029 AtomicInfo.SSID = static_cast<unsigned>(SSID);
1030 assert(getSyncScopeID() == SSID && "Value truncated");
1031 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
1032 assert(getOrdering() == Ordering && "Value truncated");
1033 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
1034 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
1035}
1036
1037/// Profile - Gather unique data for the object.
1038///
1039void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
1040 ID.AddInteger(getOffset());
1041 ID.AddInteger(Size);
1042 ID.AddPointer(getOpaqueValue());
1043 ID.AddInteger(getFlags());
1044 ID.AddInteger(getBaseAlignment());
1045}
1046
1047void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
1048 // The Value and Offset may differ due to CSE. But the flags and size
1049 // should be the same.
1050 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
1051 assert(MMO->getSize() == getSize() && "Size mismatch!");
1052
1053 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
1054 // Update the alignment value.
1055 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
1056 // Also update the base and offset, because the new alignment may
1057 // not be applicable with the old ones.
1058 PtrInfo = MMO->PtrInfo;
1059 }
1060}
1061
1062/// getAlignment - Return the minimum known alignment in bytes of the
1063/// actual memory reference.
1064uint64_t MachineMemOperand::getAlignment() const {
1065 return MinAlign(getBaseAlignment(), getOffset());
1066}
1067
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001068void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
1069 SmallVectorImpl<StringRef> &SSNs,
1070 const LLVMContext &Context,
1071 const MachineFrameInfo *MFI,
1072 const TargetInstrInfo *TII) const {
1073 OS << '(';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001074 if (isVolatile())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001075 OS << "volatile ";
1076 if (isNonTemporal())
1077 OS << "non-temporal ";
1078 if (isDereferenceable())
1079 OS << "dereferenceable ";
1080 if (isInvariant())
1081 OS << "invariant ";
1082 if (getFlags() & MachineMemOperand::MOTargetFlag1)
1083 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
1084 << "\" ";
1085 if (getFlags() & MachineMemOperand::MOTargetFlag2)
1086 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
1087 << "\" ";
1088 if (getFlags() & MachineMemOperand::MOTargetFlag3)
1089 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
1090 << "\" ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001091
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001092 assert((isLoad() || isStore()) &&
1093 "machine memory operand must be a load or store (or both)");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001094 if (isLoad())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001095 OS << "load ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001096 if (isStore())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001097 OS << "store ";
1098
1099 printSyncScope(OS, Context, getSyncScopeID(), SSNs);
1100
1101 if (getOrdering() != AtomicOrdering::NotAtomic)
1102 OS << toIRString(getOrdering()) << ' ';
1103 if (getFailureOrdering() != AtomicOrdering::NotAtomic)
1104 OS << toIRString(getFailureOrdering()) << ' ';
1105
Krzysztof Parzyszekcc3f6302018-08-20 20:37:57 +00001106 if (getSize() == MemoryLocation::UnknownSize)
1107 OS << "unknown-size";
1108 else
1109 OS << getSize();
1110
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001111 if (const Value *Val = getValue()) {
1112 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1113 printIRValueReference(OS, *Val, MST);
1114 } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
1115 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1116 assert(PVal && "Expected a pseudo source value");
1117 switch (PVal->kind()) {
1118 case PseudoSourceValue::Stack:
1119 OS << "stack";
1120 break;
1121 case PseudoSourceValue::GOT:
1122 OS << "got";
1123 break;
1124 case PseudoSourceValue::JumpTable:
1125 OS << "jump-table";
1126 break;
1127 case PseudoSourceValue::ConstantPool:
1128 OS << "constant-pool";
1129 break;
1130 case PseudoSourceValue::FixedStack: {
1131 int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1132 bool IsFixed = true;
1133 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
1134 break;
1135 }
1136 case PseudoSourceValue::GlobalValueCallEntry:
1137 OS << "call-entry ";
1138 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1139 OS, /*PrintType=*/false, MST);
1140 break;
1141 case PseudoSourceValue::ExternalSymbolCallEntry:
1142 OS << "call-entry &";
1143 printLLVMNameWithoutPrefix(
1144 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
1145 break;
Matt Arsenault3062e872019-06-14 13:26:34 +00001146 default:
Tim Renouf4db09602018-03-27 21:14:04 +00001147 // FIXME: This is not necessarily the correct MIR serialization format for
1148 // a custom pseudo source value, but at least it allows
1149 // -print-machineinstrs to work on a target with custom pseudo source
1150 // values.
1151 OS << "custom ";
1152 PVal->printCustom(OS);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001153 break;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001154 }
1155 }
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001156 MachineOperand::printOperandOffset(OS, getOffset());
1157 if (getBaseAlignment() != getSize())
1158 OS << ", align " << getBaseAlignment();
1159 auto AAInfo = getAAInfo();
1160 if (AAInfo.TBAA) {
1161 OS << ", !tbaa ";
1162 AAInfo.TBAA->printAsOperand(OS, MST);
1163 }
1164 if (AAInfo.Scope) {
1165 OS << ", !alias.scope ";
1166 AAInfo.Scope->printAsOperand(OS, MST);
1167 }
1168 if (AAInfo.NoAlias) {
1169 OS << ", !noalias ";
1170 AAInfo.NoAlias->printAsOperand(OS, MST);
1171 }
1172 if (getRanges()) {
1173 OS << ", !range ";
1174 getRanges()->printAsOperand(OS, MST);
1175 }
1176 // FIXME: Implement addrspace printing/parsing in MIR.
1177 // For now, print this even though parsing it is not available in MIR.
1178 if (unsigned AS = getAddrSpace())
1179 OS << ", addrspace " << AS;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001180
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001181 OS << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001182}