]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/CodeGen/FunctionLoweringInfo.h
Add 'contrib/pnglite/' from commit 'a70c2a23d0d84dfc63a1d9413a7f4aaede7313aa'
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / CodeGen / FunctionLoweringInfo.h
1 //===- FunctionLoweringInfo.h - Lower functions from LLVM IR ---*- 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 // This implements routines for translating functions from LLVM IR into
10 // Machine IR.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
15 #define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
16
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/IndexedMap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/CodeGen/ISDOpcodes.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/TargetRegisterInfo.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/IR/Value.h"
29 #include "llvm/Support/KnownBits.h"
30 #include <cassert>
31 #include <utility>
32 #include <vector>
33
34 namespace llvm {
35
36 class Argument;
37 class BasicBlock;
38 class BranchProbabilityInfo;
39 class LegacyDivergenceAnalysis;
40 class Function;
41 class Instruction;
42 class MachineFunction;
43 class MachineInstr;
44 class MachineRegisterInfo;
45 class MVT;
46 class SelectionDAG;
47 class TargetLowering;
48
49 //===--------------------------------------------------------------------===//
50 /// FunctionLoweringInfo - This contains information that is global to a
51 /// function that is used when lowering a region of the function.
52 ///
53 class FunctionLoweringInfo {
54 public:
55   const Function *Fn;
56   MachineFunction *MF;
57   const TargetLowering *TLI;
58   MachineRegisterInfo *RegInfo;
59   BranchProbabilityInfo *BPI;
60   const LegacyDivergenceAnalysis *DA;
61   /// CanLowerReturn - true iff the function's return value can be lowered to
62   /// registers.
63   bool CanLowerReturn;
64
65   /// True if part of the CSRs will be handled via explicit copies.
66   bool SplitCSR;
67
68   /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
69   /// allocated to hold a pointer to the hidden sret parameter.
70   Register DemoteRegister;
71
72   /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
73   DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
74
75   /// ValueMap - Since we emit code for the function a basic block at a time,
76   /// we must remember which virtual registers hold the values for
77   /// cross-basic-block values.
78   DenseMap<const Value *, Register> ValueMap;
79
80   /// VirtReg2Value map is needed by the Divergence Analysis driven
81   /// instruction selection. It is reverted ValueMap. It is computed
82   /// in lazy style - on demand. It is used to get the Value corresponding
83   /// to the live in virtual register and is called from the
84   /// TargetLowerinInfo::isSDNodeSourceOfDivergence.
85   DenseMap<Register, const Value*> VirtReg2Value;
86
87   /// This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence
88   /// to get the Value corresponding to the live-in virtual register.
89   const Value *getValueFromVirtualReg(Register Vreg);
90
91   /// Track virtual registers created for exception pointers.
92   DenseMap<const Value *, Register> CatchPadExceptionPointers;
93
94   /// Keep track of frame indices allocated for statepoints as they could be
95   /// used across basic block boundaries (e.g. for an invoke).  For each
96   /// gc.statepoint instruction, maps uniqued llvm IR values to the slots they
97   /// were spilled in.  If a value is mapped to None it means we visited the
98   /// value but didn't spill it (because it was a constant, for instance).
99   using StatepointSpillMapTy = DenseMap<const Value *, Optional<int>>;
100   DenseMap<const Instruction *, StatepointSpillMapTy> StatepointSpillMaps;
101
102   /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
103   /// the entry block.  This allows the allocas to be efficiently referenced
104   /// anywhere in the function.
105   DenseMap<const AllocaInst*, int> StaticAllocaMap;
106
107   /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
108   DenseMap<const Argument*, int> ByValArgFrameIndexMap;
109
110   /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
111   /// function arguments that are inserted after scheduling is completed.
112   SmallVector<MachineInstr*, 8> ArgDbgValues;
113
114   /// Bitvector with a bit set if corresponding argument is described in
115   /// ArgDbgValues. Using arg numbers according to Argument numbering.
116   BitVector DescribedArgs;
117
118   /// RegFixups - Registers which need to be replaced after isel is done.
119   DenseMap<Register, Register> RegFixups;
120
121   DenseSet<Register> RegsWithFixups;
122
123   /// StatepointStackSlots - A list of temporary stack slots (frame indices)
124   /// used to spill values at a statepoint.  We store them here to enable
125   /// reuse of the same stack slots across different statepoints in different
126   /// basic blocks.
127   SmallVector<unsigned, 50> StatepointStackSlots;
128
129   /// MBB - The current block.
130   MachineBasicBlock *MBB;
131
132   /// MBB - The current insert position inside the current block.
133   MachineBasicBlock::iterator InsertPt;
134
135   struct LiveOutInfo {
136     unsigned NumSignBits : 31;
137     unsigned IsValid : 1;
138     KnownBits Known = 1;
139
140     LiveOutInfo() : NumSignBits(0), IsValid(true) {}
141   };
142
143   /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
144   /// for a value.
145   DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
146
147   /// VisitedBBs - The set of basic blocks visited thus far by instruction
148   /// selection.
149   SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
150
151   /// PHINodesToUpdate - A list of phi instructions whose operand list will
152   /// be updated after processing the current basic block.
153   /// TODO: This isn't per-function state, it's per-basic-block state. But
154   /// there's no other convenient place for it to live right now.
155   std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
156   unsigned OrigNumPHINodesToUpdate;
157
158   /// If the current MBB is a landing pad, the exception pointer and exception
159   /// selector registers are copied into these virtual registers by
160   /// SelectionDAGISel::PrepareEHLandingPad().
161   unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
162
163   /// set - Initialize this FunctionLoweringInfo with the given Function
164   /// and its associated MachineFunction.
165   ///
166   void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
167
168   /// clear - Clear out all the function-specific state. This returns this
169   /// FunctionLoweringInfo to an empty state, ready to be used for a
170   /// different function.
171   void clear();
172
173   /// isExportedInst - Return true if the specified value is an instruction
174   /// exported from its block.
175   bool isExportedInst(const Value *V) const {
176     return ValueMap.count(V);
177   }
178
179   Register CreateReg(MVT VT, bool isDivergent = false);
180
181   Register CreateRegs(const Value *V);
182
183   Register CreateRegs(Type *Ty, bool isDivergent = false);
184
185   Register InitializeRegForValue(const Value *V) {
186     // Tokens never live in vregs.
187     if (V->getType()->isTokenTy())
188       return 0;
189     Register &R = ValueMap[V];
190     assert(R == 0 && "Already initialized this value register!");
191     assert(VirtReg2Value.empty());
192     return R = CreateRegs(V);
193   }
194
195   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
196   /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
197   const LiveOutInfo *GetLiveOutRegInfo(Register Reg) {
198     if (!LiveOutRegInfo.inBounds(Reg))
199       return nullptr;
200
201     const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
202     if (!LOI->IsValid)
203       return nullptr;
204
205     return LOI;
206   }
207
208   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
209   /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
210   /// the register's LiveOutInfo is for a smaller bit width, it is extended to
211   /// the larger bit width by zero extension. The bit width must be no smaller
212   /// than the LiveOutInfo's existing bit width.
213   const LiveOutInfo *GetLiveOutRegInfo(Register Reg, unsigned BitWidth);
214
215   /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
216   void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits,
217                          const KnownBits &Known) {
218     // Only install this information if it tells us something.
219     if (NumSignBits == 1 && Known.isUnknown())
220       return;
221
222     LiveOutRegInfo.grow(Reg);
223     LiveOutInfo &LOI = LiveOutRegInfo[Reg];
224     LOI.NumSignBits = NumSignBits;
225     LOI.Known.One = Known.One;
226     LOI.Known.Zero = Known.Zero;
227   }
228
229   /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
230   /// register based on the LiveOutInfo of its operands.
231   void ComputePHILiveOutRegInfo(const PHINode*);
232
233   /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
234   /// called when a block is visited before all of its predecessors.
235   void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
236     // PHIs with no uses have no ValueMap entry.
237     DenseMap<const Value*, Register>::const_iterator It = ValueMap.find(PN);
238     if (It == ValueMap.end())
239       return;
240
241     Register Reg = It->second;
242     if (Reg == 0)
243       return;
244
245     LiveOutRegInfo.grow(Reg);
246     LiveOutRegInfo[Reg].IsValid = false;
247   }
248
249   /// setArgumentFrameIndex - Record frame index for the byval
250   /// argument.
251   void setArgumentFrameIndex(const Argument *A, int FI);
252
253   /// getArgumentFrameIndex - Get frame index for the byval argument.
254   int getArgumentFrameIndex(const Argument *A);
255
256   Register getCatchPadExceptionPointerVReg(const Value *CPI,
257                                            const TargetRegisterClass *RC);
258
259 private:
260   /// LiveOutRegInfo - Information about live out vregs.
261   IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
262 };
263
264 } // end namespace llvm
265
266 #endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H