blob: d518b5d346a1e0729d4be8de1c6f5f420a45edb9 [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"
Dan Gohman6277eb22009-11-23 17:16:22 +000032#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
Dan Gohmand858e902010-04-17 15:26:15 +000057FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli)
Dan Gohman6277eb22009-11-23 17:16:22 +000058 : TLI(tli) {
59}
60
Dan Gohman7451d3e2010-05-29 17:03:36 +000061void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Dan Gohman6277eb22009-11-23 17:16:22 +000062 Fn = &fn;
63 MF = &mf;
64 RegInfo = &MF->getRegInfo();
65
Dan Gohman84023e02010-07-10 09:00:22 +000066 // Check whether the function can return without sret-demotion.
67 SmallVector<ISD::OutputArg, 4> Outs;
68 GetReturnInfo(Fn->getReturnType(),
69 Fn->getAttributes().getRetAttributes(), Outs, TLI);
Eric Christopher471e4222011-06-08 23:55:35 +000070 CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), *MF,
71 Fn->isVarArg(),
Dan Gohman84023e02010-07-10 09:00:22 +000072 Outs, Fn->getContext());
73
Dan Gohman6277eb22009-11-23 17:16:22 +000074 // Initialize the mapping of values to registers. This is only set up for
75 // instruction values that are used outside of the block that defines
76 // them.
Dan Gohmanae541aa2010-04-15 04:33:49 +000077 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
79 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I))
80 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Dan Gohman6277eb22009-11-23 17:16:22 +000081 const Type *Ty = AI->getAllocatedType();
82 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
83 unsigned Align =
84 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
85 AI->getAlignment());
86
87 TySize *= CUI->getZExtValue(); // Get total allocated size.
88 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Bill Wendlingdfc2c512010-07-27 01:55:19 +000089
90 // The object may need to be placed onto the stack near the stack
91 // protector if one exists. Determine here if this object is a suitable
92 // candidate. I.e., it would trigger the creation of a stack protector.
93 bool MayNeedSP =
94 (AI->isArrayAllocation() ||
95 (TySize > 8 && isa<ArrayType>(Ty) &&
96 cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8)));
Dan Gohman6277eb22009-11-23 17:16:22 +000097 StaticAllocaMap[AI] =
Bill Wendlingdfc2c512010-07-27 01:55:19 +000098 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, MayNeedSP);
Dan Gohman6277eb22009-11-23 17:16:22 +000099 }
100
101 for (; BB != EB; ++BB)
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000102 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
103 // Mark values used outside their block as exported, by allocating
104 // a virtual register for them.
Cameron Zwarich4ecc82e2011-02-22 03:24:52 +0000105 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000106 if (!isa<AllocaInst>(I) ||
107 !StaticAllocaMap.count(cast<AllocaInst>(I)))
108 InitializeRegForValue(I);
109
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000110 // Collect llvm.dbg.declare information. This is done now instead of
111 // during the initial isel pass through the IR so that it is done
112 // in a predictable order.
113 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
114 MachineModuleInfo &MMI = MF->getMMI();
115 if (MMI.hasDebugInfo() &&
116 DIVariable(DI->getVariable()).Verify() &&
117 !DI->getDebugLoc().isUnknown()) {
118 // Don't handle byval struct arguments or VLAs, for example.
119 // Non-byval arguments are handled here (they refer to the stack
120 // temporary alloca at this point).
121 const Value *Address = DI->getAddress();
122 if (Address) {
123 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
124 Address = BCI->getOperand(0);
125 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
126 DenseMap<const AllocaInst *, int>::iterator SI =
127 StaticAllocaMap.find(AI);
128 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
129 int FI = SI->second;
130 MMI.setVariableDbgInfo(DI->getVariable(),
131 FI, DI->getDebugLoc());
132 }
133 }
134 }
135 }
136 }
137 }
138
Dan Gohman6277eb22009-11-23 17:16:22 +0000139 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
140 // also creates the initial PHI MachineInstrs, though none of the input
141 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000142 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000143 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
144 MBBMap[BB] = MBB;
145 MF->push_back(MBB);
146
147 // Transfer the address-taken flag. This is necessary because there could
148 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
149 // the first one should be marked.
150 if (BB->hasAddressTaken())
151 MBB->setHasAddressTaken();
152
153 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
154 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000155 for (BasicBlock::const_iterator I = BB->begin();
156 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
157 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000158
Rafael Espindola3fa82832011-05-13 15:18:06 +0000159 // Skip empty types
160 if (PN->getType()->isEmptyTy())
161 continue;
162
Dan Gohmanc025c852010-04-20 14:48:02 +0000163 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000164 unsigned PHIReg = ValueMap[PN];
165 assert(PHIReg && "PHI node does not have an assigned virtual register!");
166
167 SmallVector<EVT, 4> ValueVTs;
168 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
169 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
170 EVT VT = ValueVTs[vti];
171 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
172 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
173 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000174 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000175 PHIReg += NumRegisters;
176 }
177 }
178 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000179
180 // Mark landing pad blocks.
181 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000182 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000183 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000184}
185
186/// clear - Clear out all the function-specific state. This returns this
187/// FunctionLoweringInfo to an empty state, ready to be used for a
188/// different function.
189void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000190 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
191 "Not all catch info was assigned to a landing pad!");
192
Dan Gohman6277eb22009-11-23 17:16:22 +0000193 MBBMap.clear();
194 ValueMap.clear();
195 StaticAllocaMap.clear();
196#ifndef NDEBUG
197 CatchInfoLost.clear();
198 CatchInfoFound.clear();
199#endif
200 LiveOutRegInfo.clear();
Cameron Zwaricha46cd972011-02-24 10:00:13 +0000201 VisitedBBs.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000202 ArgDbgValues.clear();
Devang Patel0b48ead2010-08-31 22:22:42 +0000203 ByValArgFrameIndexMap.clear();
Dan Gohman84023e02010-07-10 09:00:22 +0000204 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000205}
206
Dan Gohman89496d02010-07-02 00:10:16 +0000207/// CreateReg - Allocate a single virtual register for the given type.
208unsigned FunctionLoweringInfo::CreateReg(EVT VT) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000209 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
210}
211
Dan Gohman89496d02010-07-02 00:10:16 +0000212/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000213/// the correctly promoted or expanded types. Assign these registers
214/// consecutive vreg numbers and return the first assigned number.
215///
216/// In the case that the given value has struct or array type, this function
217/// will assign registers for each member or element.
218///
Dan Gohman89496d02010-07-02 00:10:16 +0000219unsigned FunctionLoweringInfo::CreateRegs(const Type *Ty) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000220 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000221 ComputeValueVTs(TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000222
223 unsigned FirstReg = 0;
224 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
225 EVT ValueVT = ValueVTs[Value];
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000226 EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000227
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000228 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000229 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000230 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000231 if (!FirstReg) FirstReg = R;
232 }
233 }
234 return FirstReg;
235}
Dan Gohman66336ed2009-11-23 17:42:46 +0000236
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000237/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
238/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
239/// the register's LiveOutInfo is for a smaller bit width, it is extended to
240/// the larger bit width by zero extension. The bit width must be no smaller
241/// than the LiveOutInfo's existing bit width.
242const FunctionLoweringInfo::LiveOutInfo *
243FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
244 if (!LiveOutRegInfo.inBounds(Reg))
245 return NULL;
246
247 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
248 if (!LOI->IsValid)
249 return NULL;
250
Cameron Zwarich33b55472011-02-25 01:10:55 +0000251 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich8fbbdca2011-02-25 01:11:01 +0000252 LOI->NumSignBits = 1;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000253 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
254 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
255 }
256
257 return LOI;
258}
259
260/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
261/// register based on the LiveOutInfo of its operands.
262void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
263 const Type *Ty = PN->getType();
264 if (!Ty->isIntegerTy() || Ty->isVectorTy())
265 return;
266
267 SmallVector<EVT, 1> ValueVTs;
268 ComputeValueVTs(TLI, Ty, ValueVTs);
269 assert(ValueVTs.size() == 1 &&
270 "PHIs with non-vector integer types should have a single VT.");
271 EVT IntVT = ValueVTs[0];
272
273 if (TLI.getNumRegisters(PN->getContext(), IntVT) != 1)
274 return;
275 IntVT = TLI.getTypeToTransformTo(PN->getContext(), IntVT);
276 unsigned BitWidth = IntVT.getSizeInBits();
277
278 unsigned DestReg = ValueMap[PN];
279 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
280 return;
281 LiveOutRegInfo.grow(DestReg);
282 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
283
284 Value *V = PN->getIncomingValue(0);
285 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
286 DestLOI.NumSignBits = 1;
287 APInt Zero(BitWidth, 0);
288 DestLOI.KnownZero = Zero;
289 DestLOI.KnownOne = Zero;
290 return;
291 }
292
293 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
294 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
295 DestLOI.NumSignBits = Val.getNumSignBits();
296 DestLOI.KnownZero = ~Val;
297 DestLOI.KnownOne = Val;
298 } else {
299 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
300 "CopyToReg node was created.");
301 unsigned SrcReg = ValueMap[V];
302 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
303 DestLOI.IsValid = false;
304 return;
305 }
306 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
307 if (!SrcLOI) {
308 DestLOI.IsValid = false;
309 return;
310 }
311 DestLOI = *SrcLOI;
312 }
313
314 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
315 DestLOI.KnownOne.getBitWidth() == BitWidth &&
316 "Masks should have the same bit width as the type.");
317
318 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
319 Value *V = PN->getIncomingValue(i);
320 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
321 DestLOI.NumSignBits = 1;
322 APInt Zero(BitWidth, 0);
323 DestLOI.KnownZero = Zero;
324 DestLOI.KnownOne = Zero;
Eric Christopher471e4222011-06-08 23:55:35 +0000325 return;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000326 }
327
328 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
329 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
330 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
331 DestLOI.KnownZero &= ~Val;
332 DestLOI.KnownOne &= Val;
333 continue;
334 }
335
336 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
337 "its CopyToReg node was created.");
338 unsigned SrcReg = ValueMap[V];
339 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
340 DestLOI.IsValid = false;
341 return;
342 }
343 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
344 if (!SrcLOI) {
345 DestLOI.IsValid = false;
346 return;
347 }
348 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
349 DestLOI.KnownZero &= SrcLOI->KnownZero;
350 DestLOI.KnownOne &= SrcLOI->KnownOne;
351 }
352}
353
Devang Patel0b48ead2010-08-31 22:22:42 +0000354/// setByValArgumentFrameIndex - Record frame index for the byval
355/// argument. This overrides previous frame index entry for this argument,
356/// if any.
Eric Christopher471e4222011-06-08 23:55:35 +0000357void FunctionLoweringInfo::setByValArgumentFrameIndex(const Argument *A,
Devang Patel0b48ead2010-08-31 22:22:42 +0000358 int FI) {
359 assert (A->hasByValAttr() && "Argument does not have byval attribute!");
360 ByValArgFrameIndexMap[A] = FI;
361}
Eric Christopher471e4222011-06-08 23:55:35 +0000362
Devang Patel0b48ead2010-08-31 22:22:42 +0000363/// getByValArgumentFrameIndex - Get frame index for the byval argument.
364/// If the argument does not have any assigned frame index then 0 is
365/// returned.
366int FunctionLoweringInfo::getByValArgumentFrameIndex(const Argument *A) {
367 assert (A->hasByValAttr() && "Argument does not have byval attribute!");
Eric Christopher471e4222011-06-08 23:55:35 +0000368 DenseMap<const Argument *, int>::iterator I =
Devang Patel0b48ead2010-08-31 22:22:42 +0000369 ByValArgFrameIndexMap.find(A);
370 if (I != ByValArgFrameIndexMap.end())
371 return I->second;
372 DEBUG(dbgs() << "Argument does not have assigned frame index!");
373 return 0;
374}
375
Dan Gohman66336ed2009-11-23 17:42:46 +0000376/// AddCatchInfo - Extract the personality and type infos from an eh.selector
377/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000378void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000379 MachineBasicBlock *MBB) {
380 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000381 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000382 assert(CE->getOpcode() == Instruction::BitCast &&
383 isa<Function>(CE->getOperand(0)) &&
384 "Personality should be a function");
385 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
386
387 // Gather all the type infos for this landing pad and pass them along to
388 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000389 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000390 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000391
Gabor Greife767e6b2010-06-30 13:45:50 +0000392 for (unsigned i = N - 1; i > 1; --i) {
393 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000394 unsigned FilterLength = CI->getZExtValue();
395 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000396 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000397
398 if (FirstCatch < N) {
399 TyInfo.reserve(N - FirstCatch);
400 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000401 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000402 MMI->addCatchTypeInfo(MBB, TyInfo);
403 TyInfo.clear();
404 }
405
406 if (!FilterLength) {
407 // Cleanup.
408 MMI->addCleanup(MBB);
409 } else {
410 // Filter.
411 TyInfo.reserve(FilterLength - 1);
412 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000413 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000414 MMI->addFilterTypeInfo(MBB, TyInfo);
415 TyInfo.clear();
416 }
417
418 N = i;
419 }
420 }
421
Gabor Greife767e6b2010-06-30 13:45:50 +0000422 if (N > 2) {
423 TyInfo.reserve(N - 2);
424 for (unsigned j = 2; j < N; ++j)
425 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000426 MMI->addCatchTypeInfo(MBB, TyInfo);
427 }
428}
429
Bill Wendlinge7147db2011-03-03 23:14:05 +0000430void llvm::CopyCatchInfo(const BasicBlock *SuccBB, const BasicBlock *LPad,
Dan Gohman5fca8b12009-11-23 18:12:11 +0000431 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Bill Wendlinge7147db2011-03-03 23:14:05 +0000432 SmallPtrSet<const BasicBlock*, 4> Visited;
433
434 // The 'eh.selector' call may not be in the direct successor of a basic block,
435 // but could be several successors deeper. If we don't find it, try going one
436 // level further. <rdar://problem/8824861>
437 while (Visited.insert(SuccBB)) {
438 for (BasicBlock::const_iterator I = SuccBB->begin(), E = --SuccBB->end();
439 I != E; ++I)
440 if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
441 // Apply the catch info to LPad.
442 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[LPad]);
Dan Gohman5fca8b12009-11-23 18:12:11 +0000443#ifndef NDEBUG
Bill Wendlinge7147db2011-03-03 23:14:05 +0000444 if (!FLI.MBBMap[SuccBB]->isLandingPad())
445 FLI.CatchInfoFound.insert(EHSel);
Dan Gohman5fca8b12009-11-23 18:12:11 +0000446#endif
Bill Wendlinge7147db2011-03-03 23:14:05 +0000447 return;
448 }
449
450 const BranchInst *Br = dyn_cast<BranchInst>(SuccBB->getTerminator());
451 if (Br && Br->isUnconditional())
452 SuccBB = Br->getSuccessor(0);
453 else
454 break;
455 }
Dan Gohman5fca8b12009-11-23 18:12:11 +0000456}