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