blob: 928e1ecd4cf4e39e0b73d39b2a2bbd5aeeaf96c9 [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 Gohman5eb6d652010-04-21 01:22:34 +000023#include "llvm/CodeGen/Analysis.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000029#include "llvm/Target/TargetRegisterInfo.h"
30#include "llvm/Target/TargetData.h"
31#include "llvm/Target/TargetFrameInfo.h"
32#include "llvm/Target/TargetInstrInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000033#include "llvm/Target/TargetLowering.h"
34#include "llvm/Target/TargetOptions.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/MathExtras.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000038#include <algorithm>
39using namespace llvm;
40
Dan Gohman6277eb22009-11-23 17:16:22 +000041/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
42/// PHI nodes or outside of the basic block that defines it, or used by a
43/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000044static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohmand84e8062010-04-20 14:50:13 +000045 if (I->use_empty()) return false;
Dan Gohman6277eb22009-11-23 17:16:22 +000046 if (isa<PHINode>(I)) return true;
Dan Gohmanae541aa2010-04-15 04:33:49 +000047 const BasicBlock *BB = I->getParent();
48 for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end();
Gabor Greif03f09a32010-07-09 16:08:33 +000049 UI != E; ++UI) {
50 const User *U = *UI;
51 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000052 return true;
Gabor Greif03f09a32010-07-09 16:08:33 +000053 }
Dan Gohman6277eb22009-11-23 17:16:22 +000054 return false;
55}
56
57/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
58/// entry block, return true. This includes arguments used by switches, since
59/// the switch may expand into multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000060static bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) {
Dan Gohman6277eb22009-11-23 17:16:22 +000061 // With FastISel active, we may be splitting blocks, so force creation
62 // of virtual registers for all non-dead arguments.
Dan Gohmand725f042010-05-01 02:44:23 +000063 if (EnableFastISel)
Dan Gohman6277eb22009-11-23 17:16:22 +000064 return A->use_empty();
65
Dan Gohmanae541aa2010-04-15 04:33:49 +000066 const BasicBlock *Entry = A->getParent()->begin();
67 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
Gabor Greif03f09a32010-07-09 16:08:33 +000068 UI != E; ++UI) {
69 const User *U = *UI;
70 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000071 return false; // Use not in entry block.
Gabor Greif03f09a32010-07-09 16:08:33 +000072 }
Dan Gohman6277eb22009-11-23 17:16:22 +000073 return true;
74}
75
Dan Gohmand858e902010-04-17 15:26:15 +000076FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli)
Dan Gohman6277eb22009-11-23 17:16:22 +000077 : TLI(tli) {
78}
79
Dan Gohman7451d3e2010-05-29 17:03:36 +000080void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Dan Gohman6277eb22009-11-23 17:16:22 +000081 Fn = &fn;
82 MF = &mf;
83 RegInfo = &MF->getRegInfo();
84
Dan Gohmanbf87e242010-07-09 00:39:23 +000085 // Check whether the function can return without sret-demotion.
86 SmallVector<ISD::OutputArg, 4> Outs;
87 GetReturnInfo(Fn->getReturnType(),
88 Fn->getAttributes().getRetAttributes(), Outs, TLI);
89 CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), Fn->isVarArg(),
90 Outs, Fn->getContext());
91
Dan Gohman6277eb22009-11-23 17:16:22 +000092 // Create a vreg for each argument register that is not dead and is used
93 // outside of the entry block for the function.
Dan Gohmanae541aa2010-04-15 04:33:49 +000094 for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
Dan Gohman6277eb22009-11-23 17:16:22 +000095 AI != E; ++AI)
96 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
97 InitializeRegForValue(AI);
98
99 // Initialize the mapping of values to registers. This is only set up for
100 // instruction values that are used outside of the block that defines
101 // them.
Dan Gohmanae541aa2010-04-15 04:33:49 +0000102 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
103 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
104 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I))
105 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000106 const Type *Ty = AI->getAllocatedType();
107 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
108 unsigned Align =
109 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
110 AI->getAlignment());
111
112 TySize *= CUI->getZExtValue(); // Get total allocated size.
113 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
114 StaticAllocaMap[AI] =
115 MF->getFrameInfo()->CreateStackObject(TySize, Align, false);
116 }
117
118 for (; BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000119 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Dan Gohmand84e8062010-04-20 14:50:13 +0000120 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000121 if (!isa<AllocaInst>(I) ||
122 !StaticAllocaMap.count(cast<AllocaInst>(I)))
123 InitializeRegForValue(I);
124
125 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
126 // also creates the initial PHI MachineInstrs, though none of the input
127 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000128 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000129 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
130 MBBMap[BB] = MBB;
131 MF->push_back(MBB);
132
133 // Transfer the address-taken flag. This is necessary because there could
134 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
135 // the first one should be marked.
136 if (BB->hasAddressTaken())
137 MBB->setHasAddressTaken();
138
139 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
140 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000141 for (BasicBlock::const_iterator I = BB->begin();
142 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
143 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000144
Dan Gohmanc025c852010-04-20 14:48:02 +0000145 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000146 unsigned PHIReg = ValueMap[PN];
147 assert(PHIReg && "PHI node does not have an assigned virtual register!");
148
149 SmallVector<EVT, 4> ValueVTs;
150 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
151 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
152 EVT VT = ValueVTs[vti];
153 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
154 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
155 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000156 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000157 PHIReg += NumRegisters;
158 }
159 }
160 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000161
162 // Mark landing pad blocks.
163 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000164 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000165 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000166}
167
168/// clear - Clear out all the function-specific state. This returns this
169/// FunctionLoweringInfo to an empty state, ready to be used for a
170/// different function.
171void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000172 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
173 "Not all catch info was assigned to a landing pad!");
174
Dan Gohman6277eb22009-11-23 17:16:22 +0000175 MBBMap.clear();
176 ValueMap.clear();
177 StaticAllocaMap.clear();
178#ifndef NDEBUG
179 CatchInfoLost.clear();
180 CatchInfoFound.clear();
181#endif
182 LiveOutRegInfo.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000183 ArgDbgValues.clear();
Dan Gohmanbf87e242010-07-09 00:39:23 +0000184 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000185}
186
Dan Gohman89496d02010-07-02 00:10:16 +0000187/// CreateReg - Allocate a single virtual register for the given type.
188unsigned FunctionLoweringInfo::CreateReg(EVT VT) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000189 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
190}
191
Dan Gohman89496d02010-07-02 00:10:16 +0000192/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000193/// the correctly promoted or expanded types. Assign these registers
194/// consecutive vreg numbers and return the first assigned number.
195///
196/// In the case that the given value has struct or array type, this function
197/// will assign registers for each member or element.
198///
Dan Gohman89496d02010-07-02 00:10:16 +0000199unsigned FunctionLoweringInfo::CreateRegs(const Type *Ty) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000200 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000201 ComputeValueVTs(TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000202
203 unsigned FirstReg = 0;
204 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
205 EVT ValueVT = ValueVTs[Value];
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000206 EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000207
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000208 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000209 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000210 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000211 if (!FirstReg) FirstReg = R;
212 }
213 }
214 return FirstReg;
215}
Dan Gohman66336ed2009-11-23 17:42:46 +0000216
Dan Gohman66336ed2009-11-23 17:42:46 +0000217/// AddCatchInfo - Extract the personality and type infos from an eh.selector
218/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000219void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000220 MachineBasicBlock *MBB) {
221 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000222 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000223 assert(CE->getOpcode() == Instruction::BitCast &&
224 isa<Function>(CE->getOperand(0)) &&
225 "Personality should be a function");
226 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
227
228 // Gather all the type infos for this landing pad and pass them along to
229 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000230 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000231 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000232
Gabor Greife767e6b2010-06-30 13:45:50 +0000233 for (unsigned i = N - 1; i > 1; --i) {
234 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000235 unsigned FilterLength = CI->getZExtValue();
236 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000237 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000238
239 if (FirstCatch < N) {
240 TyInfo.reserve(N - FirstCatch);
241 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000242 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000243 MMI->addCatchTypeInfo(MBB, TyInfo);
244 TyInfo.clear();
245 }
246
247 if (!FilterLength) {
248 // Cleanup.
249 MMI->addCleanup(MBB);
250 } else {
251 // Filter.
252 TyInfo.reserve(FilterLength - 1);
253 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000254 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000255 MMI->addFilterTypeInfo(MBB, TyInfo);
256 TyInfo.clear();
257 }
258
259 N = i;
260 }
261 }
262
Gabor Greife767e6b2010-06-30 13:45:50 +0000263 if (N > 2) {
264 TyInfo.reserve(N - 2);
265 for (unsigned j = 2; j < N; ++j)
266 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000267 MMI->addCatchTypeInfo(MBB, TyInfo);
268 }
269}
270
Dan Gohman25208642010-04-14 19:53:31 +0000271void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB,
Dan Gohman5fca8b12009-11-23 18:12:11 +0000272 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Dan Gohman25208642010-04-14 19:53:31 +0000273 for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end();
274 I != E; ++I)
275 if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
Dan Gohman5fca8b12009-11-23 18:12:11 +0000276 // Apply the catch info to DestBB.
277 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
278#ifndef NDEBUG
279 if (!FLI.MBBMap[SrcBB]->isLandingPad())
280 FLI.CatchInfoFound.insert(EHSel);
281#endif
282 }
283}