blob: f3d75cb32a7d19426b68409187191b051786893c [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"
David Majnemercde33032015-03-30 22:58:10 +000023#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000025#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Module.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000035#include "llvm/Support/raw_ostream.h"
Hans Wennborgacb842d2014-03-05 02:43:26 +000036#include "llvm/Target/TargetFrameLowering.h"
Chandler Carruth92051402014-03-05 10:30:38 +000037#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Target/TargetLowering.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000041#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000042#include <algorithm>
43using namespace llvm;
44
Chandler Carruth1b9dde02014-04-22 02:02:50 +000045#define DEBUG_TYPE "function-lowering-info"
46
Dan Gohmana3624b62009-11-23 17:16:22 +000047/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
48/// PHI nodes or outside of the basic block that defines it, or used by a
49/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohman913c9982010-04-15 04:33:49 +000050static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohman7c845e42010-04-20 14:50:13 +000051 if (I->use_empty()) return false;
Dan Gohmana3624b62009-11-23 17:16:22 +000052 if (isa<PHINode>(I)) return true;
Dan Gohman913c9982010-04-15 04:33:49 +000053 const BasicBlock *BB = I->getParent();
Chandler Carruthcdf47882014-03-09 03:16:01 +000054 for (const User *U : I->users())
Gabor Greif52617fc2010-07-09 16:08:33 +000055 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohmana3624b62009-11-23 17:16:22 +000056 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +000057
Dan Gohmana3624b62009-11-23 17:16:22 +000058 return false;
59}
60
Jiangning Liuffbc6902014-09-19 05:30:35 +000061static ISD::NodeType getPreferredExtendForValue(const Value *V) {
62 // For the users of the source value being used for compare instruction, if
63 // the number of signed predicate is greater than unsigned predicate, we
64 // prefer to use SIGN_EXTEND.
65 //
66 // With this optimization, we would be able to reduce some redundant sign or
67 // zero extension instruction, and eventually more machine CSE opportunities
68 // can be exposed.
69 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
70 unsigned NumOfSigned = 0, NumOfUnsigned = 0;
71 for (const User *U : V->users()) {
72 if (const auto *CI = dyn_cast<CmpInst>(U)) {
73 NumOfSigned += CI->isSigned();
74 NumOfUnsigned += CI->isUnsigned();
75 }
76 }
77 if (NumOfSigned > NumOfUnsigned)
78 ExtendKind = ISD::SIGN_EXTEND;
79
80 return ExtendKind;
81}
82
Hans Wennborgacb842d2014-03-05 02:43:26 +000083void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
84 SelectionDAG *DAG) {
Dan Gohmana3624b62009-11-23 17:16:22 +000085 Fn = &fn;
86 MF = &mf;
Eric Christopher2ae2de72014-10-09 00:57:31 +000087 TLI = MF->getSubtarget().getTargetLowering();
Dan Gohmana3624b62009-11-23 17:16:22 +000088 RegInfo = &MF->getRegInfo();
David Majnemercde33032015-03-30 22:58:10 +000089 MachineModuleInfo &MMI = MF->getMMI();
Dan Gohmana3624b62009-11-23 17:16:22 +000090
Dan Gohmand7b5ce32010-07-10 09:00:22 +000091 // Check whether the function can return without sret-demotion.
92 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8db01cb2013-06-06 00:11:39 +000093 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
94 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
Eric Christopher2ae2de72014-10-09 00:57:31 +000095 Fn->isVarArg(), Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000096
Dan Gohmana3624b62009-11-23 17:16:22 +000097 // Initialize the mapping of values to registers. This is only set up for
98 // instruction values that are used outside of the block that defines
99 // them.
Dan Gohman913c9982010-04-15 04:33:49 +0000100 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
Dan Gohmana3624b62009-11-23 17:16:22 +0000101 for (; BB != EB; ++BB)
Eric Christopher219d51d2012-02-24 01:59:01 +0000102 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
103 I != E; ++I) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000104 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000105 // Static allocas can be folded into the initial stack frame adjustment.
106 if (AI->isStaticAlloca()) {
107 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
108 Type *Ty = AI->getAllocatedType();
109 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
110 unsigned Align =
Eric Christopher2ae2de72014-10-09 00:57:31 +0000111 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
112 AI->getAlignment());
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000113
114 TySize *= CUI->getZExtValue(); // Get total allocated size.
115 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
116
117 StaticAllocaMap[AI] =
118 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
119
120 } else {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000121 unsigned Align = std::max(
122 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
123 AI->getAllocatedType()),
124 AI->getAlignment());
Eric Christopherd9134482014-08-04 21:25:23 +0000125 unsigned StackAlign =
Eric Christopher2ae2de72014-10-09 00:57:31 +0000126 MF->getSubtarget().getFrameLowering()->getStackAlignment();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000127 if (Align <= StackAlign)
128 Align = 0;
129 // Inform the Frame Information that we have variable-sized objects.
130 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
131 }
132 }
133
134 // Look for inline asm that clobbers the SP register.
135 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
136 ImmutableCallSite CS(I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000137 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000138 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Eric Christopher11e4df72015-02-26 22:38:43 +0000139 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000140 std::vector<TargetLowering::AsmOperandInfo> Ops =
Eric Christopher11e4df72015-02-26 22:38:43 +0000141 TLI->ParseConstraints(TRI, CS);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000142 for (size_t I = 0, E = Ops.size(); I != E; ++I) {
143 TargetLowering::AsmOperandInfo &Op = Ops[I];
144 if (Op.Type == InlineAsm::isClobber) {
145 // Clobbers don't have SDValue operands, hence SDValue().
146 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
Eric Christopher2ae2de72014-10-09 00:57:31 +0000147 std::pair<unsigned, const TargetRegisterClass *> PhysReg =
Eric Christopher11e4df72015-02-26 22:38:43 +0000148 TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
149 Op.ConstraintVT);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000150 if (PhysReg.first == SP)
151 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
152 }
153 }
154 }
155 }
156
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000157 // Look for calls to the @llvm.va_start intrinsic. We can omit some
158 // prologue boilerplate for variadic functions that don't examine their
159 // arguments.
160 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
161 if (II->getIntrinsicID() == Intrinsic::vastart)
162 MF->getFrameInfo()->setHasVAStart(true);
163 }
164
Reid Kleckner16e55412014-08-29 21:42:08 +0000165 // If we have a musttail call in a variadic funciton, we need to ensure we
166 // forward implicit register parameters.
Reid Klecknerdccd0cb2014-08-29 21:42:21 +0000167 if (const auto *CI = dyn_cast<CallInst>(I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000168 if (CI->isMustTailCall() && Fn->isVarArg())
169 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
170 }
171
Dan Gohman1e9362772010-07-16 17:54:27 +0000172 // Mark values used outside their block as exported, by allocating
173 // a virtual register for them.
Cameron Zwarichf8b22b32011-02-22 03:24:52 +0000174 if (isUsedOutsideOfDefiningBlock(I))
Dan Gohmana3624b62009-11-23 17:16:22 +0000175 if (!isa<AllocaInst>(I) ||
176 !StaticAllocaMap.count(cast<AllocaInst>(I)))
177 InitializeRegForValue(I);
178
Dan Gohman1e9362772010-07-16 17:54:27 +0000179 // Collect llvm.dbg.declare information. This is done now instead of
180 // during the initial isel pass through the IR so that it is done
181 // in a predictable order.
182 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
Duncan P. N. Exon Smithd4a19a32015-04-21 18:24:23 +0000183 assert(DI->getVariable() && "Missing variable");
184 assert(DI->getDebugLoc() && "Missing location");
185 if (MMI.hasDebugInfo()) {
Dan Gohman1e9362772010-07-16 17:54:27 +0000186 // 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.
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000252 SmallVector<const LandingPadInst *, 4> LPads;
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000253 for (BB = Fn->begin(); BB != EB; ++BB) {
David Majnemercde33032015-03-30 22:58:10 +0000254 if (const auto *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
Dan Gohman69e8e322010-04-14 16:32:56 +0000255 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000256 if (BB->isLandingPad())
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000257 LPads.push_back(BB->getLandingPadInst());
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000258 }
David Majnemercde33032015-03-30 22:58:10 +0000259
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000260 // If this is an MSVC EH personality, we need to do a bit more work.
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000261 EHPersonality Personality = EHPersonality::Unknown;
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000262 if (!LPads.empty())
263 Personality = classifyEHPersonality(LPads.back()->getPersonalityFn());
264 if (!isMSVCEHPersonality(Personality))
265 return;
266
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000267 if (Personality == EHPersonality::MSVC_Win64SEH) {
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000268 addSEHHandlersForLPads(LPads);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000269 } else if (Personality == EHPersonality::MSVC_CXX) {
David Majnemer5c65f582015-04-10 04:56:17 +0000270 const Function *WinEHParentFn = MMI.getWinEHParent(&fn);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000271 WinEHFuncInfo &EHInfo = MMI.getWinEHFuncInfo(WinEHParentFn);
272 calculateWinCXXEHStateNumbers(WinEHParentFn, EHInfo);
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000273
274 // Copy the state numbers to LandingPadInfo for the current function, which
275 // could be a handler or the parent.
276 for (const LandingPadInst *LP : LPads) {
277 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000278 MMI.addWinEHState(LPadMBB, EHInfo.LandingPadStateMap[LP]);
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000279 }
David Majnemera225a192015-03-31 22:35:44 +0000280 }
David Majnemercde33032015-03-30 22:58:10 +0000281}
282
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000283void FunctionLoweringInfo::addSEHHandlersForLPads(
284 ArrayRef<const LandingPadInst *> LPads) {
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000285 MachineModuleInfo &MMI = MF->getMMI();
286
287 // Iterate over all landing pads with llvm.eh.actions calls.
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000288 for (const LandingPadInst *LP : LPads) {
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000289 const IntrinsicInst *ActionsCall =
290 dyn_cast<IntrinsicInst>(LP->getNextNode());
291 if (!ActionsCall ||
292 ActionsCall->getIntrinsicID() != Intrinsic::eh_actions)
293 continue;
294
295 // Parse the llvm.eh.actions call we found.
296 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
Benjamin Kramera48e0652015-05-16 15:40:03 +0000297 SmallVector<std::unique_ptr<ActionHandler>, 4> Actions;
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000298 parseEHActions(ActionsCall, Actions);
299
300 // Iterate EH actions from most to least precedence, which means
301 // iterating in reverse.
302 for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) {
Benjamin Kramera48e0652015-05-16 15:40:03 +0000303 ActionHandler *Action = I->get();
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000304 if (auto *CH = dyn_cast<CatchHandler>(Action)) {
305 const auto *Filter =
306 dyn_cast<Function>(CH->getSelector()->stripPointerCasts());
307 assert((Filter || CH->getSelector()->isNullValue()) &&
308 "expected function or catch-all");
309 const auto *RecoverBA =
310 cast<BlockAddress>(CH->getHandlerBlockOrFunc());
311 MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA);
312 } else {
313 assert(isa<CleanupHandler>(Action));
314 const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc());
315 MMI.addSEHCleanupHandler(LPadMBB, Fini);
316 }
317 }
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000318 }
319}
320
Dan Gohmana3624b62009-11-23 17:16:22 +0000321/// clear - Clear out all the function-specific state. This returns this
322/// FunctionLoweringInfo to an empty state, ready to be used for a
323/// different function.
324void FunctionLoweringInfo::clear() {
Dan Gohmanad0b3ea2010-04-14 17:11:23 +0000325 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
326 "Not all catch info was assigned to a landing pad!");
327
Dan Gohmana3624b62009-11-23 17:16:22 +0000328 MBBMap.clear();
329 ValueMap.clear();
330 StaticAllocaMap.clear();
331#ifndef NDEBUG
332 CatchInfoLost.clear();
333 CatchInfoFound.clear();
334#endif
335 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000336 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000337 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000338 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000339 RegFixups.clear();
Philip Reames1a1bdb22014-12-02 18:50:36 +0000340 StatepointStackSlots.clear();
Igor Laevsky423bc9ec2015-05-20 11:37:25 +0000341 StatepointRelocatedValues.clear();
Jiangning Liu3b096172014-09-24 03:22:56 +0000342 PreferredExtendType.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000343}
344
Dan Gohman93f59202010-07-02 00:10:16 +0000345/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000346unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000347 return RegInfo->createVirtualRegister(
Eric Christopher2ae2de72014-10-09 00:57:31 +0000348 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000349}
350
Dan Gohman93f59202010-07-02 00:10:16 +0000351/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000352/// the correctly promoted or expanded types. Assign these registers
353/// consecutive vreg numbers and return the first assigned number.
354///
355/// In the case that the given value has struct or array type, this function
356/// will assign registers for each member or element.
357///
Chris Lattner229907c2011-07-18 04:54:35 +0000358unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopher2ae2de72014-10-09 00:57:31 +0000359 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000360
Dan Gohmana3624b62009-11-23 17:16:22 +0000361 SmallVector<EVT, 4> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000362 ComputeValueVTs(*TLI, Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000363
364 unsigned FirstReg = 0;
365 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
366 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000367 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000368
Bill Wendling8db01cb2013-06-06 00:11:39 +0000369 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000370 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000371 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000372 if (!FirstReg) FirstReg = R;
373 }
374 }
375 return FirstReg;
376}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000377
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000378/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
379/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
380/// the register's LiveOutInfo is for a smaller bit width, it is extended to
381/// the larger bit width by zero extension. The bit width must be no smaller
382/// than the LiveOutInfo's existing bit width.
383const FunctionLoweringInfo::LiveOutInfo *
384FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
385 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000386 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000387
388 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
389 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000390 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000391
Cameron Zwarichd2f30412011-02-25 01:10:55 +0000392 if (BitWidth > LOI->KnownZero.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000393 LOI->NumSignBits = 1;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000394 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
395 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
396 }
397
398 return LOI;
399}
400
401/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
402/// register based on the LiveOutInfo of its operands.
403void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000404 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000405 if (!Ty->isIntegerTy() || Ty->isVectorTy())
406 return;
407
408 SmallVector<EVT, 1> ValueVTs;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000409 ComputeValueVTs(*TLI, Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000410 assert(ValueVTs.size() == 1 &&
411 "PHIs with non-vector integer types should have a single VT.");
412 EVT IntVT = ValueVTs[0];
413
Bill Wendling8db01cb2013-06-06 00:11:39 +0000414 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000415 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000416 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000417 unsigned BitWidth = IntVT.getSizeInBits();
418
419 unsigned DestReg = ValueMap[PN];
420 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
421 return;
422 LiveOutRegInfo.grow(DestReg);
423 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
424
425 Value *V = PN->getIncomingValue(0);
426 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
427 DestLOI.NumSignBits = 1;
428 APInt Zero(BitWidth, 0);
429 DestLOI.KnownZero = Zero;
430 DestLOI.KnownOne = Zero;
431 return;
432 }
433
434 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
435 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
436 DestLOI.NumSignBits = Val.getNumSignBits();
437 DestLOI.KnownZero = ~Val;
438 DestLOI.KnownOne = Val;
439 } else {
440 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
441 "CopyToReg node was created.");
442 unsigned SrcReg = ValueMap[V];
443 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
444 DestLOI.IsValid = false;
445 return;
446 }
447 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
448 if (!SrcLOI) {
449 DestLOI.IsValid = false;
450 return;
451 }
452 DestLOI = *SrcLOI;
453 }
454
455 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
456 DestLOI.KnownOne.getBitWidth() == BitWidth &&
457 "Masks should have the same bit width as the type.");
458
459 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
460 Value *V = PN->getIncomingValue(i);
461 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
462 DestLOI.NumSignBits = 1;
463 APInt Zero(BitWidth, 0);
464 DestLOI.KnownZero = Zero;
465 DestLOI.KnownOne = Zero;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000466 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000467 }
468
469 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
470 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
471 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
472 DestLOI.KnownZero &= ~Val;
473 DestLOI.KnownOne &= Val;
474 continue;
475 }
476
477 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
478 "its CopyToReg node was created.");
479 unsigned SrcReg = ValueMap[V];
480 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
481 DestLOI.IsValid = false;
482 return;
483 }
484 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
485 if (!SrcLOI) {
486 DestLOI.IsValid = false;
487 return;
488 }
489 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
490 DestLOI.KnownZero &= SrcLOI->KnownZero;
491 DestLOI.KnownOne &= SrcLOI->KnownOne;
492 }
493}
494
Devang Patel9d904e12011-09-08 22:59:09 +0000495/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000496/// argument. This overrides previous frame index entry for this argument,
497/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000498void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000499 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000500 ByValArgFrameIndexMap[A] = FI;
501}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000502
Devang Patel9d904e12011-09-08 22:59:09 +0000503/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000504/// If the argument does not have any assigned frame index then 0 is
505/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000506int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Eric Christopher0713a9d2011-06-08 23:55:35 +0000507 DenseMap<const Argument *, int>::iterator I =
Devang Patel86ec8b32010-08-31 22:22:42 +0000508 ByValArgFrameIndexMap.find(A);
509 if (I != ByValArgFrameIndexMap.end())
510 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000511 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Devang Patel86ec8b32010-08-31 22:22:42 +0000512 return 0;
513}
514
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000515/// ComputeUsesVAFloatArgument - Determine if any floating-point values are
516/// being passed to this variadic function, and set the MachineModuleInfo's
517/// usesVAFloatArgument flag if so. This flag is used to emit an undefined
518/// reference to _fltused on Windows, which will link in MSVCRT's
519/// floating-point support.
520void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
521 MachineModuleInfo *MMI)
522{
523 FunctionType *FT = cast<FunctionType>(
524 I.getCalledValue()->getType()->getContainedType(0));
525 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
526 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
527 Type* T = I.getArgOperand(i)->getType();
Daniel Berlin25db4f42015-04-15 17:41:42 +0000528 for (auto i : post_order(T)) {
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000529 if (i->isFloatingPointTy()) {
530 MMI->setUsesVAFloatArgument(true);
531 return;
532 }
533 }
534 }
535 }
536}
537
Bill Wendling247fd3b2011-08-17 21:56:44 +0000538/// AddLandingPadInfo - Extract the exception handling information from the
539/// landingpad instruction and add them to the specified machine module info.
540void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
541 MachineBasicBlock *MBB) {
542 MMI.addPersonality(MBB,
543 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
544
545 if (I.isCleanup())
546 MMI.addCleanup(MBB);
547
548 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
549 // but we need to do it this way because of how the DWARF EH emitter
550 // processes the clauses.
551 for (unsigned i = I.getNumClauses(); i != 0; --i) {
552 Value *Val = I.getClause(i - 1);
553 if (I.isCatch(i - 1)) {
554 MMI.addCatchTypeInfo(MBB,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000555 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
Bill Wendling247fd3b2011-08-17 21:56:44 +0000556 } else {
557 // Add filters in a list.
558 Constant *CVal = cast<Constant>(Val);
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000559 SmallVector<const GlobalValue*, 4> FilterList;
Bill Wendling247fd3b2011-08-17 21:56:44 +0000560 for (User::op_iterator
561 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000562 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
Bill Wendling247fd3b2011-08-17 21:56:44 +0000563
564 MMI.addFilterTypeInfo(MBB, FilterList);
565 }
566 }
567}