blob: b46edad7a3d4d1df4557669d8e3dbb8655aba7db [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/PostOrderIterator.h"
18#include "llvm/CodeGen/Analysis.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000024#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Module.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetLowering.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.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();
Gabor Greif03f09a32010-07-09 16:08:33 +000050 UI != E; ++UI) {
51 const User *U = *UI;
52 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000053 return true;
Gabor Greif03f09a32010-07-09 16:08:33 +000054 }
Dan Gohman6277eb22009-11-23 17:16:22 +000055 return false;
56}
57
Dan Gohmand858e902010-04-17 15:26:15 +000058FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli)
Dan Gohman6277eb22009-11-23 17:16:22 +000059 : TLI(tli) {
60}
61
Dan Gohman7451d3e2010-05-29 17:03:36 +000062void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Dan Gohman6277eb22009-11-23 17:16:22 +000063 Fn = &fn;
64 MF = &mf;
65 RegInfo = &MF->getRegInfo();
66
Dan Gohman84023e02010-07-10 09:00:22 +000067 // Check whether the function can return without sret-demotion.
68 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +000069 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, TLI);
Eric Christopher471e4222011-06-08 23:55:35 +000070 CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), *MF,
Eric Christopher5b13ed12012-02-24 01:59:01 +000071 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())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000081 Type *Ty = AI->getAllocatedType();
Micah Villmow3574eca2012-10-08 16:38:25 +000082 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty);
Dan Gohman6277eb22009-11-23 17:16:22 +000083 unsigned Align =
Micah Villmow3574eca2012-10-08 16:38:25 +000084 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohman6277eb22009-11-23 17:16:22 +000085 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() ||
Bill Wendling9c674bb2011-11-02 23:20:58 +000095 (TySize >= 8 && isa<ArrayType>(Ty) &&
Bill Wendlingdfc2c512010-07-27 01:55:19 +000096 cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8)));
Dan Gohman6277eb22009-11-23 17:16:22 +000097 StaticAllocaMap[AI] =
Eric Christopher5b13ed12012-02-24 01:59:01 +000098 MF->getFrameInfo()->CreateStackObject(TySize, Align, false,
Nadav Rotemc05d3062012-09-06 09:17:37 +000099 MayNeedSP, AI);
Dan Gohman6277eb22009-11-23 17:16:22 +0000100 }
101
102 for (; BB != EB; ++BB)
Eric Christopher5b13ed12012-02-24 01:59:01 +0000103 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
104 I != E; ++I) {
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000105 // Mark values used outside their block as exported, by allocating
106 // a virtual register for them.
Cameron Zwarich4ecc82e2011-02-22 03:24:52 +0000107 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000108 if (!isa<AllocaInst>(I) ||
109 !StaticAllocaMap.count(cast<AllocaInst>(I)))
110 InitializeRegForValue(I);
111
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000112 // Collect llvm.dbg.declare information. This is done now instead of
113 // during the initial isel pass through the IR so that it is done
114 // in a predictable order.
115 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
116 MachineModuleInfo &MMI = MF->getMMI();
117 if (MMI.hasDebugInfo() &&
118 DIVariable(DI->getVariable()).Verify() &&
119 !DI->getDebugLoc().isUnknown()) {
120 // Don't handle byval struct arguments or VLAs, for example.
121 // Non-byval arguments are handled here (they refer to the stack
122 // temporary alloca at this point).
123 const Value *Address = DI->getAddress();
124 if (Address) {
125 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
126 Address = BCI->getOperand(0);
127 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
128 DenseMap<const AllocaInst *, int>::iterator SI =
129 StaticAllocaMap.find(AI);
130 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
131 int FI = SI->second;
132 MMI.setVariableDbgInfo(DI->getVariable(),
133 FI, DI->getDebugLoc());
134 }
135 }
136 }
137 }
138 }
139 }
140
Dan Gohman6277eb22009-11-23 17:16:22 +0000141 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
142 // also creates the initial PHI MachineInstrs, though none of the input
143 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000144 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000145 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
146 MBBMap[BB] = MBB;
147 MF->push_back(MBB);
148
149 // Transfer the address-taken flag. This is necessary because there could
150 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
151 // the first one should be marked.
152 if (BB->hasAddressTaken())
153 MBB->setHasAddressTaken();
154
155 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
156 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000157 for (BasicBlock::const_iterator I = BB->begin();
158 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
159 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000160
Rafael Espindola3fa82832011-05-13 15:18:06 +0000161 // Skip empty types
162 if (PN->getType()->isEmptyTy())
163 continue;
164
Dan Gohmanc025c852010-04-20 14:48:02 +0000165 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000166 unsigned PHIReg = ValueMap[PN];
167 assert(PHIReg && "PHI node does not have an assigned virtual register!");
168
169 SmallVector<EVT, 4> ValueVTs;
170 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
171 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
172 EVT VT = ValueVTs[vti];
173 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
174 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
175 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000176 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000177 PHIReg += NumRegisters;
178 }
179 }
180 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000181
182 // Mark landing pad blocks.
183 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000184 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000185 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000186}
187
188/// clear - Clear out all the function-specific state. This returns this
189/// FunctionLoweringInfo to an empty state, ready to be used for a
190/// different function.
191void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000192 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
193 "Not all catch info was assigned to a landing pad!");
194
Dan Gohman6277eb22009-11-23 17:16:22 +0000195 MBBMap.clear();
196 ValueMap.clear();
197 StaticAllocaMap.clear();
198#ifndef NDEBUG
199 CatchInfoLost.clear();
200 CatchInfoFound.clear();
201#endif
202 LiveOutRegInfo.clear();
Cameron Zwaricha46cd972011-02-24 10:00:13 +0000203 VisitedBBs.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000204 ArgDbgValues.clear();
Devang Patel0b48ead2010-08-31 22:22:42 +0000205 ByValArgFrameIndexMap.clear();
Dan Gohman84023e02010-07-10 09:00:22 +0000206 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000207}
208
Dan Gohman89496d02010-07-02 00:10:16 +0000209/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000210unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000211 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
212}
213
Dan Gohman89496d02010-07-02 00:10:16 +0000214/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000215/// the correctly promoted or expanded types. Assign these registers
216/// consecutive vreg numbers and return the first assigned number.
217///
218/// In the case that the given value has struct or array type, this function
219/// will assign registers for each member or element.
220///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000221unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000222 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000223 ComputeValueVTs(TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000224
225 unsigned FirstReg = 0;
226 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
227 EVT ValueVT = ValueVTs[Value];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000228 MVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000229
Dan Gohmanffda6ba2010-07-01 03:55:39 +0000230 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000231 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000232 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000233 if (!FirstReg) FirstReg = R;
234 }
235 }
236 return FirstReg;
237}
Dan Gohman66336ed2009-11-23 17:42:46 +0000238
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000239/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
240/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
241/// the register's LiveOutInfo is for a smaller bit width, it is extended to
242/// the larger bit width by zero extension. The bit width must be no smaller
243/// than the LiveOutInfo's existing bit width.
244const FunctionLoweringInfo::LiveOutInfo *
245FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
246 if (!LiveOutRegInfo.inBounds(Reg))
247 return NULL;
248
249 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
250 if (!LOI->IsValid)
251 return NULL;
252
Cameron Zwarich33b55472011-02-25 01:10:55 +0000253 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich8fbbdca2011-02-25 01:11:01 +0000254 LOI->NumSignBits = 1;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000255 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
256 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
257 }
258
259 return LOI;
260}
261
262/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
263/// register based on the LiveOutInfo of its operands.
264void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000265 Type *Ty = PN->getType();
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000266 if (!Ty->isIntegerTy() || Ty->isVectorTy())
267 return;
268
269 SmallVector<EVT, 1> ValueVTs;
270 ComputeValueVTs(TLI, Ty, ValueVTs);
271 assert(ValueVTs.size() == 1 &&
272 "PHIs with non-vector integer types should have a single VT.");
273 EVT IntVT = ValueVTs[0];
274
275 if (TLI.getNumRegisters(PN->getContext(), IntVT) != 1)
276 return;
277 IntVT = TLI.getTypeToTransformTo(PN->getContext(), IntVT);
278 unsigned BitWidth = IntVT.getSizeInBits();
279
280 unsigned DestReg = ValueMap[PN];
281 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
282 return;
283 LiveOutRegInfo.grow(DestReg);
284 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
285
286 Value *V = PN->getIncomingValue(0);
287 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
288 DestLOI.NumSignBits = 1;
289 APInt Zero(BitWidth, 0);
290 DestLOI.KnownZero = Zero;
291 DestLOI.KnownOne = Zero;
292 return;
293 }
294
295 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
296 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
297 DestLOI.NumSignBits = Val.getNumSignBits();
298 DestLOI.KnownZero = ~Val;
299 DestLOI.KnownOne = Val;
300 } else {
301 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
302 "CopyToReg node was created.");
303 unsigned SrcReg = ValueMap[V];
304 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
305 DestLOI.IsValid = false;
306 return;
307 }
308 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
309 if (!SrcLOI) {
310 DestLOI.IsValid = false;
311 return;
312 }
313 DestLOI = *SrcLOI;
314 }
315
316 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
317 DestLOI.KnownOne.getBitWidth() == BitWidth &&
318 "Masks should have the same bit width as the type.");
319
320 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
321 Value *V = PN->getIncomingValue(i);
322 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
323 DestLOI.NumSignBits = 1;
324 APInt Zero(BitWidth, 0);
325 DestLOI.KnownZero = Zero;
326 DestLOI.KnownOne = Zero;
Eric Christopher471e4222011-06-08 23:55:35 +0000327 return;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000328 }
329
330 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
331 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
332 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
333 DestLOI.KnownZero &= ~Val;
334 DestLOI.KnownOne &= Val;
335 continue;
336 }
337
338 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
339 "its CopyToReg node was created.");
340 unsigned SrcReg = ValueMap[V];
341 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
342 DestLOI.IsValid = false;
343 return;
344 }
345 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
346 if (!SrcLOI) {
347 DestLOI.IsValid = false;
348 return;
349 }
350 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
351 DestLOI.KnownZero &= SrcLOI->KnownZero;
352 DestLOI.KnownOne &= SrcLOI->KnownOne;
353 }
354}
355
Devang Patel9aee3352011-09-08 22:59:09 +0000356/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel0b48ead2010-08-31 22:22:42 +0000357/// argument. This overrides previous frame index entry for this argument,
358/// if any.
Devang Patel9aee3352011-09-08 22:59:09 +0000359void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher5b13ed12012-02-24 01:59:01 +0000360 int FI) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000361 ByValArgFrameIndexMap[A] = FI;
362}
Eric Christopher471e4222011-06-08 23:55:35 +0000363
Devang Patel9aee3352011-09-08 22:59:09 +0000364/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel0b48ead2010-08-31 22:22:42 +0000365/// If the argument does not have any assigned frame index then 0 is
366/// returned.
Devang Patel9aee3352011-09-08 22:59:09 +0000367int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
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;
Eric Christopher0822e012012-02-23 03:39:43 +0000372 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel0b48ead2010-08-31 22:22:42 +0000373 return 0;
374}
375
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000376/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
377/// being passed to this variadic function, and set the MachineModuleInfo's
378/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
379/// reference to _fltused on Windows, which will link in MSVCRT's
380/// floating-point support.
381void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
382 MachineModuleInfo *MMI)
383{
384 FunctionType *FT = cast<FunctionType>(
385 I.getCalledValue()->getType()->getContainedType(0));
386 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
387 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
388 Type* T = I.getArgOperand(i)->getType();
389 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
390 i != e; ++i) {
391 if (i->isFloatingPointTy()) {
392 MMI->setUsesVAFloatArgument(true);
393 return;
394 }
395 }
396 }
397 }
398}
399
Dan Gohman66336ed2009-11-23 17:42:46 +0000400/// AddCatchInfo - Extract the personality and type infos from an eh.selector
401/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000402void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000403 MachineBasicBlock *MBB) {
404 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000405 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000406 assert(CE->getOpcode() == Instruction::BitCast &&
407 isa<Function>(CE->getOperand(0)) &&
408 "Personality should be a function");
409 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
410
411 // Gather all the type infos for this landing pad and pass them along to
412 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000413 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000414 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000415
Gabor Greife767e6b2010-06-30 13:45:50 +0000416 for (unsigned i = N - 1; i > 1; --i) {
417 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000418 unsigned FilterLength = CI->getZExtValue();
419 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000420 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000421
422 if (FirstCatch < N) {
423 TyInfo.reserve(N - FirstCatch);
424 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000425 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000426 MMI->addCatchTypeInfo(MBB, TyInfo);
427 TyInfo.clear();
428 }
429
430 if (!FilterLength) {
431 // Cleanup.
432 MMI->addCleanup(MBB);
433 } else {
434 // Filter.
435 TyInfo.reserve(FilterLength - 1);
436 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000437 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000438 MMI->addFilterTypeInfo(MBB, TyInfo);
439 TyInfo.clear();
440 }
441
442 N = i;
443 }
444 }
445
Gabor Greife767e6b2010-06-30 13:45:50 +0000446 if (N > 2) {
447 TyInfo.reserve(N - 2);
448 for (unsigned j = 2; j < N; ++j)
449 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000450 MMI->addCatchTypeInfo(MBB, TyInfo);
451 }
452}
453
Bill Wendling2ac0e6b2011-08-17 21:56:44 +0000454/// AddLandingPadInfo - Extract the exception handling information from the
455/// landingpad instruction and add them to the specified machine module info.
456void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
457 MachineBasicBlock *MBB) {
458 MMI.addPersonality(MBB,
459 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
460
461 if (I.isCleanup())
462 MMI.addCleanup(MBB);
463
464 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
465 // but we need to do it this way because of how the DWARF EH emitter
466 // processes the clauses.
467 for (unsigned i = I.getNumClauses(); i != 0; --i) {
468 Value *Val = I.getClause(i - 1);
469 if (I.isCatch(i - 1)) {
470 MMI.addCatchTypeInfo(MBB,
471 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
472 } else {
473 // Add filters in a list.
474 Constant *CVal = cast<Constant>(Val);
475 SmallVector<const GlobalVariable*, 4> FilterList;
476 for (User::op_iterator
477 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
478 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
479
480 MMI.addFilterTypeInfo(MBB, FilterList);
481 }
482 }
483}