blob: ee3bc0abbcffba47012226280a67821b12432387 [file] [log] [blame]
Dan Gohman6277eb22009-11-23 17:16:22 +00001//===-- FunctionLoweringInfo.cpp ------------------------------------------===//
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#define DEBUG_TYPE "function-lowering-info"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000016#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Dan Gohman5fca8b12009-11-23 18:12:11 +000020#include "llvm/IntrinsicInst.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000021#include "llvm/LLVMContext.h"
22#include "llvm/Module.h"
Dan Gohman9c3d5e42010-07-16 17:54:27 +000023#include "llvm/Analysis/DebugInfo.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000024#include "llvm/CodeGen/Analysis.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/MachineModuleInfo.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000030#include "llvm/Target/TargetRegisterInfo.h"
31#include "llvm/Target/TargetData.h"
32#include "llvm/Target/TargetFrameInfo.h"
33#include "llvm/Target/TargetInstrInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000034#include "llvm/Target/TargetLowering.h"
35#include "llvm/Target/TargetOptions.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/MathExtras.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000039#include <algorithm>
40using namespace llvm;
41
Dan Gohman6277eb22009-11-23 17:16:22 +000042/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
43/// PHI nodes or outside of the basic block that defines it, or used by a
44/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000045static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohmand84e8062010-04-20 14:50:13 +000046 if (I->use_empty()) return false;
Dan Gohman6277eb22009-11-23 17:16:22 +000047 if (isa<PHINode>(I)) return true;
Dan Gohmanae541aa2010-04-15 04:33:49 +000048 const BasicBlock *BB = I->getParent();
49 for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end();
Gabor Greif03f09a32010-07-09 16:08:33 +000050 UI != E; ++UI) {
51 const User *U = *UI;
52 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000053 return true;
Gabor Greif03f09a32010-07-09 16:08:33 +000054 }
Dan Gohman6277eb22009-11-23 17:16:22 +000055 return false;
56}
57
58/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
59/// entry block, return true. This includes arguments used by switches, since
60/// the switch may expand into multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000061static bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) {
Dan Gohman6277eb22009-11-23 17:16:22 +000062 // With FastISel active, we may be splitting blocks, so force creation
63 // of virtual registers for all non-dead arguments.
Dan Gohmand725f042010-05-01 02:44:23 +000064 if (EnableFastISel)
Dan Gohman6277eb22009-11-23 17:16:22 +000065 return A->use_empty();
66
Dan Gohmanae541aa2010-04-15 04:33:49 +000067 const BasicBlock *Entry = A->getParent()->begin();
68 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
Gabor Greif03f09a32010-07-09 16:08:33 +000069 UI != E; ++UI) {
70 const User *U = *UI;
71 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000072 return false; // Use not in entry block.
Gabor Greif03f09a32010-07-09 16:08:33 +000073 }
Dan Gohman6277eb22009-11-23 17:16:22 +000074 return true;
75}
76
Dan Gohmand858e902010-04-17 15:26:15 +000077FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli)
Dan Gohman6277eb22009-11-23 17:16:22 +000078 : TLI(tli) {
79}
80
Dan Gohman7451d3e2010-05-29 17:03:36 +000081void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Dan Gohman6277eb22009-11-23 17:16:22 +000082 Fn = &fn;
83 MF = &mf;
84 RegInfo = &MF->getRegInfo();
85
Dan Gohman84023e02010-07-10 09:00:22 +000086 // Check whether the function can return without sret-demotion.
87 SmallVector<ISD::OutputArg, 4> Outs;
88 GetReturnInfo(Fn->getReturnType(),
89 Fn->getAttributes().getRetAttributes(), Outs, TLI);
90 CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), Fn->isVarArg(),
91 Outs, Fn->getContext());
92
Dan Gohman6277eb22009-11-23 17:16:22 +000093 // Create a vreg for each argument register that is not dead and is used
94 // outside of the entry block for the function.
Dan Gohmanae541aa2010-04-15 04:33:49 +000095 for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
Dan Gohman6277eb22009-11-23 17:16:22 +000096 AI != E; ++AI)
97 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
98 InitializeRegForValue(AI);
99
100 // Initialize the mapping of values to registers. This is only set up for
101 // instruction values that are used outside of the block that defines
102 // them.
Dan Gohmanae541aa2010-04-15 04:33:49 +0000103 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
104 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
105 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I))
106 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000107 const Type *Ty = AI->getAllocatedType();
108 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
109 unsigned Align =
110 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
111 AI->getAlignment());
112
113 TySize *= CUI->getZExtValue(); // Get total allocated size.
114 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
115 StaticAllocaMap[AI] =
116 MF->getFrameInfo()->CreateStackObject(TySize, Align, false);
117 }
118
119 for (; BB != EB; ++BB)
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000120 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
121 // Mark values used outside their block as exported, by allocating
122 // a virtual register for them.
Dan Gohmand84e8062010-04-20 14:50:13 +0000123 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000124 if (!isa<AllocaInst>(I) ||
125 !StaticAllocaMap.count(cast<AllocaInst>(I)))
126 InitializeRegForValue(I);
127
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000128 // Collect llvm.dbg.declare information. This is done now instead of
129 // during the initial isel pass through the IR so that it is done
130 // in a predictable order.
131 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
132 MachineModuleInfo &MMI = MF->getMMI();
133 if (MMI.hasDebugInfo() &&
134 DIVariable(DI->getVariable()).Verify() &&
135 !DI->getDebugLoc().isUnknown()) {
136 // Don't handle byval struct arguments or VLAs, for example.
137 // Non-byval arguments are handled here (they refer to the stack
138 // temporary alloca at this point).
139 const Value *Address = DI->getAddress();
140 if (Address) {
141 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
142 Address = BCI->getOperand(0);
143 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
144 DenseMap<const AllocaInst *, int>::iterator SI =
145 StaticAllocaMap.find(AI);
146 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
147 int FI = SI->second;
148 MMI.setVariableDbgInfo(DI->getVariable(),
149 FI, DI->getDebugLoc());
150 }
151 }
152 }
153 }
154 }
155 }
156
Dan Gohman6277eb22009-11-23 17:16:22 +0000157 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
158 // also creates the initial PHI MachineInstrs, though none of the input
159 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000160 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000161 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
162 MBBMap[BB] = MBB;
163 MF->push_back(MBB);
164
165 // Transfer the address-taken flag. This is necessary because there could
166 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
167 // the first one should be marked.
168 if (BB->hasAddressTaken())
169 MBB->setHasAddressTaken();
170
171 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
172 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000173 for (BasicBlock::const_iterator I = BB->begin();
174 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
175 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000176
Dan Gohmanc025c852010-04-20 14:48:02 +0000177 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000178 unsigned PHIReg = ValueMap[PN];
179 assert(PHIReg && "PHI node does not have an assigned virtual register!");
180
181 SmallVector<EVT, 4> ValueVTs;
182 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
183 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
184 EVT VT = ValueVTs[vti];
185 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
186 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
187 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000188 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000189 PHIReg += NumRegisters;
190 }
191 }
192 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000193
194 // Mark landing pad blocks.
195 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000196 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000197 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000198}
199
200/// clear - Clear out all the function-specific state. This returns this
201/// FunctionLoweringInfo to an empty state, ready to be used for a
202/// different function.
203void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000204 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
205 "Not all catch info was assigned to a landing pad!");
206
Dan Gohman6277eb22009-11-23 17:16:22 +0000207 MBBMap.clear();
208 ValueMap.clear();
209 StaticAllocaMap.clear();
210#ifndef NDEBUG
211 CatchInfoLost.clear();
212 CatchInfoFound.clear();
213#endif
214 LiveOutRegInfo.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000215 ArgDbgValues.clear();
Dan Gohman84023e02010-07-10 09:00:22 +0000216 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000217}
218
Dan Gohman89496d02010-07-02 00:10:16 +0000219/// CreateReg - Allocate a single virtual register for the given type.
220unsigned FunctionLoweringInfo::CreateReg(EVT VT) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000221 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
222}
223
Dan Gohman89496d02010-07-02 00:10:16 +0000224/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000225/// the correctly promoted or expanded types. Assign these registers
226/// consecutive vreg numbers and return the first assigned number.
227///
228/// In the case that the given value has struct or array type, this function
229/// will assign registers for each member or element.
230///
Dan Gohman89496d02010-07-02 00:10:16 +0000231unsigned FunctionLoweringInfo::CreateRegs(const Type *Ty) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000232 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000233 ComputeValueVTs(TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000234
235 unsigned FirstReg = 0;
236 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
237 EVT ValueVT = ValueVTs[Value];
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000238 EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000239
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000240 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000241 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000242 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000243 if (!FirstReg) FirstReg = R;
244 }
245 }
246 return FirstReg;
247}
Dan Gohman66336ed2009-11-23 17:42:46 +0000248
Dan Gohman66336ed2009-11-23 17:42:46 +0000249/// AddCatchInfo - Extract the personality and type infos from an eh.selector
250/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000251void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000252 MachineBasicBlock *MBB) {
253 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000254 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000255 assert(CE->getOpcode() == Instruction::BitCast &&
256 isa<Function>(CE->getOperand(0)) &&
257 "Personality should be a function");
258 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
259
260 // Gather all the type infos for this landing pad and pass them along to
261 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000262 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000263 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000264
Gabor Greife767e6b2010-06-30 13:45:50 +0000265 for (unsigned i = N - 1; i > 1; --i) {
266 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000267 unsigned FilterLength = CI->getZExtValue();
268 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000269 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000270
271 if (FirstCatch < N) {
272 TyInfo.reserve(N - FirstCatch);
273 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000274 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000275 MMI->addCatchTypeInfo(MBB, TyInfo);
276 TyInfo.clear();
277 }
278
279 if (!FilterLength) {
280 // Cleanup.
281 MMI->addCleanup(MBB);
282 } else {
283 // Filter.
284 TyInfo.reserve(FilterLength - 1);
285 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000286 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000287 MMI->addFilterTypeInfo(MBB, TyInfo);
288 TyInfo.clear();
289 }
290
291 N = i;
292 }
293 }
294
Gabor Greife767e6b2010-06-30 13:45:50 +0000295 if (N > 2) {
296 TyInfo.reserve(N - 2);
297 for (unsigned j = 2; j < N; ++j)
298 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000299 MMI->addCatchTypeInfo(MBB, TyInfo);
300 }
301}
302
Dan Gohman25208642010-04-14 19:53:31 +0000303void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB,
Dan Gohman5fca8b12009-11-23 18:12:11 +0000304 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Dan Gohman25208642010-04-14 19:53:31 +0000305 for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end();
306 I != E; ++I)
307 if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
Dan Gohman5fca8b12009-11-23 18:12:11 +0000308 // Apply the catch info to DestBB.
309 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
310#ifndef NDEBUG
311 if (!FLI.MBBMap[SrcBB]->isLandingPad())
312 FLI.CatchInfoFound.insert(EHSel);
313#endif
314 }
315}