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