1 //===-- FunctionLoweringInfo.h - Lower functions from LLVM IR to CodeGen --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements routines for translating functions from LLVM IR into
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
16 #define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
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"
34 class BranchProbabilityInfo;
40 class MachineBasicBlock;
41 class MachineFunction;
42 class MachineModuleInfo;
43 class MachineRegisterInfo;
47 //===--------------------------------------------------------------------===//
48 /// FunctionLoweringInfo - This contains information that is global to a
49 /// function that is used when lowering a region of the function.
51 class FunctionLoweringInfo {
52 const TargetMachine &TM;
53 const TargetLowering *TLI;
57 MachineRegisterInfo *RegInfo;
58 BranchProbabilityInfo *BPI;
59 /// CanLowerReturn - true iff the function's return value can be lowered to
63 /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
64 /// allocated to hold a pointer to the hidden sret parameter.
65 unsigned DemoteRegister;
67 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
68 DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
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;
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;
80 /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
81 DenseMap<const Argument*, int> ByValArgFrameIndexMap;
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;
87 /// RegFixups - Registers which need to be replaced after isel is done.
88 DenseMap<unsigned, unsigned> RegFixups;
90 /// MBB - The current block.
91 MachineBasicBlock *MBB;
93 /// MBB - The current insert position inside the current block.
94 MachineBasicBlock::iterator InsertPt;
97 SmallPtrSet<const Instruction *, 8> CatchInfoLost;
98 SmallPtrSet<const Instruction *, 8> CatchInfoFound;
102 unsigned NumSignBits : 31;
104 APInt KnownOne, KnownZero;
105 LiveOutInfo() : NumSignBits(0), IsValid(true), KnownOne(1, 0),
109 /// VisitedBBs - The set of basic blocks visited thus far by instruction
111 SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
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;
119 explicit FunctionLoweringInfo(const TargetMachine &TM);
121 /// set - Initialize this FunctionLoweringInfo with the given Function
122 /// and its associated MachineFunction.
124 void set(const Function &Fn, MachineFunction &MF);
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.
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);
137 unsigned CreateReg(MVT VT);
139 unsigned CreateRegs(Type *Ty);
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());
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))
153 const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
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);
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)
174 LiveOutRegInfo.grow(Reg);
175 LiveOutInfo &LOI = LiveOutRegInfo[Reg];
176 LOI.NumSignBits = NumSignBits;
177 LOI.KnownOne = KnownOne;
178 LOI.KnownZero = KnownZero;
181 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
182 /// register based on the LiveOutInfo of its operands.
183 void ComputePHILiveOutRegInfo(const PHINode*);
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())
193 unsigned Reg = It->second;
194 LiveOutRegInfo.grow(Reg);
195 LiveOutRegInfo[Reg].IsValid = false;
198 /// setArgumentFrameIndex - Record frame index for the byval
200 void setArgumentFrameIndex(const Argument *A, int FI);
202 /// getArgumentFrameIndex - Get frame index for the byval argument.
203 int getArgumentFrameIndex(const Argument *A);
206 /// LiveOutRegInfo - Information about live out vregs.
207 IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
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);
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);
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);
227 } // end namespace llvm