blob: 40391dd1e36457a01fdee71d43b8e22f33fc89f3 [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"
16#include "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"
Eric Christopher551754c2010-04-16 23:37:20 +000033#include "llvm/Target/TargetIntrinsicInfo.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();
50 UI != E; ++UI)
Dan Gohman6277eb22009-11-23 17:16:22 +000051 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
52 return true;
53 return false;
54}
55
56/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
57/// entry block, return true. This includes arguments used by switches, since
58/// the switch may expand into multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000059static bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) {
Dan Gohman6277eb22009-11-23 17:16:22 +000060 // With FastISel active, we may be splitting blocks, so force creation
61 // of virtual registers for all non-dead arguments.
Dan Gohmand725f042010-05-01 02:44:23 +000062 if (EnableFastISel)
Dan Gohman6277eb22009-11-23 17:16:22 +000063 return A->use_empty();
64
Dan Gohmanae541aa2010-04-15 04:33:49 +000065 const BasicBlock *Entry = A->getParent()->begin();
66 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
67 UI != E; ++UI)
Dan Gohman6277eb22009-11-23 17:16:22 +000068 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
69 return false; // Use not in entry block.
70 return true;
71}
72
Dan Gohmand858e902010-04-17 15:26:15 +000073FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli)
Dan Gohman6277eb22009-11-23 17:16:22 +000074 : TLI(tli) {
75}
76
Dan Gohman7451d3e2010-05-29 17:03:36 +000077void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Dan Gohman6277eb22009-11-23 17:16:22 +000078 Fn = &fn;
79 MF = &mf;
80 RegInfo = &MF->getRegInfo();
81
82 // Create a vreg for each argument register that is not dead and is used
83 // outside of the entry block for the function.
Dan Gohmanae541aa2010-04-15 04:33:49 +000084 for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
Dan Gohman6277eb22009-11-23 17:16:22 +000085 AI != E; ++AI)
86 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
87 InitializeRegForValue(AI);
88
89 // Initialize the mapping of values to registers. This is only set up for
90 // instruction values that are used outside of the block that defines
91 // them.
Dan Gohmanae541aa2010-04-15 04:33:49 +000092 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
93 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
94 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I))
95 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Dan Gohman6277eb22009-11-23 17:16:22 +000096 const Type *Ty = AI->getAllocatedType();
97 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
98 unsigned Align =
99 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
100 AI->getAlignment());
101
102 TySize *= CUI->getZExtValue(); // Get total allocated size.
103 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
104 StaticAllocaMap[AI] =
105 MF->getFrameInfo()->CreateStackObject(TySize, Align, false);
106 }
107
108 for (; BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000109 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Dan Gohmand84e8062010-04-20 14:50:13 +0000110 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000111 if (!isa<AllocaInst>(I) ||
112 !StaticAllocaMap.count(cast<AllocaInst>(I)))
113 InitializeRegForValue(I);
114
115 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
116 // also creates the initial PHI MachineInstrs, though none of the input
117 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000118 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000119 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
120 MBBMap[BB] = MBB;
121 MF->push_back(MBB);
122
123 // Transfer the address-taken flag. This is necessary because there could
124 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
125 // the first one should be marked.
126 if (BB->hasAddressTaken())
127 MBB->setHasAddressTaken();
128
129 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
130 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000131 for (BasicBlock::const_iterator I = BB->begin();
132 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
133 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000134
Dan Gohmanc025c852010-04-20 14:48:02 +0000135 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000136 unsigned PHIReg = ValueMap[PN];
137 assert(PHIReg && "PHI node does not have an assigned virtual register!");
138
139 SmallVector<EVT, 4> ValueVTs;
140 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
141 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
142 EVT VT = ValueVTs[vti];
143 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
144 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
145 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000146 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000147 PHIReg += NumRegisters;
148 }
149 }
150 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000151
152 // Mark landing pad blocks.
153 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000154 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000155 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000156}
157
158/// clear - Clear out all the function-specific state. This returns this
159/// FunctionLoweringInfo to an empty state, ready to be used for a
160/// different function.
161void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000162 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
163 "Not all catch info was assigned to a landing pad!");
164
Dan Gohman6277eb22009-11-23 17:16:22 +0000165 MBBMap.clear();
166 ValueMap.clear();
167 StaticAllocaMap.clear();
168#ifndef NDEBUG
169 CatchInfoLost.clear();
170 CatchInfoFound.clear();
171#endif
172 LiveOutRegInfo.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000173 ArgDbgValues.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000174}
175
176unsigned FunctionLoweringInfo::MakeReg(EVT VT) {
177 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
178}
179
180/// CreateRegForValue - Allocate the appropriate number of virtual registers of
181/// the correctly promoted or expanded types. Assign these registers
182/// consecutive vreg numbers and return the first assigned number.
183///
184/// In the case that the given value has struct or array type, this function
185/// will assign registers for each member or element.
186///
187unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
188 SmallVector<EVT, 4> ValueVTs;
189 ComputeValueVTs(TLI, V->getType(), ValueVTs);
190
191 unsigned FirstReg = 0;
192 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
193 EVT ValueVT = ValueVTs[Value];
194 EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT);
195
196 unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT);
197 for (unsigned i = 0; i != NumRegs; ++i) {
198 unsigned R = MakeReg(RegisterVT);
199 if (!FirstReg) FirstReg = R;
200 }
201 }
202 return FirstReg;
203}
Dan Gohman66336ed2009-11-23 17:42:46 +0000204
Dan Gohman66336ed2009-11-23 17:42:46 +0000205/// AddCatchInfo - Extract the personality and type infos from an eh.selector
206/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000207void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000208 MachineBasicBlock *MBB) {
209 // Inform the MachineModuleInfo of the personality for this landing pad.
Eric Christopher551754c2010-04-16 23:37:20 +0000210 const ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
Dan Gohman66336ed2009-11-23 17:42:46 +0000211 assert(CE->getOpcode() == Instruction::BitCast &&
212 isa<Function>(CE->getOperand(0)) &&
213 "Personality should be a function");
214 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
215
216 // Gather all the type infos for this landing pad and pass them along to
217 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000218 std::vector<const GlobalVariable *> TyInfo;
Eric Christopher551754c2010-04-16 23:37:20 +0000219 unsigned N = I.getNumOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000220
Eric Christopher551754c2010-04-16 23:37:20 +0000221 for (unsigned i = N - 1; i > 2; --i) {
Dan Gohman25208642010-04-14 19:53:31 +0000222 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000223 unsigned FilterLength = CI->getZExtValue();
224 unsigned FirstCatch = i + FilterLength + !FilterLength;
225 assert (FirstCatch <= N && "Invalid filter length");
226
227 if (FirstCatch < N) {
228 TyInfo.reserve(N - FirstCatch);
229 for (unsigned j = FirstCatch; j < N; ++j)
230 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
231 MMI->addCatchTypeInfo(MBB, TyInfo);
232 TyInfo.clear();
233 }
234
235 if (!FilterLength) {
236 // Cleanup.
237 MMI->addCleanup(MBB);
238 } else {
239 // Filter.
240 TyInfo.reserve(FilterLength - 1);
241 for (unsigned j = i + 1; j < FirstCatch; ++j)
242 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
243 MMI->addFilterTypeInfo(MBB, TyInfo);
244 TyInfo.clear();
245 }
246
247 N = i;
248 }
249 }
250
Eric Christopher551754c2010-04-16 23:37:20 +0000251 if (N > 3) {
252 TyInfo.reserve(N - 3);
253 for (unsigned j = 3; j < N; ++j)
Dan Gohman66336ed2009-11-23 17:42:46 +0000254 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
255 MMI->addCatchTypeInfo(MBB, TyInfo);
256 }
257}
258
Dan Gohman25208642010-04-14 19:53:31 +0000259void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB,
Dan Gohman5fca8b12009-11-23 18:12:11 +0000260 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Dan Gohman25208642010-04-14 19:53:31 +0000261 for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end();
262 I != E; ++I)
263 if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
Dan Gohman5fca8b12009-11-23 18:12:11 +0000264 // Apply the catch info to DestBB.
265 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
266#ifndef NDEBUG
267 if (!FLI.MBBMap[SrcBB]->isLandingPad())
268 FLI.CatchInfoFound.insert(EHSel);
269#endif
270 }
271}