blob: 2fb7664860a266c78ce0510c9e48cca925750914 [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 Gohman7451d3e2010-05-29 17:03:36 +000058void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Bill Wendlingd626d332013-06-19 20:32:16 +000059 const TargetLowering *TLI = TM.getTargetLowering();
60
Dan Gohman6277eb22009-11-23 17:16:22 +000061 Fn = &fn;
62 MF = &mf;
63 RegInfo = &MF->getRegInfo();
64
Dan Gohman84023e02010-07-10 09:00:22 +000065 // Check whether the function can return without sret-demotion.
66 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling384ceb82013-06-06 00:11:39 +000067 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
68 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
69 Fn->isVarArg(),
70 Outs, Fn->getContext());
Dan Gohman84023e02010-07-10 09:00:22 +000071
Dan Gohman6277eb22009-11-23 17:16:22 +000072 // Initialize the mapping of values to registers. This is only set up for
73 // instruction values that are used outside of the block that defines
74 // them.
Dan Gohmanae541aa2010-04-15 04:33:49 +000075 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
76 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
77 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I))
78 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000079 Type *Ty = AI->getAllocatedType();
Bill Wendling384ceb82013-06-06 00:11:39 +000080 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohman6277eb22009-11-23 17:16:22 +000081 unsigned Align =
Bill Wendling384ceb82013-06-06 00:11:39 +000082 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohman6277eb22009-11-23 17:16:22 +000083 AI->getAlignment());
84
85 TySize *= CUI->getZExtValue(); // Get total allocated size.
86 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Bill Wendlingdfc2c512010-07-27 01:55:19 +000087
88 // The object may need to be placed onto the stack near the stack
89 // protector if one exists. Determine here if this object is a suitable
90 // candidate. I.e., it would trigger the creation of a stack protector.
91 bool MayNeedSP =
92 (AI->isArrayAllocation() ||
Bill Wendling9c674bb2011-11-02 23:20:58 +000093 (TySize >= 8 && isa<ArrayType>(Ty) &&
Bill Wendlingdfc2c512010-07-27 01:55:19 +000094 cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8)));
Dan Gohman6277eb22009-11-23 17:16:22 +000095 StaticAllocaMap[AI] =
Bob Wilson8f637ad2013-02-08 20:35:15 +000096 MF->getFrameInfo()->CreateStackObject(TySize, Align, false,
Nadav Rotemc05d3062012-09-06 09:17:37 +000097 MayNeedSP, AI);
Dan Gohman6277eb22009-11-23 17:16:22 +000098 }
99
100 for (; BB != EB; ++BB)
Eric Christopher5b13ed12012-02-24 01:59:01 +0000101 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
102 I != E; ++I) {
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000103 // 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;
Bill Wendling384ceb82013-06-06 00:11:39 +0000168 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000169 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
170 EVT VT = ValueVTs[vti];
Bill Wendling384ceb82013-06-06 00:11:39 +0000171 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000172 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.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000208unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Bill Wendlingd626d332013-06-19 20:32:16 +0000209 return RegInfo->
210 createVirtualRegister(TM.getTargetLowering()->getRegClassFor(VT));
Dan Gohman6277eb22009-11-23 17:16:22 +0000211}
212
Dan Gohman89496d02010-07-02 00:10:16 +0000213/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000214/// the correctly promoted or expanded types. Assign these registers
215/// consecutive vreg numbers and return the first assigned number.
216///
217/// In the case that the given value has struct or array type, this function
218/// will assign registers for each member or element.
219///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000220unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Bill Wendlingd626d332013-06-19 20:32:16 +0000221 const TargetLowering *TLI = TM.getTargetLowering();
222
Dan Gohman6277eb22009-11-23 17:16:22 +0000223 SmallVector<EVT, 4> ValueVTs;
Bill Wendling384ceb82013-06-06 00:11:39 +0000224 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000225
226 unsigned FirstReg = 0;
227 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
228 EVT ValueVT = ValueVTs[Value];
Bill Wendling384ceb82013-06-06 00:11:39 +0000229 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000230
Bill Wendling384ceb82013-06-06 00:11:39 +0000231 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000232 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000233 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000234 if (!FirstReg) FirstReg = R;
235 }
236 }
237 return FirstReg;
238}
Dan Gohman66336ed2009-11-23 17:42:46 +0000239
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000240/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
241/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
242/// the register's LiveOutInfo is for a smaller bit width, it is extended to
243/// the larger bit width by zero extension. The bit width must be no smaller
244/// than the LiveOutInfo's existing bit width.
245const FunctionLoweringInfo::LiveOutInfo *
246FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
247 if (!LiveOutRegInfo.inBounds(Reg))
248 return NULL;
249
250 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
251 if (!LOI->IsValid)
252 return NULL;
253
Cameron Zwarich33b55472011-02-25 01:10:55 +0000254 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich8fbbdca2011-02-25 01:11:01 +0000255 LOI->NumSignBits = 1;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000256 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
257 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
258 }
259
260 return LOI;
261}
262
263/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
264/// register based on the LiveOutInfo of its operands.
265void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000266 Type *Ty = PN->getType();
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000267 if (!Ty->isIntegerTy() || Ty->isVectorTy())
268 return;
269
Bill Wendlingd626d332013-06-19 20:32:16 +0000270 const TargetLowering *TLI = TM.getTargetLowering();
271
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000272 SmallVector<EVT, 1> ValueVTs;
Bill Wendling384ceb82013-06-06 00:11:39 +0000273 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000274 assert(ValueVTs.size() == 1 &&
275 "PHIs with non-vector integer types should have a single VT.");
276 EVT IntVT = ValueVTs[0];
277
Bill Wendling384ceb82013-06-06 00:11:39 +0000278 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000279 return;
Bill Wendling384ceb82013-06-06 00:11:39 +0000280 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000281 unsigned BitWidth = IntVT.getSizeInBits();
282
283 unsigned DestReg = ValueMap[PN];
284 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
285 return;
286 LiveOutRegInfo.grow(DestReg);
287 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
288
289 Value *V = PN->getIncomingValue(0);
290 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
291 DestLOI.NumSignBits = 1;
292 APInt Zero(BitWidth, 0);
293 DestLOI.KnownZero = Zero;
294 DestLOI.KnownOne = Zero;
295 return;
296 }
297
298 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
299 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
300 DestLOI.NumSignBits = Val.getNumSignBits();
301 DestLOI.KnownZero = ~Val;
302 DestLOI.KnownOne = Val;
303 } else {
304 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
305 "CopyToReg node was created.");
306 unsigned SrcReg = ValueMap[V];
307 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
308 DestLOI.IsValid = false;
309 return;
310 }
311 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
312 if (!SrcLOI) {
313 DestLOI.IsValid = false;
314 return;
315 }
316 DestLOI = *SrcLOI;
317 }
318
319 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
320 DestLOI.KnownOne.getBitWidth() == BitWidth &&
321 "Masks should have the same bit width as the type.");
322
323 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
324 Value *V = PN->getIncomingValue(i);
325 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
326 DestLOI.NumSignBits = 1;
327 APInt Zero(BitWidth, 0);
328 DestLOI.KnownZero = Zero;
329 DestLOI.KnownOne = Zero;
Eric Christopher471e4222011-06-08 23:55:35 +0000330 return;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000331 }
332
333 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
334 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
335 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
336 DestLOI.KnownZero &= ~Val;
337 DestLOI.KnownOne &= Val;
338 continue;
339 }
340
341 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
342 "its CopyToReg node was created.");
343 unsigned SrcReg = ValueMap[V];
344 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
345 DestLOI.IsValid = false;
346 return;
347 }
348 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
349 if (!SrcLOI) {
350 DestLOI.IsValid = false;
351 return;
352 }
353 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
354 DestLOI.KnownZero &= SrcLOI->KnownZero;
355 DestLOI.KnownOne &= SrcLOI->KnownOne;
356 }
357}
358
Devang Patel9aee3352011-09-08 22:59:09 +0000359/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel0b48ead2010-08-31 22:22:42 +0000360/// argument. This overrides previous frame index entry for this argument,
361/// if any.
Devang Patel9aee3352011-09-08 22:59:09 +0000362void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher5b13ed12012-02-24 01:59:01 +0000363 int FI) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000364 ByValArgFrameIndexMap[A] = FI;
365}
Eric Christopher471e4222011-06-08 23:55:35 +0000366
Devang Patel9aee3352011-09-08 22:59:09 +0000367/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel0b48ead2010-08-31 22:22:42 +0000368/// If the argument does not have any assigned frame index then 0 is
369/// returned.
Devang Patel9aee3352011-09-08 22:59:09 +0000370int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher471e4222011-06-08 23:55:35 +0000371 DenseMap<const Argument *, int>::iterator I =
Devang Patel0b48ead2010-08-31 22:22:42 +0000372 ByValArgFrameIndexMap.find(A);
373 if (I != ByValArgFrameIndexMap.end())
374 return I->second;
Eric Christopher0822e012012-02-23 03:39:43 +0000375 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel0b48ead2010-08-31 22:22:42 +0000376 return 0;
377}
378
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000379/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
380/// being passed to this variadic function, and set the MachineModuleInfo's
381/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
382/// reference to _fltused on Windows, which will link in MSVCRT's
383/// floating-point support.
384void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
385 MachineModuleInfo *MMI)
386{
387 FunctionType *FT = cast<FunctionType>(
388 I.getCalledValue()->getType()->getContainedType(0));
389 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
390 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
391 Type* T = I.getArgOperand(i)->getType();
392 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
393 i != e; ++i) {
394 if (i->isFloatingPointTy()) {
395 MMI->setUsesVAFloatArgument(true);
396 return;
397 }
398 }
399 }
400 }
401}
402
Dan Gohman66336ed2009-11-23 17:42:46 +0000403/// AddCatchInfo - Extract the personality and type infos from an eh.selector
404/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000405void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000406 MachineBasicBlock *MBB) {
407 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000408 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000409 assert(CE->getOpcode() == Instruction::BitCast &&
410 isa<Function>(CE->getOperand(0)) &&
411 "Personality should be a function");
412 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
413
414 // Gather all the type infos for this landing pad and pass them along to
415 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000416 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000417 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000418
Gabor Greife767e6b2010-06-30 13:45:50 +0000419 for (unsigned i = N - 1; i > 1; --i) {
420 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000421 unsigned FilterLength = CI->getZExtValue();
422 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000423 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000424
425 if (FirstCatch < N) {
426 TyInfo.reserve(N - FirstCatch);
427 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000428 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000429 MMI->addCatchTypeInfo(MBB, TyInfo);
430 TyInfo.clear();
431 }
432
433 if (!FilterLength) {
434 // Cleanup.
435 MMI->addCleanup(MBB);
436 } else {
437 // Filter.
438 TyInfo.reserve(FilterLength - 1);
439 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000440 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000441 MMI->addFilterTypeInfo(MBB, TyInfo);
442 TyInfo.clear();
443 }
444
445 N = i;
446 }
447 }
448
Gabor Greife767e6b2010-06-30 13:45:50 +0000449 if (N > 2) {
450 TyInfo.reserve(N - 2);
451 for (unsigned j = 2; j < N; ++j)
452 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000453 MMI->addCatchTypeInfo(MBB, TyInfo);
454 }
455}
456
Bill Wendling2ac0e6b2011-08-17 21:56:44 +0000457/// AddLandingPadInfo - Extract the exception handling information from the
458/// landingpad instruction and add them to the specified machine module info.
459void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
460 MachineBasicBlock *MBB) {
461 MMI.addPersonality(MBB,
462 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
463
464 if (I.isCleanup())
465 MMI.addCleanup(MBB);
466
467 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
468 // but we need to do it this way because of how the DWARF EH emitter
469 // processes the clauses.
470 for (unsigned i = I.getNumClauses(); i != 0; --i) {
471 Value *Val = I.getClause(i - 1);
472 if (I.isCatch(i - 1)) {
473 MMI.addCatchTypeInfo(MBB,
474 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
475 } else {
476 // Add filters in a list.
477 Constant *CVal = cast<Constant>(Val);
478 SmallVector<const GlobalVariable*, 4> FilterList;
479 for (User::op_iterator
480 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
481 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
482
483 MMI.addFilterTypeInfo(MBB, FilterList);
484 }
485 }
486}