blob: b7e89cf4b133fe40aad511f0fe8f84713f1e9130 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the declaration of the MachineOperand class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CODEGEN_MACHINEOPERAND_H
14#define LLVM_CODEGEN_MACHINEOPERAND_H
15
16#include "llvm/ADT/DenseMap.h"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010017#include "llvm/CodeGen/Register.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#include "llvm/IR/Intrinsics.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/Support/LowLevelTypeImpl.h"
21#include <cassert>
22
23namespace llvm {
24
25class BlockAddress;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020026class Constant;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027class ConstantFP;
28class ConstantInt;
29class GlobalValue;
30class MachineBasicBlock;
31class MachineInstr;
32class MachineRegisterInfo;
33class MCCFIInstruction;
34class MDNode;
35class ModuleSlotTracker;
36class TargetMachine;
37class TargetIntrinsicInfo;
38class TargetRegisterInfo;
39class hash_code;
40class raw_ostream;
41class MCSymbol;
42
43/// MachineOperand class - Representation of each machine instruction operand.
44///
45/// This class isn't a POD type because it has a private constructor, but its
46/// destructor must be trivial. Functions like MachineInstr::addOperand(),
47/// MachineRegisterInfo::moveOperands(), and MF::DeleteMachineInstr() depend on
48/// not having to call the MachineOperand destructor.
49///
50class MachineOperand {
51public:
52 enum MachineOperandType : unsigned char {
53 MO_Register, ///< Register operand.
54 MO_Immediate, ///< Immediate operand
55 MO_CImmediate, ///< Immediate >64bit operand
56 MO_FPImmediate, ///< Floating-point immediate operand
57 MO_MachineBasicBlock, ///< MachineBasicBlock reference
58 MO_FrameIndex, ///< Abstract Stack Frame Index
59 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
60 MO_TargetIndex, ///< Target-dependent index+offset operand.
61 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
62 MO_ExternalSymbol, ///< Name of external global symbol
63 MO_GlobalAddress, ///< Address of a global value
64 MO_BlockAddress, ///< Address of a basic block
65 MO_RegisterMask, ///< Mask of preserved registers.
66 MO_RegisterLiveOut, ///< Mask of live-out registers.
67 MO_Metadata, ///< Metadata reference (for debug info)
68 MO_MCSymbol, ///< MCSymbol reference (for debug/eh info)
69 MO_CFIIndex, ///< MCCFIInstruction index.
70 MO_IntrinsicID, ///< Intrinsic ID for ISel
71 MO_Predicate, ///< Generic predicate for ISel
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020072 MO_ShuffleMask, ///< Other IR Constant for ISel (shuffle masks)
73 MO_Last = MO_ShuffleMask
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 };
75
76private:
77 /// OpKind - Specify what kind of operand this is. This discriminates the
78 /// union.
79 unsigned OpKind : 8;
80
81 /// Subregister number for MO_Register. A value of 0 indicates the
82 /// MO_Register has no subReg.
83 ///
84 /// For all other kinds of operands, this field holds target-specific flags.
85 unsigned SubReg_TargetFlags : 12;
86
87 /// TiedTo - Non-zero when this register operand is tied to another register
88 /// operand. The encoding of this field is described in the block comment
89 /// before MachineInstr::tieOperands().
90 unsigned TiedTo : 4;
91
92 /// IsDef - True if this is a def, false if this is a use of the register.
93 /// This is only valid on register operands.
94 ///
95 unsigned IsDef : 1;
96
97 /// IsImp - True if this is an implicit def or use, false if it is explicit.
98 /// This is only valid on register opderands.
99 ///
100 unsigned IsImp : 1;
101
102 /// IsDeadOrKill
103 /// For uses: IsKill - True if this instruction is the last use of the
104 /// register on this path through the function.
105 /// For defs: IsDead - True if this register is never used by a subsequent
106 /// instruction.
107 /// This is only valid on register operands.
108 unsigned IsDeadOrKill : 1;
109
110 /// See isRenamable().
111 unsigned IsRenamable : 1;
112
113 /// IsUndef - True if this register operand reads an "undef" value, i.e. the
114 /// read value doesn't matter. This flag can be set on both use and def
115 /// operands. On a sub-register def operand, it refers to the part of the
116 /// register that isn't written. On a full-register def operand, it is a
117 /// noop. See readsReg().
118 ///
119 /// This is only valid on registers.
120 ///
121 /// Note that an instruction may have multiple <undef> operands referring to
122 /// the same register. In that case, the instruction may depend on those
123 /// operands reading the same dont-care value. For example:
124 ///
125 /// %1 = XOR undef %2, undef %2
126 ///
127 /// Any register can be used for %2, and its value doesn't matter, but
128 /// the two operands must be the same register.
129 ///
130 unsigned IsUndef : 1;
131
132 /// IsInternalRead - True if this operand reads a value that was defined
133 /// inside the same instruction or bundle. This flag can be set on both use
134 /// and def operands. On a sub-register def operand, it refers to the part
135 /// of the register that isn't written. On a full-register def operand, it
136 /// is a noop.
137 ///
138 /// When this flag is set, the instruction bundle must contain at least one
139 /// other def of the register. If multiple instructions in the bundle define
140 /// the register, the meaning is target-defined.
141 unsigned IsInternalRead : 1;
142
143 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
144 /// by the MachineInstr before all input registers are read. This is used to
145 /// model the GCC inline asm '&' constraint modifier.
146 unsigned IsEarlyClobber : 1;
147
148 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
149 /// not a real instruction. Such uses should be ignored during codegen.
150 unsigned IsDebug : 1;
151
152 /// SmallContents - This really should be part of the Contents union, but
153 /// lives out here so we can get a better packed struct.
154 /// MO_Register: Register number.
155 /// OffsetedInfo: Low bits of offset.
156 union {
157 unsigned RegNo; // For MO_Register.
158 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
159 } SmallContents;
160
161 /// ParentMI - This is the instruction that this operand is embedded into.
162 /// This is valid for all operand types, when the operand is in an instr.
163 MachineInstr *ParentMI;
164
165 /// Contents union - This contains the payload for the various operand types.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200166 union ContentsUnion {
167 ContentsUnion() {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100168 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
169 const ConstantFP *CFP; // For MO_FPImmediate.
170 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
171 int64_t ImmVal; // For MO_Immediate.
172 const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut.
173 const MDNode *MD; // For MO_Metadata.
174 MCSymbol *Sym; // For MO_MCSymbol.
175 unsigned CFIIndex; // For MO_CFI.
176 Intrinsic::ID IntrinsicID; // For MO_IntrinsicID.
177 unsigned Pred; // For MO_Predicate
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200178 ArrayRef<int> ShuffleMask; // For MO_ShuffleMask
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100179
180 struct { // For MO_Register.
181 // Register number is in SmallContents.RegNo.
182 MachineOperand *Prev; // Access list for register. See MRI.
183 MachineOperand *Next;
184 } Reg;
185
186 /// OffsetedInfo - This struct contains the offset and an object identifier.
187 /// this represent the object as with an optional offset from it.
188 struct {
189 union {
190 int Index; // For MO_*Index - The index itself.
191 const char *SymbolName; // For MO_ExternalSymbol.
192 const GlobalValue *GV; // For MO_GlobalAddress.
193 const BlockAddress *BA; // For MO_BlockAddress.
194 } Val;
195 // Low bits of offset are in SmallContents.OffsetLo.
196 int OffsetHi; // An offset from the object, high 32 bits.
197 } OffsetedInfo;
198 } Contents;
199
200 explicit MachineOperand(MachineOperandType K)
201 : OpKind(K), SubReg_TargetFlags(0), ParentMI(nullptr) {
202 // Assert that the layout is what we expect. It's easy to grow this object.
203 static_assert(alignof(MachineOperand) <= alignof(int64_t),
204 "MachineOperand shouldn't be more than 8 byte aligned");
205 static_assert(sizeof(Contents) <= 2 * sizeof(void *),
206 "Contents should be at most two pointers");
207 static_assert(sizeof(MachineOperand) <=
208 alignTo<alignof(int64_t)>(2 * sizeof(unsigned) +
209 3 * sizeof(void *)),
210 "MachineOperand too big. Should be Kind, SmallContents, "
211 "ParentMI, and Contents");
212 }
213
214public:
215 /// getType - Returns the MachineOperandType for this operand.
216 ///
217 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
218
219 unsigned getTargetFlags() const {
220 return isReg() ? 0 : SubReg_TargetFlags;
221 }
222 void setTargetFlags(unsigned F) {
223 assert(!isReg() && "Register operands can't have target flags");
224 SubReg_TargetFlags = F;
225 assert(SubReg_TargetFlags == F && "Target flags out of range");
226 }
227 void addTargetFlag(unsigned F) {
228 assert(!isReg() && "Register operands can't have target flags");
229 SubReg_TargetFlags |= F;
230 assert((SubReg_TargetFlags & F) && "Target flags out of range");
231 }
232
233
234 /// getParent - Return the instruction that this operand belongs to.
235 ///
236 MachineInstr *getParent() { return ParentMI; }
237 const MachineInstr *getParent() const { return ParentMI; }
238
239 /// clearParent - Reset the parent pointer.
240 ///
241 /// The MachineOperand copy constructor also copies ParentMI, expecting the
242 /// original to be deleted. If a MachineOperand is ever stored outside a
243 /// MachineInstr, the parent pointer must be cleared.
244 ///
245 /// Never call clearParent() on an operand in a MachineInstr.
246 ///
247 void clearParent() { ParentMI = nullptr; }
248
249 /// Print a subreg index operand.
250 /// MO_Immediate operands can also be subreg idices. If it's the case, the
251 /// subreg index name will be printed. MachineInstr::isOperandSubregIdx can be
252 /// called to check this.
253 static void printSubRegIdx(raw_ostream &OS, uint64_t Index,
254 const TargetRegisterInfo *TRI);
255
256 /// Print operand target flags.
257 static void printTargetFlags(raw_ostream& OS, const MachineOperand &Op);
258
259 /// Print a MCSymbol as an operand.
260 static void printSymbol(raw_ostream &OS, MCSymbol &Sym);
261
262 /// Print a stack object reference.
263 static void printStackObjectReference(raw_ostream &OS, unsigned FrameIndex,
264 bool IsFixed, StringRef Name);
265
266 /// Print the offset with explicit +/- signs.
267 static void printOperandOffset(raw_ostream &OS, int64_t Offset);
268
269 /// Print an IRSlotNumber.
270 static void printIRSlotNumber(raw_ostream &OS, int Slot);
271
272 /// Print the MachineOperand to \p os.
273 /// Providing a valid \p TRI and \p IntrinsicInfo results in a more
274 /// target-specific printing. If \p TRI and \p IntrinsicInfo are null, the
275 /// function will try to pick it up from the parent.
276 void print(raw_ostream &os, const TargetRegisterInfo *TRI = nullptr,
277 const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
278
279 /// More complex way of printing a MachineOperand.
280 /// \param TypeToPrint specifies the generic type to be printed on uses and
281 /// defs. It can be determined using MachineInstr::getTypeToPrint.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200282 /// \param OpIdx - specifies the index of the operand in machine instruction.
283 /// This will be used by target dependent MIR formatter. Could be None if the
284 /// index is unknown, e.g. called by dump().
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285 /// \param PrintDef - whether we want to print `def` on an operand which
286 /// isDef. Sometimes, if the operand is printed before '=', we don't print
287 /// `def`.
288 /// \param IsStandalone - whether we want a verbose output of the MO. This
289 /// prints extra information that can be easily inferred when printing the
290 /// whole function, but not when printing only a fragment of it.
291 /// \param ShouldPrintRegisterTies - whether we want to print register ties.
292 /// Sometimes they are easily determined by the instruction's descriptor
293 /// (MachineInstr::hasComplexRegiterTies can determine if it's needed).
294 /// \param TiedOperandIdx - if we need to print register ties this needs to
295 /// provide the index of the tied register. If not, it will be ignored.
296 /// \param TRI - provide more target-specific information to the printer.
297 /// Unlike the previous function, this one will not try and get the
298 /// information from it's parent.
299 /// \param IntrinsicInfo - same as \p TRI.
300 void print(raw_ostream &os, ModuleSlotTracker &MST, LLT TypeToPrint,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200301 Optional<unsigned> OpIdx, bool PrintDef, bool IsStandalone,
302 bool ShouldPrintRegisterTies, unsigned TiedOperandIdx,
303 const TargetRegisterInfo *TRI,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100304 const TargetIntrinsicInfo *IntrinsicInfo) const;
305
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100306 /// Same as print(os, TRI, IntrinsicInfo), but allows to specify the low-level
307 /// type to be printed the same way the full version of print(...) does it.
308 void print(raw_ostream &os, LLT TypeToPrint,
309 const TargetRegisterInfo *TRI = nullptr,
310 const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
311
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100312 void dump() const;
313
314 //===--------------------------------------------------------------------===//
315 // Accessors that tell you what kind of MachineOperand you're looking at.
316 //===--------------------------------------------------------------------===//
317
318 /// isReg - Tests if this is a MO_Register operand.
319 bool isReg() const { return OpKind == MO_Register; }
320 /// isImm - Tests if this is a MO_Immediate operand.
321 bool isImm() const { return OpKind == MO_Immediate; }
322 /// isCImm - Test if this is a MO_CImmediate operand.
323 bool isCImm() const { return OpKind == MO_CImmediate; }
324 /// isFPImm - Tests if this is a MO_FPImmediate operand.
325 bool isFPImm() const { return OpKind == MO_FPImmediate; }
326 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
327 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
328 /// isFI - Tests if this is a MO_FrameIndex operand.
329 bool isFI() const { return OpKind == MO_FrameIndex; }
330 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
331 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
332 /// isTargetIndex - Tests if this is a MO_TargetIndex operand.
333 bool isTargetIndex() const { return OpKind == MO_TargetIndex; }
334 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
335 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
336 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
337 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
338 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
339 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
340 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
341 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
342 /// isRegMask - Tests if this is a MO_RegisterMask operand.
343 bool isRegMask() const { return OpKind == MO_RegisterMask; }
344 /// isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
345 bool isRegLiveOut() const { return OpKind == MO_RegisterLiveOut; }
346 /// isMetadata - Tests if this is a MO_Metadata operand.
347 bool isMetadata() const { return OpKind == MO_Metadata; }
348 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
349 bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
350 bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; }
351 bool isPredicate() const { return OpKind == MO_Predicate; }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200352 bool isShuffleMask() const { return OpKind == MO_ShuffleMask; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100353 //===--------------------------------------------------------------------===//
354 // Accessors for Register Operands
355 //===--------------------------------------------------------------------===//
356
357 /// getReg - Returns the register number.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100358 Register getReg() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359 assert(isReg() && "This is not a register operand!");
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100360 return Register(SmallContents.RegNo);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100361 }
362
363 unsigned getSubReg() const {
364 assert(isReg() && "Wrong MachineOperand accessor");
365 return SubReg_TargetFlags;
366 }
367
368 bool isUse() const {
369 assert(isReg() && "Wrong MachineOperand accessor");
370 return !IsDef;
371 }
372
373 bool isDef() const {
374 assert(isReg() && "Wrong MachineOperand accessor");
375 return IsDef;
376 }
377
378 bool isImplicit() const {
379 assert(isReg() && "Wrong MachineOperand accessor");
380 return IsImp;
381 }
382
383 bool isDead() const {
384 assert(isReg() && "Wrong MachineOperand accessor");
385 return IsDeadOrKill & IsDef;
386 }
387
388 bool isKill() const {
389 assert(isReg() && "Wrong MachineOperand accessor");
390 return IsDeadOrKill & !IsDef;
391 }
392
393 bool isUndef() const {
394 assert(isReg() && "Wrong MachineOperand accessor");
395 return IsUndef;
396 }
397
398 /// isRenamable - Returns true if this register may be renamed, i.e. it does
399 /// not generate a value that is somehow read in a way that is not represented
400 /// by the Machine IR (e.g. to meet an ABI or ISA requirement). This is only
401 /// valid on physical register operands. Virtual registers are assumed to
402 /// always be renamable regardless of the value of this field.
403 ///
404 /// Operands that are renamable can freely be changed to any other register
405 /// that is a member of the register class returned by
406 /// MI->getRegClassConstraint().
407 ///
408 /// isRenamable can return false for several different reasons:
409 ///
410 /// - ABI constraints (since liveness is not always precisely modeled). We
411 /// conservatively handle these cases by setting all physical register
412 /// operands that didn’t start out as virtual regs to not be renamable.
413 /// Also any physical register operands created after register allocation or
414 /// whose register is changed after register allocation will not be
415 /// renamable. This state is tracked in the MachineOperand::IsRenamable
416 /// bit.
417 ///
418 /// - Opcode/target constraints: for opcodes that have complex register class
419 /// requirements (e.g. that depend on other operands/instructions), we set
420 /// hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq in the machine opcode
421 /// description. Operands belonging to instructions with opcodes that are
422 /// marked hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq return false from
423 /// isRenamable(). Additionally, the AllowRegisterRenaming target property
424 /// prevents any operands from being marked renamable for targets that don't
425 /// have detailed opcode hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq
426 /// values.
427 bool isRenamable() const;
428
429 bool isInternalRead() const {
430 assert(isReg() && "Wrong MachineOperand accessor");
431 return IsInternalRead;
432 }
433
434 bool isEarlyClobber() const {
435 assert(isReg() && "Wrong MachineOperand accessor");
436 return IsEarlyClobber;
437 }
438
439 bool isTied() const {
440 assert(isReg() && "Wrong MachineOperand accessor");
441 return TiedTo;
442 }
443
444 bool isDebug() const {
445 assert(isReg() && "Wrong MachineOperand accessor");
446 return IsDebug;
447 }
448
449 /// readsReg - Returns true if this operand reads the previous value of its
450 /// register. A use operand with the <undef> flag set doesn't read its
451 /// register. A sub-register def implicitly reads the other parts of the
452 /// register being redefined unless the <undef> flag is set.
453 ///
454 /// This refers to reading the register value from before the current
455 /// instruction or bundle. Internal bundle reads are not included.
456 bool readsReg() const {
457 assert(isReg() && "Wrong MachineOperand accessor");
458 return !isUndef() && !isInternalRead() && (isUse() || getSubReg());
459 }
460
461 //===--------------------------------------------------------------------===//
462 // Mutators for Register Operands
463 //===--------------------------------------------------------------------===//
464
465 /// Change the register this operand corresponds to.
466 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200467 void setReg(Register Reg);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100468
469 void setSubReg(unsigned subReg) {
470 assert(isReg() && "Wrong MachineOperand mutator");
471 SubReg_TargetFlags = subReg;
472 assert(SubReg_TargetFlags == subReg && "SubReg out of range");
473 }
474
475 /// substVirtReg - Substitute the current register with the virtual
476 /// subregister Reg:SubReg. Take any existing SubReg index into account,
477 /// using TargetRegisterInfo to compose the subreg indices if necessary.
478 /// Reg must be a virtual register, SubIdx can be 0.
479 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200480 void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo&);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100481
482 /// substPhysReg - Substitute the current register with the physical register
483 /// Reg, taking any existing SubReg into account. For instance,
484 /// substPhysReg(%eax) will change %reg1024:sub_8bit to %al.
485 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200486 void substPhysReg(MCRegister Reg, const TargetRegisterInfo&);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100487
488 void setIsUse(bool Val = true) { setIsDef(!Val); }
489
490 /// Change a def to a use, or a use to a def.
491 void setIsDef(bool Val = true);
492
493 void setImplicit(bool Val = true) {
494 assert(isReg() && "Wrong MachineOperand mutator");
495 IsImp = Val;
496 }
497
498 void setIsKill(bool Val = true) {
499 assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
500 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
501 IsDeadOrKill = Val;
502 }
503
504 void setIsDead(bool Val = true) {
505 assert(isReg() && IsDef && "Wrong MachineOperand mutator");
506 IsDeadOrKill = Val;
507 }
508
509 void setIsUndef(bool Val = true) {
510 assert(isReg() && "Wrong MachineOperand mutator");
511 IsUndef = Val;
512 }
513
514 void setIsRenamable(bool Val = true);
515
516 void setIsInternalRead(bool Val = true) {
517 assert(isReg() && "Wrong MachineOperand mutator");
518 IsInternalRead = Val;
519 }
520
521 void setIsEarlyClobber(bool Val = true) {
522 assert(isReg() && IsDef && "Wrong MachineOperand mutator");
523 IsEarlyClobber = Val;
524 }
525
526 void setIsDebug(bool Val = true) {
527 assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
528 IsDebug = Val;
529 }
530
531 //===--------------------------------------------------------------------===//
532 // Accessors for various operand types.
533 //===--------------------------------------------------------------------===//
534
535 int64_t getImm() const {
536 assert(isImm() && "Wrong MachineOperand accessor");
537 return Contents.ImmVal;
538 }
539
540 const ConstantInt *getCImm() const {
541 assert(isCImm() && "Wrong MachineOperand accessor");
542 return Contents.CI;
543 }
544
545 const ConstantFP *getFPImm() const {
546 assert(isFPImm() && "Wrong MachineOperand accessor");
547 return Contents.CFP;
548 }
549
550 MachineBasicBlock *getMBB() const {
551 assert(isMBB() && "Wrong MachineOperand accessor");
552 return Contents.MBB;
553 }
554
555 int getIndex() const {
556 assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
557 "Wrong MachineOperand accessor");
558 return Contents.OffsetedInfo.Val.Index;
559 }
560
561 const GlobalValue *getGlobal() const {
562 assert(isGlobal() && "Wrong MachineOperand accessor");
563 return Contents.OffsetedInfo.Val.GV;
564 }
565
566 const BlockAddress *getBlockAddress() const {
567 assert(isBlockAddress() && "Wrong MachineOperand accessor");
568 return Contents.OffsetedInfo.Val.BA;
569 }
570
571 MCSymbol *getMCSymbol() const {
572 assert(isMCSymbol() && "Wrong MachineOperand accessor");
573 return Contents.Sym;
574 }
575
576 unsigned getCFIIndex() const {
577 assert(isCFIIndex() && "Wrong MachineOperand accessor");
578 return Contents.CFIIndex;
579 }
580
581 Intrinsic::ID getIntrinsicID() const {
582 assert(isIntrinsicID() && "Wrong MachineOperand accessor");
583 return Contents.IntrinsicID;
584 }
585
586 unsigned getPredicate() const {
587 assert(isPredicate() && "Wrong MachineOperand accessor");
588 return Contents.Pred;
589 }
590
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200591 ArrayRef<int> getShuffleMask() const {
592 assert(isShuffleMask() && "Wrong MachineOperand accessor");
593 return Contents.ShuffleMask;
594 }
595
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100596 /// Return the offset from the symbol in this operand. This always returns 0
597 /// for ExternalSymbol operands.
598 int64_t getOffset() const {
599 assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
600 isTargetIndex() || isBlockAddress()) &&
601 "Wrong MachineOperand accessor");
602 return int64_t(uint64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
603 SmallContents.OffsetLo;
604 }
605
606 const char *getSymbolName() const {
607 assert(isSymbol() && "Wrong MachineOperand accessor");
608 return Contents.OffsetedInfo.Val.SymbolName;
609 }
610
611 /// clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
612 /// It is sometimes necessary to detach the register mask pointer from its
613 /// machine operand. This static method can be used for such detached bit
614 /// mask pointers.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200615 static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100616 // See TargetRegisterInfo.h.
617 assert(PhysReg < (1u << 30) && "Not a physical register");
618 return !(RegMask[PhysReg / 32] & (1u << PhysReg % 32));
619 }
620
621 /// clobbersPhysReg - Returns true if this RegMask operand clobbers PhysReg.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200622 bool clobbersPhysReg(MCRegister PhysReg) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100623 return clobbersPhysReg(getRegMask(), PhysReg);
624 }
625
626 /// getRegMask - Returns a bit mask of registers preserved by this RegMask
627 /// operand.
628 const uint32_t *getRegMask() const {
629 assert(isRegMask() && "Wrong MachineOperand accessor");
630 return Contents.RegMask;
631 }
632
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100633 /// Returns number of elements needed for a regmask array.
634 static unsigned getRegMaskSize(unsigned NumRegs) {
635 return (NumRegs + 31) / 32;
636 }
637
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100638 /// getRegLiveOut - Returns a bit mask of live-out registers.
639 const uint32_t *getRegLiveOut() const {
640 assert(isRegLiveOut() && "Wrong MachineOperand accessor");
641 return Contents.RegMask;
642 }
643
644 const MDNode *getMetadata() const {
645 assert(isMetadata() && "Wrong MachineOperand accessor");
646 return Contents.MD;
647 }
648
649 //===--------------------------------------------------------------------===//
650 // Mutators for various operand types.
651 //===--------------------------------------------------------------------===//
652
653 void setImm(int64_t immVal) {
654 assert(isImm() && "Wrong MachineOperand mutator");
655 Contents.ImmVal = immVal;
656 }
657
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100658 void setCImm(const ConstantInt *CI) {
659 assert(isCImm() && "Wrong MachineOperand mutator");
660 Contents.CI = CI;
661 }
662
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100663 void setFPImm(const ConstantFP *CFP) {
664 assert(isFPImm() && "Wrong MachineOperand mutator");
665 Contents.CFP = CFP;
666 }
667
668 void setOffset(int64_t Offset) {
669 assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
670 isTargetIndex() || isBlockAddress()) &&
671 "Wrong MachineOperand mutator");
672 SmallContents.OffsetLo = unsigned(Offset);
673 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
674 }
675
676 void setIndex(int Idx) {
677 assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
678 "Wrong MachineOperand mutator");
679 Contents.OffsetedInfo.Val.Index = Idx;
680 }
681
682 void setMetadata(const MDNode *MD) {
683 assert(isMetadata() && "Wrong MachineOperand mutator");
684 Contents.MD = MD;
685 }
686
687 void setMBB(MachineBasicBlock *MBB) {
688 assert(isMBB() && "Wrong MachineOperand mutator");
689 Contents.MBB = MBB;
690 }
691
692 /// Sets value of register mask operand referencing Mask. The
693 /// operand does not take ownership of the memory referenced by Mask, it must
694 /// remain valid for the lifetime of the operand. See CreateRegMask().
695 /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
696 void setRegMask(const uint32_t *RegMaskPtr) {
697 assert(isRegMask() && "Wrong MachineOperand mutator");
698 Contents.RegMask = RegMaskPtr;
699 }
700
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200701 void setIntrinsicID(Intrinsic::ID IID) {
702 assert(isIntrinsicID() && "Wrong MachineOperand mutator");
703 Contents.IntrinsicID = IID;
704 }
705
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100706 void setPredicate(unsigned Predicate) {
707 assert(isPredicate() && "Wrong MachineOperand mutator");
708 Contents.Pred = Predicate;
709 }
710
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100711 //===--------------------------------------------------------------------===//
712 // Other methods.
713 //===--------------------------------------------------------------------===//
714
715 /// Returns true if this operand is identical to the specified operand except
716 /// for liveness related flags (isKill, isUndef and isDead). Note that this
717 /// should stay in sync with the hash_value overload below.
718 bool isIdenticalTo(const MachineOperand &Other) const;
719
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100720 /// MachineOperand hash_value overload.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100721 ///
722 /// Note that this includes the same information in the hash that
723 /// isIdenticalTo uses for comparison. It is thus suited for use in hash
724 /// tables which use that function for equality comparisons only. This must
725 /// stay exactly in sync with isIdenticalTo above.
726 friend hash_code hash_value(const MachineOperand &MO);
727
728 /// ChangeToImmediate - Replace this operand with a new immediate operand of
729 /// the specified value. If an operand is known to be an immediate already,
730 /// the setImm method should be used.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200731 void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100732
733 /// ChangeToFPImmediate - Replace this operand with a new FP immediate operand
734 /// of the specified value. If an operand is known to be an FP immediate
735 /// already, the setFPImm method should be used.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200736 void ChangeToFPImmediate(const ConstantFP *FPImm, unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100737
738 /// ChangeToES - Replace this operand with a new external symbol operand.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200739 void ChangeToES(const char *SymName, unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100740
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100741 /// ChangeToGA - Replace this operand with a new global address operand.
742 void ChangeToGA(const GlobalValue *GV, int64_t Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200743 unsigned TargetFlags = 0);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100744
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100745 /// ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200746 void ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100747
748 /// Replace this operand with a frame index.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200749 void ChangeToFrameIndex(int Idx, unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100750
751 /// Replace this operand with a target index.
752 void ChangeToTargetIndex(unsigned Idx, int64_t Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200753 unsigned TargetFlags = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100754
755 /// ChangeToRegister - Replace this operand with a new register operand of
756 /// the specified value. If an operand is known to be an register already,
757 /// the setReg method should be used.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200758 void ChangeToRegister(Register Reg, bool isDef, bool isImp = false,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100759 bool isKill = false, bool isDead = false,
760 bool isUndef = false, bool isDebug = false);
761
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200762 /// getTargetIndexName - If this MachineOperand is a TargetIndex that has a
763 /// name, attempt to get the name. Returns nullptr if the TargetIndex does not
764 /// have a name. Asserts if MO is not a TargetIndex.
765 const char *getTargetIndexName() const;
766
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100767 //===--------------------------------------------------------------------===//
768 // Construction methods.
769 //===--------------------------------------------------------------------===//
770
771 static MachineOperand CreateImm(int64_t Val) {
772 MachineOperand Op(MachineOperand::MO_Immediate);
773 Op.setImm(Val);
774 return Op;
775 }
776
777 static MachineOperand CreateCImm(const ConstantInt *CI) {
778 MachineOperand Op(MachineOperand::MO_CImmediate);
779 Op.Contents.CI = CI;
780 return Op;
781 }
782
783 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
784 MachineOperand Op(MachineOperand::MO_FPImmediate);
785 Op.Contents.CFP = CFP;
786 return Op;
787 }
788
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200789 static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp = false,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100790 bool isKill = false, bool isDead = false,
791 bool isUndef = false,
792 bool isEarlyClobber = false,
793 unsigned SubReg = 0, bool isDebug = false,
794 bool isInternalRead = false,
795 bool isRenamable = false) {
796 assert(!(isDead && !isDef) && "Dead flag on non-def");
797 assert(!(isKill && isDef) && "Kill flag on def");
798 MachineOperand Op(MachineOperand::MO_Register);
799 Op.IsDef = isDef;
800 Op.IsImp = isImp;
801 Op.IsDeadOrKill = isKill | isDead;
802 Op.IsRenamable = isRenamable;
803 Op.IsUndef = isUndef;
804 Op.IsInternalRead = isInternalRead;
805 Op.IsEarlyClobber = isEarlyClobber;
806 Op.TiedTo = 0;
807 Op.IsDebug = isDebug;
808 Op.SmallContents.RegNo = Reg;
809 Op.Contents.Reg.Prev = nullptr;
810 Op.Contents.Reg.Next = nullptr;
811 Op.setSubReg(SubReg);
812 return Op;
813 }
814 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200815 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100816 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
817 Op.setMBB(MBB);
818 Op.setTargetFlags(TargetFlags);
819 return Op;
820 }
821 static MachineOperand CreateFI(int Idx) {
822 MachineOperand Op(MachineOperand::MO_FrameIndex);
823 Op.setIndex(Idx);
824 return Op;
825 }
826 static MachineOperand CreateCPI(unsigned Idx, int Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200827 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100828 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
829 Op.setIndex(Idx);
830 Op.setOffset(Offset);
831 Op.setTargetFlags(TargetFlags);
832 return Op;
833 }
834 static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200835 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100836 MachineOperand Op(MachineOperand::MO_TargetIndex);
837 Op.setIndex(Idx);
838 Op.setOffset(Offset);
839 Op.setTargetFlags(TargetFlags);
840 return Op;
841 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200842 static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100843 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
844 Op.setIndex(Idx);
845 Op.setTargetFlags(TargetFlags);
846 return Op;
847 }
848 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200849 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100850 MachineOperand Op(MachineOperand::MO_GlobalAddress);
851 Op.Contents.OffsetedInfo.Val.GV = GV;
852 Op.setOffset(Offset);
853 Op.setTargetFlags(TargetFlags);
854 return Op;
855 }
856 static MachineOperand CreateES(const char *SymName,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200857 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100858 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
859 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
860 Op.setOffset(0); // Offset is always 0.
861 Op.setTargetFlags(TargetFlags);
862 return Op;
863 }
864 static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200865 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100866 MachineOperand Op(MachineOperand::MO_BlockAddress);
867 Op.Contents.OffsetedInfo.Val.BA = BA;
868 Op.setOffset(Offset);
869 Op.setTargetFlags(TargetFlags);
870 return Op;
871 }
872 /// CreateRegMask - Creates a register mask operand referencing Mask. The
873 /// operand does not take ownership of the memory referenced by Mask, it
874 /// must remain valid for the lifetime of the operand.
875 ///
876 /// A RegMask operand represents a set of non-clobbered physical registers
877 /// on an instruction that clobbers many registers, typically a call. The
878 /// bit mask has a bit set for each physreg that is preserved by this
879 /// instruction, as described in the documentation for
880 /// TargetRegisterInfo::getCallPreservedMask().
881 ///
882 /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
883 ///
884 static MachineOperand CreateRegMask(const uint32_t *Mask) {
885 assert(Mask && "Missing register mask");
886 MachineOperand Op(MachineOperand::MO_RegisterMask);
887 Op.Contents.RegMask = Mask;
888 return Op;
889 }
890 static MachineOperand CreateRegLiveOut(const uint32_t *Mask) {
891 assert(Mask && "Missing live-out register mask");
892 MachineOperand Op(MachineOperand::MO_RegisterLiveOut);
893 Op.Contents.RegMask = Mask;
894 return Op;
895 }
896 static MachineOperand CreateMetadata(const MDNode *Meta) {
897 MachineOperand Op(MachineOperand::MO_Metadata);
898 Op.Contents.MD = Meta;
899 return Op;
900 }
901
902 static MachineOperand CreateMCSymbol(MCSymbol *Sym,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200903 unsigned TargetFlags = 0) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100904 MachineOperand Op(MachineOperand::MO_MCSymbol);
905 Op.Contents.Sym = Sym;
906 Op.setOffset(0);
907 Op.setTargetFlags(TargetFlags);
908 return Op;
909 }
910
911 static MachineOperand CreateCFIIndex(unsigned CFIIndex) {
912 MachineOperand Op(MachineOperand::MO_CFIIndex);
913 Op.Contents.CFIIndex = CFIIndex;
914 return Op;
915 }
916
917 static MachineOperand CreateIntrinsicID(Intrinsic::ID ID) {
918 MachineOperand Op(MachineOperand::MO_IntrinsicID);
919 Op.Contents.IntrinsicID = ID;
920 return Op;
921 }
922
923 static MachineOperand CreatePredicate(unsigned Pred) {
924 MachineOperand Op(MachineOperand::MO_Predicate);
925 Op.Contents.Pred = Pred;
926 return Op;
927 }
928
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200929 static MachineOperand CreateShuffleMask(ArrayRef<int> Mask) {
930 MachineOperand Op(MachineOperand::MO_ShuffleMask);
931 Op.Contents.ShuffleMask = Mask;
932 return Op;
933 }
934
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100935 friend class MachineInstr;
936 friend class MachineRegisterInfo;
937
938private:
939 // If this operand is currently a register operand, and if this is in a
940 // function, deregister the operand from the register's use/def list.
941 void removeRegFromUses();
942
943 /// Artificial kinds for DenseMap usage.
944 enum : unsigned char {
945 MO_Empty = MO_Last + 1,
946 MO_Tombstone,
947 };
948
949 friend struct DenseMapInfo<MachineOperand>;
950
951 //===--------------------------------------------------------------------===//
952 // Methods for handling register use/def lists.
953 //===--------------------------------------------------------------------===//
954
955 /// isOnRegUseList - Return true if this operand is on a register use/def
956 /// list or false if not. This can only be called for register operands
957 /// that are part of a machine instruction.
958 bool isOnRegUseList() const {
959 assert(isReg() && "Can only add reg operand to use lists");
960 return Contents.Reg.Prev != nullptr;
961 }
962};
963
964template <> struct DenseMapInfo<MachineOperand> {
965 static MachineOperand getEmptyKey() {
966 return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
967 MachineOperand::MO_Empty));
968 }
969 static MachineOperand getTombstoneKey() {
970 return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
971 MachineOperand::MO_Tombstone));
972 }
973 static unsigned getHashValue(const MachineOperand &MO) {
974 return hash_value(MO);
975 }
976 static bool isEqual(const MachineOperand &LHS, const MachineOperand &RHS) {
977 if (LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
978 MachineOperand::MO_Empty) ||
979 LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
980 MachineOperand::MO_Tombstone))
981 return LHS.getType() == RHS.getType();
982 return LHS.isIdenticalTo(RHS);
983 }
984};
985
986inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand &MO) {
987 MO.print(OS);
988 return OS;
989}
990
991// See friend declaration above. This additional declaration is required in
992// order to compile LLVM with IBM xlC compiler.
993hash_code hash_value(const MachineOperand &MO);
994} // namespace llvm
995
996#endif