blob: 5f0006e237f0eca0bfd354cd289f9f2a2913e36c [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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#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"
Stephen Hines36b56882014-04-23 16:57:46 -070035#include "llvm/Target/TargetFrameLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000040#include <algorithm>
41using namespace llvm;
42
Dan Gohman6277eb22009-11-23 17:16:22 +000043/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
44/// PHI nodes or outside of the basic block that defines it, or used by a
45/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohmanae541aa2010-04-15 04:33:49 +000046static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohmand84e8062010-04-20 14:50:13 +000047 if (I->use_empty()) return false;
Dan Gohman6277eb22009-11-23 17:16:22 +000048 if (isa<PHINode>(I)) return true;
Dan Gohmanae541aa2010-04-15 04:33:49 +000049 const BasicBlock *BB = I->getParent();
Stephen Hines36b56882014-04-23 16:57:46 -070050 for (const User *U : I->users())
Gabor Greif03f09a32010-07-09 16:08:33 +000051 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohman6277eb22009-11-23 17:16:22 +000052 return true;
Stephen Hines36b56882014-04-23 16:57:46 -070053
Dan Gohman6277eb22009-11-23 17:16:22 +000054 return false;
55}
56
Stephen Hines36b56882014-04-23 16:57:46 -070057void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
58 SelectionDAG *DAG) {
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)
Stephen Hines36b56882014-04-23 16:57:46 -070077 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 Gohmanae541aa2010-04-15 04:33:49 +000083 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000084 Type *Ty = AI->getAllocatedType();
Bill Wendling384ceb82013-06-06 00:11:39 +000085 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohman6277eb22009-11-23 17:16:22 +000086 unsigned Align =
Bill Wendling384ceb82013-06-06 00:11:39 +000087 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohman6277eb22009-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 Wendlingdfc2c512010-07-27 01:55:19 +000092
Dan Gohman6277eb22009-11-23 17:16:22 +000093 StaticAllocaMap[AI] =
Stephen Hines36b56882014-04-23 16:57:46 -070094 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
Dan Gohman6277eb22009-11-23 17:16:22 +000095 }
Stephen Hines36b56882014-04-23 16:57:46 -070096 }
Dan Gohman6277eb22009-11-23 17:16:22 +000097
98 for (; BB != EB; ++BB)
Eric Christopher5b13ed12012-02-24 01:59:01 +000099 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
100 I != E; ++I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700101 // Look for dynamic allocas.
102 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
103 if (!AI->isStaticAlloca()) {
104 unsigned Align = std::max(
105 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
106 AI->getAllocatedType()),
107 AI->getAlignment());
108 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
109 if (Align <= StackAlign)
110 Align = 0;
111 // Inform the Frame Information that we have variable-sized objects.
112 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
113 }
114 }
115
116 // Look for inline asm that clobbers the SP register.
117 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
118 ImmutableCallSite CS(I);
119 if (isa<InlineAsm>(CS.getCalledValue())) {
120 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
121 std::vector<TargetLowering::AsmOperandInfo> Ops =
122 TLI->ParseConstraints(CS);
123 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
124 TargetLowering::AsmOperandInfo &Op = Ops[I];
125 if (Op.Type == InlineAsm::isClobber) {
126 // Clobbers don't have SDValue operands, hence SDValue().
127 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
128 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
129 TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
130 Op.ConstraintVT);
131 if (PhysReg.first == SP)
132 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
133 }
134 }
135 }
136 }
137
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000138 // Mark values used outside their block as exported, by allocating
139 // a virtual register for them.
Cameron Zwarich4ecc82e2011-02-22 03:24:52 +0000140 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohman6277eb22009-11-23 17:16:22 +0000141 if (!isa<AllocaInst>(I) ||
142 !StaticAllocaMap.count(cast<AllocaInst>(I)))
143 InitializeRegForValue(I);
144
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000145 // Collect llvm.dbg.declare information. This is done now instead of
146 // during the initial isel pass through the IR so that it is done
147 // in a predictable order.
148 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
149 MachineModuleInfo &MMI = MF->getMMI();
Manman Rencbafae62013-06-28 05:43:10 +0000150 DIVariable DIVar(DI->getVariable());
151 assert((!DIVar || DIVar.isVariable()) &&
152 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000153 if (MMI.hasDebugInfo() &&
Manman Rencbafae62013-06-28 05:43:10 +0000154 DIVar &&
Dan Gohman9c3d5e42010-07-16 17:54:27 +0000155 !DI->getDebugLoc().isUnknown()) {
156 // Don't handle byval struct arguments or VLAs, for example.
157 // Non-byval arguments are handled here (they refer to the stack
158 // temporary alloca at this point).
159 const Value *Address = DI->getAddress();
160 if (Address) {
161 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
162 Address = BCI->getOperand(0);
163 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
164 DenseMap<const AllocaInst *, int>::iterator SI =
165 StaticAllocaMap.find(AI);
166 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
167 int FI = SI->second;
168 MMI.setVariableDbgInfo(DI->getVariable(),
169 FI, DI->getDebugLoc());
170 }
171 }
172 }
173 }
174 }
175 }
176
Dan Gohman6277eb22009-11-23 17:16:22 +0000177 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
178 // also creates the initial PHI MachineInstrs, though none of the input
179 // operands are populated.
Dan Gohmand0d82752010-04-14 16:30:40 +0000180 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohman6277eb22009-11-23 17:16:22 +0000181 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
182 MBBMap[BB] = MBB;
183 MF->push_back(MBB);
184
185 // Transfer the address-taken flag. This is necessary because there could
186 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
187 // the first one should be marked.
188 if (BB->hasAddressTaken())
189 MBB->setHasAddressTaken();
190
191 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
192 // appropriate.
Dan Gohman3f1403f2010-04-20 14:46:25 +0000193 for (BasicBlock::const_iterator I = BB->begin();
194 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
195 if (PN->use_empty()) continue;
Dan Gohman6277eb22009-11-23 17:16:22 +0000196
Rafael Espindola3fa82832011-05-13 15:18:06 +0000197 // Skip empty types
198 if (PN->getType()->isEmptyTy())
199 continue;
200
Dan Gohmanc025c852010-04-20 14:48:02 +0000201 DebugLoc DL = PN->getDebugLoc();
Dan Gohman6277eb22009-11-23 17:16:22 +0000202 unsigned PHIReg = ValueMap[PN];
203 assert(PHIReg && "PHI node does not have an assigned virtual register!");
204
205 SmallVector<EVT, 4> ValueVTs;
Bill Wendling384ceb82013-06-06 00:11:39 +0000206 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000207 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
208 EVT VT = ValueVTs[vti];
Bill Wendling384ceb82013-06-06 00:11:39 +0000209 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000210 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
211 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattner518bb532010-02-09 19:54:29 +0000212 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohman6277eb22009-11-23 17:16:22 +0000213 PHIReg += NumRegisters;
214 }
215 }
216 }
Dan Gohmande4c0a72010-04-14 16:32:56 +0000217
218 // Mark landing pad blocks.
219 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohmanae541aa2010-04-15 04:33:49 +0000220 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohmande4c0a72010-04-14 16:32:56 +0000221 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohman6277eb22009-11-23 17:16:22 +0000222}
223
224/// clear - Clear out all the function-specific state. This returns this
225/// FunctionLoweringInfo to an empty state, ready to be used for a
226/// different function.
227void FunctionLoweringInfo::clear() {
Dan Gohman0e026722010-04-14 17:11:23 +0000228 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
229 "Not all catch info was assigned to a landing pad!");
230
Dan Gohman6277eb22009-11-23 17:16:22 +0000231 MBBMap.clear();
232 ValueMap.clear();
233 StaticAllocaMap.clear();
234#ifndef NDEBUG
235 CatchInfoLost.clear();
236 CatchInfoFound.clear();
237#endif
238 LiveOutRegInfo.clear();
Cameron Zwaricha46cd972011-02-24 10:00:13 +0000239 VisitedBBs.clear();
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000240 ArgDbgValues.clear();
Devang Patel0b48ead2010-08-31 22:22:42 +0000241 ByValArgFrameIndexMap.clear();
Dan Gohman84023e02010-07-10 09:00:22 +0000242 RegFixups.clear();
Dan Gohman6277eb22009-11-23 17:16:22 +0000243}
244
Dan Gohman89496d02010-07-02 00:10:16 +0000245/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000246unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Bill Wendlingd626d332013-06-19 20:32:16 +0000247 return RegInfo->
248 createVirtualRegister(TM.getTargetLowering()->getRegClassFor(VT));
Dan Gohman6277eb22009-11-23 17:16:22 +0000249}
250
Dan Gohman89496d02010-07-02 00:10:16 +0000251/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohman6277eb22009-11-23 17:16:22 +0000252/// the correctly promoted or expanded types. Assign these registers
253/// consecutive vreg numbers and return the first assigned number.
254///
255/// In the case that the given value has struct or array type, this function
256/// will assign registers for each member or element.
257///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000258unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Bill Wendlingd626d332013-06-19 20:32:16 +0000259 const TargetLowering *TLI = TM.getTargetLowering();
260
Dan Gohman6277eb22009-11-23 17:16:22 +0000261 SmallVector<EVT, 4> ValueVTs;
Bill Wendling384ceb82013-06-06 00:11:39 +0000262 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohman6277eb22009-11-23 17:16:22 +0000263
264 unsigned FirstReg = 0;
265 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
266 EVT ValueVT = ValueVTs[Value];
Bill Wendling384ceb82013-06-06 00:11:39 +0000267 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000268
Bill Wendling384ceb82013-06-06 00:11:39 +0000269 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000270 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman89496d02010-07-02 00:10:16 +0000271 unsigned R = CreateReg(RegisterVT);
Dan Gohman6277eb22009-11-23 17:16:22 +0000272 if (!FirstReg) FirstReg = R;
273 }
274 }
275 return FirstReg;
276}
Dan Gohman66336ed2009-11-23 17:42:46 +0000277
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000278/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
279/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
280/// the register's LiveOutInfo is for a smaller bit width, it is extended to
281/// the larger bit width by zero extension. The bit width must be no smaller
282/// than the LiveOutInfo's existing bit width.
283const FunctionLoweringInfo::LiveOutInfo *
284FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
285 if (!LiveOutRegInfo.inBounds(Reg))
286 return NULL;
287
288 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
289 if (!LOI->IsValid)
290 return NULL;
291
Cameron Zwarich33b55472011-02-25 01:10:55 +0000292 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich8fbbdca2011-02-25 01:11:01 +0000293 LOI->NumSignBits = 1;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000294 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
295 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
296 }
297
298 return LOI;
299}
300
301/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
302/// register based on the LiveOutInfo of its operands.
303void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000304 Type *Ty = PN->getType();
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000305 if (!Ty->isIntegerTy() || Ty->isVectorTy())
306 return;
307
Bill Wendlingd626d332013-06-19 20:32:16 +0000308 const TargetLowering *TLI = TM.getTargetLowering();
309
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000310 SmallVector<EVT, 1> ValueVTs;
Bill Wendling384ceb82013-06-06 00:11:39 +0000311 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000312 assert(ValueVTs.size() == 1 &&
313 "PHIs with non-vector integer types should have a single VT.");
314 EVT IntVT = ValueVTs[0];
315
Bill Wendling384ceb82013-06-06 00:11:39 +0000316 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000317 return;
Bill Wendling384ceb82013-06-06 00:11:39 +0000318 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000319 unsigned BitWidth = IntVT.getSizeInBits();
320
321 unsigned DestReg = ValueMap[PN];
322 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
323 return;
324 LiveOutRegInfo.grow(DestReg);
325 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
326
327 Value *V = PN->getIncomingValue(0);
328 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
329 DestLOI.NumSignBits = 1;
330 APInt Zero(BitWidth, 0);
331 DestLOI.KnownZero = Zero;
332 DestLOI.KnownOne = Zero;
333 return;
334 }
335
336 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
337 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
338 DestLOI.NumSignBits = Val.getNumSignBits();
339 DestLOI.KnownZero = ~Val;
340 DestLOI.KnownOne = Val;
341 } else {
342 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
343 "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 = *SrcLOI;
355 }
356
357 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
358 DestLOI.KnownOne.getBitWidth() == BitWidth &&
359 "Masks should have the same bit width as the type.");
360
361 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
362 Value *V = PN->getIncomingValue(i);
363 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
364 DestLOI.NumSignBits = 1;
365 APInt Zero(BitWidth, 0);
366 DestLOI.KnownZero = Zero;
367 DestLOI.KnownOne = Zero;
Eric Christopher471e4222011-06-08 23:55:35 +0000368 return;
Cameron Zwarich8ca814c2011-02-24 10:00:25 +0000369 }
370
371 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
372 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
373 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
374 DestLOI.KnownZero &= ~Val;
375 DestLOI.KnownOne &= Val;
376 continue;
377 }
378
379 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
380 "its CopyToReg node was created.");
381 unsigned SrcReg = ValueMap[V];
382 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
383 DestLOI.IsValid = false;
384 return;
385 }
386 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
387 if (!SrcLOI) {
388 DestLOI.IsValid = false;
389 return;
390 }
391 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
392 DestLOI.KnownZero &= SrcLOI->KnownZero;
393 DestLOI.KnownOne &= SrcLOI->KnownOne;
394 }
395}
396
Devang Patel9aee3352011-09-08 22:59:09 +0000397/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel0b48ead2010-08-31 22:22:42 +0000398/// argument. This overrides previous frame index entry for this argument,
399/// if any.
Devang Patel9aee3352011-09-08 22:59:09 +0000400void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher5b13ed12012-02-24 01:59:01 +0000401 int FI) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000402 ByValArgFrameIndexMap[A] = FI;
403}
Eric Christopher471e4222011-06-08 23:55:35 +0000404
Devang Patel9aee3352011-09-08 22:59:09 +0000405/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel0b48ead2010-08-31 22:22:42 +0000406/// If the argument does not have any assigned frame index then 0 is
407/// returned.
Devang Patel9aee3352011-09-08 22:59:09 +0000408int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher471e4222011-06-08 23:55:35 +0000409 DenseMap<const Argument *, int>::iterator I =
Devang Patel0b48ead2010-08-31 22:22:42 +0000410 ByValArgFrameIndexMap.find(A);
411 if (I != ByValArgFrameIndexMap.end())
412 return I->second;
Eric Christopher0822e012012-02-23 03:39:43 +0000413 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel0b48ead2010-08-31 22:22:42 +0000414 return 0;
415}
416
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000417/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
418/// being passed to this variadic function, and set the MachineModuleInfo's
419/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
420/// reference to _fltused on Windows, which will link in MSVCRT's
421/// floating-point support.
422void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
423 MachineModuleInfo *MMI)
424{
425 FunctionType *FT = cast<FunctionType>(
426 I.getCalledValue()->getType()->getContainedType(0));
427 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
428 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
429 Type* T = I.getArgOperand(i)->getType();
430 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
431 i != e; ++i) {
432 if (i->isFloatingPointTy()) {
433 MMI->setUsesVAFloatArgument(true);
434 return;
435 }
436 }
437 }
438 }
439}
440
Dan Gohman66336ed2009-11-23 17:42:46 +0000441/// AddCatchInfo - Extract the personality and type infos from an eh.selector
442/// call, and add them to the specified machine basic block.
Dan Gohman25208642010-04-14 19:53:31 +0000443void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohman66336ed2009-11-23 17:42:46 +0000444 MachineBasicBlock *MBB) {
445 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greif15184442010-06-25 08:24:59 +0000446 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohman66336ed2009-11-23 17:42:46 +0000447 assert(CE->getOpcode() == Instruction::BitCast &&
448 isa<Function>(CE->getOperand(0)) &&
449 "Personality should be a function");
450 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
451
452 // Gather all the type infos for this landing pad and pass them along to
453 // MachineModuleInfo.
Dan Gohman46510a72010-04-15 01:51:59 +0000454 std::vector<const GlobalVariable *> TyInfo;
Gabor Greife767e6b2010-06-30 13:45:50 +0000455 unsigned N = I.getNumArgOperands();
Dan Gohman66336ed2009-11-23 17:42:46 +0000456
Gabor Greife767e6b2010-06-30 13:45:50 +0000457 for (unsigned i = N - 1; i > 1; --i) {
458 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohman66336ed2009-11-23 17:42:46 +0000459 unsigned FilterLength = CI->getZExtValue();
460 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greife767e6b2010-06-30 13:45:50 +0000461 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohman66336ed2009-11-23 17:42:46 +0000462
463 if (FirstCatch < N) {
464 TyInfo.reserve(N - FirstCatch);
465 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000466 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000467 MMI->addCatchTypeInfo(MBB, TyInfo);
468 TyInfo.clear();
469 }
470
471 if (!FilterLength) {
472 // Cleanup.
473 MMI->addCleanup(MBB);
474 } else {
475 // Filter.
476 TyInfo.reserve(FilterLength - 1);
477 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greife767e6b2010-06-30 13:45:50 +0000478 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000479 MMI->addFilterTypeInfo(MBB, TyInfo);
480 TyInfo.clear();
481 }
482
483 N = i;
484 }
485 }
486
Gabor Greife767e6b2010-06-30 13:45:50 +0000487 if (N > 2) {
488 TyInfo.reserve(N - 2);
489 for (unsigned j = 2; j < N; ++j)
490 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohman66336ed2009-11-23 17:42:46 +0000491 MMI->addCatchTypeInfo(MBB, TyInfo);
492 }
493}
494
Bill Wendling2ac0e6b2011-08-17 21:56:44 +0000495/// AddLandingPadInfo - Extract the exception handling information from the
496/// landingpad instruction and add them to the specified machine module info.
497void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
498 MachineBasicBlock *MBB) {
499 MMI.addPersonality(MBB,
500 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
501
502 if (I.isCleanup())
503 MMI.addCleanup(MBB);
504
505 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
506 // but we need to do it this way because of how the DWARF EH emitter
507 // processes the clauses.
508 for (unsigned i = I.getNumClauses(); i != 0; --i) {
509 Value *Val = I.getClause(i - 1);
510 if (I.isCatch(i - 1)) {
511 MMI.addCatchTypeInfo(MBB,
512 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
513 } else {
514 // Add filters in a list.
515 Constant *CVal = cast<Constant>(Val);
516 SmallVector<const GlobalVariable*, 4> FilterList;
517 for (User::op_iterator
518 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
519 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
520
521 MMI.addFilterTypeInfo(MBB, FilterList);
522 }
523 }
524}