blob: b736037d71ddcc53367d185e0dd95049e2ab4532 [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/CodeGen/Analysis.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000022#include "llvm/CodeGen/WinEHFuncInfo.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"
Benjamin Kramer799003b2015-03-23 19:32:43 +000034#include "llvm/Support/raw_ostream.h"
Hans Wennborgacb842d2014-03-05 02:43:26 +000035#include "llvm/Target/TargetFrameLowering.h"
Chandler Carruth92051402014-03-05 10:30:38 +000036#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000040#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000041#include <algorithm>
42using namespace llvm;
43
Chandler Carruth1b9dde02014-04-22 02:02:50 +000044#define DEBUG_TYPE "function-lowering-info"
45
Dan Gohmana3624b62009-11-23 17:16:22 +000046/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
47/// PHI nodes or outside of the basic block that defines it, or used by a
48/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohman913c9982010-04-15 04:33:49 +000049static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohman7c845e42010-04-20 14:50:13 +000050 if (I->use_empty()) return false;
Dan Gohmana3624b62009-11-23 17:16:22 +000051 if (isa<PHINode>(I)) return true;
Dan Gohman913c9982010-04-15 04:33:49 +000052 const BasicBlock *BB = I->getParent();
Chandler Carruthcdf47882014-03-09 03:16:01 +000053 for (const User *U : I->users())
Gabor Greif52617fc2010-07-09 16:08:33 +000054 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohmana3624b62009-11-23 17:16:22 +000055 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +000056
Dan Gohmana3624b62009-11-23 17:16:22 +000057 return false;
58}
59
Jiangning Liuffbc6902014-09-19 05:30:35 +000060static ISD::NodeType getPreferredExtendForValue(const Value *V) {
61 // For the users of the source value being used for compare instruction, if
62 // the number of signed predicate is greater than unsigned predicate, we
63 // prefer to use SIGN_EXTEND.
64 //
65 // With this optimization, we would be able to reduce some redundant sign or
66 // zero extension instruction, and eventually more machine CSE opportunities
67 // can be exposed.
68 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
69 unsigned NumOfSigned = 0, NumOfUnsigned = 0;
70 for (const User *U : V->users()) {
71 if (const auto *CI = dyn_cast<CmpInst>(U)) {
72 NumOfSigned += CI->isSigned();
73 NumOfUnsigned += CI->isUnsigned();
74 }
75 }
76 if (NumOfSigned > NumOfUnsigned)
77 ExtendKind = ISD::SIGN_EXTEND;
78
79 return ExtendKind;
80}
81
Hans Wennborgacb842d2014-03-05 02:43:26 +000082void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
83 SelectionDAG *DAG) {
Dan Gohmana3624b62009-11-23 17:16:22 +000084 Fn = &fn;
85 MF = &mf;
Eric Christopher2ae2de72014-10-09 00:57:31 +000086 TLI = MF->getSubtarget().getTargetLowering();
Dan Gohmana3624b62009-11-23 17:16:22 +000087 RegInfo = &MF->getRegInfo();
Jonas Paulssonf12b9252015-11-28 11:02:32 +000088 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
David Majnemer1ef65402016-03-03 00:01:25 +000089 unsigned StackAlign = TFI->getStackAlignment();
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;
Mehdi Amini56228da2015-07-09 01:57:34 +000093 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
94 mf.getDataLayout());
Bill Wendling8db01cb2013-06-06 00:11:39 +000095 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
Eric Christopher2ae2de72014-10-09 00:57:31 +000096 Fn->isVarArg(), Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000097
David Majnemer1ef65402016-03-03 00:01:25 +000098 // If this personality uses funclets, we need to do a bit more work.
Reid Klecknerf7ad5342016-10-19 17:08:23 +000099 DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects;
David Majnemer1ef65402016-03-03 00:01:25 +0000100 EHPersonality Personality = classifyEHPersonality(
101 Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr);
102 if (isFuncletEHPersonality(Personality)) {
103 // Calculate state numbers if we haven't already.
104 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
105 if (Personality == EHPersonality::MSVC_CXX)
106 calculateWinCXXEHStateNumbers(&fn, EHInfo);
107 else if (isAsynchronousEHPersonality(Personality))
108 calculateSEHStateNumbers(&fn, EHInfo);
109 else if (Personality == EHPersonality::CoreCLR)
110 calculateClrEHStateNumbers(&fn, EHInfo);
111
112 // Map all BB references in the WinEH data to MBBs.
113 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
114 for (WinEHHandlerType &H : TBME.HandlerArray) {
115 if (const AllocaInst *AI = H.CatchObj.Alloca)
Reid Klecknerf7ad5342016-10-19 17:08:23 +0000116 CatchObjects.insert({AI, {}}).first->second.push_back(
117 &H.CatchObj.FrameIndex);
David Majnemer1ef65402016-03-03 00:01:25 +0000118 else
119 H.CatchObj.FrameIndex = INT_MAX;
120 }
121 }
122 }
123
Dan Gohmana3624b62009-11-23 17:16:22 +0000124 // Initialize the mapping of values to registers. This is only set up for
125 // instruction values that are used outside of the block that defines
126 // them.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000127 for (const BasicBlock &BB : *Fn) {
128 for (const Instruction &I : BB) {
129 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000130 Type *Ty = AI->getAllocatedType();
131 unsigned Align =
132 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
133 AI->getAlignment());
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000134
135 // Static allocas can be folded into the initial stack frame
136 // adjustment. For targets that don't realign the stack, don't
137 // do this if there is an extra alignment requirement.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000138 if (AI->isStaticAlloca() &&
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000139 (TFI->isStackRealignable() || (Align <= StackAlign))) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000140 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000141 uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty);
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000142
143 TySize *= CUI->getZExtValue(); // Get total allocated size.
144 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
David Majnemer1ef65402016-03-03 00:01:25 +0000145 int FrameIndex = INT_MAX;
146 auto Iter = CatchObjects.find(AI);
147 if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000148 FrameIndex = MF->getFrameInfo().CreateFixedObject(
David Majnemer1ef65402016-03-03 00:01:25 +0000149 TySize, 0, /*Immutable=*/false, /*isAliased=*/true);
Matthias Braun941a7052016-07-28 18:40:00 +0000150 MF->getFrameInfo().setObjectAlignment(FrameIndex, Align);
David Majnemer1ef65402016-03-03 00:01:25 +0000151 } else {
152 FrameIndex =
Matthias Braun941a7052016-07-28 18:40:00 +0000153 MF->getFrameInfo().CreateStackObject(TySize, Align, false, AI);
David Majnemer1ef65402016-03-03 00:01:25 +0000154 }
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000155
David Majnemer1ef65402016-03-03 00:01:25 +0000156 StaticAllocaMap[AI] = FrameIndex;
157 // Update the catch handler information.
Reid Klecknerf7ad5342016-10-19 17:08:23 +0000158 if (Iter != CatchObjects.end()) {
159 for (int *CatchObjPtr : Iter->second)
160 *CatchObjPtr = FrameIndex;
161 }
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000162 } else {
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000163 // FIXME: Overaligned static allocas should be grouped into
164 // a single dynamic allocation instead of using a separate
165 // stack allocation for each one.
Hans Wennborgacb842d2014-03-05 02:43:26 +0000166 if (Align <= StackAlign)
167 Align = 0;
168 // Inform the Frame Information that we have variable-sized objects.
Matthias Braun941a7052016-07-28 18:40:00 +0000169 MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, AI);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000170 }
171 }
172
173 // Look for inline asm that clobbers the SP register.
174 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000175 ImmutableCallSite CS(&I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000176 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000177 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Eric Christopher11e4df72015-02-26 22:38:43 +0000178 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000179 std::vector<TargetLowering::AsmOperandInfo> Ops =
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000180 TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS);
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000181 for (TargetLowering::AsmOperandInfo &Op : Ops) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000182 if (Op.Type == InlineAsm::isClobber) {
183 // Clobbers don't have SDValue operands, hence SDValue().
184 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
Eric Christopher2ae2de72014-10-09 00:57:31 +0000185 std::pair<unsigned, const TargetRegisterClass *> PhysReg =
Eric Christopher11e4df72015-02-26 22:38:43 +0000186 TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
187 Op.ConstraintVT);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000188 if (PhysReg.first == SP)
Matthias Braun941a7052016-07-28 18:40:00 +0000189 MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000190 }
191 }
192 }
193 }
194
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000195 // Look for calls to the @llvm.va_start intrinsic. We can omit some
196 // prologue boilerplate for variadic functions that don't examine their
197 // arguments.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000198 if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000199 if (II->getIntrinsicID() == Intrinsic::vastart)
Matthias Braun941a7052016-07-28 18:40:00 +0000200 MF->getFrameInfo().setHasVAStart(true);
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000201 }
202
Eric Christopherbfba5722015-12-16 23:10:53 +0000203 // If we have a musttail call in a variadic function, we need to ensure we
Reid Kleckner16e55412014-08-29 21:42:08 +0000204 // forward implicit register parameters.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000205 if (const auto *CI = dyn_cast<CallInst>(&I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000206 if (CI->isMustTailCall() && Fn->isVarArg())
Matthias Braun941a7052016-07-28 18:40:00 +0000207 MF->getFrameInfo().setHasMustTailInVarArgFunc(true);
Reid Kleckner16e55412014-08-29 21:42:08 +0000208 }
209
Dan Gohman1e9362772010-07-16 17:54:27 +0000210 // Mark values used outside their block as exported, by allocating
211 // a virtual register for them.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000212 if (isUsedOutsideOfDefiningBlock(&I))
213 if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I)))
214 InitializeRegForValue(&I);
Dan Gohmana3624b62009-11-23 17:16:22 +0000215
Jiangning Liuffbc6902014-09-19 05:30:35 +0000216 // Decide the preferred extend type for a value.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000217 PreferredExtendType[&I] = getPreferredExtendForValue(&I);
Dan Gohman1e9362772010-07-16 17:54:27 +0000218 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000219 }
Dan Gohman1e9362772010-07-16 17:54:27 +0000220
Dan Gohmana3624b62009-11-23 17:16:22 +0000221 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
222 // also creates the initial PHI MachineInstrs, though none of the input
223 // operands are populated.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000224 for (const BasicBlock &BB : *Fn) {
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000225 // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
226 // are really data, and no instructions can live here.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000227 if (BB.isEHPad()) {
228 const Instruction *PadInst = BB.getFirstNonPHI();
Reid Kleckner6ddae312015-11-05 21:09:49 +0000229 // If this is a non-landingpad EH pad, mark this function as using
David Majnemer7735a6d2015-10-06 23:31:59 +0000230 // funclets.
Reid Kleckner6ddae312015-11-05 21:09:49 +0000231 // FIXME: SEH catchpads do not create funclets, so we could avoid setting
232 // this in such cases in order to improve frame layout.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000233 if (!isa<LandingPadInst>(PadInst)) {
Matthias Braund0ee66c2016-12-01 19:32:15 +0000234 MF->setHasEHFunclets(true);
Matthias Braun941a7052016-07-28 18:40:00 +0000235 MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
Reid Kleckner6ddae312015-11-05 21:09:49 +0000236 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000237 if (isa<CatchSwitchInst>(PadInst)) {
238 assert(&*BB.begin() == PadInst &&
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000239 "WinEHPrepare failed to remove PHIs from imaginary BBs");
240 continue;
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000241 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000242 if (isa<FuncletPadInst>(PadInst))
243 assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000244 }
245
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000246 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);
247 MBBMap[&BB] = MBB;
Dan Gohmana3624b62009-11-23 17:16:22 +0000248 MF->push_back(MBB);
249
250 // Transfer the address-taken flag. This is necessary because there could
251 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
252 // the first one should be marked.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000253 if (BB.hasAddressTaken())
Dan Gohmana3624b62009-11-23 17:16:22 +0000254 MBB->setHasAddressTaken();
255
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000256 // Mark landing pad blocks.
257 if (BB.isEHPad())
258 MBB->setIsEHPad();
259
Dan Gohmana3624b62009-11-23 17:16:22 +0000260 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
261 // appropriate.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000262 for (BasicBlock::const_iterator I = BB.begin();
Dan Gohman0f055d32010-04-20 14:46:25 +0000263 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
264 if (PN->use_empty()) continue;
Dan Gohmana3624b62009-11-23 17:16:22 +0000265
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000266 // Skip empty types
267 if (PN->getType()->isEmptyTy())
268 continue;
269
Dan Gohman7b7f0882010-04-20 14:48:02 +0000270 DebugLoc DL = PN->getDebugLoc();
Dan Gohmana3624b62009-11-23 17:16:22 +0000271 unsigned PHIReg = ValueMap[PN];
272 assert(PHIReg && "PHI node does not have an assigned virtual register!");
273
274 SmallVector<EVT, 4> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +0000275 ComputeValueVTs(*TLI, MF->getDataLayout(), PN->getType(), ValueVTs);
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000276 for (EVT VT : ValueVTs) {
Bill Wendling8db01cb2013-06-06 00:11:39 +0000277 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherfc6de422014-08-05 02:39:49 +0000278 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000279 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000280 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000281 PHIReg += NumRegisters;
282 }
283 }
284 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000285
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000286 if (!isFuncletEHPersonality(Personality))
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000287 return;
288
David Majnemer0e90f462016-01-07 04:31:35 +0000289 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
Reid Kleckner78783912015-09-10 00:25:23 +0000290
291 // Map all BB references in the WinEH data to MBBs.
Reid Klecknerb005d282015-09-16 20:16:27 +0000292 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
293 for (WinEHHandlerType &H : TBME.HandlerArray) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000294 if (H.Handler)
295 H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()];
Reid Klecknerb005d282015-09-16 20:16:27 +0000296 }
297 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000298 for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
Reid Kleckner78783912015-09-10 00:25:23 +0000299 if (UME.Cleanup)
David Majnemerbfa5b982015-10-10 00:04:29 +0000300 UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()];
Reid Kleckner78783912015-09-10 00:25:23 +0000301 for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) {
302 const BasicBlock *BB = UME.Handler.get<const BasicBlock *>();
303 UME.Handler = MBBMap[BB];
Reid Kleckner94b704c2015-09-09 21:10:03 +0000304 }
Joseph Tremoulet7f8c1162015-10-06 20:30:33 +0000305 for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
306 const BasicBlock *BB = CME.Handler.get<const BasicBlock *>();
307 CME.Handler = MBBMap[BB];
308 }
David Majnemercde33032015-03-30 22:58:10 +0000309}
310
Dan Gohmana3624b62009-11-23 17:16:22 +0000311/// clear - Clear out all the function-specific state. This returns this
312/// FunctionLoweringInfo to an empty state, ready to be used for a
313/// different function.
314void FunctionLoweringInfo::clear() {
315 MBBMap.clear();
316 ValueMap.clear();
317 StaticAllocaMap.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000318 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000319 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000320 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000321 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000322 RegFixups.clear();
Philip Reames1a1bdb22014-12-02 18:50:36 +0000323 StatepointStackSlots.clear();
Sanjoy Dasc0c59fe2016-03-24 18:57:39 +0000324 StatepointSpillMaps.clear();
Jiangning Liu3b096172014-09-24 03:22:56 +0000325 PreferredExtendType.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000326}
327
Dan Gohman93f59202010-07-02 00:10:16 +0000328/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000329unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000330 return RegInfo->createVirtualRegister(
Eric Christopher2ae2de72014-10-09 00:57:31 +0000331 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000332}
333
Dan Gohman93f59202010-07-02 00:10:16 +0000334/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000335/// the correctly promoted or expanded types. Assign these registers
336/// consecutive vreg numbers and return the first assigned number.
337///
338/// In the case that the given value has struct or array type, this function
339/// will assign registers for each member or element.
340///
Chris Lattner229907c2011-07-18 04:54:35 +0000341unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopher2ae2de72014-10-09 00:57:31 +0000342 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000343
Dan Gohmana3624b62009-11-23 17:16:22 +0000344 SmallVector<EVT, 4> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +0000345 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000346
347 unsigned FirstReg = 0;
348 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
349 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000350 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000351
Bill Wendling8db01cb2013-06-06 00:11:39 +0000352 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000353 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000354 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000355 if (!FirstReg) FirstReg = R;
356 }
357 }
358 return FirstReg;
359}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000360
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000361/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
362/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
363/// the register's LiveOutInfo is for a smaller bit width, it is extended to
364/// the larger bit width by zero extension. The bit width must be no smaller
365/// than the LiveOutInfo's existing bit width.
366const FunctionLoweringInfo::LiveOutInfo *
367FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
368 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000369 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000370
371 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
372 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000373 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000374
Craig Topperd0af7e82017-04-28 05:31:46 +0000375 if (BitWidth > LOI->Known.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000376 LOI->NumSignBits = 1;
Craig Topperd938fd12017-05-03 22:07:25 +0000377 LOI->Known = LOI->Known.zextOrTrunc(BitWidth);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000378 }
379
380 return LOI;
381}
382
383/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
384/// register based on the LiveOutInfo of its operands.
385void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000386 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000387 if (!Ty->isIntegerTy() || Ty->isVectorTy())
388 return;
389
390 SmallVector<EVT, 1> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +0000391 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000392 assert(ValueVTs.size() == 1 &&
393 "PHIs with non-vector integer types should have a single VT.");
394 EVT IntVT = ValueVTs[0];
395
Bill Wendling8db01cb2013-06-06 00:11:39 +0000396 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000397 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000398 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000399 unsigned BitWidth = IntVT.getSizeInBits();
400
401 unsigned DestReg = ValueMap[PN];
402 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
403 return;
404 LiveOutRegInfo.grow(DestReg);
405 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
406
407 Value *V = PN->getIncomingValue(0);
408 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
409 DestLOI.NumSignBits = 1;
Craig Topperd0af7e82017-04-28 05:31:46 +0000410 DestLOI.Known = KnownBits(BitWidth);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000411 return;
412 }
413
414 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
415 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
416 DestLOI.NumSignBits = Val.getNumSignBits();
Craig Topperd0af7e82017-04-28 05:31:46 +0000417 DestLOI.Known.Zero = ~Val;
418 DestLOI.Known.One = Val;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000419 } else {
420 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
421 "CopyToReg node was created.");
422 unsigned SrcReg = ValueMap[V];
423 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
424 DestLOI.IsValid = false;
425 return;
426 }
427 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
428 if (!SrcLOI) {
429 DestLOI.IsValid = false;
430 return;
431 }
432 DestLOI = *SrcLOI;
433 }
434
Craig Topperd0af7e82017-04-28 05:31:46 +0000435 assert(DestLOI.Known.Zero.getBitWidth() == BitWidth &&
436 DestLOI.Known.One.getBitWidth() == BitWidth &&
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000437 "Masks should have the same bit width as the type.");
438
439 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
440 Value *V = PN->getIncomingValue(i);
441 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
442 DestLOI.NumSignBits = 1;
Craig Topperd0af7e82017-04-28 05:31:46 +0000443 DestLOI.Known = KnownBits(BitWidth);
Eric Christopher0713a9d2011-06-08 23:55:35 +0000444 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000445 }
446
447 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
448 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
449 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
Craig Topperd0af7e82017-04-28 05:31:46 +0000450 DestLOI.Known.Zero &= ~Val;
451 DestLOI.Known.One &= Val;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000452 continue;
453 }
454
455 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
456 "its CopyToReg node was created.");
457 unsigned SrcReg = ValueMap[V];
458 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
459 DestLOI.IsValid = false;
460 return;
461 }
462 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
463 if (!SrcLOI) {
464 DestLOI.IsValid = false;
465 return;
466 }
467 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
Craig Topperd0af7e82017-04-28 05:31:46 +0000468 DestLOI.Known.Zero &= SrcLOI->Known.Zero;
469 DestLOI.Known.One &= SrcLOI->Known.One;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000470 }
471}
472
Devang Patel9d904e12011-09-08 22:59:09 +0000473/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000474/// argument. This overrides previous frame index entry for this argument,
475/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000476void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000477 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000478 ByValArgFrameIndexMap[A] = FI;
479}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000480
Devang Patel9d904e12011-09-08 22:59:09 +0000481/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000482/// If the argument does not have any assigned frame index then 0 is
483/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000484int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Reid Kleckner3a363ff2017-05-09 16:02:20 +0000485 auto I = ByValArgFrameIndexMap.find(A);
Devang Patel86ec8b32010-08-31 22:22:42 +0000486 if (I != ByValArgFrameIndexMap.end())
487 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000488 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Reid Kleckner3a363ff2017-05-09 16:02:20 +0000489 return INT_MAX;
Devang Patel86ec8b32010-08-31 22:22:42 +0000490}
491
Reid Kleckner72ba7042015-10-07 00:27:33 +0000492unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg(
493 const Value *CPI, const TargetRegisterClass *RC) {
494 MachineRegisterInfo &MRI = MF->getRegInfo();
495 auto I = CatchPadExceptionPointers.insert({CPI, 0});
496 unsigned &VReg = I.first->second;
497 if (I.second)
498 VReg = MRI.createVirtualRegister(RC);
499 assert(VReg && "null vreg in exception pointer table!");
500 return VReg;
501}
502
Arnold Schwaighofer3f256582016-10-07 22:06:55 +0000503unsigned
504FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB,
505 const Value *Val) {
506 auto Key = std::make_pair(MBB, Val);
507 auto It = SwiftErrorVRegDefMap.find(Key);
508 // If this is the first use of this swifterror value in this basic block,
509 // create a new virtual register.
510 // After we processed all basic blocks we will satisfy this "upwards exposed
511 // use" by inserting a copy or phi at the beginning of this block.
512 if (It == SwiftErrorVRegDefMap.end()) {
513 auto &DL = MF->getDataLayout();
514 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
515 auto VReg = MF->getRegInfo().createVirtualRegister(RC);
516 SwiftErrorVRegDefMap[Key] = VReg;
517 SwiftErrorVRegUpwardsUse[Key] = VReg;
518 return VReg;
519 } else return It->second;
Manman Rene221a872016-04-05 18:13:16 +0000520}
521
Arnold Schwaighofer3f256582016-10-07 22:06:55 +0000522void FunctionLoweringInfo::setCurrentSwiftErrorVReg(
523 const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) {
524 SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg;
Manman Rene221a872016-04-05 18:13:16 +0000525}
Arnold Schwaighoferae9312c2017-06-15 17:34:42 +0000526
527std::pair<unsigned, bool>
528FunctionLoweringInfo::getOrCreateSwiftErrorVRegDefAt(const Instruction *I) {
529 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, true);
530 auto It = SwiftErrorVRegDefUses.find(Key);
531 if (It == SwiftErrorVRegDefUses.end()) {
532 auto &DL = MF->getDataLayout();
533 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
534 unsigned VReg = MF->getRegInfo().createVirtualRegister(RC);
535 SwiftErrorVRegDefUses[Key] = VReg;
536 return std::make_pair(VReg, true);
537 }
538 return std::make_pair(It->second, false);
539}
540
541std::pair<unsigned, bool>
542FunctionLoweringInfo::getOrCreateSwiftErrorVRegUseAt(const Instruction *I, const MachineBasicBlock *MBB, const Value *Val) {
543 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, false);
544 auto It = SwiftErrorVRegDefUses.find(Key);
545 if (It == SwiftErrorVRegDefUses.end()) {
546 unsigned VReg = getOrCreateSwiftErrorVReg(MBB, Val);
547 SwiftErrorVRegDefUses[Key] = VReg;
548 return std::make_pair(VReg, true);
549 }
550 return std::make_pair(It->second, false);
551}