blob: d842fabae25bf747c08b4e9a4652f75ef854f4e6 [file] [log] [blame]
Dan Gohmana3624b62009-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 Gohmane7846162010-07-07 16:01:37 +000016#include "llvm/CodeGen/FunctionLoweringInfo.h"
Chandler Carruthed0881b2012-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 Wendlinge38859d2012-06-28 00:05:13 +000024#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-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 Gohmana3624b62009-11-23 17:16:22 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-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 Gohmana3624b62009-11-23 17:16:22 +000039#include <algorithm>
40using namespace llvm;
41
Dan Gohmana3624b62009-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 Gohman913c9982010-04-15 04:33:49 +000045static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohman7c845e42010-04-20 14:50:13 +000046 if (I->use_empty()) return false;
Dan Gohmana3624b62009-11-23 17:16:22 +000047 if (isa<PHINode>(I)) return true;
Dan Gohman913c9982010-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 Greif52617fc2010-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 Gohmana3624b62009-11-23 17:16:22 +000053 return true;
Gabor Greif52617fc2010-07-09 16:08:33 +000054 }
Dan Gohmana3624b62009-11-23 17:16:22 +000055 return false;
56}
57
Dan Gohmand16aa542010-05-29 17:03:36 +000058void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
Bill Wendling0ccf3102013-06-19 20:32:16 +000059 const TargetLowering *TLI = TM.getTargetLowering();
60
Dan Gohmana3624b62009-11-23 17:16:22 +000061 Fn = &fn;
62 MF = &mf;
63 RegInfo = &MF->getRegInfo();
64
Dan Gohmand7b5ce32010-07-10 09:00:22 +000065 // Check whether the function can return without sret-demotion.
66 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8db01cb2013-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 Gohmand7b5ce32010-07-10 09:00:22 +000071
Dan Gohmana3624b62009-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 Gohman913c9982010-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)
Reid Klecknerdfbed592014-01-31 23:45:12 +000077 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
78 // Don't fold inalloca allocas or other dynamic allocas into the initial
79 // stack frame allocation, even if they are in the entry block.
80 if (!AI->isStaticAlloca())
81 continue;
82
Dan Gohman913c9982010-04-15 04:33:49 +000083 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Chris Lattner229907c2011-07-18 04:54:35 +000084 Type *Ty = AI->getAllocatedType();
Bill Wendling8db01cb2013-06-06 00:11:39 +000085 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmana3624b62009-11-23 17:16:22 +000086 unsigned Align =
Bill Wendling8db01cb2013-06-06 00:11:39 +000087 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmana3624b62009-11-23 17:16:22 +000088 AI->getAlignment());
89
90 TySize *= CUI->getZExtValue(); // Get total allocated size.
91 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Bill Wendling0ff1ef62010-07-27 01:55:19 +000092
Dan Gohmana3624b62009-11-23 17:16:22 +000093 StaticAllocaMap[AI] =
Josh Magee22b8ba22013-12-19 03:17:11 +000094 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
Dan Gohmana3624b62009-11-23 17:16:22 +000095 }
Reid Klecknerdfbed592014-01-31 23:45:12 +000096 }
Dan Gohmana3624b62009-11-23 17:16:22 +000097
98 for (; BB != EB; ++BB)
Eric Christopher219d51d2012-02-24 01:59:01 +000099 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
100 I != E; ++I) {
Dan Gohman1e9362772010-07-16 17:54:27 +0000101 // Mark values used outside their block as exported, by allocating
102 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000103 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000104 if (!isa<AllocaInst>(I) ||
105 !StaticAllocaMap.count(cast<AllocaInst>(I)))
106 InitializeRegForValue(I);
107
Dan Gohman1e9362772010-07-16 17:54:27 +0000108 // Collect llvm.dbg.declare information. This is done now instead of
109 // during the initial isel pass through the IR so that it is done
110 // in a predictable order.
111 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
112 MachineModuleInfo &MMI = MF->getMMI();
Manman Ren983a16c2013-06-28 05:43:10 +0000113 DIVariable DIVar(DI->getVariable());
114 assert((!DIVar || DIVar.isVariable()) &&
115 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman1e9362772010-07-16 17:54:27 +0000116 if (MMI.hasDebugInfo() &&
Manman Ren983a16c2013-06-28 05:43:10 +0000117 DIVar &&
Dan Gohman1e9362772010-07-16 17:54:27 +0000118 !DI->getDebugLoc().isUnknown()) {
119 // Don't handle byval struct arguments or VLAs, for example.
120 // Non-byval arguments are handled here (they refer to the stack
121 // temporary alloca at this point).
122 const Value *Address = DI->getAddress();
123 if (Address) {
124 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
125 Address = BCI->getOperand(0);
126 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
127 DenseMap<const AllocaInst *, int>::iterator SI =
128 StaticAllocaMap.find(AI);
129 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
130 int FI = SI->second;
131 MMI.setVariableDbgInfo(DI->getVariable(),
132 FI, DI->getDebugLoc());
133 }
134 }
135 }
136 }
137 }
138 }
139
Dan Gohmana3624b62009-11-23 17:16:22 +0000140 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
141 // also creates the initial PHI MachineInstrs, though none of the input
142 // operands are populated.
Dan Gohmanf57117d2010-04-14 16:30:40 +0000143 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohmana3624b62009-11-23 17:16:22 +0000144 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
145 MBBMap[BB] = MBB;
146 MF->push_back(MBB);
147
148 // Transfer the address-taken flag. This is necessary because there could
149 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
150 // the first one should be marked.
151 if (BB->hasAddressTaken())
152 MBB->setHasAddressTaken();
153
154 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
155 // appropriate.
Dan Gohman0f055d32010-04-20 14:46:25 +0000156 for (BasicBlock::const_iterator I = BB->begin();
157 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
158 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000159
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000160 // Skip empty types
161 if (PN->getType()->isEmptyTy())
162 continue;
163
Dan Gohman7b7f0882010-04-20 14:48:02 +0000164 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000165 unsigned PHIReg = ValueMap[PN];
166 assert(PHIReg && "PHI node does not have an assigned virtual register!");
167
168 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000169 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000170 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
171 EVT VT = ValueVTs[vti];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000172 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000173 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
174 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000175 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000176 PHIReg += NumRegisters;
177 }
178 }
179 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000180
181 // Mark landing pad blocks.
182 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohman913c9982010-04-15 04:33:49 +0000183 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000184 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmana3624b62009-11-23 17:16:22 +0000185}
186
187/// clear - Clear out all the function-specific state. This returns this
188/// FunctionLoweringInfo to an empty state, ready to be used for a
189/// different function.
190void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000191 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
192 "Not all catch info was assigned to a landing pad!");
193
Dan Gohmana3624b62009-11-23 17:16:22 +0000194 MBBMap.clear();
195 ValueMap.clear();
196 StaticAllocaMap.clear();
197#ifndef NDEBUG
198 CatchInfoLost.clear();
199 CatchInfoFound.clear();
200#endif
201 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000202 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000203 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000204 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000205 RegFixups.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000206}
207
Dan Gohman93f59202010-07-02 00:10:16 +0000208/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000209unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Bill Wendling0ccf3102013-06-19 20:32:16 +0000210 return RegInfo->
211 createVirtualRegister(TM.getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000212}
213
Dan Gohman93f59202010-07-02 00:10:16 +0000214/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-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 Lattner229907c2011-07-18 04:54:35 +0000221unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Bill Wendling0ccf3102013-06-19 20:32:16 +0000222 const TargetLowering *TLI = TM.getTargetLowering();
223
Dan Gohmana3624b62009-11-23 17:16:22 +0000224 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000225 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000226
227 unsigned FirstReg = 0;
228 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
229 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000230 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000231
Bill Wendling8db01cb2013-06-06 00:11:39 +0000232 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000233 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000234 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000235 if (!FirstReg) FirstReg = R;
236 }
237 }
238 return FirstReg;
239}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000240
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000241/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
242/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
243/// the register's LiveOutInfo is for a smaller bit width, it is extended to
244/// the larger bit width by zero extension. The bit width must be no smaller
245/// than the LiveOutInfo's existing bit width.
246const FunctionLoweringInfo::LiveOutInfo *
247FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
248 if (!LiveOutRegInfo.inBounds(Reg))
249 return NULL;
250
251 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
252 if (!LOI->IsValid)
253 return NULL;
254
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000255 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000256 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000257 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
258 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
259 }
260
261 return LOI;
262}
263
264/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
265/// register based on the LiveOutInfo of its operands.
266void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000267 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000268 if (!Ty->isIntegerTy() || Ty->isVectorTy())
269 return;
270
Bill Wendling0ccf3102013-06-19 20:32:16 +0000271 const TargetLowering *TLI = TM.getTargetLowering();
272
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000273 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000274 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000275 assert(ValueVTs.size() == 1 &&
276 "PHIs with non-vector integer types should have a single VT.");
277 EVT IntVT = ValueVTs[0];
278
Bill Wendling8db01cb2013-06-06 00:11:39 +0000279 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000280 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000281 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000282 unsigned BitWidth = IntVT.getSizeInBits();
283
284 unsigned DestReg = ValueMap[PN];
285 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
286 return;
287 LiveOutRegInfo.grow(DestReg);
288 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
289
290 Value *V = PN->getIncomingValue(0);
291 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
292 DestLOI.NumSignBits = 1;
293 APInt Zero(BitWidth, 0);
294 DestLOI.KnownZero = Zero;
295 DestLOI.KnownOne = Zero;
296 return;
297 }
298
299 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
300 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
301 DestLOI.NumSignBits = Val.getNumSignBits();
302 DestLOI.KnownZero = ~Val;
303 DestLOI.KnownOne = Val;
304 } else {
305 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
306 "CopyToReg node was created.");
307 unsigned SrcReg = ValueMap[V];
308 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
309 DestLOI.IsValid = false;
310 return;
311 }
312 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
313 if (!SrcLOI) {
314 DestLOI.IsValid = false;
315 return;
316 }
317 DestLOI = *SrcLOI;
318 }
319
320 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
321 DestLOI.KnownOne.getBitWidth() == BitWidth &&
322 "Masks should have the same bit width as the type.");
323
324 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
325 Value *V = PN->getIncomingValue(i);
326 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
327 DestLOI.NumSignBits = 1;
328 APInt Zero(BitWidth, 0);
329 DestLOI.KnownZero = Zero;
330 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000331 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000332 }
333
334 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
335 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
336 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
337 DestLOI.KnownZero &= ~Val;
338 DestLOI.KnownOne &= Val;
339 continue;
340 }
341
342 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
343 "its CopyToReg node was created.");
344 unsigned SrcReg = ValueMap[V];
345 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
346 DestLOI.IsValid = false;
347 return;
348 }
349 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
350 if (!SrcLOI) {
351 DestLOI.IsValid = false;
352 return;
353 }
354 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
355 DestLOI.KnownZero &= SrcLOI->KnownZero;
356 DestLOI.KnownOne &= SrcLOI->KnownOne;
357 }
358}
359
Devang Patel9d904e12011-09-08 22:59:09 +0000360/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000361/// argument. This overrides previous frame index entry for this argument,
362/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000363void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000364 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000365 ByValArgFrameIndexMap[A] = FI;
366}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000367
Devang Patel9d904e12011-09-08 22:59:09 +0000368/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000369/// If the argument does not have any assigned frame index then 0 is
370/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000371int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000372 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000373 ByValArgFrameIndexMap.find(A);
374 if (I != ByValArgFrameIndexMap.end())
375 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000376 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000377 return 0;
378}
379
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000380/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
381/// being passed to this variadic function, and set the MachineModuleInfo's
382/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
383/// reference to _fltused on Windows, which will link in MSVCRT's
384/// floating-point support.
385void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
386 MachineModuleInfo *MMI)
387{
388 FunctionType *FT = cast<FunctionType>(
389 I.getCalledValue()->getType()->getContainedType(0));
390 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
391 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
392 Type* T = I.getArgOperand(i)->getType();
393 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
394 i != e; ++i) {
395 if (i->isFloatingPointTy()) {
396 MMI->setUsesVAFloatArgument(true);
397 return;
398 }
399 }
400 }
401 }
402}
403
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000404/// AddCatchInfo - Extract the personality and type infos from an eh.selector
405/// call, and add them to the specified machine basic block.
Dan Gohman7deb4472010-04-14 19:53:31 +0000406void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000407 MachineBasicBlock *MBB) {
408 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greife4eed702010-06-25 08:24:59 +0000409 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000410 assert(CE->getOpcode() == Instruction::BitCast &&
411 isa<Function>(CE->getOperand(0)) &&
412 "Personality should be a function");
413 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
414
415 // Gather all the type infos for this landing pad and pass them along to
416 // MachineModuleInfo.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000417 std::vector<const GlobalVariable *> TyInfo;
Gabor Greif647d9c92010-06-30 13:45:50 +0000418 unsigned N = I.getNumArgOperands();
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000419
Gabor Greif647d9c92010-06-30 13:45:50 +0000420 for (unsigned i = N - 1; i > 1; --i) {
421 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000422 unsigned FilterLength = CI->getZExtValue();
423 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greif647d9c92010-06-30 13:45:50 +0000424 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000425
426 if (FirstCatch < N) {
427 TyInfo.reserve(N - FirstCatch);
428 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000429 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000430 MMI->addCatchTypeInfo(MBB, TyInfo);
431 TyInfo.clear();
432 }
433
434 if (!FilterLength) {
435 // Cleanup.
436 MMI->addCleanup(MBB);
437 } else {
438 // Filter.
439 TyInfo.reserve(FilterLength - 1);
440 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000441 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000442 MMI->addFilterTypeInfo(MBB, TyInfo);
443 TyInfo.clear();
444 }
445
446 N = i;
447 }
448 }
449
Gabor Greif647d9c92010-06-30 13:45:50 +0000450 if (N > 2) {
451 TyInfo.reserve(N - 2);
452 for (unsigned j = 2; j < N; ++j)
453 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000454 MMI->addCatchTypeInfo(MBB, TyInfo);
455 }
456}
457
Bill Wendling247fd3b2011-08-17 21:56:44 +0000458/// AddLandingPadInfo - Extract the exception handling information from the
459/// landingpad instruction and add them to the specified machine module info.
460void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
461 MachineBasicBlock *MBB) {
462 MMI.addPersonality(MBB,
463 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
464
465 if (I.isCleanup())
466 MMI.addCleanup(MBB);
467
468 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
469 // but we need to do it this way because of how the DWARF EH emitter
470 // processes the clauses.
471 for (unsigned i = I.getNumClauses(); i != 0; --i) {
472 Value *Val = I.getClause(i - 1);
473 if (I.isCatch(i - 1)) {
474 MMI.addCatchTypeInfo(MBB,
475 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
476 } else {
477 // Add filters in a list.
478 Constant *CVal = cast<Constant>(Val);
479 SmallVector<const GlobalVariable*, 4> FilterList;
480 for (User::op_iterator
481 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
482 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
483
484 MMI.addFilterTypeInfo(MBB, FilterList);
485 }
486 }
487}