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