1 //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 /// \file Methods common to all machine operands.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MachineOperand.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Analysis/Loads.h"
16 #include "llvm/Analysis/MemoryLocation.h"
17 #include "llvm/CodeGen/MIRPrinter.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineJumpTableInfo.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetInstrInfo.h"
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/IRPrintingPasses.h"
26 #include "llvm/IR/ModuleSlotTracker.h"
27 #include "llvm/Target/TargetIntrinsicInfo.h"
28 #include "llvm/Target/TargetMachine.h"
33 PrintRegMaskNumRegs("print-regmask-num-regs",
34 cl::desc("Number of registers to limit to when "
35 "printing regmask operands in IR dumps. "
37 cl::init(32), cl::Hidden
);
39 static const MachineFunction
*getMFIfAvailable(const MachineOperand
&MO
) {
40 if (const MachineInstr
*MI
= MO
.getParent())
41 if (const MachineBasicBlock
*MBB
= MI
->getParent())
42 if (const MachineFunction
*MF
= MBB
->getParent())
46 static MachineFunction
*getMFIfAvailable(MachineOperand
&MO
) {
47 return const_cast<MachineFunction
*>(
48 getMFIfAvailable(const_cast<const MachineOperand
&>(MO
)));
51 void MachineOperand::setReg(unsigned Reg
) {
55 // Clear the IsRenamable bit to keep it conservatively correct.
58 // Otherwise, we have to change the register. If this operand is embedded
59 // into a machine function, we need to update the old and new register's
61 if (MachineFunction
*MF
= getMFIfAvailable(*this)) {
62 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
63 MRI
.removeRegOperandFromUseList(this);
64 SmallContents
.RegNo
= Reg
;
65 MRI
.addRegOperandToUseList(this);
69 // Otherwise, just change the register, no problem. :)
70 SmallContents
.RegNo
= Reg
;
73 void MachineOperand::substVirtReg(unsigned Reg
, unsigned SubIdx
,
74 const TargetRegisterInfo
&TRI
) {
75 assert(TargetRegisterInfo::isVirtualRegister(Reg
));
76 if (SubIdx
&& getSubReg())
77 SubIdx
= TRI
.composeSubRegIndices(SubIdx
, getSubReg());
83 void MachineOperand::substPhysReg(unsigned Reg
, const TargetRegisterInfo
&TRI
) {
84 assert(TargetRegisterInfo::isPhysicalRegister(Reg
));
86 Reg
= TRI
.getSubReg(Reg
, getSubReg());
87 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
88 // That won't happen in legal code.
96 /// Change a def to a use, or a use to a def.
97 void MachineOperand::setIsDef(bool Val
) {
98 assert(isReg() && "Wrong MachineOperand accessor");
99 assert((!Val
|| !isDebug()) && "Marking a debug operation as def");
102 assert(!IsDeadOrKill
&& "Changing def/use with dead/kill set not supported");
103 // MRI may keep uses and defs in different list positions.
104 if (MachineFunction
*MF
= getMFIfAvailable(*this)) {
105 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
106 MRI
.removeRegOperandFromUseList(this);
108 MRI
.addRegOperandToUseList(this);
114 bool MachineOperand::isRenamable() const {
115 assert(isReg() && "Wrong MachineOperand accessor");
116 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
117 "isRenamable should only be checked on physical registers");
121 const MachineInstr
*MI
= getParent();
126 return !MI
->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle
);
128 assert(isUse() && "Reg is not def or use");
129 return !MI
->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle
);
132 void MachineOperand::setIsRenamable(bool Val
) {
133 assert(isReg() && "Wrong MachineOperand accessor");
134 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
135 "setIsRenamable should only be called on physical registers");
139 // If this operand is currently a register operand, and if this is in a
140 // function, deregister the operand from the register's use/def list.
141 void MachineOperand::removeRegFromUses() {
142 if (!isReg() || !isOnRegUseList())
145 if (MachineFunction
*MF
= getMFIfAvailable(*this))
146 MF
->getRegInfo().removeRegOperandFromUseList(this);
149 /// ChangeToImmediate - Replace this operand with a new immediate operand of
150 /// the specified value. If an operand is known to be an immediate already,
151 /// the setImm method should be used.
152 void MachineOperand::ChangeToImmediate(int64_t ImmVal
) {
153 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
157 OpKind
= MO_Immediate
;
158 Contents
.ImmVal
= ImmVal
;
161 void MachineOperand::ChangeToFPImmediate(const ConstantFP
*FPImm
) {
162 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
166 OpKind
= MO_FPImmediate
;
167 Contents
.CFP
= FPImm
;
170 void MachineOperand::ChangeToES(const char *SymName
,
171 unsigned char TargetFlags
) {
172 assert((!isReg() || !isTied()) &&
173 "Cannot change a tied operand into an external symbol");
177 OpKind
= MO_ExternalSymbol
;
178 Contents
.OffsetedInfo
.Val
.SymbolName
= SymName
;
179 setOffset(0); // Offset is always 0.
180 setTargetFlags(TargetFlags
);
183 void MachineOperand::ChangeToMCSymbol(MCSymbol
*Sym
) {
184 assert((!isReg() || !isTied()) &&
185 "Cannot change a tied operand into an MCSymbol");
189 OpKind
= MO_MCSymbol
;
193 void MachineOperand::ChangeToFrameIndex(int Idx
) {
194 assert((!isReg() || !isTied()) &&
195 "Cannot change a tied operand into a FrameIndex");
199 OpKind
= MO_FrameIndex
;
203 void MachineOperand::ChangeToTargetIndex(unsigned Idx
, int64_t Offset
,
204 unsigned char TargetFlags
) {
205 assert((!isReg() || !isTied()) &&
206 "Cannot change a tied operand into a FrameIndex");
210 OpKind
= MO_TargetIndex
;
213 setTargetFlags(TargetFlags
);
216 /// ChangeToRegister - Replace this operand with a new register operand of
217 /// the specified value. If an operand is known to be an register already,
218 /// the setReg method should be used.
219 void MachineOperand::ChangeToRegister(unsigned Reg
, bool isDef
, bool isImp
,
220 bool isKill
, bool isDead
, bool isUndef
,
222 MachineRegisterInfo
*RegInfo
= nullptr;
223 if (MachineFunction
*MF
= getMFIfAvailable(*this))
224 RegInfo
= &MF
->getRegInfo();
225 // If this operand is already a register operand, remove it from the
226 // register's use/def lists.
227 bool WasReg
= isReg();
228 if (RegInfo
&& WasReg
)
229 RegInfo
->removeRegOperandFromUseList(this);
231 // Change this to a register and set the reg#.
232 assert(!(isDead
&& !isDef
) && "Dead flag on non-def");
233 assert(!(isKill
&& isDef
) && "Kill flag on def");
234 OpKind
= MO_Register
;
235 SmallContents
.RegNo
= Reg
;
236 SubReg_TargetFlags
= 0;
239 IsDeadOrKill
= isKill
| isDead
;
242 IsInternalRead
= false;
243 IsEarlyClobber
= false;
245 // Ensure isOnRegUseList() returns false.
246 Contents
.Reg
.Prev
= nullptr;
247 // Preserve the tie when the operand was already a register.
251 // If this operand is embedded in a function, add the operand to the
252 // register's use/def list.
254 RegInfo
->addRegOperandToUseList(this);
257 /// isIdenticalTo - Return true if this operand is identical to the specified
258 /// operand. Note that this should stay in sync with the hash_value overload
260 bool MachineOperand::isIdenticalTo(const MachineOperand
&Other
) const {
261 if (getType() != Other
.getType() ||
262 getTargetFlags() != Other
.getTargetFlags())
266 case MachineOperand::MO_Register
:
267 return getReg() == Other
.getReg() && isDef() == Other
.isDef() &&
268 getSubReg() == Other
.getSubReg();
269 case MachineOperand::MO_Immediate
:
270 return getImm() == Other
.getImm();
271 case MachineOperand::MO_CImmediate
:
272 return getCImm() == Other
.getCImm();
273 case MachineOperand::MO_FPImmediate
:
274 return getFPImm() == Other
.getFPImm();
275 case MachineOperand::MO_MachineBasicBlock
:
276 return getMBB() == Other
.getMBB();
277 case MachineOperand::MO_FrameIndex
:
278 return getIndex() == Other
.getIndex();
279 case MachineOperand::MO_ConstantPoolIndex
:
280 case MachineOperand::MO_TargetIndex
:
281 return getIndex() == Other
.getIndex() && getOffset() == Other
.getOffset();
282 case MachineOperand::MO_JumpTableIndex
:
283 return getIndex() == Other
.getIndex();
284 case MachineOperand::MO_GlobalAddress
:
285 return getGlobal() == Other
.getGlobal() && getOffset() == Other
.getOffset();
286 case MachineOperand::MO_ExternalSymbol
:
287 return strcmp(getSymbolName(), Other
.getSymbolName()) == 0 &&
288 getOffset() == Other
.getOffset();
289 case MachineOperand::MO_BlockAddress
:
290 return getBlockAddress() == Other
.getBlockAddress() &&
291 getOffset() == Other
.getOffset();
292 case MachineOperand::MO_RegisterMask
:
293 case MachineOperand::MO_RegisterLiveOut
: {
294 // Shallow compare of the two RegMasks
295 const uint32_t *RegMask
= getRegMask();
296 const uint32_t *OtherRegMask
= Other
.getRegMask();
297 if (RegMask
== OtherRegMask
)
300 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
301 // Calculate the size of the RegMask
302 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
303 unsigned RegMaskSize
= (TRI
->getNumRegs() + 31) / 32;
305 // Deep compare of the two RegMasks
306 return std::equal(RegMask
, RegMask
+ RegMaskSize
, OtherRegMask
);
308 // We don't know the size of the RegMask, so we can't deep compare the two
312 case MachineOperand::MO_MCSymbol
:
313 return getMCSymbol() == Other
.getMCSymbol();
314 case MachineOperand::MO_CFIIndex
:
315 return getCFIIndex() == Other
.getCFIIndex();
316 case MachineOperand::MO_Metadata
:
317 return getMetadata() == Other
.getMetadata();
318 case MachineOperand::MO_IntrinsicID
:
319 return getIntrinsicID() == Other
.getIntrinsicID();
320 case MachineOperand::MO_Predicate
:
321 return getPredicate() == Other
.getPredicate();
323 llvm_unreachable("Invalid machine operand type");
326 // Note: this must stay exactly in sync with isIdenticalTo above.
327 hash_code
llvm::hash_value(const MachineOperand
&MO
) {
328 switch (MO
.getType()) {
329 case MachineOperand::MO_Register
:
330 // Register operands don't have target flags.
331 return hash_combine(MO
.getType(), MO
.getReg(), MO
.getSubReg(), MO
.isDef());
332 case MachineOperand::MO_Immediate
:
333 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getImm());
334 case MachineOperand::MO_CImmediate
:
335 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getCImm());
336 case MachineOperand::MO_FPImmediate
:
337 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getFPImm());
338 case MachineOperand::MO_MachineBasicBlock
:
339 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMBB());
340 case MachineOperand::MO_FrameIndex
:
341 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex());
342 case MachineOperand::MO_ConstantPoolIndex
:
343 case MachineOperand::MO_TargetIndex
:
344 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex(),
346 case MachineOperand::MO_JumpTableIndex
:
347 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex());
348 case MachineOperand::MO_ExternalSymbol
:
349 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getOffset(),
351 case MachineOperand::MO_GlobalAddress
:
352 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getGlobal(),
354 case MachineOperand::MO_BlockAddress
:
355 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getBlockAddress(),
357 case MachineOperand::MO_RegisterMask
:
358 case MachineOperand::MO_RegisterLiveOut
:
359 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getRegMask());
360 case MachineOperand::MO_Metadata
:
361 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMetadata());
362 case MachineOperand::MO_MCSymbol
:
363 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMCSymbol());
364 case MachineOperand::MO_CFIIndex
:
365 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getCFIIndex());
366 case MachineOperand::MO_IntrinsicID
:
367 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIntrinsicID());
368 case MachineOperand::MO_Predicate
:
369 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getPredicate());
371 llvm_unreachable("Invalid machine operand type");
374 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
376 static void tryToGetTargetInfo(const MachineOperand
&MO
,
377 const TargetRegisterInfo
*&TRI
,
378 const TargetIntrinsicInfo
*&IntrinsicInfo
) {
379 if (const MachineFunction
*MF
= getMFIfAvailable(MO
)) {
380 TRI
= MF
->getSubtarget().getRegisterInfo();
381 IntrinsicInfo
= MF
->getTarget().getIntrinsicInfo();
385 static const char *getTargetIndexName(const MachineFunction
&MF
, int Index
) {
386 const auto *TII
= MF
.getSubtarget().getInstrInfo();
387 assert(TII
&& "expected instruction info");
388 auto Indices
= TII
->getSerializableTargetIndices();
389 auto Found
= find_if(Indices
, [&](const std::pair
<int, const char *> &I
) {
390 return I
.first
== Index
;
392 if (Found
!= Indices
.end())
393 return Found
->second
;
397 static const char *getTargetFlagName(const TargetInstrInfo
*TII
, unsigned TF
) {
398 auto Flags
= TII
->getSerializableDirectMachineOperandTargetFlags();
399 for (const auto &I
: Flags
) {
407 static void printCFIRegister(unsigned DwarfReg
, raw_ostream
&OS
,
408 const TargetRegisterInfo
*TRI
) {
410 OS
<< "%dwarfreg." << DwarfReg
;
414 int Reg
= TRI
->getLLVMRegNum(DwarfReg
, true);
419 OS
<< printReg(Reg
, TRI
);
422 static void printIRBlockReference(raw_ostream
&OS
, const BasicBlock
&BB
,
423 ModuleSlotTracker
&MST
) {
426 printLLVMNameWithoutPrefix(OS
, BB
.getName());
430 if (const Function
*F
= BB
.getParent()) {
431 if (F
== MST
.getCurrentFunction()) {
432 Slot
= MST
.getLocalSlot(&BB
);
433 } else if (const Module
*M
= F
->getParent()) {
434 ModuleSlotTracker
CustomMST(M
, /*ShouldInitializeAllMetadata=*/false);
435 CustomMST
.incorporateFunction(*F
);
436 Slot
= CustomMST
.getLocalSlot(&BB
);
440 MachineOperand::printIRSlotNumber(OS
, *Slot
);
445 static void printIRValueReference(raw_ostream
&OS
, const Value
&V
,
446 ModuleSlotTracker
&MST
) {
447 if (isa
<GlobalValue
>(V
)) {
448 V
.printAsOperand(OS
, /*PrintType=*/false, MST
);
451 if (isa
<Constant
>(V
)) {
452 // Machine memory operands can load/store to/from constant value pointers.
454 V
.printAsOperand(OS
, /*PrintType=*/true, MST
);
460 printLLVMNameWithoutPrefix(OS
, V
.getName());
463 int Slot
= MST
.getCurrentFunction() ? MST
.getLocalSlot(&V
) : -1;
464 MachineOperand::printIRSlotNumber(OS
, Slot
);
467 static void printSyncScope(raw_ostream
&OS
, const LLVMContext
&Context
,
469 SmallVectorImpl
<StringRef
> &SSNs
) {
471 case SyncScope::System
:
475 Context
.getSyncScopeNames(SSNs
);
477 OS
<< "syncscope(\"";
478 printEscapedString(SSNs
[SSID
], OS
);
484 static const char *getTargetMMOFlagName(const TargetInstrInfo
&TII
,
486 auto Flags
= TII
.getSerializableMachineMemOperandTargetFlags();
487 for (const auto &I
: Flags
) {
488 if (I
.first
== TMMOFlag
) {
495 static void printFrameIndex(raw_ostream
& OS
, int FrameIndex
, bool IsFixed
,
496 const MachineFrameInfo
*MFI
) {
499 IsFixed
= MFI
->isFixedObjectIndex(FrameIndex
);
500 if (const AllocaInst
*Alloca
= MFI
->getObjectAllocation(FrameIndex
))
501 if (Alloca
->hasName())
502 Name
= Alloca
->getName();
504 FrameIndex
-= MFI
->getObjectIndexBegin();
506 MachineOperand::printStackObjectReference(OS
, FrameIndex
, IsFixed
, Name
);
509 void MachineOperand::printSubRegIdx(raw_ostream
&OS
, uint64_t Index
,
510 const TargetRegisterInfo
*TRI
) {
513 OS
<< TRI
->getSubRegIndexName(Index
);
518 void MachineOperand::printTargetFlags(raw_ostream
&OS
,
519 const MachineOperand
&Op
) {
520 if (!Op
.getTargetFlags())
522 const MachineFunction
*MF
= getMFIfAvailable(Op
);
526 const auto *TII
= MF
->getSubtarget().getInstrInfo();
527 assert(TII
&& "expected instruction info");
528 auto Flags
= TII
->decomposeMachineOperandsTargetFlags(Op
.getTargetFlags());
529 OS
<< "target-flags(";
530 const bool HasDirectFlags
= Flags
.first
;
531 const bool HasBitmaskFlags
= Flags
.second
;
532 if (!HasDirectFlags
&& !HasBitmaskFlags
) {
536 if (HasDirectFlags
) {
537 if (const auto *Name
= getTargetFlagName(TII
, Flags
.first
))
540 OS
<< "<unknown target flag>";
542 if (!HasBitmaskFlags
) {
546 bool IsCommaNeeded
= HasDirectFlags
;
547 unsigned BitMask
= Flags
.second
;
548 auto BitMasks
= TII
->getSerializableBitmaskMachineOperandTargetFlags();
549 for (const auto &Mask
: BitMasks
) {
550 // Check if the flag's bitmask has the bits of the current mask set.
551 if ((BitMask
& Mask
.first
) == Mask
.first
) {
554 IsCommaNeeded
= true;
556 // Clear the bits which were serialized from the flag's bitmask.
557 BitMask
&= ~(Mask
.first
);
561 // When the resulting flag's bitmask isn't zero, we know that we didn't
562 // serialize all of the bit flags.
565 OS
<< "<unknown bitmask target flag>";
570 void MachineOperand::printSymbol(raw_ostream
&OS
, MCSymbol
&Sym
) {
571 OS
<< "<mcsymbol " << Sym
<< ">";
574 void MachineOperand::printStackObjectReference(raw_ostream
&OS
,
576 bool IsFixed
, StringRef Name
) {
578 OS
<< "%fixed-stack." << FrameIndex
;
582 OS
<< "%stack." << FrameIndex
;
587 void MachineOperand::printOperandOffset(raw_ostream
&OS
, int64_t Offset
) {
591 OS
<< " - " << -Offset
;
594 OS
<< " + " << Offset
;
597 void MachineOperand::printIRSlotNumber(raw_ostream
&OS
, int Slot
) {
604 static void printCFI(raw_ostream
&OS
, const MCCFIInstruction
&CFI
,
605 const TargetRegisterInfo
*TRI
) {
606 switch (CFI
.getOperation()) {
607 case MCCFIInstruction::OpSameValue
:
609 if (MCSymbol
*Label
= CFI
.getLabel())
610 MachineOperand::printSymbol(OS
, *Label
);
611 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
613 case MCCFIInstruction::OpRememberState
:
614 OS
<< "remember_state ";
615 if (MCSymbol
*Label
= CFI
.getLabel())
616 MachineOperand::printSymbol(OS
, *Label
);
618 case MCCFIInstruction::OpRestoreState
:
619 OS
<< "restore_state ";
620 if (MCSymbol
*Label
= CFI
.getLabel())
621 MachineOperand::printSymbol(OS
, *Label
);
623 case MCCFIInstruction::OpOffset
:
625 if (MCSymbol
*Label
= CFI
.getLabel())
626 MachineOperand::printSymbol(OS
, *Label
);
627 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
628 OS
<< ", " << CFI
.getOffset();
630 case MCCFIInstruction::OpDefCfaRegister
:
631 OS
<< "def_cfa_register ";
632 if (MCSymbol
*Label
= CFI
.getLabel())
633 MachineOperand::printSymbol(OS
, *Label
);
634 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
636 case MCCFIInstruction::OpDefCfaOffset
:
637 OS
<< "def_cfa_offset ";
638 if (MCSymbol
*Label
= CFI
.getLabel())
639 MachineOperand::printSymbol(OS
, *Label
);
640 OS
<< CFI
.getOffset();
642 case MCCFIInstruction::OpDefCfa
:
644 if (MCSymbol
*Label
= CFI
.getLabel())
645 MachineOperand::printSymbol(OS
, *Label
);
646 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
647 OS
<< ", " << CFI
.getOffset();
649 case MCCFIInstruction::OpRelOffset
:
651 if (MCSymbol
*Label
= CFI
.getLabel())
652 MachineOperand::printSymbol(OS
, *Label
);
653 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
654 OS
<< ", " << CFI
.getOffset();
656 case MCCFIInstruction::OpAdjustCfaOffset
:
657 OS
<< "adjust_cfa_offset ";
658 if (MCSymbol
*Label
= CFI
.getLabel())
659 MachineOperand::printSymbol(OS
, *Label
);
660 OS
<< CFI
.getOffset();
662 case MCCFIInstruction::OpRestore
:
664 if (MCSymbol
*Label
= CFI
.getLabel())
665 MachineOperand::printSymbol(OS
, *Label
);
666 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
668 case MCCFIInstruction::OpEscape
: {
670 if (MCSymbol
*Label
= CFI
.getLabel())
671 MachineOperand::printSymbol(OS
, *Label
);
672 if (!CFI
.getValues().empty()) {
673 size_t e
= CFI
.getValues().size() - 1;
674 for (size_t i
= 0; i
< e
; ++i
)
675 OS
<< format("0x%02x", uint8_t(CFI
.getValues()[i
])) << ", ";
676 OS
<< format("0x%02x", uint8_t(CFI
.getValues()[e
])) << ", ";
680 case MCCFIInstruction::OpUndefined
:
682 if (MCSymbol
*Label
= CFI
.getLabel())
683 MachineOperand::printSymbol(OS
, *Label
);
684 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
686 case MCCFIInstruction::OpRegister
:
688 if (MCSymbol
*Label
= CFI
.getLabel())
689 MachineOperand::printSymbol(OS
, *Label
);
690 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
692 printCFIRegister(CFI
.getRegister2(), OS
, TRI
);
694 case MCCFIInstruction::OpWindowSave
:
695 OS
<< "window_save ";
696 if (MCSymbol
*Label
= CFI
.getLabel())
697 MachineOperand::printSymbol(OS
, *Label
);
699 case MCCFIInstruction::OpNegateRAState
:
700 OS
<< "negate_ra_sign_state ";
701 if (MCSymbol
*Label
= CFI
.getLabel())
702 MachineOperand::printSymbol(OS
, *Label
);
705 // TODO: Print the other CFI Operations.
706 OS
<< "<unserializable cfi directive>";
711 void MachineOperand::print(raw_ostream
&OS
, const TargetRegisterInfo
*TRI
,
712 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
713 print(OS
, LLT
{}, TRI
, IntrinsicInfo
);
716 void MachineOperand::print(raw_ostream
&OS
, LLT TypeToPrint
,
717 const TargetRegisterInfo
*TRI
,
718 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
719 tryToGetTargetInfo(*this, TRI
, IntrinsicInfo
);
720 ModuleSlotTracker
DummyMST(nullptr);
721 print(OS
, DummyMST
, TypeToPrint
, /*PrintDef=*/false, /*IsStandalone=*/true,
722 /*ShouldPrintRegisterTies=*/true,
723 /*TiedOperandIdx=*/0, TRI
, IntrinsicInfo
);
726 void MachineOperand::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
727 LLT TypeToPrint
, bool PrintDef
, bool IsStandalone
,
728 bool ShouldPrintRegisterTies
,
729 unsigned TiedOperandIdx
,
730 const TargetRegisterInfo
*TRI
,
731 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
732 printTargetFlags(OS
, *this);
734 case MachineOperand::MO_Register
: {
735 unsigned Reg
= getReg();
737 OS
<< (isDef() ? "implicit-def " : "implicit ");
738 else if (PrintDef
&& isDef())
739 // Print the 'def' flag only when the operand is defined after '='.
741 if (isInternalRead())
749 if (isEarlyClobber())
750 OS
<< "early-clobber ";
751 if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable())
753 // isDebug() is exactly true for register operands of a DBG_VALUE. So we
754 // simply infer it when parsing and do not need to print it.
756 const MachineRegisterInfo
*MRI
= nullptr;
757 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
758 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
759 MRI
= &MF
->getRegInfo();
763 OS
<< printReg(Reg
, TRI
, 0, MRI
);
764 // Print the sub register.
765 if (unsigned SubReg
= getSubReg()) {
767 OS
<< '.' << TRI
->getSubRegIndexName(SubReg
);
769 OS
<< ".subreg" << SubReg
;
771 // Print the register class / bank.
772 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
773 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
774 const MachineRegisterInfo
&MRI
= MF
->getRegInfo();
775 if (IsStandalone
|| !PrintDef
|| MRI
.def_empty(Reg
)) {
777 OS
<< printRegClassOrBank(Reg
, MRI
, TRI
);
782 if (ShouldPrintRegisterTies
&& isTied() && !isDef())
783 OS
<< "(tied-def " << TiedOperandIdx
<< ")";
785 if (TypeToPrint
.isValid())
786 OS
<< '(' << TypeToPrint
<< ')';
789 case MachineOperand::MO_Immediate
:
792 case MachineOperand::MO_CImmediate
:
793 getCImm()->printAsOperand(OS
, /*PrintType=*/true, MST
);
795 case MachineOperand::MO_FPImmediate
:
796 getFPImm()->printAsOperand(OS
, /*PrintType=*/true, MST
);
798 case MachineOperand::MO_MachineBasicBlock
:
799 OS
<< printMBBReference(*getMBB());
801 case MachineOperand::MO_FrameIndex
: {
802 int FrameIndex
= getIndex();
803 bool IsFixed
= false;
804 const MachineFrameInfo
*MFI
= nullptr;
805 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
806 MFI
= &MF
->getFrameInfo();
807 printFrameIndex(OS
, FrameIndex
, IsFixed
, MFI
);
810 case MachineOperand::MO_ConstantPoolIndex
:
811 OS
<< "%const." << getIndex();
812 printOperandOffset(OS
, getOffset());
814 case MachineOperand::MO_TargetIndex
: {
815 OS
<< "target-index(";
816 const char *Name
= "<unknown>";
817 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
818 if (const auto *TargetIndexName
= getTargetIndexName(*MF
, getIndex()))
819 Name
= TargetIndexName
;
821 printOperandOffset(OS
, getOffset());
824 case MachineOperand::MO_JumpTableIndex
:
825 OS
<< printJumpTableEntryReference(getIndex());
827 case MachineOperand::MO_GlobalAddress
:
828 getGlobal()->printAsOperand(OS
, /*PrintType=*/false, MST
);
829 printOperandOffset(OS
, getOffset());
831 case MachineOperand::MO_ExternalSymbol
: {
832 StringRef Name
= getSymbolName();
837 printLLVMNameWithoutPrefix(OS
, Name
);
839 printOperandOffset(OS
, getOffset());
842 case MachineOperand::MO_BlockAddress
: {
843 OS
<< "blockaddress(";
844 getBlockAddress()->getFunction()->printAsOperand(OS
, /*PrintType=*/false,
847 printIRBlockReference(OS
, *getBlockAddress()->getBasicBlock(), MST
);
849 MachineOperand::printOperandOffset(OS
, getOffset());
852 case MachineOperand::MO_RegisterMask
: {
855 unsigned NumRegsInMask
= 0;
856 unsigned NumRegsEmitted
= 0;
857 for (unsigned i
= 0; i
< TRI
->getNumRegs(); ++i
) {
858 unsigned MaskWord
= i
/ 32;
859 unsigned MaskBit
= i
% 32;
860 if (getRegMask()[MaskWord
] & (1 << MaskBit
)) {
861 if (PrintRegMaskNumRegs
< 0 ||
862 NumRegsEmitted
<= static_cast<unsigned>(PrintRegMaskNumRegs
)) {
863 OS
<< " " << printReg(i
, TRI
);
869 if (NumRegsEmitted
!= NumRegsInMask
)
870 OS
<< " and " << (NumRegsInMask
- NumRegsEmitted
) << " more...";
877 case MachineOperand::MO_RegisterLiveOut
: {
878 const uint32_t *RegMask
= getRegLiveOut();
883 bool IsCommaNeeded
= false;
884 for (unsigned Reg
= 0, E
= TRI
->getNumRegs(); Reg
< E
; ++Reg
) {
885 if (RegMask
[Reg
/ 32] & (1U << (Reg
% 32))) {
888 OS
<< printReg(Reg
, TRI
);
889 IsCommaNeeded
= true;
896 case MachineOperand::MO_Metadata
:
897 getMetadata()->printAsOperand(OS
, MST
);
899 case MachineOperand::MO_MCSymbol
:
900 printSymbol(OS
, *getMCSymbol());
902 case MachineOperand::MO_CFIIndex
: {
903 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
904 printCFI(OS
, MF
->getFrameInstructions()[getCFIIndex()], TRI
);
906 OS
<< "<cfi directive>";
909 case MachineOperand::MO_IntrinsicID
: {
910 Intrinsic::ID ID
= getIntrinsicID();
911 if (ID
< Intrinsic::num_intrinsics
)
912 OS
<< "intrinsic(@" << Intrinsic::getName(ID
, None
) << ')';
913 else if (IntrinsicInfo
)
914 OS
<< "intrinsic(@" << IntrinsicInfo
->getName(ID
) << ')';
916 OS
<< "intrinsic(" << ID
<< ')';
919 case MachineOperand::MO_Predicate
: {
920 auto Pred
= static_cast<CmpInst::Predicate
>(getPredicate());
921 OS
<< (CmpInst::isIntPredicate(Pred
) ? "int" : "float") << "pred("
922 << CmpInst::getPredicateName(Pred
) << ')';
928 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
929 LLVM_DUMP_METHOD
void MachineOperand::dump() const { dbgs() << *this << '\n'; }
932 //===----------------------------------------------------------------------===//
933 // MachineMemOperand Implementation
934 //===----------------------------------------------------------------------===//
936 /// getAddrSpace - Return the LLVM IR address space number that this pointer
938 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace
; }
940 /// isDereferenceable - Return true if V is always dereferenceable for
941 /// Offset + Size byte.
942 bool MachinePointerInfo::isDereferenceable(unsigned Size
, LLVMContext
&C
,
943 const DataLayout
&DL
) const {
944 if (!V
.is
<const Value
*>())
947 const Value
*BasePtr
= V
.get
<const Value
*>();
948 if (BasePtr
== nullptr)
951 return isDereferenceableAndAlignedPointer(
952 BasePtr
, 1, APInt(DL
.getPointerSizeInBits(), Offset
+ Size
), DL
);
955 /// getConstantPool - Return a MachinePointerInfo record that refers to the
957 MachinePointerInfo
MachinePointerInfo::getConstantPool(MachineFunction
&MF
) {
958 return MachinePointerInfo(MF
.getPSVManager().getConstantPool());
961 /// getFixedStack - Return a MachinePointerInfo record that refers to the
962 /// the specified FrameIndex.
963 MachinePointerInfo
MachinePointerInfo::getFixedStack(MachineFunction
&MF
,
964 int FI
, int64_t Offset
) {
965 return MachinePointerInfo(MF
.getPSVManager().getFixedStack(FI
), Offset
);
968 MachinePointerInfo
MachinePointerInfo::getJumpTable(MachineFunction
&MF
) {
969 return MachinePointerInfo(MF
.getPSVManager().getJumpTable());
972 MachinePointerInfo
MachinePointerInfo::getGOT(MachineFunction
&MF
) {
973 return MachinePointerInfo(MF
.getPSVManager().getGOT());
976 MachinePointerInfo
MachinePointerInfo::getStack(MachineFunction
&MF
,
977 int64_t Offset
, uint8_t ID
) {
978 return MachinePointerInfo(MF
.getPSVManager().getStack(), Offset
, ID
);
981 MachinePointerInfo
MachinePointerInfo::getUnknownStack(MachineFunction
&MF
) {
982 return MachinePointerInfo(MF
.getDataLayout().getAllocaAddrSpace());
985 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo
, Flags f
,
986 uint64_t s
, uint64_t a
,
987 const AAMDNodes
&AAInfo
,
988 const MDNode
*Ranges
, SyncScope::ID SSID
,
989 AtomicOrdering Ordering
,
990 AtomicOrdering FailureOrdering
)
991 : PtrInfo(ptrinfo
), Size(s
), FlagVals(f
), BaseAlignLog2(Log2_32(a
) + 1),
992 AAInfo(AAInfo
), Ranges(Ranges
) {
993 assert((PtrInfo
.V
.isNull() || PtrInfo
.V
.is
<const PseudoSourceValue
*>() ||
994 isa
<PointerType
>(PtrInfo
.V
.get
<const Value
*>()->getType())) &&
995 "invalid pointer value");
996 assert(getBaseAlignment() == a
&& a
!= 0 && "Alignment is not a power of 2!");
997 assert((isLoad() || isStore()) && "Not a load/store!");
999 AtomicInfo
.SSID
= static_cast<unsigned>(SSID
);
1000 assert(getSyncScopeID() == SSID
&& "Value truncated");
1001 AtomicInfo
.Ordering
= static_cast<unsigned>(Ordering
);
1002 assert(getOrdering() == Ordering
&& "Value truncated");
1003 AtomicInfo
.FailureOrdering
= static_cast<unsigned>(FailureOrdering
);
1004 assert(getFailureOrdering() == FailureOrdering
&& "Value truncated");
1007 /// Profile - Gather unique data for the object.
1009 void MachineMemOperand::Profile(FoldingSetNodeID
&ID
) const {
1010 ID
.AddInteger(getOffset());
1011 ID
.AddInteger(Size
);
1012 ID
.AddPointer(getOpaqueValue());
1013 ID
.AddInteger(getFlags());
1014 ID
.AddInteger(getBaseAlignment());
1017 void MachineMemOperand::refineAlignment(const MachineMemOperand
*MMO
) {
1018 // The Value and Offset may differ due to CSE. But the flags and size
1019 // should be the same.
1020 assert(MMO
->getFlags() == getFlags() && "Flags mismatch!");
1021 assert(MMO
->getSize() == getSize() && "Size mismatch!");
1023 if (MMO
->getBaseAlignment() >= getBaseAlignment()) {
1024 // Update the alignment value.
1025 BaseAlignLog2
= Log2_32(MMO
->getBaseAlignment()) + 1;
1026 // Also update the base and offset, because the new alignment may
1027 // not be applicable with the old ones.
1028 PtrInfo
= MMO
->PtrInfo
;
1032 /// getAlignment - Return the minimum known alignment in bytes of the
1033 /// actual memory reference.
1034 uint64_t MachineMemOperand::getAlignment() const {
1035 return MinAlign(getBaseAlignment(), getOffset());
1038 void MachineMemOperand::print(raw_ostream
&OS
) const {
1039 ModuleSlotTracker
DummyMST(nullptr);
1040 print(OS
, DummyMST
);
1043 void MachineMemOperand::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
) const {
1044 SmallVector
<StringRef
, 0> SSNs
;
1046 print(OS
, MST
, SSNs
, Ctx
, nullptr, nullptr);
1049 void MachineMemOperand::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
1050 SmallVectorImpl
<StringRef
> &SSNs
,
1051 const LLVMContext
&Context
,
1052 const MachineFrameInfo
*MFI
,
1053 const TargetInstrInfo
*TII
) const {
1057 if (isNonTemporal())
1058 OS
<< "non-temporal ";
1059 if (isDereferenceable())
1060 OS
<< "dereferenceable ";
1063 if (getFlags() & MachineMemOperand::MOTargetFlag1
)
1064 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag1
)
1066 if (getFlags() & MachineMemOperand::MOTargetFlag2
)
1067 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag2
)
1069 if (getFlags() & MachineMemOperand::MOTargetFlag3
)
1070 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag3
)
1073 assert((isLoad() || isStore()) &&
1074 "machine memory operand must be a load or store (or both)");
1080 printSyncScope(OS
, Context
, getSyncScopeID(), SSNs
);
1082 if (getOrdering() != AtomicOrdering::NotAtomic
)
1083 OS
<< toIRString(getOrdering()) << ' ';
1084 if (getFailureOrdering() != AtomicOrdering::NotAtomic
)
1085 OS
<< toIRString(getFailureOrdering()) << ' ';
1087 if (getSize() == MemoryLocation::UnknownSize
)
1088 OS
<< "unknown-size";
1092 if (const Value
*Val
= getValue()) {
1093 OS
<< ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1094 printIRValueReference(OS
, *Val
, MST
);
1095 } else if (const PseudoSourceValue
*PVal
= getPseudoValue()) {
1096 OS
<< ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1097 assert(PVal
&& "Expected a pseudo source value");
1098 switch (PVal
->kind()) {
1099 case PseudoSourceValue::Stack
:
1102 case PseudoSourceValue::GOT
:
1105 case PseudoSourceValue::JumpTable
:
1108 case PseudoSourceValue::ConstantPool
:
1109 OS
<< "constant-pool";
1111 case PseudoSourceValue::FixedStack
: {
1112 int FrameIndex
= cast
<FixedStackPseudoSourceValue
>(PVal
)->getFrameIndex();
1113 bool IsFixed
= true;
1114 printFrameIndex(OS
, FrameIndex
, IsFixed
, MFI
);
1117 case PseudoSourceValue::GlobalValueCallEntry
:
1118 OS
<< "call-entry ";
1119 cast
<GlobalValuePseudoSourceValue
>(PVal
)->getValue()->printAsOperand(
1120 OS
, /*PrintType=*/false, MST
);
1122 case PseudoSourceValue::ExternalSymbolCallEntry
:
1123 OS
<< "call-entry &";
1124 printLLVMNameWithoutPrefix(
1125 OS
, cast
<ExternalSymbolPseudoSourceValue
>(PVal
)->getSymbol());
1127 case PseudoSourceValue::TargetCustom
:
1128 // FIXME: This is not necessarily the correct MIR serialization format for
1129 // a custom pseudo source value, but at least it allows
1130 // -print-machineinstrs to work on a target with custom pseudo source
1133 PVal
->printCustom(OS
);
1137 MachineOperand::printOperandOffset(OS
, getOffset());
1138 if (getBaseAlignment() != getSize())
1139 OS
<< ", align " << getBaseAlignment();
1140 auto AAInfo
= getAAInfo();
1143 AAInfo
.TBAA
->printAsOperand(OS
, MST
);
1146 OS
<< ", !alias.scope ";
1147 AAInfo
.Scope
->printAsOperand(OS
, MST
);
1149 if (AAInfo
.NoAlias
) {
1150 OS
<< ", !noalias ";
1151 AAInfo
.NoAlias
->printAsOperand(OS
, MST
);
1155 getRanges()->printAsOperand(OS
, MST
);
1157 // FIXME: Implement addrspace printing/parsing in MIR.
1158 // For now, print this even though parsing it is not available in MIR.
1159 if (unsigned AS
= getAddrSpace())
1160 OS
<< ", addrspace " << AS
;