blob: 1f58d7c301f3ba5c4489578179f914357b96fb9c [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
Dan Gohmane7846162010-07-07 16:01:37 +000015#include "llvm/CodeGen/FunctionLoweringInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/PostOrderIterator.h"
17#include "llvm/CodeGen/Analysis.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000024#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Module.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/MathExtras.h"
Hans Wennborgacb842d2014-03-05 02:43:26 +000034#include "llvm/Target/TargetFrameLowering.h"
Chandler Carruth92051402014-03-05 10:30:38 +000035#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetLowering.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000040#include <algorithm>
41using namespace llvm;
42
Chandler Carruth1b9dde02014-04-22 02:02:50 +000043#define DEBUG_TYPE "function-lowering-info"
44
Dan Gohmana3624b62009-11-23 17:16:22 +000045/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
46/// PHI nodes or outside of the basic block that defines it, or used by a
47/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohman913c9982010-04-15 04:33:49 +000048static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohman7c845e42010-04-20 14:50:13 +000049 if (I->use_empty()) return false;
Dan Gohmana3624b62009-11-23 17:16:22 +000050 if (isa<PHINode>(I)) return true;
Dan Gohman913c9982010-04-15 04:33:49 +000051 const BasicBlock *BB = I->getParent();
Chandler Carruthcdf47882014-03-09 03:16:01 +000052 for (const User *U : I->users())
Gabor Greif52617fc2010-07-09 16:08:33 +000053 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohmana3624b62009-11-23 17:16:22 +000054 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +000055
Dan Gohmana3624b62009-11-23 17:16:22 +000056 return false;
57}
58
Hans Wennborgacb842d2014-03-05 02:43:26 +000059void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
60 SelectionDAG *DAG) {
Eric Christopherd9134482014-08-04 21:25:23 +000061 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +000062
Dan Gohmana3624b62009-11-23 17:16:22 +000063 Fn = &fn;
64 MF = &mf;
65 RegInfo = &MF->getRegInfo();
66
Dan Gohmand7b5ce32010-07-10 09:00:22 +000067 // Check whether the function can return without sret-demotion.
68 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8db01cb2013-06-06 00:11:39 +000069 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
70 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
71 Fn->isVarArg(),
72 Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000073
Dan Gohmana3624b62009-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 Gohman913c9982010-04-15 04:33:49 +000077 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
Dan Gohmana3624b62009-11-23 17:16:22 +000078 for (; BB != EB; ++BB)
Eric Christopher219d51d2012-02-24 01:59:01 +000079 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
80 I != E; ++I) {
Hans Wennborgacb842d2014-03-05 02:43:26 +000081 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +000082 // Static allocas can be folded into the initial stack frame adjustment.
83 if (AI->isStaticAlloca()) {
84 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
85 Type *Ty = AI->getAllocatedType();
86 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
87 unsigned Align =
88 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
89 AI->getAlignment());
90
91 TySize *= CUI->getZExtValue(); // Get total allocated size.
92 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
93
94 StaticAllocaMap[AI] =
95 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
96
97 } else {
Hans Wennborgacb842d2014-03-05 02:43:26 +000098 unsigned Align = std::max(
99 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
100 AI->getAllocatedType()),
101 AI->getAlignment());
Eric Christopherd9134482014-08-04 21:25:23 +0000102 unsigned StackAlign =
103 TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000104 if (Align <= StackAlign)
105 Align = 0;
106 // Inform the Frame Information that we have variable-sized objects.
107 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
108 }
109 }
110
111 // Look for inline asm that clobbers the SP register.
112 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
113 ImmutableCallSite CS(I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000114 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000115 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
116 std::vector<TargetLowering::AsmOperandInfo> Ops =
117 TLI->ParseConstraints(CS);
118 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
119 TargetLowering::AsmOperandInfo &Op = Ops[I];
120 if (Op.Type == InlineAsm::isClobber) {
121 // Clobbers don't have SDValue operands, hence SDValue().
122 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
123 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
124 TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
125 Op.ConstraintVT);
126 if (PhysReg.first == SP)
127 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
128 }
129 }
130 }
131 }
132
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000133 // Look for calls to the @llvm.va_start intrinsic. We can omit some
134 // prologue boilerplate for variadic functions that don't examine their
135 // arguments.
136 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
137 if (II->getIntrinsicID() == Intrinsic::vastart)
138 MF->getFrameInfo()->setHasVAStart(true);
139 }
140
Reid Kleckner16e55412014-08-29 21:42:08 +0000141 // If we have a musttail call in a variadic funciton, we need to ensure we
142 // forward implicit register parameters.
Reid Klecknerdccd0cb2014-08-29 21:42:21 +0000143 if (const auto *CI = dyn_cast<CallInst>(I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000144 if (CI->isMustTailCall() && Fn->isVarArg())
145 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
146 }
147
Dan Gohman1e9362772010-07-16 17:54:27 +0000148 // Mark values used outside their block as exported, by allocating
149 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000150 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000151 if (!isa<AllocaInst>(I) ||
152 !StaticAllocaMap.count(cast<AllocaInst>(I)))
153 InitializeRegForValue(I);
154
Dan Gohman1e9362772010-07-16 17:54:27 +0000155 // Collect llvm.dbg.declare information. This is done now instead of
156 // during the initial isel pass through the IR so that it is done
157 // in a predictable order.
158 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
159 MachineModuleInfo &MMI = MF->getMMI();
Manman Ren983a16c2013-06-28 05:43:10 +0000160 DIVariable DIVar(DI->getVariable());
161 assert((!DIVar || DIVar.isVariable()) &&
162 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman1e9362772010-07-16 17:54:27 +0000163 if (MMI.hasDebugInfo() &&
Manman Ren983a16c2013-06-28 05:43:10 +0000164 DIVar &&
Dan Gohman1e9362772010-07-16 17:54:27 +0000165 !DI->getDebugLoc().isUnknown()) {
166 // Don't handle byval struct arguments or VLAs, for example.
167 // Non-byval arguments are handled here (they refer to the stack
168 // temporary alloca at this point).
169 const Value *Address = DI->getAddress();
170 if (Address) {
171 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
172 Address = BCI->getOperand(0);
173 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
174 DenseMap<const AllocaInst *, int>::iterator SI =
175 StaticAllocaMap.find(AI);
176 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
177 int FI = SI->second;
178 MMI.setVariableDbgInfo(DI->getVariable(),
179 FI, DI->getDebugLoc());
180 }
181 }
182 }
183 }
184 }
185 }
186
Dan Gohmana3624b62009-11-23 17:16:22 +0000187 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
188 // also creates the initial PHI MachineInstrs, though none of the input
189 // operands are populated.
Dan Gohmanf57117d2010-04-14 16:30:40 +0000190 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohmana3624b62009-11-23 17:16:22 +0000191 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
192 MBBMap[BB] = MBB;
193 MF->push_back(MBB);
194
195 // Transfer the address-taken flag. This is necessary because there could
196 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
197 // the first one should be marked.
198 if (BB->hasAddressTaken())
199 MBB->setHasAddressTaken();
200
201 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
202 // appropriate.
Dan Gohman0f055d32010-04-20 14:46:25 +0000203 for (BasicBlock::const_iterator I = BB->begin();
204 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
205 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000206
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000207 // Skip empty types
208 if (PN->getType()->isEmptyTy())
209 continue;
210
Dan Gohman7b7f0882010-04-20 14:48:02 +0000211 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000212 unsigned PHIReg = ValueMap[PN];
213 assert(PHIReg && "PHI node does not have an assigned virtual register!");
214
215 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000216 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000217 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
218 EVT VT = ValueVTs[vti];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000219 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherfc6de422014-08-05 02:39:49 +0000220 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000221 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000222 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000223 PHIReg += NumRegisters;
224 }
225 }
226 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000227
228 // Mark landing pad blocks.
229 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohman913c9982010-04-15 04:33:49 +0000230 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000231 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmana3624b62009-11-23 17:16:22 +0000232}
233
234/// clear - Clear out all the function-specific state. This returns this
235/// FunctionLoweringInfo to an empty state, ready to be used for a
236/// different function.
237void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000238 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
239 "Not all catch info was assigned to a landing pad!");
240
Dan Gohmana3624b62009-11-23 17:16:22 +0000241 MBBMap.clear();
242 ValueMap.clear();
243 StaticAllocaMap.clear();
244#ifndef NDEBUG
245 CatchInfoLost.clear();
246 CatchInfoFound.clear();
247#endif
248 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000249 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000250 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000251 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000252 RegFixups.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000253}
254
Dan Gohman93f59202010-07-02 00:10:16 +0000255/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000256unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000257 return RegInfo->createVirtualRegister(
258 TM.getSubtargetImpl()->getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000259}
260
Dan Gohman93f59202010-07-02 00:10:16 +0000261/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000262/// the correctly promoted or expanded types. Assign these registers
263/// consecutive vreg numbers and return the first assigned number.
264///
265/// In the case that the given value has struct or array type, this function
266/// will assign registers for each member or element.
267///
Chris Lattner229907c2011-07-18 04:54:35 +0000268unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopherd9134482014-08-04 21:25:23 +0000269 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000270
Dan Gohmana3624b62009-11-23 17:16:22 +0000271 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000272 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000273
274 unsigned FirstReg = 0;
275 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
276 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000277 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000278
Bill Wendling8db01cb2013-06-06 00:11:39 +0000279 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000280 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000281 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000282 if (!FirstReg) FirstReg = R;
283 }
284 }
285 return FirstReg;
286}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000287
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000288/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
289/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
290/// the register's LiveOutInfo is for a smaller bit width, it is extended to
291/// the larger bit width by zero extension. The bit width must be no smaller
292/// than the LiveOutInfo's existing bit width.
293const FunctionLoweringInfo::LiveOutInfo *
294FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
295 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000296 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000297
298 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
299 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000300 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000301
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000302 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000303 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000304 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
305 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
306 }
307
308 return LOI;
309}
310
311/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
312/// register based on the LiveOutInfo of its operands.
313void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000314 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000315 if (!Ty->isIntegerTy() || Ty->isVectorTy())
316 return;
317
Eric Christopherd9134482014-08-04 21:25:23 +0000318 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000319
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000320 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000321 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000322 assert(ValueVTs.size() == 1 &&
323 "PHIs with non-vector integer types should have a single VT.");
324 EVT IntVT = ValueVTs[0];
325
Bill Wendling8db01cb2013-06-06 00:11:39 +0000326 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000327 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000328 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000329 unsigned BitWidth = IntVT.getSizeInBits();
330
331 unsigned DestReg = ValueMap[PN];
332 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
333 return;
334 LiveOutRegInfo.grow(DestReg);
335 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
336
337 Value *V = PN->getIncomingValue(0);
338 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
339 DestLOI.NumSignBits = 1;
340 APInt Zero(BitWidth, 0);
341 DestLOI.KnownZero = Zero;
342 DestLOI.KnownOne = Zero;
343 return;
344 }
345
346 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
347 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
348 DestLOI.NumSignBits = Val.getNumSignBits();
349 DestLOI.KnownZero = ~Val;
350 DestLOI.KnownOne = Val;
351 } else {
352 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
353 "CopyToReg node was created.");
354 unsigned SrcReg = ValueMap[V];
355 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
356 DestLOI.IsValid = false;
357 return;
358 }
359 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
360 if (!SrcLOI) {
361 DestLOI.IsValid = false;
362 return;
363 }
364 DestLOI = *SrcLOI;
365 }
366
367 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
368 DestLOI.KnownOne.getBitWidth() == BitWidth &&
369 "Masks should have the same bit width as the type.");
370
371 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
372 Value *V = PN->getIncomingValue(i);
373 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
374 DestLOI.NumSignBits = 1;
375 APInt Zero(BitWidth, 0);
376 DestLOI.KnownZero = Zero;
377 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000378 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000379 }
380
381 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
382 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
383 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
384 DestLOI.KnownZero &= ~Val;
385 DestLOI.KnownOne &= Val;
386 continue;
387 }
388
389 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
390 "its CopyToReg node was created.");
391 unsigned SrcReg = ValueMap[V];
392 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
393 DestLOI.IsValid = false;
394 return;
395 }
396 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
397 if (!SrcLOI) {
398 DestLOI.IsValid = false;
399 return;
400 }
401 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
402 DestLOI.KnownZero &= SrcLOI->KnownZero;
403 DestLOI.KnownOne &= SrcLOI->KnownOne;
404 }
405}
406
Devang Patel9d904e12011-09-08 22:59:09 +0000407/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000408/// argument. This overrides previous frame index entry for this argument,
409/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000410void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000411 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000412 ByValArgFrameIndexMap[A] = FI;
413}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000414
Devang Patel9d904e12011-09-08 22:59:09 +0000415/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000416/// If the argument does not have any assigned frame index then 0 is
417/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000418int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000419 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000420 ByValArgFrameIndexMap.find(A);
421 if (I != ByValArgFrameIndexMap.end())
422 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000423 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000424 return 0;
425}
426
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000427/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
428/// being passed to this variadic function, and set the MachineModuleInfo's
429/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
430/// reference to _fltused on Windows, which will link in MSVCRT's
431/// floating-point support.
432void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
433 MachineModuleInfo *MMI)
434{
435 FunctionType *FT = cast<FunctionType>(
436 I.getCalledValue()->getType()->getContainedType(0));
437 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
438 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
439 Type* T = I.getArgOperand(i)->getType();
440 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
441 i != e; ++i) {
442 if (i->isFloatingPointTy()) {
443 MMI->setUsesVAFloatArgument(true);
444 return;
445 }
446 }
447 }
448 }
449}
450
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000451/// AddCatchInfo - Extract the personality and type infos from an eh.selector
452/// call, and add them to the specified machine basic block.
Dan Gohman7deb4472010-04-14 19:53:31 +0000453void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000454 MachineBasicBlock *MBB) {
455 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greife4eed702010-06-25 08:24:59 +0000456 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000457 assert(CE->getOpcode() == Instruction::BitCast &&
458 isa<Function>(CE->getOperand(0)) &&
459 "Personality should be a function");
460 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
461
462 // Gather all the type infos for this landing pad and pass them along to
463 // MachineModuleInfo.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000464 std::vector<const GlobalVariable *> TyInfo;
Gabor Greif647d9c92010-06-30 13:45:50 +0000465 unsigned N = I.getNumArgOperands();
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000466
Gabor Greif647d9c92010-06-30 13:45:50 +0000467 for (unsigned i = N - 1; i > 1; --i) {
468 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000469 unsigned FilterLength = CI->getZExtValue();
470 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greif647d9c92010-06-30 13:45:50 +0000471 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000472
473 if (FirstCatch < N) {
474 TyInfo.reserve(N - FirstCatch);
475 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000476 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000477 MMI->addCatchTypeInfo(MBB, TyInfo);
478 TyInfo.clear();
479 }
480
481 if (!FilterLength) {
482 // Cleanup.
483 MMI->addCleanup(MBB);
484 } else {
485 // Filter.
486 TyInfo.reserve(FilterLength - 1);
487 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000488 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000489 MMI->addFilterTypeInfo(MBB, TyInfo);
490 TyInfo.clear();
491 }
492
493 N = i;
494 }
495 }
496
Gabor Greif647d9c92010-06-30 13:45:50 +0000497 if (N > 2) {
498 TyInfo.reserve(N - 2);
499 for (unsigned j = 2; j < N; ++j)
500 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000501 MMI->addCatchTypeInfo(MBB, TyInfo);
502 }
503}
504
Bill Wendling247fd3b2011-08-17 21:56:44 +0000505/// AddLandingPadInfo - Extract the exception handling information from the
506/// landingpad instruction and add them to the specified machine module info.
507void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
508 MachineBasicBlock *MBB) {
509 MMI.addPersonality(MBB,
510 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
511
512 if (I.isCleanup())
513 MMI.addCleanup(MBB);
514
515 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
516 // but we need to do it this way because of how the DWARF EH emitter
517 // processes the clauses.
518 for (unsigned i = I.getNumClauses(); i != 0; --i) {
519 Value *Val = I.getClause(i - 1);
520 if (I.isCatch(i - 1)) {
521 MMI.addCatchTypeInfo(MBB,
522 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
523 } else {
524 // Add filters in a list.
525 Constant *CVal = cast<Constant>(Val);
526 SmallVector<const GlobalVariable*, 4> FilterList;
527 for (User::op_iterator
528 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
529 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
530
531 MMI.addFilterTypeInfo(MBB, FilterList);
532 }
533 }
534}