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