1//===- llvm/CodeGen/MachineFunction.h ---------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Collect native machine code for a function. This class contains a list of
10// MachineBasicBlock instances that make up the current compiled function.
11//
12// This class also contains pointers to various classes which hold
13// target-specific information about the generated code.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
18#define LLVM_CODEGEN_MACHINEFUNCTION_H
19
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/GraphTraits.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/ilist.h"
25#include "llvm/ADT/iterator.h"
26#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineMemOperand.h"
29#include "llvm/IR/EHPersonalities.h"
30#include "llvm/Support/Allocator.h"
31#include "llvm/Support/ArrayRecycler.h"
32#include "llvm/Support/AtomicOrdering.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/Recycler.h"
35#include "llvm/Target/TargetOptions.h"
36#include <bitset>
37#include <cassert>
38#include <cstdint>
39#include <memory>
40#include <utility>
41#include <variant>
42#include <vector>
43
44namespace llvm {
45
46class BasicBlock;
47class BlockAddress;
48class DataLayout;
49class DebugLoc;
50struct DenormalMode;
51class DIExpression;
52class DILocalVariable;
53class DILocation;
54class Function;
55class GISelChangeObserver;
56class GlobalValue;
57class TargetMachine;
58class MachineConstantPool;
59class MachineFrameInfo;
60class MachineFunction;
61class MachineJumpTableInfo;
62class MachineRegisterInfo;
63class MCContext;
64class MCInstrDesc;
65class MCSymbol;
66class MCSection;
67class Pass;
68class PseudoSourceValueManager;
69class raw_ostream;
70class SlotIndexes;
71class StringRef;
72class TargetRegisterClass;
73class TargetSubtargetInfo;
74struct WasmEHFuncInfo;
75struct WinEHFuncInfo;
76
77template <> struct ilist_alloc_traits<MachineBasicBlock> {
78 LLVM_ABI void deleteNode(MachineBasicBlock *MBB);
79};
80
81template <> struct ilist_callback_traits<MachineBasicBlock> {
82 LLVM_ABI void addNodeToList(MachineBasicBlock *N);
83 LLVM_ABI void removeNodeFromList(MachineBasicBlock *N);
84
85 template <class Iterator>
86 void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator) {
87 assert(this == &OldList && "never transfer MBBs between functions");
88 }
89};
90
91// The hotness of static data tracked by a MachineFunction and not represented
92// as a global object in the module IR / MIR. Typical examples are
93// MachineJumpTableInfo and MachineConstantPool.
94enum class MachineFunctionDataHotness {
95 Unknown,
96 Cold,
97 Hot,
98};
99
100/// MachineFunctionInfo - This class can be derived from and used by targets to
101/// hold private target-specific information for each MachineFunction. Objects
102/// of type are accessed/created with MF::getInfo and destroyed when the
103/// MachineFunction is destroyed.
104struct LLVM_ABI MachineFunctionInfo {
105 virtual ~MachineFunctionInfo();
106
107 /// Factory function: default behavior is to call new using the
108 /// supplied allocator.
109 ///
110 /// This function can be overridden in a derive class.
111 template <typename FuncInfoTy, typename SubtargetTy = TargetSubtargetInfo>
112 static FuncInfoTy *create(BumpPtrAllocator &Allocator, const Function &F,
113 const SubtargetTy *STI) {
114 return new (Allocator.Allocate<FuncInfoTy>()) FuncInfoTy(F, STI);
115 }
116
117 template <typename Ty>
118 static Ty *create(BumpPtrAllocator &Allocator, const Ty &MFI) {
119 return new (Allocator.Allocate<Ty>()) Ty(MFI);
120 }
121
122 /// Make a functionally equivalent copy of this MachineFunctionInfo in \p MF.
123 /// This requires remapping MachineBasicBlock references from the original
124 /// parent to values in the new function. Targets may assume that virtual
125 /// register and frame index values are preserved in the new function.
126 virtual MachineFunctionInfo *
127 clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
128 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
129 const {
130 return nullptr;
131 }
132};
133
134/// Properties which a MachineFunction may have at a given point in time.
135/// Each of these has checking code in the MachineVerifier, and passes can
136/// require that a property be set.
137class MachineFunctionProperties {
138 // Possible TODO: Allow targets to extend this (perhaps by allowing the
139 // constructor to specify the size of the bit vector)
140 // Possible TODO: Allow requiring the negative (e.g. VRegsAllocated could be
141 // stated as the negative of "has vregs"
142
143public:
144 // The properties are stated in "positive" form; i.e. a pass could require
145 // that the property hold, but not that it does not hold.
146
147 // Property descriptions:
148 // IsSSA: True when the machine function is in SSA form and virtual registers
149 // have a single def.
150 // NoPHIs: The machine function does not contain any PHI instruction.
151 // TracksLiveness: True when tracking register liveness accurately.
152 // While this property is set, register liveness information in basic block
153 // live-in lists and machine instruction operands (e.g. implicit defs) is
154 // accurate, kill flags are conservatively accurate (kill flag correctly
155 // indicates the last use of a register, an operand without kill flag may or
156 // may not be the last use of a register). This means it can be used to
157 // change the code in ways that affect the values in registers, for example
158 // by the register scavenger.
159 // When this property is cleared at a very late time, liveness is no longer
160 // reliable.
161 // NoVRegs: The machine function does not use any virtual registers.
162 // Legalized: In GlobalISel: the MachineLegalizer ran and all pre-isel generic
163 // instructions have been legalized; i.e., all instructions are now one of:
164 // - generic and always legal (e.g., COPY)
165 // - target-specific
166 // - legal pre-isel generic instructions.
167 // RegBankSelected: In GlobalISel: the RegBankSelect pass ran and all generic
168 // virtual registers have been assigned to a register bank.
169 // Selected: In GlobalISel: the InstructionSelect pass ran and all pre-isel
170 // generic instructions have been eliminated; i.e., all instructions are now
171 // target-specific or non-pre-isel generic instructions (e.g., COPY).
172 // Since only pre-isel generic instructions can have generic virtual register
173 // operands, this also means that all generic virtual registers have been
174 // constrained to virtual registers (assigned to register classes) and that
175 // all sizes attached to them have been eliminated.
176 // TiedOpsRewritten: The twoaddressinstruction pass will set this flag, it
177 // means that tied-def have been rewritten to meet the RegConstraint.
178 // FailsVerification: Means that the function is not expected to pass machine
179 // verification. This can be set by passes that introduce known problems that
180 // have not been fixed yet.
181 // TracksDebugUserValues: Without this property enabled, debug instructions
182 // such as DBG_VALUE are allowed to reference virtual registers even if those
183 // registers do not have a definition. With the property enabled virtual
184 // registers must only be used if they have a definition. This property
185 // allows earlier passes in the pipeline to skip updates of `DBG_VALUE`
186 // instructions to save compile time.
187 enum class Property : unsigned {
188 IsSSA,
189 NoPHIs,
190 TracksLiveness,
191 NoVRegs,
192 FailedISel,
193 Legalized,
194 RegBankSelected,
195 Selected,
196 TiedOpsRewritten,
197 FailsVerification,
198 FailedRegAlloc,
199 TracksDebugUserValues,
200 LastProperty = TracksDebugUserValues,
201 };
202
203 bool hasProperty(Property P) const {
204 return Properties[static_cast<unsigned>(P)];
205 }
206
207 MachineFunctionProperties &set(Property P) {
208 Properties.set(position: static_cast<unsigned>(P));
209 return *this;
210 }
211
212 MachineFunctionProperties &reset(Property P) {
213 Properties.reset(position: static_cast<unsigned>(P));
214 return *this;
215 }
216
217 // Per property has/set/reset accessors.
218#define PPACCESSORS(X) \
219 bool has##X() const { return hasProperty(Property::X); } \
220 MachineFunctionProperties &set##X(void) { return set(Property::X); } \
221 MachineFunctionProperties &reset##X(void) { return reset(Property::X); }
222
223 PPACCESSORS(IsSSA)
224 PPACCESSORS(NoPHIs)
225 PPACCESSORS(TracksLiveness)
226 PPACCESSORS(NoVRegs)
227 PPACCESSORS(FailedISel)
228 PPACCESSORS(Legalized)
229 PPACCESSORS(RegBankSelected)
230 PPACCESSORS(Selected)
231 PPACCESSORS(TiedOpsRewritten)
232 PPACCESSORS(FailsVerification)
233 PPACCESSORS(FailedRegAlloc)
234 PPACCESSORS(TracksDebugUserValues)
235
236 /// Reset all the properties.
237 MachineFunctionProperties &reset() {
238 Properties.reset();
239 return *this;
240 }
241
242 MachineFunctionProperties &set(const MachineFunctionProperties &MFP) {
243 Properties |= MFP.Properties;
244 return *this;
245 }
246
247 MachineFunctionProperties &reset(const MachineFunctionProperties &MFP) {
248 Properties &= ~MFP.Properties;
249 return *this;
250 }
251
252 // Returns true if all properties set in V (i.e. required by a pass) are set
253 // in this.
254 bool verifyRequiredProperties(const MachineFunctionProperties &V) const {
255 return (Properties | ~V.Properties).all();
256 }
257
258 /// Print the MachineFunctionProperties in human-readable form.
259 LLVM_ABI void print(raw_ostream &OS) const;
260
261private:
262 std::bitset<static_cast<unsigned>(Property::LastProperty) + 1> Properties;
263};
264
265struct SEHHandler {
266 /// Filter or finally function. Null indicates a catch-all.
267 const Function *FilterOrFinally;
268
269 /// Address of block to recover at. Null for a finally handler.
270 const BlockAddress *RecoverBA;
271};
272
273/// This structure is used to retain landing pad info for the current function.
274struct LandingPadInfo {
275 MachineBasicBlock *LandingPadBlock; // Landing pad block.
276 SmallVector<MCSymbol *, 1> BeginLabels; // Labels prior to invoke.
277 SmallVector<MCSymbol *, 1> EndLabels; // Labels after invoke.
278 SmallVector<SEHHandler, 1> SEHHandlers; // SEH handlers active at this lpad.
279 MCSymbol *LandingPadLabel = nullptr; // Label at beginning of landing pad.
280 std::vector<int> TypeIds; // List of type ids (filters negative).
281
282 explicit LandingPadInfo(MachineBasicBlock *MBB)
283 : LandingPadBlock(MBB) {}
284};
285
286class LLVM_ABI MachineFunction {
287 Function &F;
288 const TargetMachine &Target;
289 const TargetSubtargetInfo *STI;
290 MCContext &Ctx;
291
292 // RegInfo - Information about each register in use in the function.
293 MachineRegisterInfo *RegInfo;
294
295 // Used to keep track of target-specific per-machine-function information for
296 // the target implementation.
297 MachineFunctionInfo *MFInfo;
298
299 // Keep track of objects allocated on the stack.
300 MachineFrameInfo *FrameInfo;
301
302 // Keep track of constants which are spilled to memory
303 MachineConstantPool *ConstantPool;
304
305 // Keep track of jump tables for switch instructions
306 MachineJumpTableInfo *JumpTableInfo;
307
308 // Keep track of the function section.
309 MCSection *Section = nullptr;
310
311 // Catchpad unwind destination info for wasm EH.
312 // Keeps track of Wasm exception handling related data. This will be null for
313 // functions that aren't using a wasm EH personality.
314 WasmEHFuncInfo *WasmEHInfo = nullptr;
315
316 // Keeps track of Windows exception handling related data. This will be null
317 // for functions that aren't using a funclet-based EH personality.
318 WinEHFuncInfo *WinEHInfo = nullptr;
319
320 // Function-level unique numbering for MachineBasicBlocks. When a
321 // MachineBasicBlock is inserted into a MachineFunction is it automatically
322 // numbered and this vector keeps track of the mapping from ID's to MBB's.
323 std::vector<MachineBasicBlock*> MBBNumbering;
324
325 // MBBNumbering epoch, incremented after renumbering to detect use of old
326 // block numbers.
327 unsigned MBBNumberingEpoch = 0;
328
329 // Pool-allocate MachineFunction-lifetime and IR objects.
330 BumpPtrAllocator Allocator;
331
332 // Allocation management for instructions in function.
333 Recycler<MachineInstr> InstructionRecycler;
334
335 // Allocation management for operand arrays on instructions.
336 ArrayRecycler<MachineOperand> OperandRecycler;
337
338 // Allocation management for basic blocks in function.
339 Recycler<MachineBasicBlock> BasicBlockRecycler;
340
341 // List of machine basic blocks in function
342 using BasicBlockListType = ilist<MachineBasicBlock>;
343 BasicBlockListType BasicBlocks;
344
345 /// FunctionNumber - This provides a unique ID for each function emitted in
346 /// this translation unit.
347 ///
348 unsigned FunctionNumber;
349
350 /// Alignment - The alignment of the function.
351 Align Alignment;
352
353 /// ExposesReturnsTwice - True if the function calls setjmp or related
354 /// functions with attribute "returns twice", but doesn't have
355 /// the attribute itself.
356 /// This is used to limit optimizations which cannot reason
357 /// about the control flow of such functions.
358 bool ExposesReturnsTwice = false;
359
360 /// True if the function includes any inline assembly.
361 bool HasInlineAsm = false;
362
363 /// True if any WinCFI instruction have been emitted in this function.
364 bool HasWinCFI = false;
365
366 /// Current high-level properties of the IR of the function (e.g. is in SSA
367 /// form or whether registers have been allocated)
368 MachineFunctionProperties Properties;
369
370 // Allocation management for pseudo source values.
371 std::unique_ptr<PseudoSourceValueManager> PSVManager;
372
373 /// List of moves done by a function's prolog. Used to construct frame maps
374 /// by debug and exception handling consumers.
375 std::vector<MCCFIInstruction> FrameInstructions;
376
377 /// List of basic blocks immediately following calls to _setjmp. Used to
378 /// construct a table of valid longjmp targets for Windows Control Flow Guard.
379 std::vector<MCSymbol *> LongjmpTargets;
380
381 /// List of basic blocks that are the targets for Windows EH Continuation
382 /// Guard.
383 std::vector<MCSymbol *> EHContTargets;
384
385 /// \name Exception Handling
386 /// \{
387
388 /// List of LandingPadInfo describing the landing pad information.
389 std::vector<LandingPadInfo> LandingPads;
390
391 /// Map a landing pad's EH symbol to the call site indexes.
392 DenseMap<MCSymbol*, SmallVector<unsigned, 4>> LPadToCallSiteMap;
393
394 /// Map a landing pad to its index.
395 DenseMap<const MachineBasicBlock *, unsigned> WasmLPadToIndexMap;
396
397 /// Map of invoke call site index values to associated begin EH_LABEL.
398 DenseMap<MCSymbol*, unsigned> CallSiteMap;
399
400 /// CodeView label annotations.
401 std::vector<std::pair<MCSymbol *, MDNode *>> CodeViewAnnotations;
402
403 bool CallsEHReturn = false;
404 bool CallsUnwindInit = false;
405 bool HasEHContTarget = false;
406 bool HasEHScopes = false;
407 bool HasEHFunclets = false;
408 bool HasFakeUses = false;
409 bool IsOutlined = false;
410
411 /// BBID to assign to the next basic block of this function.
412 unsigned NextBBID = 0;
413
414 /// Section Type for basic blocks, only relevant with basic block sections.
415 BasicBlockSection BBSectionsType = BasicBlockSection::None;
416
417 /// List of C++ TypeInfo used.
418 std::vector<const GlobalValue *> TypeInfos;
419
420 /// List of typeids encoding filters used.
421 std::vector<unsigned> FilterIds;
422
423 /// List of the indices in FilterIds corresponding to filter terminators.
424 std::vector<unsigned> FilterEnds;
425
426 EHPersonality PersonalityTypeCache = EHPersonality::Unknown;
427
428 /// \}
429
430 /// Clear all the members of this MachineFunction, but the ones used to
431 /// initialize again the MachineFunction. More specifically, this deallocates
432 /// all the dynamically allocated objects and get rids of all the XXXInfo data
433 /// structure, but keeps unchanged the references to Fn, Target, and
434 /// FunctionNumber.
435 void clear();
436 /// Allocate and initialize the different members.
437 /// In particular, the XXXInfo data structure.
438 /// \pre Fn, Target, and FunctionNumber are properly set.
439 void init();
440
441public:
442 /// Description of the location of a variable whose Address is valid and
443 /// unchanging during function execution. The Address may be:
444 /// * A stack index, which can be negative for fixed stack objects.
445 /// * A MCRegister, whose entry value contains the address of the variable.
446 class VariableDbgInfo {
447 std::variant<int, MCRegister> Address;
448
449 public:
450 const DILocalVariable *Var;
451 const DIExpression *Expr;
452 const DILocation *Loc;
453
454 VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
455 int Slot, const DILocation *Loc)
456 : Address(Slot), Var(Var), Expr(Expr), Loc(Loc) {}
457
458 VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
459 MCRegister EntryValReg, const DILocation *Loc)
460 : Address(EntryValReg), Var(Var), Expr(Expr), Loc(Loc) {}
461
462 /// Return true if this variable is in a stack slot.
463 bool inStackSlot() const { return std::holds_alternative<int>(v: Address); }
464
465 /// Return true if this variable is in the entry value of a register.
466 bool inEntryValueRegister() const {
467 return std::holds_alternative<MCRegister>(v: Address);
468 }
469
470 /// Returns the stack slot of this variable, assuming `inStackSlot()` is
471 /// true.
472 int getStackSlot() const { return std::get<int>(v: Address); }
473
474 /// Returns the MCRegister of this variable, assuming
475 /// `inEntryValueRegister()` is true.
476 MCRegister getEntryValueRegister() const {
477 return std::get<MCRegister>(v: Address);
478 }
479
480 /// Updates the stack slot of this variable, assuming `inStackSlot()` is
481 /// true.
482 void updateStackSlot(int NewSlot) {
483 assert(inStackSlot());
484 Address = NewSlot;
485 }
486 };
487
488 class LLVM_ABI Delegate {
489 virtual void anchor();
490
491 public:
492 virtual ~Delegate() = default;
493 /// Callback after an insertion. This should not modify the MI directly.
494 virtual void MF_HandleInsertion(MachineInstr &MI) = 0;
495 /// Callback before a removal. This should not modify the MI directly.
496 virtual void MF_HandleRemoval(MachineInstr &MI) = 0;
497 /// Callback before changing MCInstrDesc. This should not modify the MI
498 /// directly.
499 virtual void MF_HandleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID) {
500 }
501 };
502
503 /// Structure used to represent pair of argument number after call lowering
504 /// and register used to transfer that argument.
505 /// For now we support only cases when argument is transferred through one
506 /// register.
507 struct ArgRegPair {
508 Register Reg;
509 uint16_t ArgNo;
510 ArgRegPair(Register R, unsigned Arg) : Reg(R), ArgNo(Arg) {
511 assert(Arg < (1 << 16) && "Arg out of range");
512 }
513 };
514
515 struct CallSiteInfo {
516 /// Vector of call argument and its forwarding register.
517 SmallVector<ArgRegPair, 1> ArgRegPairs;
518 };
519
520 struct CalledGlobalInfo {
521 const GlobalValue *Callee;
522 unsigned TargetFlags;
523 };
524
525private:
526 Delegate *TheDelegate = nullptr;
527 GISelChangeObserver *Observer = nullptr;
528
529 using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;
530 /// Map a call instruction to call site arguments forwarding info.
531 CallSiteInfoMap CallSitesInfo;
532
533 /// A helper function that returns call site info for a give call
534 /// instruction if debug entry value support is enabled.
535 CallSiteInfoMap::iterator getCallSiteInfo(const MachineInstr *MI);
536
537 using CalledGlobalsMap = DenseMap<const MachineInstr *, CalledGlobalInfo>;
538 /// Mapping of call instruction to the global value and target flags that it
539 /// calls, if applicable.
540 CalledGlobalsMap CalledGlobalsInfo;
541
542 // Callbacks for insertion and removal.
543 void handleInsertion(MachineInstr &MI);
544 void handleRemoval(MachineInstr &MI);
545 friend struct ilist_traits<MachineInstr>;
546
547public:
548 // Need to be accessed from MachineInstr::setDesc.
549 void handleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID);
550
551 using VariableDbgInfoMapTy = SmallVector<VariableDbgInfo, 4>;
552 VariableDbgInfoMapTy VariableDbgInfos;
553
554 /// A count of how many instructions in the function have had numbers
555 /// assigned to them. Used for debug value tracking, to determine the
556 /// next instruction number.
557 unsigned DebugInstrNumberingCount = 0;
558
559 /// Set value of DebugInstrNumberingCount field. Avoid using this unless
560 /// you're deserializing this data.
561 void setDebugInstrNumberingCount(unsigned Num);
562
563 /// Pair of instruction number and operand number.
564 using DebugInstrOperandPair = std::pair<unsigned, unsigned>;
565
566 /// Replacement definition for a debug instruction reference. Made up of a
567 /// source instruction / operand pair, destination pair, and a qualifying
568 /// subregister indicating what bits in the operand make up the substitution.
569 // For example, a debug user
570 /// of %1:
571 /// %0:gr32 = someinst, debug-instr-number 1
572 /// %1:gr16 = %0.some_16_bit_subreg, debug-instr-number 2
573 /// Would receive the substitution {{2, 0}, {1, 0}, $subreg}, where $subreg is
574 /// the subregister number for some_16_bit_subreg.
575 class DebugSubstitution {
576 public:
577 DebugInstrOperandPair Src; ///< Source instruction / operand pair.
578 DebugInstrOperandPair Dest; ///< Replacement instruction / operand pair.
579 unsigned Subreg; ///< Qualifier for which part of Dest is read.
580
581 DebugSubstitution(const DebugInstrOperandPair &Src,
582 const DebugInstrOperandPair &Dest, unsigned Subreg)
583 : Src(Src), Dest(Dest), Subreg(Subreg) {}
584
585 /// Order only by source instruction / operand pair: there should never
586 /// be duplicate entries for the same source in any collection.
587 bool operator<(const DebugSubstitution &Other) const {
588 return Src < Other.Src;
589 }
590 };
591
592 /// Debug value substitutions: a collection of DebugSubstitution objects,
593 /// recording changes in where a value is defined. For example, when one
594 /// instruction is substituted for another. Keeping a record allows recovery
595 /// of variable locations after compilation finishes.
596 SmallVector<DebugSubstitution, 8> DebugValueSubstitutions;
597
598 /// Location of a PHI instruction that is also a debug-info variable value,
599 /// for the duration of register allocation. Loaded by the PHI-elimination
600 /// pass, and emitted as DBG_PHI instructions during VirtRegRewriter, with
601 /// maintenance applied by intermediate passes that edit registers (such as
602 /// coalescing and the allocator passes).
603 class DebugPHIRegallocPos {
604 public:
605 MachineBasicBlock *MBB; ///< Block where this PHI was originally located.
606 Register Reg; ///< VReg where the control-flow-merge happens.
607 unsigned SubReg; ///< Optional subreg qualifier within Reg.
608 DebugPHIRegallocPos(MachineBasicBlock *MBB, Register Reg, unsigned SubReg)
609 : MBB(MBB), Reg(Reg), SubReg(SubReg) {}
610 };
611
612 /// Map of debug instruction numbers to the position of their PHI instructions
613 /// during register allocation. See DebugPHIRegallocPos.
614 DenseMap<unsigned, DebugPHIRegallocPos> DebugPHIPositions;
615
616 /// Flag for whether this function contains DBG_VALUEs (false) or
617 /// DBG_INSTR_REF (true).
618 bool UseDebugInstrRef = false;
619
620 /// Create a substitution between one <instr,operand> value to a different,
621 /// new value.
622 void makeDebugValueSubstitution(DebugInstrOperandPair, DebugInstrOperandPair,
623 unsigned SubReg = 0);
624
625 /// Create substitutions for any tracked values in \p Old, to point at
626 /// \p New. Needed when we re-create an instruction during optimization,
627 /// which has the same signature (i.e., def operands in the same place) but
628 /// a modified instruction type, flags, or otherwise. An example: X86 moves
629 /// are sometimes transformed into equivalent LEAs.
630 /// If the two instructions are not the same opcode, limit which operands to
631 /// examine for substitutions to the first N operands by setting
632 /// \p MaxOperand.
633 void substituteDebugValuesForInst(const MachineInstr &Old, MachineInstr &New,
634 unsigned MaxOperand = UINT_MAX);
635
636 /// Find the underlying defining instruction / operand for a COPY instruction
637 /// while in SSA form. Copies do not actually define values -- they move them
638 /// between registers. Labelling a COPY-like instruction with an instruction
639 /// number is to be avoided as it makes value numbers non-unique later in
640 /// compilation. This method follows the definition chain for any sequence of
641 /// COPY-like instructions to find whatever non-COPY-like instruction defines
642 /// the copied value; or for parameters, creates a DBG_PHI on entry.
643 /// May insert instructions into the entry block!
644 /// \p MI The copy-like instruction to salvage.
645 /// \p DbgPHICache A container to cache already-solved COPYs.
646 /// \returns An instruction/operand pair identifying the defining value.
647 DebugInstrOperandPair
648 salvageCopySSA(MachineInstr &MI,
649 DenseMap<Register, DebugInstrOperandPair> &DbgPHICache);
650
651 DebugInstrOperandPair salvageCopySSAImpl(MachineInstr &MI);
652
653 /// Finalise any partially emitted debug instructions. These are DBG_INSTR_REF
654 /// instructions where we only knew the vreg of the value they use, not the
655 /// instruction that defines that vreg. Once isel finishes, we should have
656 /// enough information for every DBG_INSTR_REF to point at an instruction
657 /// (or DBG_PHI).
658 void finalizeDebugInstrRefs();
659
660 /// Determine whether, in the current machine configuration, we should use
661 /// instruction referencing or not.
662 bool shouldUseDebugInstrRef() const;
663
664 /// Returns true if the function's variable locations are tracked with
665 /// instruction referencing.
666 bool useDebugInstrRef() const;
667
668 /// Set whether this function will use instruction referencing or not.
669 void setUseDebugInstrRef(bool UseInstrRef);
670
671 /// A reserved operand number representing the instructions memory operand,
672 /// for instructions that have a stack spill fused into them.
673 const static unsigned int DebugOperandMemNumber;
674
675 MachineFunction(Function &F, const TargetMachine &Target,
676 const TargetSubtargetInfo &STI, MCContext &Ctx,
677 unsigned FunctionNum);
678 MachineFunction(const MachineFunction &) = delete;
679 MachineFunction &operator=(const MachineFunction &) = delete;
680 ~MachineFunction();
681
682 /// Reset the instance as if it was just created.
683 void reset() {
684 clear();
685 init();
686 }
687
688 /// Reset the currently registered delegate - otherwise assert.
689 void resetDelegate(Delegate *delegate) {
690 assert(TheDelegate == delegate &&
691 "Only the current delegate can perform reset!");
692 TheDelegate = nullptr;
693 }
694
695 /// Set the delegate. resetDelegate must be called before attempting
696 /// to set.
697 void setDelegate(Delegate *delegate) {
698 assert(delegate && !TheDelegate &&
699 "Attempted to set delegate to null, or to change it without "
700 "first resetting it!");
701
702 TheDelegate = delegate;
703 }
704
705 void setObserver(GISelChangeObserver *O) { Observer = O; }
706
707 GISelChangeObserver *getObserver() const { return Observer; }
708
709 MCContext &getContext() const { return Ctx; }
710
711 /// Returns the Section this function belongs to.
712 MCSection *getSection() const { return Section; }
713
714 /// Indicates the Section this function belongs to.
715 void setSection(MCSection *S) { Section = S; }
716
717 PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }
718
719 /// Return the DataLayout attached to the Module associated to this MF.
720 const DataLayout &getDataLayout() const;
721
722 /// Return the LLVM function that this machine code represents
723 Function &getFunction() { return F; }
724
725 /// Return the LLVM function that this machine code represents
726 const Function &getFunction() const { return F; }
727
728 /// getName - Return the name of the corresponding LLVM function.
729 StringRef getName() const;
730
731 /// getFunctionNumber - Return a unique ID for the current function.
732 unsigned getFunctionNumber() const { return FunctionNumber; }
733
734 /// Returns true if this function has basic block sections enabled.
735 bool hasBBSections() const {
736 return (BBSectionsType == BasicBlockSection::All ||
737 BBSectionsType == BasicBlockSection::List ||
738 BBSectionsType == BasicBlockSection::Preset);
739 }
740
741 void setBBSectionsType(BasicBlockSection V) { BBSectionsType = V; }
742
743 /// Assign IsBeginSection IsEndSection fields for basic blocks in this
744 /// function.
745 void assignBeginEndSections();
746
747 /// getTarget - Return the target machine this machine code is compiled with
748 const TargetMachine &getTarget() const { return Target; }
749
750 /// getSubtarget - Return the subtarget for which this machine code is being
751 /// compiled.
752 const TargetSubtargetInfo &getSubtarget() const { return *STI; }
753
754 /// getSubtarget - This method returns a pointer to the specified type of
755 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
756 /// returned is of the correct type.
757 template<typename STC> const STC &getSubtarget() const {
758 return *static_cast<const STC *>(STI);
759 }
760
761 /// getRegInfo - Return information about the registers currently in use.
762 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
763 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
764
765 /// getFrameInfo - Return the frame info object for the current function.
766 /// This object contains information about objects allocated on the stack
767 /// frame of the current function in an abstract way.
768 MachineFrameInfo &getFrameInfo() { return *FrameInfo; }
769 const MachineFrameInfo &getFrameInfo() const { return *FrameInfo; }
770
771 /// getJumpTableInfo - Return the jump table info object for the current
772 /// function. This object contains information about jump tables in the
773 /// current function. If the current function has no jump tables, this will
774 /// return null.
775 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
776 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
777
778 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
779 /// does already exist, allocate one.
780 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
781
782 /// getConstantPool - Return the constant pool object for the current
783 /// function.
784 MachineConstantPool *getConstantPool() { return ConstantPool; }
785 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
786
787 /// getWasmEHFuncInfo - Return information about how the current function uses
788 /// Wasm exception handling. Returns null for functions that don't use wasm
789 /// exception handling.
790 const WasmEHFuncInfo *getWasmEHFuncInfo() const { return WasmEHInfo; }
791 WasmEHFuncInfo *getWasmEHFuncInfo() { return WasmEHInfo; }
792
793 /// getWinEHFuncInfo - Return information about how the current function uses
794 /// Windows exception handling. Returns null for functions that don't use
795 /// funclets for exception handling.
796 const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; }
797 WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; }
798
799 /// getAlignment - Return the alignment of the function.
800 Align getAlignment() const { return Alignment; }
801
802 /// setAlignment - Set the alignment of the function.
803 void setAlignment(Align A) { Alignment = A; }
804
805 /// ensureAlignment - Make sure the function is at least A bytes aligned.
806 void ensureAlignment(Align A) {
807 if (Alignment < A)
808 Alignment = A;
809 }
810
811 /// exposesReturnsTwice - Returns true if the function calls setjmp or
812 /// any other similar functions with attribute "returns twice" without
813 /// having the attribute itself.
814 bool exposesReturnsTwice() const {
815 return ExposesReturnsTwice;
816 }
817
818 /// setCallsSetJmp - Set a flag that indicates if there's a call to
819 /// a "returns twice" function.
820 void setExposesReturnsTwice(bool B) {
821 ExposesReturnsTwice = B;
822 }
823
824 /// Returns true if the function contains any inline assembly.
825 bool hasInlineAsm() const {
826 return HasInlineAsm;
827 }
828
829 /// Set a flag that indicates that the function contains inline assembly.
830 void setHasInlineAsm(bool B) {
831 HasInlineAsm = B;
832 }
833
834 bool hasWinCFI() const {
835 return HasWinCFI;
836 }
837 void setHasWinCFI(bool v) { HasWinCFI = v; }
838
839 /// True if this function needs frame moves for debug or exceptions.
840 bool needsFrameMoves() const;
841
842 /// Get the function properties
843 const MachineFunctionProperties &getProperties() const { return Properties; }
844 MachineFunctionProperties &getProperties() { return Properties; }
845
846 /// getInfo - Keep track of various per-function pieces of information for
847 /// backends that would like to do so.
848 ///
849 template<typename Ty>
850 Ty *getInfo() {
851 return static_cast<Ty*>(MFInfo);
852 }
853
854 template<typename Ty>
855 const Ty *getInfo() const {
856 return static_cast<const Ty *>(MFInfo);
857 }
858
859 template <typename Ty> Ty *cloneInfo(const Ty &Old) {
860 assert(!MFInfo);
861 MFInfo = Ty::template create<Ty>(Allocator, Old);
862 return static_cast<Ty *>(MFInfo);
863 }
864
865 /// Initialize the target specific MachineFunctionInfo
866 void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI);
867
868 MachineFunctionInfo *cloneInfoFrom(
869 const MachineFunction &OrigMF,
870 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) {
871 assert(!MFInfo && "new function already has MachineFunctionInfo");
872 if (!OrigMF.MFInfo)
873 return nullptr;
874 return OrigMF.MFInfo->clone(Allocator, DestMF&: *this, Src2DstMBB);
875 }
876
877 /// Returns the denormal handling type for the default rounding mode of the
878 /// function.
879 DenormalMode getDenormalMode(const fltSemantics &FPType) const;
880
881 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
882 /// are inserted into the machine function. The block number for a machine
883 /// basic block can be found by using the MBB::getNumber method, this method
884 /// provides the inverse mapping.
885 MachineBasicBlock *getBlockNumbered(unsigned N) const {
886 assert(N < MBBNumbering.size() && "Illegal block number");
887 assert(MBBNumbering[N] && "Block was removed from the machine function!");
888 return MBBNumbering[N];
889 }
890
891 /// Should we be emitting segmented stack stuff for the function
892 bool shouldSplitStack() const;
893
894 /// getNumBlockIDs - Return the number of MBB ID's allocated.
895 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
896
897 /// Return the numbering "epoch" of block numbers, incremented after each
898 /// numbering. Intended for asserting that no renumbering was performed when
899 /// used by, e.g., preserved analyses.
900 unsigned getBlockNumberEpoch() const { return MBBNumberingEpoch; }
901
902 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
903 /// recomputes them. This guarantees that the MBB numbers are sequential,
904 /// dense, and match the ordering of the blocks within the function. If a
905 /// specific MachineBasicBlock is specified, only that block and those after
906 /// it are renumbered.
907 void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
908
909 /// Return an estimate of the function's code size,
910 /// taking into account block and function alignment
911 int64_t estimateFunctionSizeInBytes();
912
913 /// print - Print out the MachineFunction in a format suitable for debugging
914 /// to the specified stream.
915 void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
916
917 /// viewCFG - This function is meant for use from the debugger. You can just
918 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
919 /// program, displaying the CFG of the current function with the code for each
920 /// basic block inside. This depends on there being a 'dot' and 'gv' program
921 /// in your path.
922 void viewCFG() const;
923
924 /// viewCFGOnly - This function is meant for use from the debugger. It works
925 /// just like viewCFG, but it does not include the contents of basic blocks
926 /// into the nodes, just the label. If you are only interested in the CFG
927 /// this can make the graph smaller.
928 ///
929 void viewCFGOnly() const;
930
931 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
932 void dump() const;
933
934 /// Run the current MachineFunction through the machine code verifier, useful
935 /// for debugger use.
936 /// \returns true if no problems were found.
937 bool verify(Pass *p = nullptr, const char *Banner = nullptr,
938 raw_ostream *OS = nullptr, bool AbortOnError = true) const;
939
940 /// For New Pass Manager: Run the current MachineFunction through the machine
941 /// code verifier, useful for debugger use.
942 /// \returns true if no problems were found.
943 bool verify(MachineFunctionAnalysisManager &MFAM,
944 const char *Banner = nullptr, raw_ostream *OS = nullptr,
945 bool AbortOnError = true) const;
946
947 /// Run the current MachineFunction through the machine code verifier, useful
948 /// for debugger use.
949 /// TODO: Add the param for LiveStacks analysis.
950 /// \returns true if no problems were found.
951 bool verify(LiveIntervals *LiveInts, SlotIndexes *Indexes,
952 const char *Banner = nullptr, raw_ostream *OS = nullptr,
953 bool AbortOnError = true) const;
954
955 // Provide accessors for the MachineBasicBlock list...
956 using iterator = BasicBlockListType::iterator;
957 using const_iterator = BasicBlockListType::const_iterator;
958 using const_reverse_iterator = BasicBlockListType::const_reverse_iterator;
959 using reverse_iterator = BasicBlockListType::reverse_iterator;
960
961 /// Support for MachineBasicBlock::getNextNode().
962 static BasicBlockListType MachineFunction::*
963 getSublistAccess(MachineBasicBlock *) {
964 return &MachineFunction::BasicBlocks;
965 }
966
967 /// addLiveIn - Add the specified physical register as a live-in value and
968 /// create a corresponding virtual register for it.
969 Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC);
970
971 //===--------------------------------------------------------------------===//
972 // BasicBlock accessor functions.
973 //
974 iterator begin() { return BasicBlocks.begin(); }
975 const_iterator begin() const { return BasicBlocks.begin(); }
976 iterator end () { return BasicBlocks.end(); }
977 const_iterator end () const { return BasicBlocks.end(); }
978
979 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
980 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
981 reverse_iterator rend () { return BasicBlocks.rend(); }
982 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
983
984 unsigned size() const { return (unsigned)BasicBlocks.size();}
985 bool empty() const { return BasicBlocks.empty(); }
986 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
987 MachineBasicBlock &front() { return BasicBlocks.front(); }
988 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
989 MachineBasicBlock & back() { return BasicBlocks.back(); }
990
991 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (val: MBB); }
992 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(val: MBB); }
993 void insert(iterator MBBI, MachineBasicBlock *MBB) {
994 BasicBlocks.insert(where: MBBI, New: MBB);
995 }
996 void splice(iterator InsertPt, iterator MBBI) {
997 BasicBlocks.splice(where: InsertPt, L2&: BasicBlocks, first: MBBI);
998 }
999 void splice(iterator InsertPt, MachineBasicBlock *MBB) {
1000 BasicBlocks.splice(where: InsertPt, L2&: BasicBlocks, N: MBB);
1001 }
1002 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
1003 BasicBlocks.splice(where: InsertPt, L2&: BasicBlocks, first: MBBI, last: MBBE);
1004 }
1005
1006 void remove(iterator MBBI) { BasicBlocks.remove(IT&: MBBI); }
1007 void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(IT: MBBI); }
1008 void erase(iterator MBBI) { BasicBlocks.erase(where: MBBI); }
1009 void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(IT: MBBI); }
1010
1011 template <typename Comp>
1012 void sort(Comp comp) {
1013 BasicBlocks.sort(comp);
1014 }
1015
1016 /// Return the number of \p MachineInstrs in this \p MachineFunction.
1017 unsigned getInstructionCount() const {
1018 unsigned InstrCount = 0;
1019 for (const MachineBasicBlock &MBB : BasicBlocks)
1020 InstrCount += MBB.size();
1021 return InstrCount;
1022 }
1023
1024 //===--------------------------------------------------------------------===//
1025 // Internal functions used to automatically number MachineBasicBlocks
1026
1027 /// Adds the MBB to the internal numbering. Returns the unique number
1028 /// assigned to the MBB.
1029 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
1030 MBBNumbering.push_back(x: MBB);
1031 return (unsigned)MBBNumbering.size()-1;
1032 }
1033
1034 /// removeFromMBBNumbering - Remove the specific machine basic block from our
1035 /// tracker, this is only really to be used by the MachineBasicBlock
1036 /// implementation.
1037 void removeFromMBBNumbering(unsigned N) {
1038 assert(N < MBBNumbering.size() && "Illegal basic block #");
1039 MBBNumbering[N] = nullptr;
1040 }
1041
1042 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
1043 /// of `new MachineInstr'.
1044 MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, DebugLoc DL,
1045 bool NoImplicit = false);
1046
1047 /// Create a new MachineInstr which is a copy of \p Orig, identical in all
1048 /// ways except the instruction has no parent, prev, or next. Bundling flags
1049 /// are reset.
1050 ///
1051 /// Note: Clones a single instruction, not whole instruction bundles.
1052 /// Does not perform target specific adjustments; consider using
1053 /// TargetInstrInfo::duplicate() instead.
1054 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
1055
1056 /// Clones instruction or the whole instruction bundle \p Orig and insert
1057 /// into \p MBB before \p InsertBefore.
1058 ///
1059 /// Note: Does not perform target specific adjustments; consider using
1060 /// TargetInstrInfo::duplicate() instead.
1061 MachineInstr &
1062 cloneMachineInstrBundle(MachineBasicBlock &MBB,
1063 MachineBasicBlock::iterator InsertBefore,
1064 const MachineInstr &Orig);
1065
1066 /// DeleteMachineInstr - Delete the given MachineInstr.
1067 void deleteMachineInstr(MachineInstr *MI);
1068
1069 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
1070 /// instead of `new MachineBasicBlock'. Sets `MachineBasicBlock::BBID` if
1071 /// basic-block-sections is enabled for the function.
1072 MachineBasicBlock *
1073 CreateMachineBasicBlock(const BasicBlock *BB = nullptr,
1074 std::optional<UniqueBBID> BBID = std::nullopt);
1075
1076 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
1077 void deleteMachineBasicBlock(MachineBasicBlock *MBB);
1078
1079 /// getMachineMemOperand - Allocate a new MachineMemOperand.
1080 /// MachineMemOperands are owned by the MachineFunction and need not be
1081 /// explicitly deallocated.
1082 MachineMemOperand *getMachineMemOperand(
1083 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy,
1084 Align base_alignment, const AAMDNodes &AAInfo = AAMDNodes(),
1085 const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1086 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1087 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1088 MachineMemOperand *getMachineMemOperand(
1089 MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LocationSize Size,
1090 Align BaseAlignment, const AAMDNodes &AAInfo = AAMDNodes(),
1091 const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1092 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1093 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1094 MachineMemOperand *getMachineMemOperand(
1095 MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, uint64_t Size,
1096 Align BaseAlignment, const AAMDNodes &AAInfo = AAMDNodes(),
1097 const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1098 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1099 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic) {
1100 return getMachineMemOperand(PtrInfo, F, Size: LocationSize::precise(Value: Size),
1101 BaseAlignment, AAInfo, Ranges, SSID, Ordering,
1102 FailureOrdering);
1103 }
1104 MachineMemOperand *getMachineMemOperand(
1105 MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, TypeSize Size,
1106 Align BaseAlignment, const AAMDNodes &AAInfo = AAMDNodes(),
1107 const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1108 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1109 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic) {
1110 return getMachineMemOperand(PtrInfo, F, Size: LocationSize::precise(Value: Size),
1111 BaseAlignment, AAInfo, Ranges, SSID, Ordering,
1112 FailureOrdering);
1113 }
1114
1115 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1116 /// an existing one, adjusting by an offset and using the given size.
1117 /// MachineMemOperands are owned by the MachineFunction and need not be
1118 /// explicitly deallocated.
1119 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1120 int64_t Offset, LLT Ty);
1121 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1122 int64_t Offset, LocationSize Size) {
1123 return getMachineMemOperand(
1124 MMO, Offset,
1125 Ty: !Size.hasValue() ? LLT()
1126 : Size.isScalable()
1127 ? LLT::scalable_vector(MinNumElements: 1, ScalarSizeInBits: 8 * Size.getValue().getKnownMinValue())
1128 : LLT::scalar(SizeInBits: 8 * Size.getValue().getKnownMinValue()));
1129 }
1130 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1131 int64_t Offset, uint64_t Size) {
1132 return getMachineMemOperand(MMO, Offset, Size: LocationSize::precise(Value: Size));
1133 }
1134 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1135 int64_t Offset, TypeSize Size) {
1136 return getMachineMemOperand(MMO, Offset, Size: LocationSize::precise(Value: Size));
1137 }
1138
1139 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1140 /// an existing one, replacing only the MachinePointerInfo and size.
1141 /// MachineMemOperands are owned by the MachineFunction and need not be
1142 /// explicitly deallocated.
1143 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1144 const MachinePointerInfo &PtrInfo,
1145 LocationSize Size);
1146 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1147 const MachinePointerInfo &PtrInfo,
1148 LLT Ty);
1149 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1150 const MachinePointerInfo &PtrInfo,
1151 uint64_t Size) {
1152 return getMachineMemOperand(MMO, PtrInfo, Size: LocationSize::precise(Value: Size));
1153 }
1154 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1155 const MachinePointerInfo &PtrInfo,
1156 TypeSize Size) {
1157 return getMachineMemOperand(MMO, PtrInfo, Size: LocationSize::precise(Value: Size));
1158 }
1159
1160 /// Allocate a new MachineMemOperand by copying an existing one,
1161 /// replacing only AliasAnalysis information. MachineMemOperands are owned
1162 /// by the MachineFunction and need not be explicitly deallocated.
1163 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1164 const AAMDNodes &AAInfo);
1165
1166 /// Allocate a new MachineMemOperand by copying an existing one,
1167 /// replacing the flags. MachineMemOperands are owned
1168 /// by the MachineFunction and need not be explicitly deallocated.
1169 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1170 MachineMemOperand::Flags Flags);
1171
1172 using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity;
1173
1174 /// Allocate an array of MachineOperands. This is only intended for use by
1175 /// internal MachineInstr functions.
1176 MachineOperand *allocateOperandArray(OperandCapacity Cap) {
1177 return OperandRecycler.allocate(Cap, Allocator);
1178 }
1179
1180 /// Dellocate an array of MachineOperands and recycle the memory. This is
1181 /// only intended for use by internal MachineInstr functions.
1182 /// Cap must be the same capacity that was used to allocate the array.
1183 void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
1184 OperandRecycler.deallocate(Cap, Ptr: Array);
1185 }
1186
1187 /// Allocate and initialize a register mask with @p NumRegister bits.
1188 uint32_t *allocateRegMask();
1189
1190 ArrayRef<int> allocateShuffleMask(ArrayRef<int> Mask);
1191
1192 /// Allocate and construct an extra info structure for a `MachineInstr`.
1193 ///
1194 /// This is allocated on the function's allocator and so lives the life of
1195 /// the function.
1196 MachineInstr::ExtraInfo *createMIExtraInfo(
1197 ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol = nullptr,
1198 MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr,
1199 MDNode *PCSections = nullptr, uint32_t CFIType = 0,
1200 MDNode *MMRAs = nullptr);
1201
1202 /// Allocate a string and populate it with the given external symbol name.
1203 const char *createExternalSymbolName(StringRef Name);
1204
1205 //===--------------------------------------------------------------------===//
1206 // Label Manipulation.
1207
1208 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
1209 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
1210 /// normal 'L' label is returned.
1211 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
1212 bool isLinkerPrivate = false) const;
1213
1214 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
1215 /// base.
1216 MCSymbol *getPICBaseSymbol() const;
1217
1218 /// Returns a reference to a list of cfi instructions in the function's
1219 /// prologue. Used to construct frame maps for debug and exception handling
1220 /// comsumers.
1221 const std::vector<MCCFIInstruction> &getFrameInstructions() const {
1222 return FrameInstructions;
1223 }
1224
1225 [[nodiscard]] unsigned addFrameInst(const MCCFIInstruction &Inst);
1226
1227 /// Returns a reference to a list of symbols immediately following calls to
1228 /// _setjmp in the function. Used to construct the longjmp target table used
1229 /// by Windows Control Flow Guard.
1230 const std::vector<MCSymbol *> &getLongjmpTargets() const {
1231 return LongjmpTargets;
1232 }
1233
1234 /// Add the specified symbol to the list of valid longjmp targets for Windows
1235 /// Control Flow Guard.
1236 void addLongjmpTarget(MCSymbol *Target) { LongjmpTargets.push_back(x: Target); }
1237
1238 /// Returns a reference to a list of symbols that are targets for Windows
1239 /// EH Continuation Guard.
1240 const std::vector<MCSymbol *> &getEHContTargets() const {
1241 return EHContTargets;
1242 }
1243
1244 /// Add the specified symbol to the list of targets for Windows EH
1245 /// Continuation Guard.
1246 void addEHContTarget(MCSymbol *Target) { EHContTargets.push_back(x: Target); }
1247
1248 /// Tries to get the global and target flags for a call site, if the
1249 /// instruction is a call to a global.
1250 CalledGlobalInfo tryGetCalledGlobal(const MachineInstr *MI) const {
1251 return CalledGlobalsInfo.lookup(Val: MI);
1252 }
1253
1254 /// Notes the global and target flags for a call site.
1255 void addCalledGlobal(const MachineInstr *MI, CalledGlobalInfo Details) {
1256 assert(MI && "MI must not be null");
1257 assert(Details.Callee && "Global must not be null");
1258 CalledGlobalsInfo.insert(KV: {MI, Details});
1259 }
1260
1261 /// Iterates over the full set of call sites and their associated globals.
1262 auto getCalledGlobals() const {
1263 return llvm::make_range(x: CalledGlobalsInfo.begin(), y: CalledGlobalsInfo.end());
1264 }
1265
1266 /// \name Exception Handling
1267 /// \{
1268
1269 bool callsEHReturn() const { return CallsEHReturn; }
1270 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
1271
1272 bool callsUnwindInit() const { return CallsUnwindInit; }
1273 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
1274
1275 bool hasEHContTarget() const { return HasEHContTarget; }
1276 void setHasEHContTarget(bool V) { HasEHContTarget = V; }
1277
1278 bool hasEHScopes() const { return HasEHScopes; }
1279 void setHasEHScopes(bool V) { HasEHScopes = V; }
1280
1281 bool hasEHFunclets() const { return HasEHFunclets; }
1282 void setHasEHFunclets(bool V) { HasEHFunclets = V; }
1283
1284 bool hasFakeUses() const { return HasFakeUses; }
1285 void setHasFakeUses(bool V) { HasFakeUses = V; }
1286
1287 bool isOutlined() const { return IsOutlined; }
1288 void setIsOutlined(bool V) { IsOutlined = V; }
1289
1290 /// Find or create an LandingPadInfo for the specified MachineBasicBlock.
1291 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1292
1293 /// Return a reference to the landing pad info for the current function.
1294 const std::vector<LandingPadInfo> &getLandingPads() const {
1295 return LandingPads;
1296 }
1297
1298 /// Provide the begin and end labels of an invoke style call and associate it
1299 /// with a try landing pad block.
1300 void addInvoke(MachineBasicBlock *LandingPad,
1301 MCSymbol *BeginLabel, MCSymbol *EndLabel);
1302
1303 /// Add a new panding pad, and extract the exception handling information from
1304 /// the landingpad instruction. Returns the label ID for the landing pad
1305 /// entry.
1306 MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
1307
1308 /// Return the type id for the specified typeinfo. This is function wide.
1309 unsigned getTypeIDFor(const GlobalValue *TI);
1310
1311 /// Return the id of the filter encoded by TyIds. This is function wide.
1312 int getFilterIDFor(ArrayRef<unsigned> TyIds);
1313
1314 /// Map the landing pad's EH symbol to the call site indexes.
1315 void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
1316
1317 /// Return if there is any wasm exception handling.
1318 bool hasAnyWasmLandingPadIndex() const {
1319 return !WasmLPadToIndexMap.empty();
1320 }
1321
1322 /// Map the landing pad to its index. Used for Wasm exception handling.
1323 void setWasmLandingPadIndex(const MachineBasicBlock *LPad, unsigned Index) {
1324 WasmLPadToIndexMap[LPad] = Index;
1325 }
1326
1327 /// Returns true if the landing pad has an associate index in wasm EH.
1328 bool hasWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1329 return WasmLPadToIndexMap.count(Val: LPad);
1330 }
1331
1332 /// Get the index in wasm EH for a given landing pad.
1333 unsigned getWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1334 assert(hasWasmLandingPadIndex(LPad));
1335 return WasmLPadToIndexMap.lookup(Val: LPad);
1336 }
1337
1338 bool hasAnyCallSiteLandingPad() const {
1339 return !LPadToCallSiteMap.empty();
1340 }
1341
1342 /// Get the call site indexes for a landing pad EH symbol.
1343 SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
1344 assert(hasCallSiteLandingPad(Sym) &&
1345 "missing call site number for landing pad!");
1346 return LPadToCallSiteMap[Sym];
1347 }
1348
1349 /// Return true if the landing pad Eh symbol has an associated call site.
1350 bool hasCallSiteLandingPad(MCSymbol *Sym) {
1351 return !LPadToCallSiteMap[Sym].empty();
1352 }
1353
1354 bool hasAnyCallSiteLabel() const {
1355 return !CallSiteMap.empty();
1356 }
1357
1358 /// Map the begin label for a call site.
1359 void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
1360 CallSiteMap[BeginLabel] = Site;
1361 }
1362
1363 /// Get the call site number for a begin label.
1364 unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1365 assert(hasCallSiteBeginLabel(BeginLabel) &&
1366 "Missing call site number for EH_LABEL!");
1367 return CallSiteMap.lookup(Val: BeginLabel);
1368 }
1369
1370 /// Return true if the begin label has a call site number associated with it.
1371 bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1372 return CallSiteMap.count(Val: BeginLabel);
1373 }
1374
1375 /// Record annotations associated with a particular label.
1376 void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD) {
1377 CodeViewAnnotations.push_back(x: {Label, MD});
1378 }
1379
1380 ArrayRef<std::pair<MCSymbol *, MDNode *>> getCodeViewAnnotations() const {
1381 return CodeViewAnnotations;
1382 }
1383
1384 /// Return a reference to the C++ typeinfo for the current function.
1385 const std::vector<const GlobalValue *> &getTypeInfos() const {
1386 return TypeInfos;
1387 }
1388
1389 /// Return a reference to the typeids encoding filters used in the current
1390 /// function.
1391 const std::vector<unsigned> &getFilterIds() const {
1392 return FilterIds;
1393 }
1394
1395 /// \}
1396
1397 /// Collect information used to emit debugging information of a variable in a
1398 /// stack slot.
1399 void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1400 int Slot, const DILocation *Loc) {
1401 VariableDbgInfos.emplace_back(Args&: Var, Args&: Expr, Args&: Slot, Args&: Loc);
1402 }
1403
1404 /// Collect information used to emit debugging information of a variable in
1405 /// the entry value of a register.
1406 void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1407 MCRegister Reg, const DILocation *Loc) {
1408 VariableDbgInfos.emplace_back(Args&: Var, Args&: Expr, Args&: Reg, Args&: Loc);
1409 }
1410
1411 VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
1412 const VariableDbgInfoMapTy &getVariableDbgInfo() const {
1413 return VariableDbgInfos;
1414 }
1415
1416 /// Returns the collection of variables for which we have debug info and that
1417 /// have been assigned a stack slot.
1418 auto getInStackSlotVariableDbgInfo() {
1419 return make_filter_range(Range&: getVariableDbgInfo(), Pred: [](auto &VarInfo) {
1420 return VarInfo.inStackSlot();
1421 });
1422 }
1423
1424 /// Returns the collection of variables for which we have debug info and that
1425 /// have been assigned a stack slot.
1426 auto getInStackSlotVariableDbgInfo() const {
1427 return make_filter_range(Range: getVariableDbgInfo(), Pred: [](const auto &VarInfo) {
1428 return VarInfo.inStackSlot();
1429 });
1430 }
1431
1432 /// Returns the collection of variables for which we have debug info and that
1433 /// have been assigned an entry value register.
1434 auto getEntryValueVariableDbgInfo() const {
1435 return make_filter_range(Range: getVariableDbgInfo(), Pred: [](const auto &VarInfo) {
1436 return VarInfo.inEntryValueRegister();
1437 });
1438 }
1439
1440 /// Start tracking the arguments passed to the call \p CallI.
1441 void addCallSiteInfo(const MachineInstr *CallI, CallSiteInfo &&CallInfo) {
1442 assert(CallI->isCandidateForAdditionalCallInfo());
1443 bool Inserted =
1444 CallSitesInfo.try_emplace(Key: CallI, Args: std::move(CallInfo)).second;
1445 (void)Inserted;
1446 assert(Inserted && "Call site info not unique");
1447 }
1448
1449 const CallSiteInfoMap &getCallSitesInfo() const {
1450 return CallSitesInfo;
1451 }
1452
1453 /// Following functions update call site info. They should be called before
1454 /// removing, replacing or copying call instruction.
1455
1456 /// Erase the call site info for \p MI. It is used to remove a call
1457 /// instruction from the instruction stream.
1458 void eraseAdditionalCallInfo(const MachineInstr *MI);
1459 /// Copy the call site info from \p Old to \ New. Its usage is when we are
1460 /// making a copy of the instruction that will be inserted at different point
1461 /// of the instruction stream.
1462 void copyAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New);
1463
1464 /// Move the call site info from \p Old to \New call site info. This function
1465 /// is used when we are replacing one call instruction with another one to
1466 /// the same callee.
1467 void moveAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New);
1468
1469 unsigned getNewDebugInstrNum() {
1470 return ++DebugInstrNumberingCount;
1471 }
1472};
1473
1474//===--------------------------------------------------------------------===//
1475// GraphTraits specializations for function basic block graphs (CFGs)
1476//===--------------------------------------------------------------------===//
1477
1478// Provide specializations of GraphTraits to be able to treat a
1479// machine function as a graph of machine basic blocks... these are
1480// the same as the machine basic block iterators, except that the root
1481// node is implicitly the first node of the function.
1482//
1483template <> struct GraphTraits<MachineFunction*> :
1484 public GraphTraits<MachineBasicBlock*> {
1485 static NodeRef getEntryNode(MachineFunction *F) { return &F->front(); }
1486
1487 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1488 using nodes_iterator = pointer_iterator<MachineFunction::iterator>;
1489
1490 static nodes_iterator nodes_begin(MachineFunction *F) {
1491 return nodes_iterator(F->begin());
1492 }
1493
1494 static nodes_iterator nodes_end(MachineFunction *F) {
1495 return nodes_iterator(F->end());
1496 }
1497
1498 static unsigned size (MachineFunction *F) { return F->size(); }
1499
1500 static unsigned getMaxNumber(MachineFunction *F) {
1501 return F->getNumBlockIDs();
1502 }
1503 static unsigned getNumberEpoch(MachineFunction *F) {
1504 return F->getBlockNumberEpoch();
1505 }
1506};
1507template <> struct GraphTraits<const MachineFunction*> :
1508 public GraphTraits<const MachineBasicBlock*> {
1509 static NodeRef getEntryNode(const MachineFunction *F) { return &F->front(); }
1510
1511 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1512 using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
1513
1514 static nodes_iterator nodes_begin(const MachineFunction *F) {
1515 return nodes_iterator(F->begin());
1516 }
1517
1518 static nodes_iterator nodes_end (const MachineFunction *F) {
1519 return nodes_iterator(F->end());
1520 }
1521
1522 static unsigned size (const MachineFunction *F) {
1523 return F->size();
1524 }
1525
1526 static unsigned getMaxNumber(const MachineFunction *F) {
1527 return F->getNumBlockIDs();
1528 }
1529 static unsigned getNumberEpoch(const MachineFunction *F) {
1530 return F->getBlockNumberEpoch();
1531 }
1532};
1533
1534// Provide specializations of GraphTraits to be able to treat a function as a
1535// graph of basic blocks... and to walk it in inverse order. Inverse order for
1536// a function is considered to be when traversing the predecessor edges of a BB
1537// instead of the successor edges.
1538//
1539template <> struct GraphTraits<Inverse<MachineFunction*>> :
1540 public GraphTraits<Inverse<MachineBasicBlock*>> {
1541 static NodeRef getEntryNode(Inverse<MachineFunction *> G) {
1542 return &G.Graph->front();
1543 }
1544
1545 static unsigned getMaxNumber(MachineFunction *F) {
1546 return F->getNumBlockIDs();
1547 }
1548 static unsigned getNumberEpoch(MachineFunction *F) {
1549 return F->getBlockNumberEpoch();
1550 }
1551};
1552template <> struct GraphTraits<Inverse<const MachineFunction*>> :
1553 public GraphTraits<Inverse<const MachineBasicBlock*>> {
1554 static NodeRef getEntryNode(Inverse<const MachineFunction *> G) {
1555 return &G.Graph->front();
1556 }
1557
1558 static unsigned getMaxNumber(const MachineFunction *F) {
1559 return F->getNumBlockIDs();
1560 }
1561 static unsigned getNumberEpoch(const MachineFunction *F) {
1562 return F->getBlockNumberEpoch();
1563 }
1564};
1565
1566LLVM_ABI void verifyMachineFunction(const std::string &Banner,
1567 const MachineFunction &MF);
1568
1569} // end namespace llvm
1570
1571#endif // LLVM_CODEGEN_MACHINEFUNCTION_H
1572

source code of llvm/include/llvm/CodeGen/MachineFunction.h