LLVM  10.0.0
ReachingDefAnalysis.h
Go to the documentation of this file.
1 //==--- llvm/CodeGen/ReachingDefAnalysis.h - Reaching Def Analysis -*- 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 /// \file Reaching Defs Analysis pass.
10 ///
11 /// This pass tracks for each instruction what is the "closest" reaching def of
12 /// a given register. It is used by BreakFalseDeps (for clearance calculation)
13 /// and ExecutionDomainFix (for arbitrating conflicting domains).
14 ///
15 /// Note that this is different from the usual definition notion of liveness.
16 /// The CPU doesn't care whether or not we consider a register killed.
17 ///
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_CODEGEN_REACHINGDEFSANALYSIS_H
22 #define LLVM_CODEGEN_REACHINGDEFSANALYSIS_H
23 
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/InitializePasses.h"
29 
30 namespace llvm {
31 
32 class MachineBasicBlock;
33 class MachineInstr;
34 
35 /// This class provides the reaching def analysis.
37 private:
38  MachineFunction *MF;
39  const TargetRegisterInfo *TRI;
40  unsigned NumRegUnits;
41  /// Instruction that defined each register, relative to the beginning of the
42  /// current basic block. When a LiveRegsDefInfo is used to represent a
43  /// live-out register, this value is relative to the end of the basic block,
44  /// so it will be a negative number.
45  using LiveRegsDefInfo = std::vector<int>;
46  LiveRegsDefInfo LiveRegs;
47 
48  /// Keeps clearance information for all registers. Note that this
49  /// is different from the usual definition notion of liveness. The CPU
50  /// doesn't care whether or not we consider a register killed.
52  OutRegsInfoMap MBBOutRegsInfos;
53 
54  /// Current instruction number.
55  /// The first instruction in each basic block is 0.
56  int CurInstr;
57 
58  /// Maps instructions to their instruction Ids, relative to the begining of
59  /// their basic blocks.
61 
62  /// All reaching defs of a given RegUnit for a given MBB.
64  /// All reaching defs of all reg units for a given MBB
65  using MBBDefsInfo = std::vector<MBBRegUnitDefs>;
66  /// All reaching defs of all reg units for a all MBBs
68  MBBReachingDefsInfo MBBReachingDefs;
69 
70  /// Default values are 'nothing happened a long time ago'.
71  const int ReachingDefDefaultVal = -(1 << 20);
72 
73 public:
74  static char ID; // Pass identification, replacement for typeid
75 
78  }
79  void releaseMemory() override;
80 
81  void getAnalysisUsage(AnalysisUsage &AU) const override {
82  AU.setPreservesAll();
84  }
85 
86  bool runOnMachineFunction(MachineFunction &MF) override;
87 
92  }
93 
94  /// Provides the instruction id of the closest reaching def instruction of
95  /// PhysReg that reaches MI, relative to the begining of MI's basic block.
96  int getReachingDef(MachineInstr *MI, int PhysReg);
97 
98  /// Provides the instruction of the closest reaching def instruction of
99  /// PhysReg that reaches MI, relative to the begining of MI's basic block.
100  MachineInstr *getReachingMIDef(MachineInstr *MI, int PhysReg);
101 
102  /// Provides the MI, from the given block, corresponding to the Id or a
103  /// nullptr if the id does not refer to the block.
104  MachineInstr *getInstFromId(MachineBasicBlock *MBB, int InstId);
105 
106  /// Return whether A and B use the same def of PhysReg.
107  bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, int PhysReg);
108 
109  /// Return whether the reaching def for MI also is live out of its parent
110  /// block.
111  bool isReachingDefLiveOut(MachineInstr *MI, int PhysReg);
112 
113  /// Return the local MI that produces the live out value for PhysReg, or
114  /// nullptr for a non-live out or non-local def.
116  int PhysReg);
117 
118  /// Return whether the given register is used after MI, whether it's a local
119  /// use or a live out.
120  bool isRegUsedAfter(MachineInstr *MI, int PhysReg);
121 
122  /// Provides the first instruction before MI that uses PhysReg
124 
125  /// Provides all instructions before MI that uses PhysReg
126  void getAllInstWithUseBefore(MachineInstr *MI, int PhysReg,
128 
129  /// Provides the clearance - the number of instructions since the closest
130  /// reaching def instuction of PhysReg that reaches MI.
131  int getClearance(MachineInstr *MI, MCPhysReg PhysReg);
132 
133  /// Provides the uses, in the same block as MI, of register that MI defines.
134  /// This does not consider live-outs.
135  void getReachingLocalUses(MachineInstr *MI, int PhysReg,
137 
138  /// Provide the number of uses, in the same block as MI, of the register that
139  /// MI defines.
140  unsigned getNumUses(MachineInstr *MI, int PhysReg);
141 
142 private:
143  /// Set up LiveRegs by merging predecessor live-out values.
144  void enterBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
145 
146  /// Update live-out values.
147  void leaveBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
148 
149  /// Process he given basic block.
150  void processBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
151 
152  /// Update def-ages for registers defined by MI.
153  /// Also break dependencies on partial defs and undef uses.
154  void processDefs(MachineInstr *);
155 };
156 
157 } // namespace llvm
158 
159 #endif // LLVM_CODEGEN_REACHINGDEFSANALYSIS_H
bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, int PhysReg)
Return whether A and B use the same def of PhysReg.
void initializeReachingDefAnalysisPass(PassRegistry &)
void getAllInstWithUseBefore(MachineInstr *MI, int PhysReg, SmallVectorImpl< MachineInstr *> &Uses)
Provides all instructions before MI that uses PhysReg.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
This class provides the reaching def analysis.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
unsigned getNumUses(MachineInstr *MI, int PhysReg)
Provide the number of uses, in the same block as MI, of the register that MI defines.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineInstr * getInstFromId(MachineBasicBlock *MBB, int InstId)
Provides the MI, from the given block, corresponding to the Id or a nullptr if the id does not refer ...
Represent the analysis usage information of a pass.
MachineInstr * getReachingMIDef(MachineInstr *MI, int PhysReg)
Provides the instruction of the closest reaching def instruction of PhysReg that reaches MI...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void getReachingLocalUses(MachineInstr *MI, int PhysReg, SmallVectorImpl< MachineInstr *> &Uses)
Provides the uses, in the same block as MI, of register that MI defines.
bool isReachingDefLiveOut(MachineInstr *MI, int PhysReg)
Return whether the reaching def for MI also is live out of its parent block.
bool isRegUsedAfter(MachineInstr *MI, int PhysReg)
Return whether the given register is used after MI, whether it&#39;s a local use or a live out...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineInstr * getInstWithUseBefore(MachineInstr *MI, int PhysReg)
Provides the first instruction before MI that uses PhysReg.
void setPreservesAll()
Set by analyses that do not transform their input at all.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:63
int getClearance(MachineInstr *MI, MCPhysReg PhysReg)
Provides the clearance - the number of instructions since the closest reaching def instuction of Phys...
int getReachingDef(MachineInstr *MI, int PhysReg)
Provides the instruction id of the closest reaching def instruction of PhysReg that reaches MI...
MachineFunctionProperties getRequiredProperties() const override
MachineInstr * getLocalLiveOutMIDef(MachineBasicBlock *MBB, int PhysReg)
Return the local MI that produces the live out value for PhysReg, or nullptr for a non-live out or no...
IRTranslator LLVM IR MI
Properties which a MachineFunction may have at a given point in time.