Revert "RegisterPressure: allocatable physreg uses are always kills"
[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 #ifndef NDEBUG
118   SmallPtrSet<const Instruction *, 8> CatchInfoLost;
119   SmallPtrSet<const Instruction *, 8> CatchInfoFound;
120 #endif
121
122   struct LiveOutInfo {
123     unsigned NumSignBits : 31;
124     bool IsValid : 1;
125     APInt KnownOne, KnownZero;
126     LiveOutInfo() : NumSignBits(0), IsValid(true), KnownOne(1, 0),
127                     KnownZero(1, 0) {}
128   };
129
130   /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
131   /// for a value.
132   DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
133
134   /// VisitedBBs - The set of basic blocks visited thus far by instruction
135   /// selection.
136   SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
137
138   /// PHINodesToUpdate - A list of phi instructions whose operand list will
139   /// be updated after processing the current basic block.
140   /// TODO: This isn't per-function state, it's per-basic-block state. But
141   /// there's no other convenient place for it to live right now.
142   std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
143   unsigned OrigNumPHINodesToUpdate;
144
145   /// If the current MBB is a landing pad, the exception pointer and exception
146   /// selector registers are copied into these virtual registers by
147   /// SelectionDAGISel::PrepareEHLandingPad().
148   unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
149
150   /// set - Initialize this FunctionLoweringInfo with the given Function
151   /// and its associated MachineFunction.
152   ///
153   void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
154
155   /// clear - Clear out all the function-specific state. This returns this
156   /// FunctionLoweringInfo to an empty state, ready to be used for a
157   /// different function.
158   void clear();
159
160   /// isExportedInst - Return true if the specified value is an instruction
161   /// exported from its block.
162   bool isExportedInst(const Value *V) {
163     return ValueMap.count(V);
164   }
165
166   unsigned CreateReg(MVT VT);
167
168   unsigned CreateRegs(Type *Ty);
169
170   unsigned InitializeRegForValue(const Value *V) {
171     // Tokens never live in vregs.
172     if (V->getType()->isTokenTy())
173       return 0;
174     unsigned &R = ValueMap[V];
175     assert(R == 0 && "Already initialized this value register!");
176     return R = CreateRegs(V->getType());
177   }
178
179   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
180   /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
181   const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg) {
182     if (!LiveOutRegInfo.inBounds(Reg))
183       return nullptr;
184
185     const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
186     if (!LOI->IsValid)
187       return nullptr;
188
189     return LOI;
190   }
191
192   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
193   /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
194   /// the register's LiveOutInfo is for a smaller bit width, it is extended to
195   /// the larger bit width by zero extension. The bit width must be no smaller
196   /// than the LiveOutInfo's existing bit width.
197   const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth);
198
199   /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
200   void AddLiveOutRegInfo(unsigned Reg, unsigned NumSignBits,
201                          const APInt &KnownZero, const APInt &KnownOne) {
202     // Only install this information if it tells us something.
203     if (NumSignBits == 1 && KnownZero == 0 && KnownOne == 0)
204       return;
205
206     LiveOutRegInfo.grow(Reg);
207     LiveOutInfo &LOI = LiveOutRegInfo[Reg];
208     LOI.NumSignBits = NumSignBits;
209     LOI.KnownOne = KnownOne;
210     LOI.KnownZero = KnownZero;
211   }
212
213   /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
214   /// register based on the LiveOutInfo of its operands.
215   void ComputePHILiveOutRegInfo(const PHINode*);
216
217   /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
218   /// called when a block is visited before all of its predecessors.
219   void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
220     // PHIs with no uses have no ValueMap entry.
221     DenseMap<const Value*, unsigned>::const_iterator It = ValueMap.find(PN);
222     if (It == ValueMap.end())
223       return;
224
225     unsigned Reg = It->second;
226     if (Reg == 0)
227       return;
228
229     LiveOutRegInfo.grow(Reg);
230     LiveOutRegInfo[Reg].IsValid = false;
231   }
232
233   /// setArgumentFrameIndex - Record frame index for the byval
234   /// argument.
235   void setArgumentFrameIndex(const Argument *A, int FI);
236
237   /// getArgumentFrameIndex - Get frame index for the byval argument.
238   int getArgumentFrameIndex(const Argument *A);
239
240   unsigned getCatchPadExceptionPointerVReg(const Value *CPI,
241                                            const TargetRegisterClass *RC);
242
243 private:
244   void addSEHHandlersForLPads(ArrayRef<const LandingPadInst *> LPads);
245
246   /// LiveOutRegInfo - Information about live out vregs.
247   IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
248 };
249
250 /// ComputeUsesVAFloatArgument - Determine if any floating-point values are
251 /// being passed to this variadic function, and set the MachineModuleInfo's
252 /// usesVAFloatArgument flag if so. This flag is used to emit an undefined
253 /// reference to _fltused on Windows, which will link in MSVCRT's
254 /// floating-point support.
255 void ComputeUsesVAFloatArgument(const CallInst &I, MachineModuleInfo *MMI);
256
257 /// AddLandingPadInfo - Extract the exception handling information from the
258 /// landingpad instruction and add them to the specified machine module info.
259 void AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
260                        MachineBasicBlock *MBB);
261
262 } // end namespace llvm
263
264 #endif