blob: 82b6c4022d7b5738ea5efc036c5497f64f949402 [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
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 Gohman84023e02010-07-10 09:00:22 +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.
Bill Wendlingdfc2c512010-07-27 01:55:19 +0000114
115 // The object may need to be placed onto the stack near the stack
116 // protector if one exists. Determine here if this object is a suitable
117 // candidate. I.e., it would trigger the creation of a stack protector.
118 bool MayNeedSP =
119 (AI->isArrayAllocation() ||
120 (TySize > 8 && isa<ArrayType>(Ty) &&
121 cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8)));
Dan Gohman6277eb22009-11-23 17:16:22 +0000122 StaticAllocaMap[AI] =
Bill Wendlingdfc2c512010-07-27 01:55:19 +0000123 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, MayNeedSP);
Dan Gohman6277eb22009-11-23 17:16:22 +0000124 }
125
126 for (; BB != EB; ++BB)
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000127 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
128 // Mark values used outside their block as exported, by allocating
129 // a virtual register for them.
Cameron Zwarich4ecc82e2011-02-22 03:24:52 +0000130 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000131 if (!isa<AllocaInst>(I) ||
132 !StaticAllocaMap.count(cast<AllocaInst>(I)))
133 InitializeRegForValue(I);
134
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000135 // Collect llvm.dbg.declare information. This is done now instead of
136 // during the initial isel pass through the IR so that it is done
137 // in a predictable order.
138 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
139 MachineModuleInfo &MMI = MF->getMMI();
140 if (MMI.hasDebugInfo() &&
141 DIVariable(DI->getVariable()).Verify() &&
142 !DI->getDebugLoc().isUnknown()) {
143 // Don't handle byval struct arguments or VLAs, for example.
144 // Non-byval arguments are handled here (they refer to the stack
145 // temporary alloca at this point).
146 const Value *Address = DI->getAddress();
147 if (Address) {
148 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
149 Address = BCI->getOperand(0);
150 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
151 DenseMap<const AllocaInst *, int>::iterator SI =
152 StaticAllocaMap.find(AI);
153 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
154 int FI = SI->second;
155 MMI.setVariableDbgInfo(DI->getVariable(),
156 FI, DI->getDebugLoc());
157 }
158 }
159 }
160 }
161 }
162 }
163
Dan Gohman6277eb22009-11-23 17:16:22 +0000164 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
165 // also creates the initial PHI MachineInstrs, though none of the input
166 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000167 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000168 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
169 MBBMap[BB] = MBB;
170 MF->push_back(MBB);
171
172 // Transfer the address-taken flag. This is necessary because there could
173 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
174 // the first one should be marked.
175 if (BB->hasAddressTaken())
176 MBB->setHasAddressTaken();
177
178 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
179 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000180 for (BasicBlock::const_iterator I = BB->begin();
181 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
182 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000183
Dan Gohmanc025c852010-04-20 14:48:02 +0000184 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000185 unsigned PHIReg = ValueMap[PN];
186 assert(PHIReg && "PHI node does not have an assigned virtual register!");
187
188 SmallVector<EVT, 4> ValueVTs;
189 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
190 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
191 EVT VT = ValueVTs[vti];
192 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
193 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
194 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000195 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000196 PHIReg += NumRegisters;
197 }
198 }
199 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000200
201 // Mark landing pad blocks.
202 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000203 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000204 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000205}
206
207/// clear - Clear out all the function-specific state. This returns this
208/// FunctionLoweringInfo to an empty state, ready to be used for a
209/// different function.
210void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000211 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
212 "Not all catch info was assigned to a landing pad!");
213
Dan Gohman6277eb22009-11-23 17:16:22 +0000214 MBBMap.clear();
215 ValueMap.clear();
216 StaticAllocaMap.clear();
217#ifndef NDEBUG
218 CatchInfoLost.clear();
219 CatchInfoFound.clear();
220#endif
221 LiveOutRegInfo.clear();
Cameron Zwaricha46cd972011-02-24 10:00:13 +0000222 VisitedBBs.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000223 ArgDbgValues.clear();
Devang Patel0b48ead2010-08-31 22:22:42 +0000224 ByValArgFrameIndexMap.clear();
Dan Gohman84023e02010-07-10 09:00:22 +0000225 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000226}
227
Dan Gohman89496d02010-07-02 00:10:16 +0000228/// CreateReg - Allocate a single virtual register for the given type.
229unsigned FunctionLoweringInfo::CreateReg(EVT VT) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000230 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
231}
232
Dan Gohman89496d02010-07-02 00:10:16 +0000233/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000234/// the correctly promoted or expanded types. Assign these registers
235/// consecutive vreg numbers and return the first assigned number.
236///
237/// In the case that the given value has struct or array type, this function
238/// will assign registers for each member or element.
239///
Dan Gohman89496d02010-07-02 00:10:16 +0000240unsigned FunctionLoweringInfo::CreateRegs(const Type *Ty) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000241 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000242 ComputeValueVTs(TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000243
244 unsigned FirstReg = 0;
245 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
246 EVT ValueVT = ValueVTs[Value];
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000247 EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000248
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000249 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000250 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000251 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000252 if (!FirstReg) FirstReg = R;
253 }
254 }
255 return FirstReg;
256}
Dan Gohman66336ed2009-11-23 17:42:46 +0000257
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000258/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
259/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
260/// the register's LiveOutInfo is for a smaller bit width, it is extended to
261/// the larger bit width by zero extension. The bit width must be no smaller
262/// than the LiveOutInfo's existing bit width.
263const FunctionLoweringInfo::LiveOutInfo *
264FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
265 if (!LiveOutRegInfo.inBounds(Reg))
266 return NULL;
267
268 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
269 if (!LOI->IsValid)
270 return NULL;
271
272 if (BitWidth >= LOI->KnownZero.getBitWidth()) {
273 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
274 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
275 }
276
277 return LOI;
278}
279
280/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
281/// register based on the LiveOutInfo of its operands.
282void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
283 const Type *Ty = PN->getType();
284 if (!Ty->isIntegerTy() || Ty->isVectorTy())
285 return;
286
287 SmallVector<EVT, 1> ValueVTs;
288 ComputeValueVTs(TLI, Ty, ValueVTs);
289 assert(ValueVTs.size() == 1 &&
290 "PHIs with non-vector integer types should have a single VT.");
291 EVT IntVT = ValueVTs[0];
292
293 if (TLI.getNumRegisters(PN->getContext(), IntVT) != 1)
294 return;
295 IntVT = TLI.getTypeToTransformTo(PN->getContext(), IntVT);
296 unsigned BitWidth = IntVT.getSizeInBits();
297
298 unsigned DestReg = ValueMap[PN];
299 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
300 return;
301 LiveOutRegInfo.grow(DestReg);
302 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
303
304 Value *V = PN->getIncomingValue(0);
305 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
306 DestLOI.NumSignBits = 1;
307 APInt Zero(BitWidth, 0);
308 DestLOI.KnownZero = Zero;
309 DestLOI.KnownOne = Zero;
310 return;
311 }
312
313 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
314 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
315 DestLOI.NumSignBits = Val.getNumSignBits();
316 DestLOI.KnownZero = ~Val;
317 DestLOI.KnownOne = Val;
318 } else {
319 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
320 "CopyToReg node was created.");
321 unsigned SrcReg = ValueMap[V];
322 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
323 DestLOI.IsValid = false;
324 return;
325 }
326 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
327 if (!SrcLOI) {
328 DestLOI.IsValid = false;
329 return;
330 }
331 DestLOI = *SrcLOI;
332 }
333
334 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
335 DestLOI.KnownOne.getBitWidth() == BitWidth &&
336 "Masks should have the same bit width as the type.");
337
338 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
339 Value *V = PN->getIncomingValue(i);
340 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
341 DestLOI.NumSignBits = 1;
342 APInt Zero(BitWidth, 0);
343 DestLOI.KnownZero = Zero;
344 DestLOI.KnownOne = Zero;
345 return;
346 }
347
348 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
349 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
350 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
351 DestLOI.KnownZero &= ~Val;
352 DestLOI.KnownOne &= Val;
353 continue;
354 }
355
356 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
357 "its CopyToReg node was created.");
358 unsigned SrcReg = ValueMap[V];
359 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
360 DestLOI.IsValid = false;
361 return;
362 }
363 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
364 if (!SrcLOI) {
365 DestLOI.IsValid = false;
366 return;
367 }
368 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
369 DestLOI.KnownZero &= SrcLOI->KnownZero;
370 DestLOI.KnownOne &= SrcLOI->KnownOne;
371 }
372}
373
Devang Patel0b48ead2010-08-31 22:22:42 +0000374/// setByValArgumentFrameIndex - Record frame index for the byval
375/// argument. This overrides previous frame index entry for this argument,
376/// if any.
377void FunctionLoweringInfo::setByValArgumentFrameIndex(const Argument *A,
378 int FI) {
379 assert (A->hasByValAttr() && "Argument does not have byval attribute!");
380 ByValArgFrameIndexMap[A] = FI;
381}
382
383/// getByValArgumentFrameIndex - Get frame index for the byval argument.
384/// If the argument does not have any assigned frame index then 0 is
385/// returned.
386int FunctionLoweringInfo::getByValArgumentFrameIndex(const Argument *A) {
387 assert (A->hasByValAttr() && "Argument does not have byval attribute!");
388 DenseMap<const Argument *, int>::iterator I =
389 ByValArgFrameIndexMap.find(A);
390 if (I != ByValArgFrameIndexMap.end())
391 return I->second;
392 DEBUG(dbgs() << "Argument does not have assigned frame index!");
393 return 0;
394}
395
Dan Gohman66336ed2009-11-23 17:42:46 +0000396/// AddCatchInfo - Extract the personality and type infos from an eh.selector
397/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000398void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000399 MachineBasicBlock *MBB) {
400 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000401 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000402 assert(CE->getOpcode() == Instruction::BitCast &&
403 isa<Function>(CE->getOperand(0)) &&
404 "Personality should be a function");
405 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
406
407 // Gather all the type infos for this landing pad and pass them along to
408 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000409 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000410 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000411
Gabor Greife767e6b2010-06-30 13:45:50 +0000412 for (unsigned i = N - 1; i > 1; --i) {
413 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000414 unsigned FilterLength = CI->getZExtValue();
415 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000416 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000417
418 if (FirstCatch < N) {
419 TyInfo.reserve(N - FirstCatch);
420 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000421 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000422 MMI->addCatchTypeInfo(MBB, TyInfo);
423 TyInfo.clear();
424 }
425
426 if (!FilterLength) {
427 // Cleanup.
428 MMI->addCleanup(MBB);
429 } else {
430 // Filter.
431 TyInfo.reserve(FilterLength - 1);
432 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000433 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000434 MMI->addFilterTypeInfo(MBB, TyInfo);
435 TyInfo.clear();
436 }
437
438 N = i;
439 }
440 }
441
Gabor Greife767e6b2010-06-30 13:45:50 +0000442 if (N > 2) {
443 TyInfo.reserve(N - 2);
444 for (unsigned j = 2; j < N; ++j)
445 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000446 MMI->addCatchTypeInfo(MBB, TyInfo);
447 }
448}
449
Dan Gohman25208642010-04-14 19:53:31 +0000450void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB,
Dan Gohman5fca8b12009-11-23 18:12:11 +0000451 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Dan Gohman25208642010-04-14 19:53:31 +0000452 for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end();
453 I != E; ++I)
454 if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
Dan Gohman5fca8b12009-11-23 18:12:11 +0000455 // Apply the catch info to DestBB.
456 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
457#ifndef NDEBUG
458 if (!FLI.MBBMap[SrcBB]->isLandingPad())
459 FLI.CatchInfoFound.insert(EHSel);
460#endif
461 }
462}