blob: 112889e55c85686f0329f649e57556f748562c15 [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
Jiangning Liuffbc6902014-09-19 05:30:35 +000059static ISD::NodeType getPreferredExtendForValue(const Value *V) {
60 // For the users of the source value being used for compare instruction, if
61 // the number of signed predicate is greater than unsigned predicate, we
62 // prefer to use SIGN_EXTEND.
63 //
64 // With this optimization, we would be able to reduce some redundant sign or
65 // zero extension instruction, and eventually more machine CSE opportunities
66 // can be exposed.
67 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
68 unsigned NumOfSigned = 0, NumOfUnsigned = 0;
69 for (const User *U : V->users()) {
70 if (const auto *CI = dyn_cast<CmpInst>(U)) {
71 NumOfSigned += CI->isSigned();
72 NumOfUnsigned += CI->isUnsigned();
73 }
74 }
75 if (NumOfSigned > NumOfUnsigned)
76 ExtendKind = ISD::SIGN_EXTEND;
77
78 return ExtendKind;
79}
80
Hans Wennborgacb842d2014-03-05 02:43:26 +000081void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
82 SelectionDAG *DAG) {
Dan Gohmana3624b62009-11-23 17:16:22 +000083 Fn = &fn;
84 MF = &mf;
Eric Christopher2ae2de72014-10-09 00:57:31 +000085 TLI = MF->getSubtarget().getTargetLowering();
Dan Gohmana3624b62009-11-23 17:16:22 +000086 RegInfo = &MF->getRegInfo();
87
Dan Gohmand7b5ce32010-07-10 09:00:22 +000088 // Check whether the function can return without sret-demotion.
89 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8db01cb2013-06-06 00:11:39 +000090 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
91 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
Eric Christopher2ae2de72014-10-09 00:57:31 +000092 Fn->isVarArg(), Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000093
Dan Gohmana3624b62009-11-23 17:16:22 +000094 // Initialize the mapping of values to registers. This is only set up for
95 // instruction values that are used outside of the block that defines
96 // them.
Dan Gohman913c9982010-04-15 04:33:49 +000097 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
Dan Gohmana3624b62009-11-23 17:16:22 +000098 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) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000101 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000102 // Static allocas can be folded into the initial stack frame adjustment.
103 if (AI->isStaticAlloca()) {
104 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
105 Type *Ty = AI->getAllocatedType();
106 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
107 unsigned Align =
Eric Christopher2ae2de72014-10-09 00:57:31 +0000108 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
109 AI->getAlignment());
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000110
111 TySize *= CUI->getZExtValue(); // Get total allocated size.
112 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
113
114 StaticAllocaMap[AI] =
115 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
116
117 } else {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000118 unsigned Align = std::max(
119 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
120 AI->getAllocatedType()),
121 AI->getAlignment());
Eric Christopherd9134482014-08-04 21:25:23 +0000122 unsigned StackAlign =
Eric Christopher2ae2de72014-10-09 00:57:31 +0000123 MF->getSubtarget().getFrameLowering()->getStackAlignment();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000124 if (Align <= StackAlign)
125 Align = 0;
126 // Inform the Frame Information that we have variable-sized objects.
127 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
128 }
129 }
130
131 // Look for inline asm that clobbers the SP register.
132 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
133 ImmutableCallSite CS(I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000134 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000135 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
136 std::vector<TargetLowering::AsmOperandInfo> Ops =
137 TLI->ParseConstraints(CS);
138 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
139 TargetLowering::AsmOperandInfo &Op = Ops[I];
140 if (Op.Type == InlineAsm::isClobber) {
141 // Clobbers don't have SDValue operands, hence SDValue().
142 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
Eric Christopher2ae2de72014-10-09 00:57:31 +0000143 std::pair<unsigned, const TargetRegisterClass *> PhysReg =
144 TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
145 Op.ConstraintVT);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000146 if (PhysReg.first == SP)
147 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
148 }
149 }
150 }
151 }
152
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000153 // Look for calls to the @llvm.va_start intrinsic. We can omit some
154 // prologue boilerplate for variadic functions that don't examine their
155 // arguments.
156 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
157 if (II->getIntrinsicID() == Intrinsic::vastart)
158 MF->getFrameInfo()->setHasVAStart(true);
159 }
160
Reid Kleckner16e55412014-08-29 21:42:08 +0000161 // If we have a musttail call in a variadic funciton, we need to ensure we
162 // forward implicit register parameters.
Reid Klecknerdccd0cb2014-08-29 21:42:21 +0000163 if (const auto *CI = dyn_cast<CallInst>(I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000164 if (CI->isMustTailCall() && Fn->isVarArg())
165 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
166 }
167
Dan Gohman1e9362772010-07-16 17:54:27 +0000168 // Mark values used outside their block as exported, by allocating
169 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000170 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000171 if (!isa<AllocaInst>(I) ||
172 !StaticAllocaMap.count(cast<AllocaInst>(I)))
173 InitializeRegForValue(I);
174
Dan Gohman1e9362772010-07-16 17:54:27 +0000175 // Collect llvm.dbg.declare information. This is done now instead of
176 // during the initial isel pass through the IR so that it is done
177 // in a predictable order.
178 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
179 MachineModuleInfo &MMI = MF->getMMI();
Manman Ren983a16c2013-06-28 05:43:10 +0000180 DIVariable DIVar(DI->getVariable());
181 assert((!DIVar || DIVar.isVariable()) &&
182 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman1e9362772010-07-16 17:54:27 +0000183 if (MMI.hasDebugInfo() &&
Manman Ren983a16c2013-06-28 05:43:10 +0000184 DIVar &&
Dan Gohman1e9362772010-07-16 17:54:27 +0000185 !DI->getDebugLoc().isUnknown()) {
186 // Don't handle byval struct arguments or VLAs, for example.
187 // Non-byval arguments are handled here (they refer to the stack
188 // temporary alloca at this point).
189 const Value *Address = DI->getAddress();
190 if (Address) {
191 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
192 Address = BCI->getOperand(0);
193 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
194 DenseMap<const AllocaInst *, int>::iterator SI =
195 StaticAllocaMap.find(AI);
196 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
197 int FI = SI->second;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000198 MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(),
Dan Gohman1e9362772010-07-16 17:54:27 +0000199 FI, DI->getDebugLoc());
200 }
201 }
202 }
203 }
204 }
Jiangning Liuffbc6902014-09-19 05:30:35 +0000205
206 // Decide the preferred extend type for a value.
207 PreferredExtendType[I] = getPreferredExtendForValue(I);
Dan Gohman1e9362772010-07-16 17:54:27 +0000208 }
209
Dan Gohmana3624b62009-11-23 17:16:22 +0000210 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
211 // also creates the initial PHI MachineInstrs, though none of the input
212 // operands are populated.
Dan Gohmanf57117d2010-04-14 16:30:40 +0000213 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohmana3624b62009-11-23 17:16:22 +0000214 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
215 MBBMap[BB] = MBB;
216 MF->push_back(MBB);
217
218 // Transfer the address-taken flag. This is necessary because there could
219 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
220 // the first one should be marked.
221 if (BB->hasAddressTaken())
222 MBB->setHasAddressTaken();
223
224 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
225 // appropriate.
Dan Gohman0f055d32010-04-20 14:46:25 +0000226 for (BasicBlock::const_iterator I = BB->begin();
227 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
228 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000229
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000230 // Skip empty types
231 if (PN->getType()->isEmptyTy())
232 continue;
233
Dan Gohman7b7f0882010-04-20 14:48:02 +0000234 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000235 unsigned PHIReg = ValueMap[PN];
236 assert(PHIReg && "PHI node does not have an assigned virtual register!");
237
238 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000239 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000240 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
241 EVT VT = ValueVTs[vti];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000242 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherfc6de422014-08-05 02:39:49 +0000243 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000244 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000245 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000246 PHIReg += NumRegisters;
247 }
248 }
249 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000250
251 // Mark landing pad blocks.
252 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohman913c9982010-04-15 04:33:49 +0000253 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000254 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmana3624b62009-11-23 17:16:22 +0000255}
256
257/// clear - Clear out all the function-specific state. This returns this
258/// FunctionLoweringInfo to an empty state, ready to be used for a
259/// different function.
260void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000261 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
262 "Not all catch info was assigned to a landing pad!");
263
Dan Gohmana3624b62009-11-23 17:16:22 +0000264 MBBMap.clear();
265 ValueMap.clear();
266 StaticAllocaMap.clear();
267#ifndef NDEBUG
268 CatchInfoLost.clear();
269 CatchInfoFound.clear();
270#endif
271 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000272 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000273 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000274 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000275 RegFixups.clear();
Philip Reames1a1bdb22014-12-02 18:50:36 +0000276 StatepointStackSlots.clear();
Jiangning Liu3b096172014-09-24 03:22:56 +0000277 PreferredExtendType.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000278}
279
Dan Gohman93f59202010-07-02 00:10:16 +0000280/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000281unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000282 return RegInfo->createVirtualRegister(
Eric Christopher2ae2de72014-10-09 00:57:31 +0000283 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000284}
285
Dan Gohman93f59202010-07-02 00:10:16 +0000286/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000287/// the correctly promoted or expanded types. Assign these registers
288/// consecutive vreg numbers and return the first assigned number.
289///
290/// In the case that the given value has struct or array type, this function
291/// will assign registers for each member or element.
292///
Chris Lattner229907c2011-07-18 04:54:35 +0000293unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopher2ae2de72014-10-09 00:57:31 +0000294 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000295
Dan Gohmana3624b62009-11-23 17:16:22 +0000296 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000297 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000298
299 unsigned FirstReg = 0;
300 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
301 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000302 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000303
Bill Wendling8db01cb2013-06-06 00:11:39 +0000304 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000305 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000306 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000307 if (!FirstReg) FirstReg = R;
308 }
309 }
310 return FirstReg;
311}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000312
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000313/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
314/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
315/// the register's LiveOutInfo is for a smaller bit width, it is extended to
316/// the larger bit width by zero extension. The bit width must be no smaller
317/// than the LiveOutInfo's existing bit width.
318const FunctionLoweringInfo::LiveOutInfo *
319FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
320 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000321 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000322
323 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
324 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000325 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000326
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000327 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000328 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000329 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
330 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
331 }
332
333 return LOI;
334}
335
336/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
337/// register based on the LiveOutInfo of its operands.
338void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000339 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000340 if (!Ty->isIntegerTy() || Ty->isVectorTy())
341 return;
342
343 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000344 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000345 assert(ValueVTs.size() == 1 &&
346 "PHIs with non-vector integer types should have a single VT.");
347 EVT IntVT = ValueVTs[0];
348
Bill Wendling8db01cb2013-06-06 00:11:39 +0000349 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000350 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000351 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000352 unsigned BitWidth = IntVT.getSizeInBits();
353
354 unsigned DestReg = ValueMap[PN];
355 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
356 return;
357 LiveOutRegInfo.grow(DestReg);
358 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
359
360 Value *V = PN->getIncomingValue(0);
361 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
362 DestLOI.NumSignBits = 1;
363 APInt Zero(BitWidth, 0);
364 DestLOI.KnownZero = Zero;
365 DestLOI.KnownOne = Zero;
366 return;
367 }
368
369 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
370 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
371 DestLOI.NumSignBits = Val.getNumSignBits();
372 DestLOI.KnownZero = ~Val;
373 DestLOI.KnownOne = Val;
374 } else {
375 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
376 "CopyToReg node was created.");
377 unsigned SrcReg = ValueMap[V];
378 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
379 DestLOI.IsValid = false;
380 return;
381 }
382 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
383 if (!SrcLOI) {
384 DestLOI.IsValid = false;
385 return;
386 }
387 DestLOI = *SrcLOI;
388 }
389
390 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
391 DestLOI.KnownOne.getBitWidth() == BitWidth &&
392 "Masks should have the same bit width as the type.");
393
394 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
395 Value *V = PN->getIncomingValue(i);
396 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
397 DestLOI.NumSignBits = 1;
398 APInt Zero(BitWidth, 0);
399 DestLOI.KnownZero = Zero;
400 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000401 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000402 }
403
404 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
405 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
406 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
407 DestLOI.KnownZero &= ~Val;
408 DestLOI.KnownOne &= Val;
409 continue;
410 }
411
412 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
413 "its CopyToReg node was created.");
414 unsigned SrcReg = ValueMap[V];
415 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
416 DestLOI.IsValid = false;
417 return;
418 }
419 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
420 if (!SrcLOI) {
421 DestLOI.IsValid = false;
422 return;
423 }
424 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
425 DestLOI.KnownZero &= SrcLOI->KnownZero;
426 DestLOI.KnownOne &= SrcLOI->KnownOne;
427 }
428}
429
Devang Patel9d904e12011-09-08 22:59:09 +0000430/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000431/// argument. This overrides previous frame index entry for this argument,
432/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000433void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000434 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000435 ByValArgFrameIndexMap[A] = FI;
436}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000437
Devang Patel9d904e12011-09-08 22:59:09 +0000438/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000439/// If the argument does not have any assigned frame index then 0 is
440/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000441int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000442 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000443 ByValArgFrameIndexMap.find(A);
444 if (I != ByValArgFrameIndexMap.end())
445 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000446 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000447 return 0;
448}
449
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000450/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
451/// being passed to this variadic function, and set the MachineModuleInfo's
452/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
453/// reference to _fltused on Windows, which will link in MSVCRT's
454/// floating-point support.
455void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
456 MachineModuleInfo *MMI)
457{
458 FunctionType *FT = cast<FunctionType>(
459 I.getCalledValue()->getType()->getContainedType(0));
460 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
461 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
462 Type* T = I.getArgOperand(i)->getType();
463 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
464 i != e; ++i) {
465 if (i->isFloatingPointTy()) {
466 MMI->setUsesVAFloatArgument(true);
467 return;
468 }
469 }
470 }
471 }
472}
473
Bill Wendling247fd3b2011-08-17 21:56:44 +0000474/// AddLandingPadInfo - Extract the exception handling information from the
475/// landingpad instruction and add them to the specified machine module info.
476void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
477 MachineBasicBlock *MBB) {
478 MMI.addPersonality(MBB,
479 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
480
481 if (I.isCleanup())
482 MMI.addCleanup(MBB);
483
484 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
485 // but we need to do it this way because of how the DWARF EH emitter
486 // processes the clauses.
487 for (unsigned i = I.getNumClauses(); i != 0; --i) {
488 Value *Val = I.getClause(i - 1);
489 if (I.isCatch(i - 1)) {
490 MMI.addCatchTypeInfo(MBB,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000491 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
Bill Wendling247fd3b2011-08-17 21:56:44 +0000492 } else {
493 // Add filters in a list.
494 Constant *CVal = cast<Constant>(Val);
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000495 SmallVector<const GlobalValue*, 4> FilterList;
Bill Wendling247fd3b2011-08-17 21:56:44 +0000496 for (User::op_iterator
497 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000498 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
Bill Wendling247fd3b2011-08-17 21:56:44 +0000499
500 MMI.addFilterTypeInfo(MBB, FilterList);
501 }
502 }
503}