Refactor the LiveOutInfo interface into a few methods on FunctionLoweringInfo
[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/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/ADT/SmallVector.h"
24 #ifndef NDEBUG
25 #include "llvm/ADT/SmallSet.h"
26 #endif
27 #include "llvm/CodeGen/ValueTypes.h"
28 #include "llvm/CodeGen/ISDOpcodes.h"
29 #include "llvm/CodeGen/MachineBasicBlock.h"
30 #include "llvm/Support/CallSite.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include <vector>
33
34 namespace llvm {
35
36 class AllocaInst;
37 class BasicBlock;
38 class CallInst;
39 class Function;
40 class GlobalVariable;
41 class Instruction;
42 class MachineInstr;
43 class MachineBasicBlock;
44 class MachineFunction;
45 class MachineModuleInfo;
46 class MachineRegisterInfo;
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 TargetLowering &TLI;
57   const Function *Fn;
58   MachineFunction *MF;
59   MachineRegisterInfo *RegInfo;
60
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   /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
78   /// the entry block.  This allows the allocas to be efficiently referenced
79   /// anywhere in the function.
80   DenseMap<const AllocaInst*, int> StaticAllocaMap;
81
82   /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
83   DenseMap<const Argument*, int> ByValArgFrameIndexMap;
84
85   /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
86   /// function arguments that are inserted after scheduling is completed.
87   SmallVector<MachineInstr*, 8> ArgDbgValues;
88
89   /// RegFixups - Registers which need to be replaced after isel is done.
90   DenseMap<unsigned, unsigned> RegFixups;
91
92   /// MBB - The current block.
93   MachineBasicBlock *MBB;
94
95   /// MBB - The current insert position inside the current block.
96   MachineBasicBlock::iterator InsertPt;
97
98 #ifndef NDEBUG
99   SmallSet<const Instruction *, 8> CatchInfoLost;
100   SmallSet<const Instruction *, 8> CatchInfoFound;
101 #endif
102
103   struct LiveOutInfo {
104     unsigned NumSignBits;
105     APInt KnownOne, KnownZero;
106     LiveOutInfo() : NumSignBits(0), KnownOne(1, 0), KnownZero(1, 0) {}
107   };
108
109   /// PHINodesToUpdate - A list of phi instructions whose operand list will
110   /// be updated after processing the current basic block.
111   /// TODO: This isn't per-function state, it's per-basic-block state. But
112   /// there's no other convenient place for it to live right now.
113   std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
114
115   explicit FunctionLoweringInfo(const TargetLowering &TLI);
116
117   /// set - Initialize this FunctionLoweringInfo with the given Function
118   /// and its associated MachineFunction.
119   ///
120   void set(const Function &Fn, MachineFunction &MF);
121
122   /// clear - Clear out all the function-specific state. This returns this
123   /// FunctionLoweringInfo to an empty state, ready to be used for a
124   /// different function.
125   void clear();
126
127   /// isExportedInst - Return true if the specified value is an instruction
128   /// exported from its block.
129   bool isExportedInst(const Value *V) {
130     return ValueMap.count(V);
131   }
132
133   unsigned CreateReg(EVT VT);
134   
135   unsigned CreateRegs(const Type *Ty);
136   
137   unsigned InitializeRegForValue(const Value *V) {
138     unsigned &R = ValueMap[V];
139     assert(R == 0 && "Already initialized this value register!");
140     return R = CreateRegs(V->getType());
141   }
142
143   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
144   /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
145   const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg) {
146     if (!LiveOutRegInfo.inBounds(Reg))
147       return NULL;
148     return &LiveOutRegInfo[Reg];
149   }
150
151   /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
152   void AddLiveOutRegInfo(unsigned Reg, unsigned NumSignBits,
153                          const APInt &KnownZero, const APInt &KnownOne) {
154     // Only install this information if it tells us something.
155     if (NumSignBits == 1 && KnownZero == 0 && KnownOne == 0)
156       return;
157
158     LiveOutRegInfo.grow(Reg);
159     LiveOutInfo &LOI = LiveOutRegInfo[Reg];
160     LOI.NumSignBits = NumSignBits;
161     LOI.KnownOne = KnownOne;
162     LOI.KnownZero = KnownZero;
163   }
164
165   /// setByValArgumentFrameIndex - Record frame index for the byval
166   /// argument.
167   void setByValArgumentFrameIndex(const Argument *A, int FI);
168   
169   /// getByValArgumentFrameIndex - Get frame index for the byval argument.
170   int getByValArgumentFrameIndex(const Argument *A);
171
172 private:
173   /// LiveOutRegInfo - Information about live out vregs.
174   IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
175 };
176
177 /// AddCatchInfo - Extract the personality and type infos from an eh.selector
178 /// call, and add them to the specified machine basic block.
179 void AddCatchInfo(const CallInst &I,
180                   MachineModuleInfo *MMI, MachineBasicBlock *MBB);
181
182 /// CopyCatchInfo - Copy catch information from DestBB to SrcBB.
183 void CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB,
184                    MachineModuleInfo *MMI, FunctionLoweringInfo &FLI);
185
186 } // end namespace llvm
187
188 #endif