blob: 34e44ccf0c72444f93fb4fc3f8295811d4998d4d [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();
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Reid Klecknerdfbed592014-01-31 23:45:12 +000079 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
80 // Don't fold inalloca allocas or other dynamic allocas into the initial
81 // stack frame allocation, even if they are in the entry block.
82 if (!AI->isStaticAlloca())
83 continue;
84
Dan Gohman913c9982010-04-15 04:33:49 +000085 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Chris Lattner229907c2011-07-18 04:54:35 +000086 Type *Ty = AI->getAllocatedType();
Bill Wendling8db01cb2013-06-06 00:11:39 +000087 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmana3624b62009-11-23 17:16:22 +000088 unsigned Align =
Bill Wendling8db01cb2013-06-06 00:11:39 +000089 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmana3624b62009-11-23 17:16:22 +000090 AI->getAlignment());
91
92 TySize *= CUI->getZExtValue(); // Get total allocated size.
93 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Bill Wendling0ff1ef62010-07-27 01:55:19 +000094
Dan Gohmana3624b62009-11-23 17:16:22 +000095 StaticAllocaMap[AI] =
Josh Magee22b8ba22013-12-19 03:17:11 +000096 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
Dan Gohmana3624b62009-11-23 17:16:22 +000097 }
Reid Klecknerdfbed592014-01-31 23:45:12 +000098 }
Dan Gohmana3624b62009-11-23 17:16:22 +000099
100 for (; BB != EB; ++BB)
Eric Christopher219d51d2012-02-24 01:59:01 +0000101 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
102 I != E; ++I) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000103 // Look for dynamic allocas.
104 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
105 if (!AI->isStaticAlloca()) {
106 unsigned Align = std::max(
107 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
108 AI->getAllocatedType()),
109 AI->getAlignment());
Eric Christopherd9134482014-08-04 21:25:23 +0000110 unsigned StackAlign =
111 TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000112 if (Align <= StackAlign)
113 Align = 0;
114 // Inform the Frame Information that we have variable-sized objects.
115 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
116 }
117 }
118
119 // Look for inline asm that clobbers the SP register.
120 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
121 ImmutableCallSite CS(I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000122 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000123 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
124 std::vector<TargetLowering::AsmOperandInfo> Ops =
125 TLI->ParseConstraints(CS);
126 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
127 TargetLowering::AsmOperandInfo &Op = Ops[I];
128 if (Op.Type == InlineAsm::isClobber) {
129 // Clobbers don't have SDValue operands, hence SDValue().
130 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
131 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
132 TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
133 Op.ConstraintVT);
134 if (PhysReg.first == SP)
135 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
136 }
137 }
138 }
139 }
140
Dan Gohman1e9362772010-07-16 17:54:27 +0000141 // Mark values used outside their block as exported, by allocating
142 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000143 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000144 if (!isa<AllocaInst>(I) ||
145 !StaticAllocaMap.count(cast<AllocaInst>(I)))
146 InitializeRegForValue(I);
147
Dan Gohman1e9362772010-07-16 17:54:27 +0000148 // Collect llvm.dbg.declare information. This is done now instead of
149 // during the initial isel pass through the IR so that it is done
150 // in a predictable order.
151 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
152 MachineModuleInfo &MMI = MF->getMMI();
Manman Ren983a16c2013-06-28 05:43:10 +0000153 DIVariable DIVar(DI->getVariable());
154 assert((!DIVar || DIVar.isVariable()) &&
155 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman1e9362772010-07-16 17:54:27 +0000156 if (MMI.hasDebugInfo() &&
Manman Ren983a16c2013-06-28 05:43:10 +0000157 DIVar &&
Dan Gohman1e9362772010-07-16 17:54:27 +0000158 !DI->getDebugLoc().isUnknown()) {
159 // Don't handle byval struct arguments or VLAs, for example.
160 // Non-byval arguments are handled here (they refer to the stack
161 // temporary alloca at this point).
162 const Value *Address = DI->getAddress();
163 if (Address) {
164 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
165 Address = BCI->getOperand(0);
166 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
167 DenseMap<const AllocaInst *, int>::iterator SI =
168 StaticAllocaMap.find(AI);
169 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
170 int FI = SI->second;
171 MMI.setVariableDbgInfo(DI->getVariable(),
172 FI, DI->getDebugLoc());
173 }
174 }
175 }
176 }
177 }
178 }
179
Dan Gohmana3624b62009-11-23 17:16:22 +0000180 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
181 // also creates the initial PHI MachineInstrs, though none of the input
182 // operands are populated.
Dan Gohmanf57117d2010-04-14 16:30:40 +0000183 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohmana3624b62009-11-23 17:16:22 +0000184 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
185 MBBMap[BB] = MBB;
186 MF->push_back(MBB);
187
188 // Transfer the address-taken flag. This is necessary because there could
189 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
190 // the first one should be marked.
191 if (BB->hasAddressTaken())
192 MBB->setHasAddressTaken();
193
194 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
195 // appropriate.
Dan Gohman0f055d32010-04-20 14:46:25 +0000196 for (BasicBlock::const_iterator I = BB->begin();
197 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
198 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000199
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000200 // Skip empty types
201 if (PN->getType()->isEmptyTy())
202 continue;
203
Dan Gohman7b7f0882010-04-20 14:48:02 +0000204 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000205 unsigned PHIReg = ValueMap[PN];
206 assert(PHIReg && "PHI node does not have an assigned virtual register!");
207
208 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000209 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000210 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
211 EVT VT = ValueVTs[vti];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000212 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherd9134482014-08-04 21:25:23 +0000213 const TargetInstrInfo *TII =
214 MF->getTarget().getSubtargetImpl()->getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000215 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000216 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000217 PHIReg += NumRegisters;
218 }
219 }
220 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000221
222 // Mark landing pad blocks.
223 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohman913c9982010-04-15 04:33:49 +0000224 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000225 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmana3624b62009-11-23 17:16:22 +0000226}
227
228/// clear - Clear out all the function-specific state. This returns this
229/// FunctionLoweringInfo to an empty state, ready to be used for a
230/// different function.
231void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000232 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
233 "Not all catch info was assigned to a landing pad!");
234
Dan Gohmana3624b62009-11-23 17:16:22 +0000235 MBBMap.clear();
236 ValueMap.clear();
237 StaticAllocaMap.clear();
238#ifndef NDEBUG
239 CatchInfoLost.clear();
240 CatchInfoFound.clear();
241#endif
242 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000243 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000244 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000245 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000246 RegFixups.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000247}
248
Dan Gohman93f59202010-07-02 00:10:16 +0000249/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000250unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000251 return RegInfo->createVirtualRegister(
252 TM.getSubtargetImpl()->getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000253}
254
Dan Gohman93f59202010-07-02 00:10:16 +0000255/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000256/// the correctly promoted or expanded types. Assign these registers
257/// consecutive vreg numbers and return the first assigned number.
258///
259/// In the case that the given value has struct or array type, this function
260/// will assign registers for each member or element.
261///
Chris Lattner229907c2011-07-18 04:54:35 +0000262unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopherd9134482014-08-04 21:25:23 +0000263 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000264
Dan Gohmana3624b62009-11-23 17:16:22 +0000265 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000266 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000267
268 unsigned FirstReg = 0;
269 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
270 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000271 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000272
Bill Wendling8db01cb2013-06-06 00:11:39 +0000273 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000274 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000275 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000276 if (!FirstReg) FirstReg = R;
277 }
278 }
279 return FirstReg;
280}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000281
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000282/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
283/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
284/// the register's LiveOutInfo is for a smaller bit width, it is extended to
285/// the larger bit width by zero extension. The bit width must be no smaller
286/// than the LiveOutInfo's existing bit width.
287const FunctionLoweringInfo::LiveOutInfo *
288FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
289 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000290 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000291
292 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
293 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000294 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000295
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000296 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000297 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000298 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
299 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
300 }
301
302 return LOI;
303}
304
305/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
306/// register based on the LiveOutInfo of its operands.
307void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000308 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000309 if (!Ty->isIntegerTy() || Ty->isVectorTy())
310 return;
311
Eric Christopherd9134482014-08-04 21:25:23 +0000312 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000313
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000314 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000315 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000316 assert(ValueVTs.size() == 1 &&
317 "PHIs with non-vector integer types should have a single VT.");
318 EVT IntVT = ValueVTs[0];
319
Bill Wendling8db01cb2013-06-06 00:11:39 +0000320 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000321 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000322 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000323 unsigned BitWidth = IntVT.getSizeInBits();
324
325 unsigned DestReg = ValueMap[PN];
326 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
327 return;
328 LiveOutRegInfo.grow(DestReg);
329 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
330
331 Value *V = PN->getIncomingValue(0);
332 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
333 DestLOI.NumSignBits = 1;
334 APInt Zero(BitWidth, 0);
335 DestLOI.KnownZero = Zero;
336 DestLOI.KnownOne = Zero;
337 return;
338 }
339
340 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
341 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
342 DestLOI.NumSignBits = Val.getNumSignBits();
343 DestLOI.KnownZero = ~Val;
344 DestLOI.KnownOne = Val;
345 } else {
346 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
347 "CopyToReg node was created.");
348 unsigned SrcReg = ValueMap[V];
349 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
350 DestLOI.IsValid = false;
351 return;
352 }
353 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
354 if (!SrcLOI) {
355 DestLOI.IsValid = false;
356 return;
357 }
358 DestLOI = *SrcLOI;
359 }
360
361 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
362 DestLOI.KnownOne.getBitWidth() == BitWidth &&
363 "Masks should have the same bit width as the type.");
364
365 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
366 Value *V = PN->getIncomingValue(i);
367 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
368 DestLOI.NumSignBits = 1;
369 APInt Zero(BitWidth, 0);
370 DestLOI.KnownZero = Zero;
371 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000372 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000373 }
374
375 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
376 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
377 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
378 DestLOI.KnownZero &= ~Val;
379 DestLOI.KnownOne &= Val;
380 continue;
381 }
382
383 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
384 "its CopyToReg node was created.");
385 unsigned SrcReg = ValueMap[V];
386 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
387 DestLOI.IsValid = false;
388 return;
389 }
390 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
391 if (!SrcLOI) {
392 DestLOI.IsValid = false;
393 return;
394 }
395 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
396 DestLOI.KnownZero &= SrcLOI->KnownZero;
397 DestLOI.KnownOne &= SrcLOI->KnownOne;
398 }
399}
400
Devang Patel9d904e12011-09-08 22:59:09 +0000401/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000402/// argument. This overrides previous frame index entry for this argument,
403/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000404void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000405 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000406 ByValArgFrameIndexMap[A] = FI;
407}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000408
Devang Patel9d904e12011-09-08 22:59:09 +0000409/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000410/// If the argument does not have any assigned frame index then 0 is
411/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000412int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000413 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000414 ByValArgFrameIndexMap.find(A);
415 if (I != ByValArgFrameIndexMap.end())
416 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000417 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000418 return 0;
419}
420
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000421/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
422/// being passed to this variadic function, and set the MachineModuleInfo's
423/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
424/// reference to _fltused on Windows, which will link in MSVCRT's
425/// floating-point support.
426void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
427 MachineModuleInfo *MMI)
428{
429 FunctionType *FT = cast<FunctionType>(
430 I.getCalledValue()->getType()->getContainedType(0));
431 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
432 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
433 Type* T = I.getArgOperand(i)->getType();
434 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
435 i != e; ++i) {
436 if (i->isFloatingPointTy()) {
437 MMI->setUsesVAFloatArgument(true);
438 return;
439 }
440 }
441 }
442 }
443}
444
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000445/// AddCatchInfo - Extract the personality and type infos from an eh.selector
446/// call, and add them to the specified machine basic block.
Dan Gohman7deb4472010-04-14 19:53:31 +0000447void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000448 MachineBasicBlock *MBB) {
449 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greife4eed702010-06-25 08:24:59 +0000450 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000451 assert(CE->getOpcode() == Instruction::BitCast &&
452 isa<Function>(CE->getOperand(0)) &&
453 "Personality should be a function");
454 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
455
456 // Gather all the type infos for this landing pad and pass them along to
457 // MachineModuleInfo.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000458 std::vector<const GlobalVariable *> TyInfo;
Gabor Greif647d9c92010-06-30 13:45:50 +0000459 unsigned N = I.getNumArgOperands();
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000460
Gabor Greif647d9c92010-06-30 13:45:50 +0000461 for (unsigned i = N - 1; i > 1; --i) {
462 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000463 unsigned FilterLength = CI->getZExtValue();
464 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greif647d9c92010-06-30 13:45:50 +0000465 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000466
467 if (FirstCatch < N) {
468 TyInfo.reserve(N - FirstCatch);
469 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000470 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000471 MMI->addCatchTypeInfo(MBB, TyInfo);
472 TyInfo.clear();
473 }
474
475 if (!FilterLength) {
476 // Cleanup.
477 MMI->addCleanup(MBB);
478 } else {
479 // Filter.
480 TyInfo.reserve(FilterLength - 1);
481 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000482 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000483 MMI->addFilterTypeInfo(MBB, TyInfo);
484 TyInfo.clear();
485 }
486
487 N = i;
488 }
489 }
490
Gabor Greif647d9c92010-06-30 13:45:50 +0000491 if (N > 2) {
492 TyInfo.reserve(N - 2);
493 for (unsigned j = 2; j < N; ++j)
494 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000495 MMI->addCatchTypeInfo(MBB, TyInfo);
496 }
497}
498
Bill Wendling247fd3b2011-08-17 21:56:44 +0000499/// AddLandingPadInfo - Extract the exception handling information from the
500/// landingpad instruction and add them to the specified machine module info.
501void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
502 MachineBasicBlock *MBB) {
503 MMI.addPersonality(MBB,
504 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
505
506 if (I.isCleanup())
507 MMI.addCleanup(MBB);
508
509 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
510 // but we need to do it this way because of how the DWARF EH emitter
511 // processes the clauses.
512 for (unsigned i = I.getNumClauses(); i != 0; --i) {
513 Value *Val = I.getClause(i - 1);
514 if (I.isCatch(i - 1)) {
515 MMI.addCatchTypeInfo(MBB,
516 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
517 } else {
518 // Add filters in a list.
519 Constant *CVal = cast<Constant>(Val);
520 SmallVector<const GlobalVariable*, 4> FilterList;
521 for (User::op_iterator
522 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
523 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
524
525 MMI.addFilterTypeInfo(MBB, FilterList);
526 }
527 }
528}