blob: c06612dcfa244db8b68ab33c8d348e46df66891a [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) {
Eric Christopherd9134482014-08-04 21:25:23 +000083 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +000084
Dan Gohmana3624b62009-11-23 17:16:22 +000085 Fn = &fn;
86 MF = &mf;
87 RegInfo = &MF->getRegInfo();
88
Dan Gohmand7b5ce32010-07-10 09:00:22 +000089 // Check whether the function can return without sret-demotion.
90 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8db01cb2013-06-06 00:11:39 +000091 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
92 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
93 Fn->isVarArg(),
94 Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000095
Dan Gohmana3624b62009-11-23 17:16:22 +000096 // Initialize the mapping of values to registers. This is only set up for
97 // instruction values that are used outside of the block that defines
98 // them.
Dan Gohman913c9982010-04-15 04:33:49 +000099 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
Dan Gohmana3624b62009-11-23 17:16:22 +0000100 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 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000104 // Static allocas can be folded into the initial stack frame adjustment.
105 if (AI->isStaticAlloca()) {
106 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
107 Type *Ty = AI->getAllocatedType();
108 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
109 unsigned Align =
110 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
111 AI->getAlignment());
112
113 TySize *= CUI->getZExtValue(); // Get total allocated size.
114 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
115
116 StaticAllocaMap[AI] =
117 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
118
119 } else {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000120 unsigned Align = std::max(
121 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
122 AI->getAllocatedType()),
123 AI->getAlignment());
Eric Christopherd9134482014-08-04 21:25:23 +0000124 unsigned StackAlign =
125 TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000126 if (Align <= StackAlign)
127 Align = 0;
128 // Inform the Frame Information that we have variable-sized objects.
129 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
130 }
131 }
132
133 // Look for inline asm that clobbers the SP register.
134 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
135 ImmutableCallSite CS(I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000136 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000137 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
138 std::vector<TargetLowering::AsmOperandInfo> Ops =
139 TLI->ParseConstraints(CS);
140 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
141 TargetLowering::AsmOperandInfo &Op = Ops[I];
142 if (Op.Type == InlineAsm::isClobber) {
143 // Clobbers don't have SDValue operands, hence SDValue().
144 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
145 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
146 TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
147 Op.ConstraintVT);
148 if (PhysReg.first == SP)
149 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
150 }
151 }
152 }
153 }
154
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000155 // Look for calls to the @llvm.va_start intrinsic. We can omit some
156 // prologue boilerplate for variadic functions that don't examine their
157 // arguments.
158 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
159 if (II->getIntrinsicID() == Intrinsic::vastart)
160 MF->getFrameInfo()->setHasVAStart(true);
161 }
162
Reid Kleckner16e55412014-08-29 21:42:08 +0000163 // If we have a musttail call in a variadic funciton, we need to ensure we
164 // forward implicit register parameters.
Reid Klecknerdccd0cb2014-08-29 21:42:21 +0000165 if (const auto *CI = dyn_cast<CallInst>(I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000166 if (CI->isMustTailCall() && Fn->isVarArg())
167 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
168 }
169
Dan Gohman1e9362772010-07-16 17:54:27 +0000170 // Mark values used outside their block as exported, by allocating
171 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000172 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000173 if (!isa<AllocaInst>(I) ||
174 !StaticAllocaMap.count(cast<AllocaInst>(I)))
175 InitializeRegForValue(I);
176
Dan Gohman1e9362772010-07-16 17:54:27 +0000177 // Collect llvm.dbg.declare information. This is done now instead of
178 // during the initial isel pass through the IR so that it is done
179 // in a predictable order.
180 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
181 MachineModuleInfo &MMI = MF->getMMI();
Manman Ren983a16c2013-06-28 05:43:10 +0000182 DIVariable DIVar(DI->getVariable());
183 assert((!DIVar || DIVar.isVariable()) &&
184 "Variable in DbgDeclareInst should be either null or a DIVariable.");
Dan Gohman1e9362772010-07-16 17:54:27 +0000185 if (MMI.hasDebugInfo() &&
Manman Ren983a16c2013-06-28 05:43:10 +0000186 DIVar &&
Dan Gohman1e9362772010-07-16 17:54:27 +0000187 !DI->getDebugLoc().isUnknown()) {
188 // Don't handle byval struct arguments or VLAs, for example.
189 // Non-byval arguments are handled here (they refer to the stack
190 // temporary alloca at this point).
191 const Value *Address = DI->getAddress();
192 if (Address) {
193 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
194 Address = BCI->getOperand(0);
195 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
196 DenseMap<const AllocaInst *, int>::iterator SI =
197 StaticAllocaMap.find(AI);
198 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
199 int FI = SI->second;
200 MMI.setVariableDbgInfo(DI->getVariable(),
201 FI, DI->getDebugLoc());
202 }
203 }
204 }
205 }
206 }
Jiangning Liuffbc6902014-09-19 05:30:35 +0000207
208 // Decide the preferred extend type for a value.
209 PreferredExtendType[I] = getPreferredExtendForValue(I);
Dan Gohman1e9362772010-07-16 17:54:27 +0000210 }
211
Dan Gohmana3624b62009-11-23 17:16:22 +0000212 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
213 // also creates the initial PHI MachineInstrs, though none of the input
214 // operands are populated.
Dan Gohmanf57117d2010-04-14 16:30:40 +0000215 for (BB = Fn->begin(); BB != EB; ++BB) {
Dan Gohmana3624b62009-11-23 17:16:22 +0000216 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
217 MBBMap[BB] = MBB;
218 MF->push_back(MBB);
219
220 // Transfer the address-taken flag. This is necessary because there could
221 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
222 // the first one should be marked.
223 if (BB->hasAddressTaken())
224 MBB->setHasAddressTaken();
225
226 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
227 // appropriate.
Dan Gohman0f055d32010-04-20 14:46:25 +0000228 for (BasicBlock::const_iterator I = BB->begin();
229 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
230 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000231
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000232 // Skip empty types
233 if (PN->getType()->isEmptyTy())
234 continue;
235
Dan Gohman7b7f0882010-04-20 14:48:02 +0000236 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000237 unsigned PHIReg = ValueMap[PN];
238 assert(PHIReg && "PHI node does not have an assigned virtual register!");
239
240 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000241 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000242 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
243 EVT VT = ValueVTs[vti];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000244 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherfc6de422014-08-05 02:39:49 +0000245 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000246 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000247 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000248 PHIReg += NumRegisters;
249 }
250 }
251 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000252
253 // Mark landing pad blocks.
254 for (BB = Fn->begin(); BB != EB; ++BB)
Dan Gohman913c9982010-04-15 04:33:49 +0000255 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000256 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmana3624b62009-11-23 17:16:22 +0000257}
258
259/// clear - Clear out all the function-specific state. This returns this
260/// FunctionLoweringInfo to an empty state, ready to be used for a
261/// different function.
262void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000263 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
264 "Not all catch info was assigned to a landing pad!");
265
Dan Gohmana3624b62009-11-23 17:16:22 +0000266 MBBMap.clear();
267 ValueMap.clear();
268 StaticAllocaMap.clear();
269#ifndef NDEBUG
270 CatchInfoLost.clear();
271 CatchInfoFound.clear();
272#endif
273 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000274 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000275 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000276 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000277 RegFixups.clear();
Jiangning Liu3b096172014-09-24 03:22:56 +0000278 PreferredExtendType.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000279}
280
Dan Gohman93f59202010-07-02 00:10:16 +0000281/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000282unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000283 return RegInfo->createVirtualRegister(
284 TM.getSubtargetImpl()->getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000285}
286
Dan Gohman93f59202010-07-02 00:10:16 +0000287/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000288/// the correctly promoted or expanded types. Assign these registers
289/// consecutive vreg numbers and return the first assigned number.
290///
291/// In the case that the given value has struct or array type, this function
292/// will assign registers for each member or element.
293///
Chris Lattner229907c2011-07-18 04:54:35 +0000294unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopherd9134482014-08-04 21:25:23 +0000295 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000296
Dan Gohmana3624b62009-11-23 17:16:22 +0000297 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000298 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000299
300 unsigned FirstReg = 0;
301 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
302 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000303 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000304
Bill Wendling8db01cb2013-06-06 00:11:39 +0000305 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000306 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000307 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000308 if (!FirstReg) FirstReg = R;
309 }
310 }
311 return FirstReg;
312}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000313
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000314/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
315/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
316/// the register's LiveOutInfo is for a smaller bit width, it is extended to
317/// the larger bit width by zero extension. The bit width must be no smaller
318/// than the LiveOutInfo's existing bit width.
319const FunctionLoweringInfo::LiveOutInfo *
320FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
321 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000322 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000323
324 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
325 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000326 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000327
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000328 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000329 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000330 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
331 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
332 }
333
334 return LOI;
335}
336
337/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
338/// register based on the LiveOutInfo of its operands.
339void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000340 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000341 if (!Ty->isIntegerTy() || Ty->isVectorTy())
342 return;
343
Eric Christopherd9134482014-08-04 21:25:23 +0000344 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000345
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000346 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000347 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000348 assert(ValueVTs.size() == 1 &&
349 "PHIs with non-vector integer types should have a single VT.");
350 EVT IntVT = ValueVTs[0];
351
Bill Wendling8db01cb2013-06-06 00:11:39 +0000352 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000353 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000354 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000355 unsigned BitWidth = IntVT.getSizeInBits();
356
357 unsigned DestReg = ValueMap[PN];
358 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
359 return;
360 LiveOutRegInfo.grow(DestReg);
361 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
362
363 Value *V = PN->getIncomingValue(0);
364 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
365 DestLOI.NumSignBits = 1;
366 APInt Zero(BitWidth, 0);
367 DestLOI.KnownZero = Zero;
368 DestLOI.KnownOne = Zero;
369 return;
370 }
371
372 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
373 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
374 DestLOI.NumSignBits = Val.getNumSignBits();
375 DestLOI.KnownZero = ~Val;
376 DestLOI.KnownOne = Val;
377 } else {
378 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
379 "CopyToReg node was created.");
380 unsigned SrcReg = ValueMap[V];
381 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
382 DestLOI.IsValid = false;
383 return;
384 }
385 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
386 if (!SrcLOI) {
387 DestLOI.IsValid = false;
388 return;
389 }
390 DestLOI = *SrcLOI;
391 }
392
393 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
394 DestLOI.KnownOne.getBitWidth() == BitWidth &&
395 "Masks should have the same bit width as the type.");
396
397 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
398 Value *V = PN->getIncomingValue(i);
399 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
400 DestLOI.NumSignBits = 1;
401 APInt Zero(BitWidth, 0);
402 DestLOI.KnownZero = Zero;
403 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000404 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000405 }
406
407 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
408 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
409 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
410 DestLOI.KnownZero &= ~Val;
411 DestLOI.KnownOne &= Val;
412 continue;
413 }
414
415 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
416 "its CopyToReg node was created.");
417 unsigned SrcReg = ValueMap[V];
418 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
419 DestLOI.IsValid = false;
420 return;
421 }
422 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
423 if (!SrcLOI) {
424 DestLOI.IsValid = false;
425 return;
426 }
427 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
428 DestLOI.KnownZero &= SrcLOI->KnownZero;
429 DestLOI.KnownOne &= SrcLOI->KnownOne;
430 }
431}
432
Devang Patel9d904e12011-09-08 22:59:09 +0000433/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000434/// argument. This overrides previous frame index entry for this argument,
435/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000436void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000437 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000438 ByValArgFrameIndexMap[A] = FI;
439}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000440
Devang Patel9d904e12011-09-08 22:59:09 +0000441/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000442/// If the argument does not have any assigned frame index then 0 is
443/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000444int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000445 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000446 ByValArgFrameIndexMap.find(A);
447 if (I != ByValArgFrameIndexMap.end())
448 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000449 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000450 return 0;
451}
452
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000453/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
454/// being passed to this variadic function, and set the MachineModuleInfo's
455/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
456/// reference to _fltused on Windows, which will link in MSVCRT's
457/// floating-point support.
458void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
459 MachineModuleInfo *MMI)
460{
461 FunctionType *FT = cast<FunctionType>(
462 I.getCalledValue()->getType()->getContainedType(0));
463 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
464 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
465 Type* T = I.getArgOperand(i)->getType();
466 for (po_iterator<Type*> i = po_begin(T), e = po_end(T);
467 i != e; ++i) {
468 if (i->isFloatingPointTy()) {
469 MMI->setUsesVAFloatArgument(true);
470 return;
471 }
472 }
473 }
474 }
475}
476
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000477/// AddCatchInfo - Extract the personality and type infos from an eh.selector
478/// call, and add them to the specified machine basic block.
Dan Gohman7deb4472010-04-14 19:53:31 +0000479void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000480 MachineBasicBlock *MBB) {
481 // Inform the MachineModuleInfo of the personality for this landing pad.
Gabor Greife4eed702010-06-25 08:24:59 +0000482 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000483 assert(CE->getOpcode() == Instruction::BitCast &&
484 isa<Function>(CE->getOperand(0)) &&
485 "Personality should be a function");
486 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
487
488 // Gather all the type infos for this landing pad and pass them along to
489 // MachineModuleInfo.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000490 std::vector<const GlobalVariable *> TyInfo;
Gabor Greif647d9c92010-06-30 13:45:50 +0000491 unsigned N = I.getNumArgOperands();
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000492
Gabor Greif647d9c92010-06-30 13:45:50 +0000493 for (unsigned i = N - 1; i > 1; --i) {
494 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) {
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000495 unsigned FilterLength = CI->getZExtValue();
496 unsigned FirstCatch = i + FilterLength + !FilterLength;
Gabor Greif647d9c92010-06-30 13:45:50 +0000497 assert(FirstCatch <= N && "Invalid filter length");
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000498
499 if (FirstCatch < N) {
500 TyInfo.reserve(N - FirstCatch);
501 for (unsigned j = FirstCatch; j < N; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000502 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000503 MMI->addCatchTypeInfo(MBB, TyInfo);
504 TyInfo.clear();
505 }
506
507 if (!FilterLength) {
508 // Cleanup.
509 MMI->addCleanup(MBB);
510 } else {
511 // Filter.
512 TyInfo.reserve(FilterLength - 1);
513 for (unsigned j = i + 1; j < FirstCatch; ++j)
Gabor Greif647d9c92010-06-30 13:45:50 +0000514 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000515 MMI->addFilterTypeInfo(MBB, TyInfo);
516 TyInfo.clear();
517 }
518
519 N = i;
520 }
521 }
522
Gabor Greif647d9c92010-06-30 13:45:50 +0000523 if (N > 2) {
524 TyInfo.reserve(N - 2);
525 for (unsigned j = 2; j < N; ++j)
526 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j)));
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000527 MMI->addCatchTypeInfo(MBB, TyInfo);
528 }
529}
530
Bill Wendling247fd3b2011-08-17 21:56:44 +0000531/// AddLandingPadInfo - Extract the exception handling information from the
532/// landingpad instruction and add them to the specified machine module info.
533void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
534 MachineBasicBlock *MBB) {
535 MMI.addPersonality(MBB,
536 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
537
538 if (I.isCleanup())
539 MMI.addCleanup(MBB);
540
541 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
542 // but we need to do it this way because of how the DWARF EH emitter
543 // processes the clauses.
544 for (unsigned i = I.getNumClauses(); i != 0; --i) {
545 Value *Val = I.getClause(i - 1);
546 if (I.isCatch(i - 1)) {
547 MMI.addCatchTypeInfo(MBB,
548 dyn_cast<GlobalVariable>(Val->stripPointerCasts()));
549 } else {
550 // Add filters in a list.
551 Constant *CVal = cast<Constant>(Val);
552 SmallVector<const GlobalVariable*, 4> FilterList;
553 for (User::op_iterator
554 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
555 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts()));
556
557 MMI.addFilterTypeInfo(MBB, FilterList);
558 }
559 }
560}