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/StableHashing.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Analysis/Loads.h"
17 #include "llvm/CodeGen/MIRFormatter.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/Instructions.h"
27 #include "llvm/IR/ModuleSlotTracker.h"
28 #include "llvm/MC/MCDwarf.h"
29 #include "llvm/Target/TargetIntrinsicInfo.h"
30 #include "llvm/Target/TargetMachine.h"
36 PrintRegMaskNumRegs("print-regmask-num-regs",
37 cl::desc("Number of registers to limit to when "
38 "printing regmask operands in IR dumps. "
40 cl::init(32), cl::Hidden
);
42 static const MachineFunction
*getMFIfAvailable(const MachineOperand
&MO
) {
43 if (const MachineInstr
*MI
= MO
.getParent())
44 if (const MachineBasicBlock
*MBB
= MI
->getParent())
45 if (const MachineFunction
*MF
= MBB
->getParent())
50 static MachineFunction
*getMFIfAvailable(MachineOperand
&MO
) {
51 return const_cast<MachineFunction
*>(
52 getMFIfAvailable(const_cast<const MachineOperand
&>(MO
)));
55 unsigned MachineOperand::getOperandNo() const {
56 assert(getParent() && "Operand does not belong to any instruction!");
57 return getParent()->getOperandNo(this);
60 void MachineOperand::setReg(Register Reg
) {
64 // Clear the IsRenamable bit to keep it conservatively correct.
67 // Otherwise, we have to change the register. If this operand is embedded
68 // into a machine function, we need to update the old and new register's
70 if (MachineFunction
*MF
= getMFIfAvailable(*this)) {
71 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
72 MRI
.removeRegOperandFromUseList(this);
73 SmallContents
.RegNo
= Reg
;
74 MRI
.addRegOperandToUseList(this);
78 // Otherwise, just change the register, no problem. :)
79 SmallContents
.RegNo
= Reg
;
82 void MachineOperand::substVirtReg(Register Reg
, unsigned SubIdx
,
83 const TargetRegisterInfo
&TRI
) {
84 assert(Reg
.isVirtual());
85 if (SubIdx
&& getSubReg())
86 SubIdx
= TRI
.composeSubRegIndices(SubIdx
, getSubReg());
92 void MachineOperand::substPhysReg(MCRegister Reg
, const TargetRegisterInfo
&TRI
) {
93 assert(Register::isPhysicalRegister(Reg
));
95 Reg
= TRI
.getSubReg(Reg
, getSubReg());
96 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
97 // That won't happen in legal code.
105 /// Change a def to a use, or a use to a def.
106 void MachineOperand::setIsDef(bool Val
) {
107 assert(isReg() && "Wrong MachineOperand accessor");
108 assert((!Val
|| !isDebug()) && "Marking a debug operation as def");
111 assert(!IsDeadOrKill
&& "Changing def/use with dead/kill set not supported");
112 // MRI may keep uses and defs in different list positions.
113 if (MachineFunction
*MF
= getMFIfAvailable(*this)) {
114 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
115 MRI
.removeRegOperandFromUseList(this);
117 MRI
.addRegOperandToUseList(this);
123 bool MachineOperand::isRenamable() const {
124 assert(isReg() && "Wrong MachineOperand accessor");
125 assert(getReg().isPhysical() &&
126 "isRenamable should only be checked on physical registers");
130 const MachineInstr
*MI
= getParent();
135 return !MI
->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle
);
137 assert(isUse() && "Reg is not def or use");
138 return !MI
->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle
);
141 void MachineOperand::setIsRenamable(bool Val
) {
142 assert(isReg() && "Wrong MachineOperand accessor");
143 assert(getReg().isPhysical() &&
144 "setIsRenamable should only be called on physical registers");
148 // If this operand is currently a register operand, and if this is in a
149 // function, deregister the operand from the register's use/def list.
150 void MachineOperand::removeRegFromUses() {
151 if (!isReg() || !isOnRegUseList())
154 if (MachineFunction
*MF
= getMFIfAvailable(*this))
155 MF
->getRegInfo().removeRegOperandFromUseList(this);
158 /// ChangeToImmediate - Replace this operand with a new immediate operand of
159 /// the specified value. If an operand is known to be an immediate already,
160 /// the setImm method should be used.
161 void MachineOperand::ChangeToImmediate(int64_t ImmVal
, unsigned TargetFlags
) {
162 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
166 OpKind
= MO_Immediate
;
167 Contents
.ImmVal
= ImmVal
;
168 setTargetFlags(TargetFlags
);
171 void MachineOperand::ChangeToFPImmediate(const ConstantFP
*FPImm
,
172 unsigned TargetFlags
) {
173 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
177 OpKind
= MO_FPImmediate
;
178 Contents
.CFP
= FPImm
;
179 setTargetFlags(TargetFlags
);
182 void MachineOperand::ChangeToES(const char *SymName
,
183 unsigned TargetFlags
) {
184 assert((!isReg() || !isTied()) &&
185 "Cannot change a tied operand into an external symbol");
189 OpKind
= MO_ExternalSymbol
;
190 Contents
.OffsetedInfo
.Val
.SymbolName
= SymName
;
191 setOffset(0); // Offset is always 0.
192 setTargetFlags(TargetFlags
);
195 void MachineOperand::ChangeToGA(const GlobalValue
*GV
, int64_t Offset
,
196 unsigned TargetFlags
) {
197 assert((!isReg() || !isTied()) &&
198 "Cannot change a tied operand into a global address");
202 OpKind
= MO_GlobalAddress
;
203 Contents
.OffsetedInfo
.Val
.GV
= GV
;
205 setTargetFlags(TargetFlags
);
208 void MachineOperand::ChangeToMCSymbol(MCSymbol
*Sym
, unsigned TargetFlags
) {
209 assert((!isReg() || !isTied()) &&
210 "Cannot change a tied operand into an MCSymbol");
214 OpKind
= MO_MCSymbol
;
216 setTargetFlags(TargetFlags
);
219 void MachineOperand::ChangeToFrameIndex(int Idx
, unsigned TargetFlags
) {
220 assert((!isReg() || !isTied()) &&
221 "Cannot change a tied operand into a FrameIndex");
225 OpKind
= MO_FrameIndex
;
227 setTargetFlags(TargetFlags
);
230 void MachineOperand::ChangeToTargetIndex(unsigned Idx
, int64_t Offset
,
231 unsigned TargetFlags
) {
232 assert((!isReg() || !isTied()) &&
233 "Cannot change a tied operand into a FrameIndex");
237 OpKind
= MO_TargetIndex
;
240 setTargetFlags(TargetFlags
);
243 void MachineOperand::ChangeToDbgInstrRef(unsigned InstrIdx
, unsigned OpIdx
,
244 unsigned TargetFlags
) {
245 assert((!isReg() || !isTied()) &&
246 "Cannot change a tied operand into a DbgInstrRef");
250 OpKind
= MO_DbgInstrRef
;
251 setInstrRefInstrIndex(InstrIdx
);
252 setInstrRefOpIndex(OpIdx
);
253 setTargetFlags(TargetFlags
);
256 /// ChangeToRegister - Replace this operand with a new register operand of
257 /// the specified value. If an operand is known to be an register already,
258 /// the setReg method should be used.
259 void MachineOperand::ChangeToRegister(Register Reg
, bool isDef
, bool isImp
,
260 bool isKill
, bool isDead
, bool isUndef
,
262 MachineRegisterInfo
*RegInfo
= nullptr;
263 if (MachineFunction
*MF
= getMFIfAvailable(*this))
264 RegInfo
= &MF
->getRegInfo();
265 // If this operand is already a register operand, remove it from the
266 // register's use/def lists.
267 bool WasReg
= isReg();
268 if (RegInfo
&& WasReg
)
269 RegInfo
->removeRegOperandFromUseList(this);
271 // Ensure debug instructions set debug flag on register uses.
272 const MachineInstr
*MI
= getParent();
273 if (!isDef
&& MI
&& MI
->isDebugInstr())
276 // Change this to a register and set the reg#.
277 assert(!(isDead
&& !isDef
) && "Dead flag on non-def");
278 assert(!(isKill
&& isDef
) && "Kill flag on def");
279 OpKind
= MO_Register
;
280 SmallContents
.RegNo
= Reg
;
281 SubReg_TargetFlags
= 0;
284 IsDeadOrKill
= isKill
| isDead
;
287 IsInternalRead
= false;
288 IsEarlyClobber
= false;
290 // Ensure isOnRegUseList() returns false.
291 Contents
.Reg
.Prev
= nullptr;
292 // Preserve the tie when the operand was already a register.
296 // If this operand is embedded in a function, add the operand to the
297 // register's use/def list.
299 RegInfo
->addRegOperandToUseList(this);
302 /// isIdenticalTo - Return true if this operand is identical to the specified
303 /// operand. Note that this should stay in sync with the hash_value overload
305 bool MachineOperand::isIdenticalTo(const MachineOperand
&Other
) const {
306 if (getType() != Other
.getType() ||
307 getTargetFlags() != Other
.getTargetFlags())
311 case MachineOperand::MO_Register
:
312 return getReg() == Other
.getReg() && isDef() == Other
.isDef() &&
313 getSubReg() == Other
.getSubReg();
314 case MachineOperand::MO_Immediate
:
315 return getImm() == Other
.getImm();
316 case MachineOperand::MO_CImmediate
:
317 return getCImm() == Other
.getCImm();
318 case MachineOperand::MO_FPImmediate
:
319 return getFPImm() == Other
.getFPImm();
320 case MachineOperand::MO_MachineBasicBlock
:
321 return getMBB() == Other
.getMBB();
322 case MachineOperand::MO_FrameIndex
:
323 return getIndex() == Other
.getIndex();
324 case MachineOperand::MO_ConstantPoolIndex
:
325 case MachineOperand::MO_TargetIndex
:
326 return getIndex() == Other
.getIndex() && getOffset() == Other
.getOffset();
327 case MachineOperand::MO_JumpTableIndex
:
328 return getIndex() == Other
.getIndex();
329 case MachineOperand::MO_GlobalAddress
:
330 return getGlobal() == Other
.getGlobal() && getOffset() == Other
.getOffset();
331 case MachineOperand::MO_ExternalSymbol
:
332 return strcmp(getSymbolName(), Other
.getSymbolName()) == 0 &&
333 getOffset() == Other
.getOffset();
334 case MachineOperand::MO_BlockAddress
:
335 return getBlockAddress() == Other
.getBlockAddress() &&
336 getOffset() == Other
.getOffset();
337 case MachineOperand::MO_RegisterMask
:
338 case MachineOperand::MO_RegisterLiveOut
: {
339 // Shallow compare of the two RegMasks
340 const uint32_t *RegMask
= getRegMask();
341 const uint32_t *OtherRegMask
= Other
.getRegMask();
342 if (RegMask
== OtherRegMask
)
345 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
346 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
347 unsigned RegMaskSize
= MachineOperand::getRegMaskSize(TRI
->getNumRegs());
348 // Deep compare of the two RegMasks
349 return std::equal(RegMask
, RegMask
+ RegMaskSize
, OtherRegMask
);
351 // We don't know the size of the RegMask, so we can't deep compare the two
355 case MachineOperand::MO_MCSymbol
:
356 return getMCSymbol() == Other
.getMCSymbol();
357 case MachineOperand::MO_DbgInstrRef
:
358 return getInstrRefInstrIndex() == Other
.getInstrRefInstrIndex() &&
359 getInstrRefOpIndex() == Other
.getInstrRefOpIndex();
360 case MachineOperand::MO_CFIIndex
:
361 return getCFIIndex() == Other
.getCFIIndex();
362 case MachineOperand::MO_Metadata
:
363 return getMetadata() == Other
.getMetadata();
364 case MachineOperand::MO_IntrinsicID
:
365 return getIntrinsicID() == Other
.getIntrinsicID();
366 case MachineOperand::MO_Predicate
:
367 return getPredicate() == Other
.getPredicate();
368 case MachineOperand::MO_ShuffleMask
:
369 return getShuffleMask() == Other
.getShuffleMask();
371 llvm_unreachable("Invalid machine operand type");
374 // Note: this must stay exactly in sync with isIdenticalTo above.
375 hash_code
llvm::hash_value(const MachineOperand
&MO
) {
376 switch (MO
.getType()) {
377 case MachineOperand::MO_Register
:
378 // Register operands don't have target flags.
379 return hash_combine(MO
.getType(), (unsigned)MO
.getReg(), MO
.getSubReg(), MO
.isDef());
380 case MachineOperand::MO_Immediate
:
381 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getImm());
382 case MachineOperand::MO_CImmediate
:
383 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getCImm());
384 case MachineOperand::MO_FPImmediate
:
385 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getFPImm());
386 case MachineOperand::MO_MachineBasicBlock
:
387 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMBB());
388 case MachineOperand::MO_FrameIndex
:
389 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex());
390 case MachineOperand::MO_ConstantPoolIndex
:
391 case MachineOperand::MO_TargetIndex
:
392 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex(),
394 case MachineOperand::MO_JumpTableIndex
:
395 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIndex());
396 case MachineOperand::MO_ExternalSymbol
:
397 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getOffset(),
398 StringRef(MO
.getSymbolName()));
399 case MachineOperand::MO_GlobalAddress
:
400 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getGlobal(),
402 case MachineOperand::MO_BlockAddress
:
403 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getBlockAddress(),
405 case MachineOperand::MO_RegisterMask
:
406 case MachineOperand::MO_RegisterLiveOut
: {
407 if (const MachineFunction
*MF
= getMFIfAvailable(MO
)) {
408 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
409 unsigned RegMaskSize
= MachineOperand::getRegMaskSize(TRI
->getNumRegs());
410 const uint32_t *RegMask
= MO
.getRegMask();
411 std::vector
<stable_hash
> RegMaskHashes(RegMask
, RegMask
+ RegMaskSize
);
412 return hash_combine(MO
.getType(), MO
.getTargetFlags(),
413 stable_hash_combine_array(RegMaskHashes
.data(),
414 RegMaskHashes
.size()));
417 assert(0 && "MachineOperand not associated with any MachineFunction");
418 return hash_combine(MO
.getType(), MO
.getTargetFlags());
420 case MachineOperand::MO_Metadata
:
421 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMetadata());
422 case MachineOperand::MO_MCSymbol
:
423 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getMCSymbol());
424 case MachineOperand::MO_DbgInstrRef
:
425 return hash_combine(MO
.getType(), MO
.getTargetFlags(),
426 MO
.getInstrRefInstrIndex(), MO
.getInstrRefOpIndex());
427 case MachineOperand::MO_CFIIndex
:
428 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getCFIIndex());
429 case MachineOperand::MO_IntrinsicID
:
430 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getIntrinsicID());
431 case MachineOperand::MO_Predicate
:
432 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getPredicate());
433 case MachineOperand::MO_ShuffleMask
:
434 return hash_combine(MO
.getType(), MO
.getTargetFlags(), MO
.getShuffleMask());
436 llvm_unreachable("Invalid machine operand type");
439 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
441 static void tryToGetTargetInfo(const MachineOperand
&MO
,
442 const TargetRegisterInfo
*&TRI
,
443 const TargetIntrinsicInfo
*&IntrinsicInfo
) {
444 if (const MachineFunction
*MF
= getMFIfAvailable(MO
)) {
445 TRI
= MF
->getSubtarget().getRegisterInfo();
446 IntrinsicInfo
= MF
->getTarget().getIntrinsicInfo();
450 static const char *getTargetIndexName(const MachineFunction
&MF
, int Index
) {
451 const auto *TII
= MF
.getSubtarget().getInstrInfo();
452 assert(TII
&& "expected instruction info");
453 auto Indices
= TII
->getSerializableTargetIndices();
454 auto Found
= find_if(Indices
, [&](const std::pair
<int, const char *> &I
) {
455 return I
.first
== Index
;
457 if (Found
!= Indices
.end())
458 return Found
->second
;
462 const char *MachineOperand::getTargetIndexName() const {
463 const MachineFunction
*MF
= getMFIfAvailable(*this);
464 return MF
? ::getTargetIndexName(*MF
, this->getIndex()) : nullptr;
467 static const char *getTargetFlagName(const TargetInstrInfo
*TII
, unsigned TF
) {
468 auto Flags
= TII
->getSerializableDirectMachineOperandTargetFlags();
469 for (const auto &I
: Flags
) {
477 static void printCFIRegister(unsigned DwarfReg
, raw_ostream
&OS
,
478 const TargetRegisterInfo
*TRI
) {
480 OS
<< "%dwarfreg." << DwarfReg
;
484 if (std::optional
<unsigned> Reg
= TRI
->getLLVMRegNum(DwarfReg
, true))
485 OS
<< printReg(*Reg
, TRI
);
490 static void printIRBlockReference(raw_ostream
&OS
, const BasicBlock
&BB
,
491 ModuleSlotTracker
&MST
) {
494 printLLVMNameWithoutPrefix(OS
, BB
.getName());
497 std::optional
<int> Slot
;
498 if (const Function
*F
= BB
.getParent()) {
499 if (F
== MST
.getCurrentFunction()) {
500 Slot
= MST
.getLocalSlot(&BB
);
501 } else if (const Module
*M
= F
->getParent()) {
502 ModuleSlotTracker
CustomMST(M
, /*ShouldInitializeAllMetadata=*/false);
503 CustomMST
.incorporateFunction(*F
);
504 Slot
= CustomMST
.getLocalSlot(&BB
);
508 MachineOperand::printIRSlotNumber(OS
, *Slot
);
513 static void printSyncScope(raw_ostream
&OS
, const LLVMContext
&Context
,
515 SmallVectorImpl
<StringRef
> &SSNs
) {
517 case SyncScope::System
:
521 Context
.getSyncScopeNames(SSNs
);
523 OS
<< "syncscope(\"";
524 printEscapedString(SSNs
[SSID
], OS
);
530 static const char *getTargetMMOFlagName(const TargetInstrInfo
&TII
,
532 auto Flags
= TII
.getSerializableMachineMemOperandTargetFlags();
533 for (const auto &I
: Flags
) {
534 if (I
.first
== TMMOFlag
) {
541 static void printFrameIndex(raw_ostream
& OS
, int FrameIndex
, bool IsFixed
,
542 const MachineFrameInfo
*MFI
) {
545 IsFixed
= MFI
->isFixedObjectIndex(FrameIndex
);
546 if (const AllocaInst
*Alloca
= MFI
->getObjectAllocation(FrameIndex
))
547 if (Alloca
->hasName())
548 Name
= Alloca
->getName();
550 FrameIndex
-= MFI
->getObjectIndexBegin();
552 MachineOperand::printStackObjectReference(OS
, FrameIndex
, IsFixed
, Name
);
555 void MachineOperand::printSubRegIdx(raw_ostream
&OS
, uint64_t Index
,
556 const TargetRegisterInfo
*TRI
) {
558 if (TRI
&& Index
!= 0 && Index
< TRI
->getNumSubRegIndices())
559 OS
<< TRI
->getSubRegIndexName(Index
);
564 void MachineOperand::printTargetFlags(raw_ostream
&OS
,
565 const MachineOperand
&Op
) {
566 if (!Op
.getTargetFlags())
568 const MachineFunction
*MF
= getMFIfAvailable(Op
);
572 const auto *TII
= MF
->getSubtarget().getInstrInfo();
573 assert(TII
&& "expected instruction info");
574 auto Flags
= TII
->decomposeMachineOperandsTargetFlags(Op
.getTargetFlags());
575 OS
<< "target-flags(";
576 const bool HasDirectFlags
= Flags
.first
;
577 const bool HasBitmaskFlags
= Flags
.second
;
578 if (!HasDirectFlags
&& !HasBitmaskFlags
) {
582 if (HasDirectFlags
) {
583 if (const auto *Name
= getTargetFlagName(TII
, Flags
.first
))
586 OS
<< "<unknown target flag>";
588 if (!HasBitmaskFlags
) {
592 bool IsCommaNeeded
= HasDirectFlags
;
593 unsigned BitMask
= Flags
.second
;
594 auto BitMasks
= TII
->getSerializableBitmaskMachineOperandTargetFlags();
595 for (const auto &Mask
: BitMasks
) {
596 // Check if the flag's bitmask has the bits of the current mask set.
597 if ((BitMask
& Mask
.first
) == Mask
.first
) {
600 IsCommaNeeded
= true;
602 // Clear the bits which were serialized from the flag's bitmask.
603 BitMask
&= ~(Mask
.first
);
607 // When the resulting flag's bitmask isn't zero, we know that we didn't
608 // serialize all of the bit flags.
611 OS
<< "<unknown bitmask target flag>";
616 void MachineOperand::printSymbol(raw_ostream
&OS
, MCSymbol
&Sym
) {
617 OS
<< "<mcsymbol " << Sym
<< ">";
620 void MachineOperand::printStackObjectReference(raw_ostream
&OS
,
622 bool IsFixed
, StringRef Name
) {
624 OS
<< "%fixed-stack." << FrameIndex
;
628 OS
<< "%stack." << FrameIndex
;
633 void MachineOperand::printOperandOffset(raw_ostream
&OS
, int64_t Offset
) {
637 OS
<< " - " << -Offset
;
640 OS
<< " + " << Offset
;
643 void MachineOperand::printIRSlotNumber(raw_ostream
&OS
, int Slot
) {
650 static void printCFI(raw_ostream
&OS
, const MCCFIInstruction
&CFI
,
651 const TargetRegisterInfo
*TRI
) {
652 switch (CFI
.getOperation()) {
653 case MCCFIInstruction::OpSameValue
:
655 if (MCSymbol
*Label
= CFI
.getLabel())
656 MachineOperand::printSymbol(OS
, *Label
);
657 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
659 case MCCFIInstruction::OpRememberState
:
660 OS
<< "remember_state ";
661 if (MCSymbol
*Label
= CFI
.getLabel())
662 MachineOperand::printSymbol(OS
, *Label
);
664 case MCCFIInstruction::OpRestoreState
:
665 OS
<< "restore_state ";
666 if (MCSymbol
*Label
= CFI
.getLabel())
667 MachineOperand::printSymbol(OS
, *Label
);
669 case MCCFIInstruction::OpOffset
:
671 if (MCSymbol
*Label
= CFI
.getLabel())
672 MachineOperand::printSymbol(OS
, *Label
);
673 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
674 OS
<< ", " << CFI
.getOffset();
676 case MCCFIInstruction::OpDefCfaRegister
:
677 OS
<< "def_cfa_register ";
678 if (MCSymbol
*Label
= CFI
.getLabel())
679 MachineOperand::printSymbol(OS
, *Label
);
680 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
682 case MCCFIInstruction::OpDefCfaOffset
:
683 OS
<< "def_cfa_offset ";
684 if (MCSymbol
*Label
= CFI
.getLabel())
685 MachineOperand::printSymbol(OS
, *Label
);
686 OS
<< CFI
.getOffset();
688 case MCCFIInstruction::OpDefCfa
:
690 if (MCSymbol
*Label
= CFI
.getLabel())
691 MachineOperand::printSymbol(OS
, *Label
);
692 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
693 OS
<< ", " << CFI
.getOffset();
695 case MCCFIInstruction::OpLLVMDefAspaceCfa
:
696 OS
<< "llvm_def_aspace_cfa ";
697 if (MCSymbol
*Label
= CFI
.getLabel())
698 MachineOperand::printSymbol(OS
, *Label
);
699 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
700 OS
<< ", " << CFI
.getOffset();
701 OS
<< ", " << CFI
.getAddressSpace();
703 case MCCFIInstruction::OpRelOffset
:
705 if (MCSymbol
*Label
= CFI
.getLabel())
706 MachineOperand::printSymbol(OS
, *Label
);
707 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
708 OS
<< ", " << CFI
.getOffset();
710 case MCCFIInstruction::OpAdjustCfaOffset
:
711 OS
<< "adjust_cfa_offset ";
712 if (MCSymbol
*Label
= CFI
.getLabel())
713 MachineOperand::printSymbol(OS
, *Label
);
714 OS
<< CFI
.getOffset();
716 case MCCFIInstruction::OpRestore
:
718 if (MCSymbol
*Label
= CFI
.getLabel())
719 MachineOperand::printSymbol(OS
, *Label
);
720 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
722 case MCCFIInstruction::OpEscape
: {
724 if (MCSymbol
*Label
= CFI
.getLabel())
725 MachineOperand::printSymbol(OS
, *Label
);
726 if (!CFI
.getValues().empty()) {
727 size_t e
= CFI
.getValues().size() - 1;
728 for (size_t i
= 0; i
< e
; ++i
)
729 OS
<< format("0x%02x", uint8_t(CFI
.getValues()[i
])) << ", ";
730 OS
<< format("0x%02x", uint8_t(CFI
.getValues()[e
]));
734 case MCCFIInstruction::OpUndefined
:
736 if (MCSymbol
*Label
= CFI
.getLabel())
737 MachineOperand::printSymbol(OS
, *Label
);
738 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
740 case MCCFIInstruction::OpRegister
:
742 if (MCSymbol
*Label
= CFI
.getLabel())
743 MachineOperand::printSymbol(OS
, *Label
);
744 printCFIRegister(CFI
.getRegister(), OS
, TRI
);
746 printCFIRegister(CFI
.getRegister2(), OS
, TRI
);
748 case MCCFIInstruction::OpWindowSave
:
749 OS
<< "window_save ";
750 if (MCSymbol
*Label
= CFI
.getLabel())
751 MachineOperand::printSymbol(OS
, *Label
);
753 case MCCFIInstruction::OpNegateRAState
:
754 OS
<< "negate_ra_sign_state ";
755 if (MCSymbol
*Label
= CFI
.getLabel())
756 MachineOperand::printSymbol(OS
, *Label
);
759 // TODO: Print the other CFI Operations.
760 OS
<< "<unserializable cfi directive>";
765 void MachineOperand::print(raw_ostream
&OS
, const TargetRegisterInfo
*TRI
,
766 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
767 print(OS
, LLT
{}, TRI
, IntrinsicInfo
);
770 void MachineOperand::print(raw_ostream
&OS
, LLT TypeToPrint
,
771 const TargetRegisterInfo
*TRI
,
772 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
773 tryToGetTargetInfo(*this, TRI
, IntrinsicInfo
);
774 ModuleSlotTracker
DummyMST(nullptr);
775 print(OS
, DummyMST
, TypeToPrint
, std::nullopt
, /*PrintDef=*/false,
776 /*IsStandalone=*/true,
777 /*ShouldPrintRegisterTies=*/true,
778 /*TiedOperandIdx=*/0, TRI
, IntrinsicInfo
);
781 void MachineOperand::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
782 LLT TypeToPrint
, std::optional
<unsigned> OpIdx
,
783 bool PrintDef
, bool IsStandalone
,
784 bool ShouldPrintRegisterTies
,
785 unsigned TiedOperandIdx
,
786 const TargetRegisterInfo
*TRI
,
787 const TargetIntrinsicInfo
*IntrinsicInfo
) const {
788 printTargetFlags(OS
, *this);
790 case MachineOperand::MO_Register
: {
791 Register Reg
= getReg();
793 OS
<< (isDef() ? "implicit-def " : "implicit ");
794 else if (PrintDef
&& isDef())
795 // Print the 'def' flag only when the operand is defined after '='.
797 if (isInternalRead())
805 if (isEarlyClobber())
806 OS
<< "early-clobber ";
807 if (getReg().isPhysical() && isRenamable())
809 // isDebug() is exactly true for register operands of a DBG_VALUE. So we
810 // simply infer it when parsing and do not need to print it.
812 const MachineRegisterInfo
*MRI
= nullptr;
813 if (Reg
.isVirtual()) {
814 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
815 MRI
= &MF
->getRegInfo();
819 OS
<< printReg(Reg
, TRI
, 0, MRI
);
820 // Print the sub register.
821 if (unsigned SubReg
= getSubReg()) {
823 OS
<< '.' << TRI
->getSubRegIndexName(SubReg
);
825 OS
<< ".subreg" << SubReg
;
827 // Print the register class / bank.
828 if (Reg
.isVirtual()) {
829 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
830 const MachineRegisterInfo
&MRI
= MF
->getRegInfo();
831 if (IsStandalone
|| !PrintDef
|| MRI
.def_empty(Reg
)) {
833 OS
<< printRegClassOrBank(Reg
, MRI
, TRI
);
838 if (ShouldPrintRegisterTies
&& isTied() && !isDef())
839 OS
<< "(tied-def " << TiedOperandIdx
<< ")";
841 if (TypeToPrint
.isValid())
842 OS
<< '(' << TypeToPrint
<< ')';
845 case MachineOperand::MO_Immediate
: {
846 const MIRFormatter
*Formatter
= nullptr;
847 if (const MachineFunction
*MF
= getMFIfAvailable(*this)) {
848 const auto *TII
= MF
->getSubtarget().getInstrInfo();
849 assert(TII
&& "expected instruction info");
850 Formatter
= TII
->getMIRFormatter();
853 Formatter
->printImm(OS
, *getParent(), OpIdx
, getImm());
858 case MachineOperand::MO_CImmediate
:
859 getCImm()->printAsOperand(OS
, /*PrintType=*/true, MST
);
861 case MachineOperand::MO_FPImmediate
:
862 getFPImm()->printAsOperand(OS
, /*PrintType=*/true, MST
);
864 case MachineOperand::MO_MachineBasicBlock
:
865 OS
<< printMBBReference(*getMBB());
867 case MachineOperand::MO_FrameIndex
: {
868 int FrameIndex
= getIndex();
869 bool IsFixed
= false;
870 const MachineFrameInfo
*MFI
= nullptr;
871 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
872 MFI
= &MF
->getFrameInfo();
873 printFrameIndex(OS
, FrameIndex
, IsFixed
, MFI
);
876 case MachineOperand::MO_ConstantPoolIndex
:
877 OS
<< "%const." << getIndex();
878 printOperandOffset(OS
, getOffset());
880 case MachineOperand::MO_TargetIndex
: {
881 OS
<< "target-index(";
882 const char *Name
= "<unknown>";
883 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
884 if (const auto *TargetIndexName
= ::getTargetIndexName(*MF
, getIndex()))
885 Name
= TargetIndexName
;
887 printOperandOffset(OS
, getOffset());
890 case MachineOperand::MO_JumpTableIndex
:
891 OS
<< printJumpTableEntryReference(getIndex());
893 case MachineOperand::MO_GlobalAddress
:
894 getGlobal()->printAsOperand(OS
, /*PrintType=*/false, MST
);
895 printOperandOffset(OS
, getOffset());
897 case MachineOperand::MO_ExternalSymbol
: {
898 StringRef Name
= getSymbolName();
903 printLLVMNameWithoutPrefix(OS
, Name
);
905 printOperandOffset(OS
, getOffset());
908 case MachineOperand::MO_BlockAddress
: {
909 OS
<< "blockaddress(";
910 getBlockAddress()->getFunction()->printAsOperand(OS
, /*PrintType=*/false,
913 printIRBlockReference(OS
, *getBlockAddress()->getBasicBlock(), MST
);
915 MachineOperand::printOperandOffset(OS
, getOffset());
918 case MachineOperand::MO_RegisterMask
: {
921 unsigned NumRegsInMask
= 0;
922 unsigned NumRegsEmitted
= 0;
923 for (unsigned i
= 0; i
< TRI
->getNumRegs(); ++i
) {
924 unsigned MaskWord
= i
/ 32;
925 unsigned MaskBit
= i
% 32;
926 if (getRegMask()[MaskWord
] & (1 << MaskBit
)) {
927 if (PrintRegMaskNumRegs
< 0 ||
928 NumRegsEmitted
<= static_cast<unsigned>(PrintRegMaskNumRegs
)) {
929 OS
<< " " << printReg(i
, TRI
);
935 if (NumRegsEmitted
!= NumRegsInMask
)
936 OS
<< " and " << (NumRegsInMask
- NumRegsEmitted
) << " more...";
943 case MachineOperand::MO_RegisterLiveOut
: {
944 const uint32_t *RegMask
= getRegLiveOut();
949 bool IsCommaNeeded
= false;
950 for (unsigned Reg
= 0, E
= TRI
->getNumRegs(); Reg
< E
; ++Reg
) {
951 if (RegMask
[Reg
/ 32] & (1U << (Reg
% 32))) {
954 OS
<< printReg(Reg
, TRI
);
955 IsCommaNeeded
= true;
962 case MachineOperand::MO_Metadata
:
963 getMetadata()->printAsOperand(OS
, MST
);
965 case MachineOperand::MO_MCSymbol
:
966 printSymbol(OS
, *getMCSymbol());
968 case MachineOperand::MO_DbgInstrRef
: {
969 OS
<< "dbg-instr-ref(" << getInstrRefInstrIndex() << ", "
970 << getInstrRefOpIndex() << ')';
973 case MachineOperand::MO_CFIIndex
: {
974 if (const MachineFunction
*MF
= getMFIfAvailable(*this))
975 printCFI(OS
, MF
->getFrameInstructions()[getCFIIndex()], TRI
);
977 OS
<< "<cfi directive>";
980 case MachineOperand::MO_IntrinsicID
: {
981 Intrinsic::ID ID
= getIntrinsicID();
982 if (ID
< Intrinsic::num_intrinsics
)
983 OS
<< "intrinsic(@" << Intrinsic::getBaseName(ID
) << ')';
984 else if (IntrinsicInfo
)
985 OS
<< "intrinsic(@" << IntrinsicInfo
->getName(ID
) << ')';
987 OS
<< "intrinsic(" << ID
<< ')';
990 case MachineOperand::MO_Predicate
: {
991 auto Pred
= static_cast<CmpInst::Predicate
>(getPredicate());
992 OS
<< (CmpInst::isIntPredicate(Pred
) ? "int" : "float") << "pred("
996 case MachineOperand::MO_ShuffleMask
:
997 OS
<< "shufflemask(";
998 ArrayRef
<int> Mask
= getShuffleMask();
1000 for (int Elt
: Mask
) {
1002 OS
<< Separator
<< "undef";
1004 OS
<< Separator
<< Elt
;
1013 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1014 LLVM_DUMP_METHOD
void MachineOperand::dump() const { dbgs() << *this << '\n'; }
1017 //===----------------------------------------------------------------------===//
1018 // MachineMemOperand Implementation
1019 //===----------------------------------------------------------------------===//
1021 /// getAddrSpace - Return the LLVM IR address space number that this pointer
1023 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace
; }
1025 /// isDereferenceable - Return true if V is always dereferenceable for
1026 /// Offset + Size byte.
1027 bool MachinePointerInfo::isDereferenceable(unsigned Size
, LLVMContext
&C
,
1028 const DataLayout
&DL
) const {
1029 if (!isa
<const Value
*>(V
))
1032 const Value
*BasePtr
= cast
<const Value
*>(V
);
1033 if (BasePtr
== nullptr)
1036 return isDereferenceableAndAlignedPointer(
1037 BasePtr
, Align(1), APInt(DL
.getPointerSizeInBits(), Offset
+ Size
), DL
);
1040 /// getConstantPool - Return a MachinePointerInfo record that refers to the
1042 MachinePointerInfo
MachinePointerInfo::getConstantPool(MachineFunction
&MF
) {
1043 return MachinePointerInfo(MF
.getPSVManager().getConstantPool());
1046 /// getFixedStack - Return a MachinePointerInfo record that refers to the
1047 /// the specified FrameIndex.
1048 MachinePointerInfo
MachinePointerInfo::getFixedStack(MachineFunction
&MF
,
1049 int FI
, int64_t Offset
) {
1050 return MachinePointerInfo(MF
.getPSVManager().getFixedStack(FI
), Offset
);
1053 MachinePointerInfo
MachinePointerInfo::getJumpTable(MachineFunction
&MF
) {
1054 return MachinePointerInfo(MF
.getPSVManager().getJumpTable());
1057 MachinePointerInfo
MachinePointerInfo::getGOT(MachineFunction
&MF
) {
1058 return MachinePointerInfo(MF
.getPSVManager().getGOT());
1061 MachinePointerInfo
MachinePointerInfo::getStack(MachineFunction
&MF
,
1062 int64_t Offset
, uint8_t ID
) {
1063 return MachinePointerInfo(MF
.getPSVManager().getStack(), Offset
, ID
);
1066 MachinePointerInfo
MachinePointerInfo::getUnknownStack(MachineFunction
&MF
) {
1067 return MachinePointerInfo(MF
.getDataLayout().getAllocaAddrSpace());
1070 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo
, Flags f
,
1071 LLT type
, Align a
, const AAMDNodes
&AAInfo
,
1072 const MDNode
*Ranges
, SyncScope::ID SSID
,
1073 AtomicOrdering Ordering
,
1074 AtomicOrdering FailureOrdering
)
1075 : PtrInfo(ptrinfo
), MemoryType(type
), FlagVals(f
), BaseAlign(a
),
1076 AAInfo(AAInfo
), Ranges(Ranges
) {
1077 assert((PtrInfo
.V
.isNull() || isa
<const PseudoSourceValue
*>(PtrInfo
.V
) ||
1078 isa
<PointerType
>(cast
<const Value
*>(PtrInfo
.V
)->getType())) &&
1079 "invalid pointer value");
1080 assert((isLoad() || isStore()) && "Not a load/store!");
1082 AtomicInfo
.SSID
= static_cast<unsigned>(SSID
);
1083 assert(getSyncScopeID() == SSID
&& "Value truncated");
1084 AtomicInfo
.Ordering
= static_cast<unsigned>(Ordering
);
1085 assert(getSuccessOrdering() == Ordering
&& "Value truncated");
1086 AtomicInfo
.FailureOrdering
= static_cast<unsigned>(FailureOrdering
);
1087 assert(getFailureOrdering() == FailureOrdering
&& "Value truncated");
1090 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo
, Flags f
,
1091 uint64_t s
, Align a
,
1092 const AAMDNodes
&AAInfo
,
1093 const MDNode
*Ranges
, SyncScope::ID SSID
,
1094 AtomicOrdering Ordering
,
1095 AtomicOrdering FailureOrdering
)
1096 : MachineMemOperand(ptrinfo
, f
,
1097 s
== ~UINT64_C(0) ? LLT() : LLT::scalar(8 * s
), a
,
1098 AAInfo
, Ranges
, SSID
, Ordering
, FailureOrdering
) {}
1100 void MachineMemOperand::refineAlignment(const MachineMemOperand
*MMO
) {
1101 // The Value and Offset may differ due to CSE. But the flags and size
1102 // should be the same.
1103 assert(MMO
->getFlags() == getFlags() && "Flags mismatch!");
1104 assert((MMO
->getSize() == ~UINT64_C(0) || getSize() == ~UINT64_C(0) ||
1105 MMO
->getSize() == getSize()) &&
1108 if (MMO
->getBaseAlign() >= getBaseAlign()) {
1109 // Update the alignment value.
1110 BaseAlign
= MMO
->getBaseAlign();
1111 // Also update the base and offset, because the new alignment may
1112 // not be applicable with the old ones.
1113 PtrInfo
= MMO
->PtrInfo
;
1117 /// getAlign - Return the minimum known alignment in bytes of the
1118 /// actual memory reference.
1119 Align
MachineMemOperand::getAlign() const {
1120 return commonAlignment(getBaseAlign(), getOffset());
1123 void MachineMemOperand::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
1124 SmallVectorImpl
<StringRef
> &SSNs
,
1125 const LLVMContext
&Context
,
1126 const MachineFrameInfo
*MFI
,
1127 const TargetInstrInfo
*TII
) const {
1131 if (isNonTemporal())
1132 OS
<< "non-temporal ";
1133 if (isDereferenceable())
1134 OS
<< "dereferenceable ";
1138 if (getFlags() & MachineMemOperand::MOTargetFlag1
)
1139 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag1
)
1141 if (getFlags() & MachineMemOperand::MOTargetFlag2
)
1142 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag2
)
1144 if (getFlags() & MachineMemOperand::MOTargetFlag3
)
1145 OS
<< '"' << getTargetMMOFlagName(*TII
, MachineMemOperand::MOTargetFlag3
)
1148 if (getFlags() & MachineMemOperand::MOTargetFlag1
)
1149 OS
<< "\"MOTargetFlag1\" ";
1150 if (getFlags() & MachineMemOperand::MOTargetFlag2
)
1151 OS
<< "\"MOTargetFlag2\" ";
1152 if (getFlags() & MachineMemOperand::MOTargetFlag3
)
1153 OS
<< "\"MOTargetFlag3\" ";
1156 assert((isLoad() || isStore()) &&
1157 "machine memory operand must be a load or store (or both)");
1163 printSyncScope(OS
, Context
, getSyncScopeID(), SSNs
);
1165 if (getSuccessOrdering() != AtomicOrdering::NotAtomic
)
1166 OS
<< toIRString(getSuccessOrdering()) << ' ';
1167 if (getFailureOrdering() != AtomicOrdering::NotAtomic
)
1168 OS
<< toIRString(getFailureOrdering()) << ' ';
1170 if (getMemoryType().isValid())
1171 OS
<< '(' << getMemoryType() << ')';
1173 OS
<< "unknown-size";
1175 if (const Value
*Val
= getValue()) {
1176 OS
<< ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1177 MIRFormatter::printIRValue(OS
, *Val
, MST
);
1178 } else if (const PseudoSourceValue
*PVal
= getPseudoValue()) {
1179 OS
<< ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1180 assert(PVal
&& "Expected a pseudo source value");
1181 switch (PVal
->kind()) {
1182 case PseudoSourceValue::Stack
:
1185 case PseudoSourceValue::GOT
:
1188 case PseudoSourceValue::JumpTable
:
1191 case PseudoSourceValue::ConstantPool
:
1192 OS
<< "constant-pool";
1194 case PseudoSourceValue::FixedStack
: {
1195 int FrameIndex
= cast
<FixedStackPseudoSourceValue
>(PVal
)->getFrameIndex();
1196 bool IsFixed
= true;
1197 printFrameIndex(OS
, FrameIndex
, IsFixed
, MFI
);
1200 case PseudoSourceValue::GlobalValueCallEntry
:
1201 OS
<< "call-entry ";
1202 cast
<GlobalValuePseudoSourceValue
>(PVal
)->getValue()->printAsOperand(
1203 OS
, /*PrintType=*/false, MST
);
1205 case PseudoSourceValue::ExternalSymbolCallEntry
:
1206 OS
<< "call-entry &";
1207 printLLVMNameWithoutPrefix(
1208 OS
, cast
<ExternalSymbolPseudoSourceValue
>(PVal
)->getSymbol());
1211 const MIRFormatter
*Formatter
= TII
->getMIRFormatter();
1212 // FIXME: This is not necessarily the correct MIR serialization format for
1213 // a custom pseudo source value, but at least it allows
1214 // MIR printing to work on a target with custom pseudo source
1217 Formatter
->printCustomPseudoSourceValue(OS
, MST
, *PVal
);
1222 } else if (getOpaqueValue() == nullptr && getOffset() != 0) {
1223 OS
<< ((isLoad() && isStore()) ? " on "
1224 : isLoad() ? " from "
1226 << "unknown-address";
1228 MachineOperand::printOperandOffset(OS
, getOffset());
1229 if (getSize() > 0 && getAlign() != getSize())
1230 OS
<< ", align " << getAlign().value();
1231 if (getAlign() != getBaseAlign())
1232 OS
<< ", basealign " << getBaseAlign().value();
1233 auto AAInfo
= getAAInfo();
1236 AAInfo
.TBAA
->printAsOperand(OS
, MST
);
1239 OS
<< ", !alias.scope ";
1240 AAInfo
.Scope
->printAsOperand(OS
, MST
);
1242 if (AAInfo
.NoAlias
) {
1243 OS
<< ", !noalias ";
1244 AAInfo
.NoAlias
->printAsOperand(OS
, MST
);
1248 getRanges()->printAsOperand(OS
, MST
);
1250 // FIXME: Implement addrspace printing/parsing in MIR.
1251 // For now, print this even though parsing it is not available in MIR.
1252 if (unsigned AS
= getAddrSpace())
1253 OS
<< ", addrspace " << AS
;