blob: cda92358793a0cf2445fd28b77d21945e226155e [file] [log] [blame]
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001//===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Francis Visoiu Mistrih3aa8eaa2017-11-28 19:23:39 +000010/// \file Methods common to all machine operands.
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineOperand.h"
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +000015#include "llvm/ADT/StringExtras.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000016#include "llvm/Analysis/Loads.h"
17#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"
23#include "llvm/IR/Constants.h"
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +000024#include "llvm/IR/IRPrintingPasses.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000025#include "llvm/IR/ModuleSlotTracker.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000026#include "llvm/Target/TargetIntrinsicInfo.h"
27#include "llvm/Target/TargetMachine.h"
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000028
29using namespace llvm;
30
31static cl::opt<int>
32 PrintRegMaskNumRegs("print-regmask-num-regs",
33 cl::desc("Number of registers to limit to when "
34 "printing regmask operands in IR dumps. "
35 "unlimited = -1"),
36 cl::init(32), cl::Hidden);
37
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000038static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
39 if (const MachineInstr *MI = MO.getParent())
40 if (const MachineBasicBlock *MBB = MI->getParent())
41 if (const MachineFunction *MF = MBB->getParent())
42 return MF;
43 return nullptr;
44}
45static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
46 return const_cast<MachineFunction *>(
47 getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
48}
49
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000050void MachineOperand::setReg(unsigned Reg) {
51 if (getReg() == Reg)
52 return; // No change.
53
Geoff Berryf8bf2ec2018-02-23 18:25:08 +000054 // Clear the IsRenamable bit to keep it conservatively correct.
55 IsRenamable = false;
56
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000057 // Otherwise, we have to change the register. If this operand is embedded
58 // into a machine function, we need to update the old and new register's
59 // use/def lists.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000060 if (MachineFunction *MF = getMFIfAvailable(*this)) {
61 MachineRegisterInfo &MRI = MF->getRegInfo();
62 MRI.removeRegOperandFromUseList(this);
63 SmallContents.RegNo = Reg;
64 MRI.addRegOperandToUseList(this);
65 return;
Francis Visoiu Mistrihc2641d62017-12-06 11:57:53 +000066 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000067
68 // Otherwise, just change the register, no problem. :)
69 SmallContents.RegNo = Reg;
70}
71
72void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
73 const TargetRegisterInfo &TRI) {
74 assert(TargetRegisterInfo::isVirtualRegister(Reg));
75 if (SubIdx && getSubReg())
76 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
77 setReg(Reg);
78 if (SubIdx)
79 setSubReg(SubIdx);
80}
81
82void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
83 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
84 if (getSubReg()) {
85 Reg = TRI.getSubReg(Reg, getSubReg());
86 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
87 // That won't happen in legal code.
88 setSubReg(0);
89 if (isDef())
90 setIsUndef(false);
91 }
92 setReg(Reg);
93}
94
95/// Change a def to a use, or a use to a def.
96void MachineOperand::setIsDef(bool Val) {
97 assert(isReg() && "Wrong MachineOperand accessor");
98 assert((!Val || !isDebug()) && "Marking a debug operation as def");
99 if (IsDef == Val)
100 return;
Geoff Berry60c43102017-12-12 17:53:59 +0000101 assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000102 // MRI may keep uses and defs in different list positions.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000103 if (MachineFunction *MF = getMFIfAvailable(*this)) {
104 MachineRegisterInfo &MRI = MF->getRegInfo();
105 MRI.removeRegOperandFromUseList(this);
106 IsDef = Val;
107 MRI.addRegOperandToUseList(this);
108 return;
109 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000110 IsDef = Val;
111}
112
Geoff Berry60c43102017-12-12 17:53:59 +0000113bool MachineOperand::isRenamable() const {
114 assert(isReg() && "Wrong MachineOperand accessor");
115 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
116 "isRenamable should only be checked on physical registers");
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000117 if (!IsRenamable)
118 return false;
119
120 const MachineInstr *MI = getParent();
121 if (!MI)
122 return true;
123
124 if (isDef())
125 return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle);
126
127 assert(isUse() && "Reg is not def or use");
128 return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle);
Geoff Berry60c43102017-12-12 17:53:59 +0000129}
130
131void MachineOperand::setIsRenamable(bool Val) {
132 assert(isReg() && "Wrong MachineOperand accessor");
133 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
134 "setIsRenamable should only be called on physical registers");
Geoff Berry60c43102017-12-12 17:53:59 +0000135 IsRenamable = Val;
136}
137
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000138// If this operand is currently a register operand, and if this is in a
139// function, deregister the operand from the register's use/def list.
140void MachineOperand::removeRegFromUses() {
141 if (!isReg() || !isOnRegUseList())
142 return;
143
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000144 if (MachineFunction *MF = getMFIfAvailable(*this))
145 MF->getRegInfo().removeRegOperandFromUseList(this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000146}
147
148/// ChangeToImmediate - Replace this operand with a new immediate operand of
149/// the specified value. If an operand is known to be an immediate already,
150/// the setImm method should be used.
151void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
152 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
153
154 removeRegFromUses();
155
156 OpKind = MO_Immediate;
157 Contents.ImmVal = ImmVal;
158}
159
160void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
161 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
162
163 removeRegFromUses();
164
165 OpKind = MO_FPImmediate;
166 Contents.CFP = FPImm;
167}
168
169void MachineOperand::ChangeToES(const char *SymName,
170 unsigned char TargetFlags) {
171 assert((!isReg() || !isTied()) &&
172 "Cannot change a tied operand into an external symbol");
173
174 removeRegFromUses();
175
176 OpKind = MO_ExternalSymbol;
177 Contents.OffsetedInfo.Val.SymbolName = SymName;
178 setOffset(0); // Offset is always 0.
179 setTargetFlags(TargetFlags);
180}
181
182void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
183 assert((!isReg() || !isTied()) &&
184 "Cannot change a tied operand into an MCSymbol");
185
186 removeRegFromUses();
187
188 OpKind = MO_MCSymbol;
189 Contents.Sym = Sym;
190}
191
192void MachineOperand::ChangeToFrameIndex(int Idx) {
193 assert((!isReg() || !isTied()) &&
194 "Cannot change a tied operand into a FrameIndex");
195
196 removeRegFromUses();
197
198 OpKind = MO_FrameIndex;
199 setIndex(Idx);
200}
201
202void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
203 unsigned char TargetFlags) {
204 assert((!isReg() || !isTied()) &&
205 "Cannot change a tied operand into a FrameIndex");
206
207 removeRegFromUses();
208
209 OpKind = MO_TargetIndex;
210 setIndex(Idx);
211 setOffset(Offset);
212 setTargetFlags(TargetFlags);
213}
214
215/// ChangeToRegister - Replace this operand with a new register operand of
216/// the specified value. If an operand is known to be an register already,
217/// the setReg method should be used.
218void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
219 bool isKill, bool isDead, bool isUndef,
220 bool isDebug) {
221 MachineRegisterInfo *RegInfo = nullptr;
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000222 if (MachineFunction *MF = getMFIfAvailable(*this))
223 RegInfo = &MF->getRegInfo();
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000224 // If this operand is already a register operand, remove it from the
225 // register's use/def lists.
226 bool WasReg = isReg();
227 if (RegInfo && WasReg)
228 RegInfo->removeRegOperandFromUseList(this);
229
230 // Change this to a register and set the reg#.
Geoff Berry60c43102017-12-12 17:53:59 +0000231 assert(!(isDead && !isDef) && "Dead flag on non-def");
232 assert(!(isKill && isDef) && "Kill flag on def");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000233 OpKind = MO_Register;
234 SmallContents.RegNo = Reg;
235 SubReg_TargetFlags = 0;
236 IsDef = isDef;
237 IsImp = isImp;
Geoff Berry60c43102017-12-12 17:53:59 +0000238 IsDeadOrKill = isKill | isDead;
239 IsRenamable = false;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000240 IsUndef = isUndef;
241 IsInternalRead = false;
242 IsEarlyClobber = false;
243 IsDebug = isDebug;
244 // Ensure isOnRegUseList() returns false.
245 Contents.Reg.Prev = nullptr;
246 // Preserve the tie when the operand was already a register.
247 if (!WasReg)
248 TiedTo = 0;
249
250 // If this operand is embedded in a function, add the operand to the
251 // register's use/def list.
252 if (RegInfo)
253 RegInfo->addRegOperandToUseList(this);
254}
255
256/// isIdenticalTo - Return true if this operand is identical to the specified
257/// operand. Note that this should stay in sync with the hash_value overload
258/// below.
259bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
260 if (getType() != Other.getType() ||
261 getTargetFlags() != Other.getTargetFlags())
262 return false;
263
264 switch (getType()) {
265 case MachineOperand::MO_Register:
266 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
267 getSubReg() == Other.getSubReg();
268 case MachineOperand::MO_Immediate:
269 return getImm() == Other.getImm();
270 case MachineOperand::MO_CImmediate:
271 return getCImm() == Other.getCImm();
272 case MachineOperand::MO_FPImmediate:
273 return getFPImm() == Other.getFPImm();
274 case MachineOperand::MO_MachineBasicBlock:
275 return getMBB() == Other.getMBB();
276 case MachineOperand::MO_FrameIndex:
277 return getIndex() == Other.getIndex();
278 case MachineOperand::MO_ConstantPoolIndex:
279 case MachineOperand::MO_TargetIndex:
280 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
281 case MachineOperand::MO_JumpTableIndex:
282 return getIndex() == Other.getIndex();
283 case MachineOperand::MO_GlobalAddress:
284 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
285 case MachineOperand::MO_ExternalSymbol:
286 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
287 getOffset() == Other.getOffset();
288 case MachineOperand::MO_BlockAddress:
289 return getBlockAddress() == Other.getBlockAddress() &&
290 getOffset() == Other.getOffset();
291 case MachineOperand::MO_RegisterMask:
292 case MachineOperand::MO_RegisterLiveOut: {
293 // Shallow compare of the two RegMasks
294 const uint32_t *RegMask = getRegMask();
295 const uint32_t *OtherRegMask = Other.getRegMask();
296 if (RegMask == OtherRegMask)
297 return true;
298
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000299 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
300 // Calculate the size of the RegMask
301 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
302 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000303
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000304 // Deep compare of the two RegMasks
305 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
306 }
307 // We don't know the size of the RegMask, so we can't deep compare the two
308 // reg masks.
309 return false;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000310 }
311 case MachineOperand::MO_MCSymbol:
312 return getMCSymbol() == Other.getMCSymbol();
313 case MachineOperand::MO_CFIIndex:
314 return getCFIIndex() == Other.getCFIIndex();
315 case MachineOperand::MO_Metadata:
316 return getMetadata() == Other.getMetadata();
317 case MachineOperand::MO_IntrinsicID:
318 return getIntrinsicID() == Other.getIntrinsicID();
319 case MachineOperand::MO_Predicate:
320 return getPredicate() == Other.getPredicate();
321 }
322 llvm_unreachable("Invalid machine operand type");
323}
324
325// Note: this must stay exactly in sync with isIdenticalTo above.
326hash_code llvm::hash_value(const MachineOperand &MO) {
327 switch (MO.getType()) {
328 case MachineOperand::MO_Register:
329 // Register operands don't have target flags.
330 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
331 case MachineOperand::MO_Immediate:
332 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
333 case MachineOperand::MO_CImmediate:
334 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
335 case MachineOperand::MO_FPImmediate:
336 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
337 case MachineOperand::MO_MachineBasicBlock:
338 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
339 case MachineOperand::MO_FrameIndex:
340 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
341 case MachineOperand::MO_ConstantPoolIndex:
342 case MachineOperand::MO_TargetIndex:
343 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
344 MO.getOffset());
345 case MachineOperand::MO_JumpTableIndex:
346 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
347 case MachineOperand::MO_ExternalSymbol:
348 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
349 MO.getSymbolName());
350 case MachineOperand::MO_GlobalAddress:
351 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
352 MO.getOffset());
353 case MachineOperand::MO_BlockAddress:
354 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
355 MO.getOffset());
356 case MachineOperand::MO_RegisterMask:
357 case MachineOperand::MO_RegisterLiveOut:
358 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
359 case MachineOperand::MO_Metadata:
360 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
361 case MachineOperand::MO_MCSymbol:
362 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
363 case MachineOperand::MO_CFIIndex:
364 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
365 case MachineOperand::MO_IntrinsicID:
366 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
367 case MachineOperand::MO_Predicate:
368 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
369 }
370 llvm_unreachable("Invalid machine operand type");
371}
372
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000373// Try to crawl up to the machine function and get TRI and IntrinsicInfo from
374// it.
375static void tryToGetTargetInfo(const MachineOperand &MO,
376 const TargetRegisterInfo *&TRI,
377 const TargetIntrinsicInfo *&IntrinsicInfo) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000378 if (const MachineFunction *MF = getMFIfAvailable(MO)) {
379 TRI = MF->getSubtarget().getRegisterInfo();
380 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000381 }
382}
383
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000384static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
385 const auto *TII = MF.getSubtarget().getInstrInfo();
386 assert(TII && "expected instruction info");
387 auto Indices = TII->getSerializableTargetIndices();
388 auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
389 return I.first == Index;
390 });
391 if (Found != Indices.end())
392 return Found->second;
393 return nullptr;
394}
395
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000396static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
397 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
398 for (const auto &I : Flags) {
399 if (I.first == TF) {
400 return I.second;
401 }
402 }
403 return nullptr;
404}
405
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000406static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
407 const TargetRegisterInfo *TRI) {
408 if (!TRI) {
409 OS << "%dwarfreg." << DwarfReg;
410 return;
411 }
412
413 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
414 if (Reg == -1) {
415 OS << "<badreg>";
416 return;
417 }
418 OS << printReg(Reg, TRI);
419}
420
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000421static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
422 ModuleSlotTracker &MST) {
423 OS << "%ir-block.";
424 if (BB.hasName()) {
425 printLLVMNameWithoutPrefix(OS, BB.getName());
426 return;
427 }
428 Optional<int> Slot;
429 if (const Function *F = BB.getParent()) {
430 if (F == MST.getCurrentFunction()) {
431 Slot = MST.getLocalSlot(&BB);
432 } else if (const Module *M = F->getParent()) {
433 ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
434 CustomMST.incorporateFunction(*F);
435 Slot = CustomMST.getLocalSlot(&BB);
436 }
437 }
438 if (Slot)
439 MachineOperand::printIRSlotNumber(OS, *Slot);
440 else
441 OS << "<unknown>";
442}
443
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000444static void printIRValueReference(raw_ostream &OS, const Value &V,
445 ModuleSlotTracker &MST) {
446 if (isa<GlobalValue>(V)) {
447 V.printAsOperand(OS, /*PrintType=*/false, MST);
448 return;
449 }
450 if (isa<Constant>(V)) {
451 // Machine memory operands can load/store to/from constant value pointers.
452 OS << '`';
453 V.printAsOperand(OS, /*PrintType=*/true, MST);
454 OS << '`';
455 return;
456 }
457 OS << "%ir.";
458 if (V.hasName()) {
459 printLLVMNameWithoutPrefix(OS, V.getName());
460 return;
461 }
462 MachineOperand::printIRSlotNumber(OS, MST.getLocalSlot(&V));
463}
464
465static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
466 SyncScope::ID SSID,
467 SmallVectorImpl<StringRef> &SSNs) {
468 switch (SSID) {
469 case SyncScope::System:
470 break;
471 default:
472 if (SSNs.empty())
473 Context.getSyncScopeNames(SSNs);
474
475 OS << "syncscope(\"";
476 PrintEscapedString(SSNs[SSID], OS);
477 OS << "\") ";
478 break;
479 }
480}
481
482static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
483 unsigned TMMOFlag) {
484 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
485 for (const auto &I : Flags) {
486 if (I.first == TMMOFlag) {
487 return I.second;
488 }
489 }
490 return nullptr;
491}
492
493static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
494 const MachineFrameInfo *MFI) {
495 StringRef Name;
496 if (MFI) {
497 IsFixed = MFI->isFixedObjectIndex(FrameIndex);
498 if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
499 if (Alloca->hasName())
500 Name = Alloca->getName();
501 if (IsFixed)
502 FrameIndex -= MFI->getObjectIndexBegin();
503 }
504 MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
505}
506
Francis Visoiu Mistrihecd0b832018-01-16 10:53:11 +0000507void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +0000508 const TargetRegisterInfo *TRI) {
509 OS << "%subreg.";
510 if (TRI)
511 OS << TRI->getSubRegIndexName(Index);
512 else
513 OS << Index;
514}
515
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000516void MachineOperand::printTargetFlags(raw_ostream &OS,
517 const MachineOperand &Op) {
518 if (!Op.getTargetFlags())
519 return;
520 const MachineFunction *MF = getMFIfAvailable(Op);
521 if (!MF)
522 return;
523
524 const auto *TII = MF->getSubtarget().getInstrInfo();
525 assert(TII && "expected instruction info");
526 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
527 OS << "target-flags(";
528 const bool HasDirectFlags = Flags.first;
529 const bool HasBitmaskFlags = Flags.second;
530 if (!HasDirectFlags && !HasBitmaskFlags) {
531 OS << "<unknown>) ";
532 return;
533 }
534 if (HasDirectFlags) {
535 if (const auto *Name = getTargetFlagName(TII, Flags.first))
536 OS << Name;
537 else
538 OS << "<unknown target flag>";
539 }
540 if (!HasBitmaskFlags) {
541 OS << ") ";
542 return;
543 }
544 bool IsCommaNeeded = HasDirectFlags;
545 unsigned BitMask = Flags.second;
546 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
547 for (const auto &Mask : BitMasks) {
548 // Check if the flag's bitmask has the bits of the current mask set.
549 if ((BitMask & Mask.first) == Mask.first) {
550 if (IsCommaNeeded)
551 OS << ", ";
552 IsCommaNeeded = true;
553 OS << Mask.second;
554 // Clear the bits which were serialized from the flag's bitmask.
555 BitMask &= ~(Mask.first);
556 }
557 }
558 if (BitMask) {
559 // When the resulting flag's bitmask isn't zero, we know that we didn't
560 // serialize all of the bit flags.
561 if (IsCommaNeeded)
562 OS << ", ";
563 OS << "<unknown bitmask target flag>";
564 }
565 OS << ") ";
566}
567
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000568void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
569 OS << "<mcsymbol " << Sym << ">";
570}
571
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000572void MachineOperand::printStackObjectReference(raw_ostream &OS,
573 unsigned FrameIndex,
574 bool IsFixed, StringRef Name) {
575 if (IsFixed) {
576 OS << "%fixed-stack." << FrameIndex;
577 return;
578 }
579
580 OS << "%stack." << FrameIndex;
581 if (!Name.empty())
582 OS << '.' << Name;
583}
584
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000585void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
586 if (Offset == 0)
587 return;
588 if (Offset < 0) {
589 OS << " - " << -Offset;
590 return;
591 }
592 OS << " + " << Offset;
593}
594
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000595void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
596 if (Slot == -1)
597 OS << "<badref>";
598 else
599 OS << Slot;
600}
601
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000602static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
603 const TargetRegisterInfo *TRI) {
604 switch (CFI.getOperation()) {
605 case MCCFIInstruction::OpSameValue:
606 OS << "same_value ";
607 if (MCSymbol *Label = CFI.getLabel())
608 MachineOperand::printSymbol(OS, *Label);
609 printCFIRegister(CFI.getRegister(), OS, TRI);
610 break;
611 case MCCFIInstruction::OpRememberState:
612 OS << "remember_state ";
613 if (MCSymbol *Label = CFI.getLabel())
614 MachineOperand::printSymbol(OS, *Label);
615 break;
616 case MCCFIInstruction::OpRestoreState:
617 OS << "restore_state ";
618 if (MCSymbol *Label = CFI.getLabel())
619 MachineOperand::printSymbol(OS, *Label);
620 break;
621 case MCCFIInstruction::OpOffset:
622 OS << "offset ";
623 if (MCSymbol *Label = CFI.getLabel())
624 MachineOperand::printSymbol(OS, *Label);
625 printCFIRegister(CFI.getRegister(), OS, TRI);
626 OS << ", " << CFI.getOffset();
627 break;
628 case MCCFIInstruction::OpDefCfaRegister:
629 OS << "def_cfa_register ";
630 if (MCSymbol *Label = CFI.getLabel())
631 MachineOperand::printSymbol(OS, *Label);
632 printCFIRegister(CFI.getRegister(), OS, TRI);
633 break;
634 case MCCFIInstruction::OpDefCfaOffset:
635 OS << "def_cfa_offset ";
636 if (MCSymbol *Label = CFI.getLabel())
637 MachineOperand::printSymbol(OS, *Label);
638 OS << CFI.getOffset();
639 break;
640 case MCCFIInstruction::OpDefCfa:
641 OS << "def_cfa ";
642 if (MCSymbol *Label = CFI.getLabel())
643 MachineOperand::printSymbol(OS, *Label);
644 printCFIRegister(CFI.getRegister(), OS, TRI);
645 OS << ", " << CFI.getOffset();
646 break;
647 case MCCFIInstruction::OpRelOffset:
648 OS << "rel_offset ";
649 if (MCSymbol *Label = CFI.getLabel())
650 MachineOperand::printSymbol(OS, *Label);
651 printCFIRegister(CFI.getRegister(), OS, TRI);
652 OS << ", " << CFI.getOffset();
653 break;
654 case MCCFIInstruction::OpAdjustCfaOffset:
655 OS << "adjust_cfa_offset ";
656 if (MCSymbol *Label = CFI.getLabel())
657 MachineOperand::printSymbol(OS, *Label);
658 OS << CFI.getOffset();
659 break;
660 case MCCFIInstruction::OpRestore:
661 OS << "restore ";
662 if (MCSymbol *Label = CFI.getLabel())
663 MachineOperand::printSymbol(OS, *Label);
664 printCFIRegister(CFI.getRegister(), OS, TRI);
665 break;
666 case MCCFIInstruction::OpEscape: {
667 OS << "escape ";
668 if (MCSymbol *Label = CFI.getLabel())
669 MachineOperand::printSymbol(OS, *Label);
670 if (!CFI.getValues().empty()) {
671 size_t e = CFI.getValues().size() - 1;
672 for (size_t i = 0; i < e; ++i)
673 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
674 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
675 }
676 break;
677 }
678 case MCCFIInstruction::OpUndefined:
679 OS << "undefined ";
680 if (MCSymbol *Label = CFI.getLabel())
681 MachineOperand::printSymbol(OS, *Label);
682 printCFIRegister(CFI.getRegister(), OS, TRI);
683 break;
684 case MCCFIInstruction::OpRegister:
685 OS << "register ";
686 if (MCSymbol *Label = CFI.getLabel())
687 MachineOperand::printSymbol(OS, *Label);
688 printCFIRegister(CFI.getRegister(), OS, TRI);
689 OS << ", ";
690 printCFIRegister(CFI.getRegister2(), OS, TRI);
691 break;
692 case MCCFIInstruction::OpWindowSave:
693 OS << "window_save ";
694 if (MCSymbol *Label = CFI.getLabel())
695 MachineOperand::printSymbol(OS, *Label);
696 break;
697 default:
698 // TODO: Print the other CFI Operations.
699 OS << "<unserializable cfi directive>";
700 break;
701 }
702}
703
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000704void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
705 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000706 tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000707 ModuleSlotTracker DummyMST(nullptr);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000708 print(OS, DummyMST, LLT{}, /*PrintDef=*/false, /*IsStandalone=*/true,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000709 /*ShouldPrintRegisterTies=*/true,
710 /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000711}
712
713void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000714 LLT TypeToPrint, bool PrintDef, bool IsStandalone,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000715 bool ShouldPrintRegisterTies,
716 unsigned TiedOperandIdx,
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000717 const TargetRegisterInfo *TRI,
718 const TargetIntrinsicInfo *IntrinsicInfo) const {
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +0000719 printTargetFlags(OS, *this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000720 switch (getType()) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000721 case MachineOperand::MO_Register: {
722 unsigned Reg = getReg();
723 if (isImplicit())
724 OS << (isDef() ? "implicit-def " : "implicit ");
725 else if (PrintDef && isDef())
726 // Print the 'def' flag only when the operand is defined after '='.
727 OS << "def ";
728 if (isInternalRead())
729 OS << "internal ";
730 if (isDead())
731 OS << "dead ";
732 if (isKill())
733 OS << "killed ";
734 if (isUndef())
735 OS << "undef ";
736 if (isEarlyClobber())
737 OS << "early-clobber ";
738 if (isDebug())
739 OS << "debug-use ";
Geoff Berry60c43102017-12-12 17:53:59 +0000740 if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable())
741 OS << "renamable ";
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000742
743 const MachineRegisterInfo *MRI = nullptr;
744 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
745 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
746 MRI = &MF->getRegInfo();
747 }
748 }
749
750 OS << printReg(Reg, TRI, 0, MRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000751 // Print the sub register.
752 if (unsigned SubReg = getSubReg()) {
753 if (TRI)
754 OS << '.' << TRI->getSubRegIndexName(SubReg);
755 else
756 OS << ".subreg" << SubReg;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000757 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000758 // Print the register class / bank.
759 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000760 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
761 const MachineRegisterInfo &MRI = MF->getRegInfo();
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +0000762 if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
Francis Visoiu Mistrih567611e2017-12-07 14:32:15 +0000763 OS << ':';
764 OS << printRegClassOrBank(Reg, MRI, TRI);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000765 }
766 }
767 }
768 // Print ties.
769 if (ShouldPrintRegisterTies && isTied() && !isDef())
770 OS << "(tied-def " << TiedOperandIdx << ")";
771 // Print types.
772 if (TypeToPrint.isValid())
773 OS << '(' << TypeToPrint << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000774 break;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000775 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000776 case MachineOperand::MO_Immediate:
777 OS << getImm();
778 break;
779 case MachineOperand::MO_CImmediate:
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +0000780 getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000781 break;
782 case MachineOperand::MO_FPImmediate:
Francis Visoiu Mistrih3b265c82017-12-19 21:47:00 +0000783 getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000784 break;
785 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000786 OS << printMBBReference(*getMBB());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000787 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000788 case MachineOperand::MO_FrameIndex: {
789 int FrameIndex = getIndex();
790 bool IsFixed = false;
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +0000791 const MachineFrameInfo *MFI = nullptr;
792 if (const MachineFunction *MF = getMFIfAvailable(*this))
793 MFI = &MF->getFrameInfo();
794 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000795 break;
Francis Visoiu Mistrih0b5bdce2017-12-15 16:33:45 +0000796 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000797 case MachineOperand::MO_ConstantPoolIndex:
Francis Visoiu Mistrih26ae8a62017-12-13 10:30:45 +0000798 OS << "%const." << getIndex();
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000799 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000800 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000801 case MachineOperand::MO_TargetIndex: {
802 OS << "target-index(";
803 const char *Name = "<unknown>";
804 if (const MachineFunction *MF = getMFIfAvailable(*this))
805 if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
806 Name = TargetIndexName;
807 OS << Name << ')';
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000808 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000809 break;
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000810 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000811 case MachineOperand::MO_JumpTableIndex:
Francis Visoiu Mistrihb41dbbe2017-12-13 10:30:59 +0000812 OS << printJumpTableEntryReference(getIndex());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000813 break;
814 case MachineOperand::MO_GlobalAddress:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000815 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000816 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000817 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000818 case MachineOperand::MO_ExternalSymbol: {
819 StringRef Name = getSymbolName();
Puyan Lotfife6c9cb2018-01-10 00:56:48 +0000820 OS << '&';
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000821 if (Name.empty()) {
822 OS << "\"\"";
823 } else {
824 printLLVMNameWithoutPrefix(OS, Name);
825 }
Francis Visoiu Mistrih81226602017-12-19 21:46:55 +0000826 printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000827 break;
Francis Visoiu Mistrihe76c5fc2017-12-14 10:02:58 +0000828 }
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000829 case MachineOperand::MO_BlockAddress: {
830 OS << "blockaddress(";
831 getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
832 MST);
833 OS << ", ";
834 printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
835 OS << ')';
836 MachineOperand::printOperandOffset(OS, getOffset());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000837 break;
Francis Visoiu Mistrihf81727d2017-12-19 21:47:14 +0000838 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000839 case MachineOperand::MO_RegisterMask: {
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000840 OS << "<regmask";
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000841 if (TRI) {
842 unsigned NumRegsInMask = 0;
843 unsigned NumRegsEmitted = 0;
844 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
845 unsigned MaskWord = i / 32;
846 unsigned MaskBit = i % 32;
847 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
848 if (PrintRegMaskNumRegs < 0 ||
849 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
850 OS << " " << printReg(i, TRI);
851 NumRegsEmitted++;
852 }
853 NumRegsInMask++;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000854 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000855 }
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000856 if (NumRegsEmitted != NumRegsInMask)
857 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
858 } else {
859 OS << " ...";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000860 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000861 OS << ">";
862 break;
863 }
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000864 case MachineOperand::MO_RegisterLiveOut: {
865 const uint32_t *RegMask = getRegLiveOut();
866 OS << "liveout(";
867 if (!TRI) {
868 OS << "<unknown>";
869 } else {
870 bool IsCommaNeeded = false;
871 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
872 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
873 if (IsCommaNeeded)
874 OS << ", ";
875 OS << printReg(Reg, TRI);
876 IsCommaNeeded = true;
877 }
878 }
879 }
880 OS << ")";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000881 break;
Francis Visoiu Mistrihbdaf8bf2017-12-14 10:03:14 +0000882 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000883 case MachineOperand::MO_Metadata:
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000884 getMetadata()->printAsOperand(OS, MST);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000885 break;
886 case MachineOperand::MO_MCSymbol:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000887 printSymbol(OS, *getMCSymbol());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000888 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000889 case MachineOperand::MO_CFIIndex: {
890 if (const MachineFunction *MF = getMFIfAvailable(*this))
891 printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
892 else
893 OS << "<cfi directive>";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000894 break;
Francis Visoiu Mistrih874ae6f2017-12-19 16:51:52 +0000895 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000896 case MachineOperand::MO_IntrinsicID: {
897 Intrinsic::ID ID = getIntrinsicID();
898 if (ID < Intrinsic::num_intrinsics)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000899 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000900 else if (IntrinsicInfo)
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000901 OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000902 else
Francis Visoiu Mistrihbbd610a2017-12-19 21:47:05 +0000903 OS << "intrinsic(" << ID << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000904 break;
905 }
906 case MachineOperand::MO_Predicate: {
907 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
Francis Visoiu Mistrihcb2683d2017-12-19 21:47:10 +0000908 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
909 << CmpInst::getPredicateName(Pred) << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000910 break;
911 }
912 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000913}
914
915#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
916LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
917#endif
918
919//===----------------------------------------------------------------------===//
920// MachineMemOperand Implementation
921//===----------------------------------------------------------------------===//
922
923/// getAddrSpace - Return the LLVM IR address space number that this pointer
924/// points into.
Yaxun Liu49477042017-12-02 22:13:22 +0000925unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000926
927/// isDereferenceable - Return true if V is always dereferenceable for
928/// Offset + Size byte.
929bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
930 const DataLayout &DL) const {
931 if (!V.is<const Value *>())
932 return false;
933
934 const Value *BasePtr = V.get<const Value *>();
935 if (BasePtr == nullptr)
936 return false;
937
938 return isDereferenceableAndAlignedPointer(
939 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
940}
941
942/// getConstantPool - Return a MachinePointerInfo record that refers to the
943/// constant pool.
944MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
945 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
946}
947
948/// getFixedStack - Return a MachinePointerInfo record that refers to the
949/// the specified FrameIndex.
950MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
951 int FI, int64_t Offset) {
952 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
953}
954
955MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
956 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
957}
958
959MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
960 return MachinePointerInfo(MF.getPSVManager().getGOT());
961}
962
963MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
964 int64_t Offset, uint8_t ID) {
965 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
966}
967
Yaxun Liu49477042017-12-02 22:13:22 +0000968MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
969 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
970}
971
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000972MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
973 uint64_t s, unsigned int a,
974 const AAMDNodes &AAInfo,
975 const MDNode *Ranges, SyncScope::ID SSID,
976 AtomicOrdering Ordering,
977 AtomicOrdering FailureOrdering)
978 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
979 AAInfo(AAInfo), Ranges(Ranges) {
980 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
981 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
982 "invalid pointer value");
983 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
984 assert((isLoad() || isStore()) && "Not a load/store!");
985
986 AtomicInfo.SSID = static_cast<unsigned>(SSID);
987 assert(getSyncScopeID() == SSID && "Value truncated");
988 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
989 assert(getOrdering() == Ordering && "Value truncated");
990 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
991 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
992}
993
994/// Profile - Gather unique data for the object.
995///
996void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
997 ID.AddInteger(getOffset());
998 ID.AddInteger(Size);
999 ID.AddPointer(getOpaqueValue());
1000 ID.AddInteger(getFlags());
1001 ID.AddInteger(getBaseAlignment());
1002}
1003
1004void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
1005 // The Value and Offset may differ due to CSE. But the flags and size
1006 // should be the same.
1007 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
1008 assert(MMO->getSize() == getSize() && "Size mismatch!");
1009
1010 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
1011 // Update the alignment value.
1012 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
1013 // Also update the base and offset, because the new alignment may
1014 // not be applicable with the old ones.
1015 PtrInfo = MMO->PtrInfo;
1016 }
1017}
1018
1019/// getAlignment - Return the minimum known alignment in bytes of the
1020/// actual memory reference.
1021uint64_t MachineMemOperand::getAlignment() const {
1022 return MinAlign(getBaseAlignment(), getOffset());
1023}
1024
1025void MachineMemOperand::print(raw_ostream &OS) const {
1026 ModuleSlotTracker DummyMST(nullptr);
1027 print(OS, DummyMST);
1028}
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001029
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001030void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001031 SmallVector<StringRef, 0> SSNs;
1032 LLVMContext Ctx;
1033 print(OS, MST, SSNs, Ctx, nullptr, nullptr);
1034}
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001035
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001036void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
1037 SmallVectorImpl<StringRef> &SSNs,
1038 const LLVMContext &Context,
1039 const MachineFrameInfo *MFI,
1040 const TargetInstrInfo *TII) const {
1041 OS << '(';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001042 if (isVolatile())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001043 OS << "volatile ";
1044 if (isNonTemporal())
1045 OS << "non-temporal ";
1046 if (isDereferenceable())
1047 OS << "dereferenceable ";
1048 if (isInvariant())
1049 OS << "invariant ";
1050 if (getFlags() & MachineMemOperand::MOTargetFlag1)
1051 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
1052 << "\" ";
1053 if (getFlags() & MachineMemOperand::MOTargetFlag2)
1054 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
1055 << "\" ";
1056 if (getFlags() & MachineMemOperand::MOTargetFlag3)
1057 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
1058 << "\" ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001059
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001060 assert((isLoad() || isStore()) &&
1061 "machine memory operand must be a load or store (or both)");
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001062 if (isLoad())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001063 OS << "load ";
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001064 if (isStore())
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001065 OS << "store ";
1066
1067 printSyncScope(OS, Context, getSyncScopeID(), SSNs);
1068
1069 if (getOrdering() != AtomicOrdering::NotAtomic)
1070 OS << toIRString(getOrdering()) << ' ';
1071 if (getFailureOrdering() != AtomicOrdering::NotAtomic)
1072 OS << toIRString(getFailureOrdering()) << ' ';
1073
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001074 OS << getSize();
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001075 if (const Value *Val = getValue()) {
1076 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1077 printIRValueReference(OS, *Val, MST);
1078 } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
1079 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1080 assert(PVal && "Expected a pseudo source value");
1081 switch (PVal->kind()) {
1082 case PseudoSourceValue::Stack:
1083 OS << "stack";
1084 break;
1085 case PseudoSourceValue::GOT:
1086 OS << "got";
1087 break;
1088 case PseudoSourceValue::JumpTable:
1089 OS << "jump-table";
1090 break;
1091 case PseudoSourceValue::ConstantPool:
1092 OS << "constant-pool";
1093 break;
1094 case PseudoSourceValue::FixedStack: {
1095 int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1096 bool IsFixed = true;
1097 printFrameIndex(OS, FrameIndex, IsFixed, MFI);
1098 break;
1099 }
1100 case PseudoSourceValue::GlobalValueCallEntry:
1101 OS << "call-entry ";
1102 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1103 OS, /*PrintType=*/false, MST);
1104 break;
1105 case PseudoSourceValue::ExternalSymbolCallEntry:
1106 OS << "call-entry &";
1107 printLLVMNameWithoutPrefix(
1108 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
1109 break;
1110 case PseudoSourceValue::TargetCustom:
Tim Renouf4db09602018-03-27 21:14:04 +00001111 // FIXME: This is not necessarily the correct MIR serialization format for
1112 // a custom pseudo source value, but at least it allows
1113 // -print-machineinstrs to work on a target with custom pseudo source
1114 // values.
1115 OS << "custom ";
1116 PVal->printCustom(OS);
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001117 break;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001118 }
1119 }
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001120 MachineOperand::printOperandOffset(OS, getOffset());
1121 if (getBaseAlignment() != getSize())
1122 OS << ", align " << getBaseAlignment();
1123 auto AAInfo = getAAInfo();
1124 if (AAInfo.TBAA) {
1125 OS << ", !tbaa ";
1126 AAInfo.TBAA->printAsOperand(OS, MST);
1127 }
1128 if (AAInfo.Scope) {
1129 OS << ", !alias.scope ";
1130 AAInfo.Scope->printAsOperand(OS, MST);
1131 }
1132 if (AAInfo.NoAlias) {
1133 OS << ", !noalias ";
1134 AAInfo.NoAlias->printAsOperand(OS, MST);
1135 }
1136 if (getRanges()) {
1137 OS << ", !range ";
1138 getRanges()->printAsOperand(OS, MST);
1139 }
1140 // FIXME: Implement addrspace printing/parsing in MIR.
1141 // For now, print this even though parsing it is not available in MIR.
1142 if (unsigned AS = getAddrSpace())
1143 OS << ", addrspace " << AS;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001144
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001145 OS << ')';
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001146}