blob: 0f4a0320f62eae0f5a4210ea99026de88f572f75 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000021#include "llvm/CodeGen/TargetFrameLowering.h"
22#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetLowering.h"
24#include "llvm/CodeGen/TargetRegisterInfo.h"
25#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000026#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000037#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Target/TargetOptions.h"
Dan Gohmana3624b62009-11-23 17:16:22 +000039#include <algorithm>
40using namespace llvm;
41
Chandler Carruth1b9dde02014-04-22 02:02:50 +000042#define DEBUG_TYPE "function-lowering-info"
43
Dan Gohmana3624b62009-11-23 17:16:22 +000044/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
45/// PHI nodes or outside of the basic block that defines it, or used by a
46/// switch or atomic instruction, which may expand to multiple basic blocks.
Dan Gohman913c9982010-04-15 04:33:49 +000047static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
Dan Gohman7c845e42010-04-20 14:50:13 +000048 if (I->use_empty()) return false;
Dan Gohmana3624b62009-11-23 17:16:22 +000049 if (isa<PHINode>(I)) return true;
Dan Gohman913c9982010-04-15 04:33:49 +000050 const BasicBlock *BB = I->getParent();
Chandler Carruthcdf47882014-03-09 03:16:01 +000051 for (const User *U : I->users())
Gabor Greif52617fc2010-07-09 16:08:33 +000052 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
Dan Gohmana3624b62009-11-23 17:16:22 +000053 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +000054
Dan Gohmana3624b62009-11-23 17:16:22 +000055 return false;
56}
57
Jiangning Liuffbc6902014-09-19 05:30:35 +000058static ISD::NodeType getPreferredExtendForValue(const Value *V) {
59 // For the users of the source value being used for compare instruction, if
60 // the number of signed predicate is greater than unsigned predicate, we
61 // prefer to use SIGN_EXTEND.
62 //
63 // With this optimization, we would be able to reduce some redundant sign or
64 // zero extension instruction, and eventually more machine CSE opportunities
65 // can be exposed.
66 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
67 unsigned NumOfSigned = 0, NumOfUnsigned = 0;
68 for (const User *U : V->users()) {
69 if (const auto *CI = dyn_cast<CmpInst>(U)) {
70 NumOfSigned += CI->isSigned();
71 NumOfUnsigned += CI->isUnsigned();
72 }
73 }
74 if (NumOfSigned > NumOfUnsigned)
75 ExtendKind = ISD::SIGN_EXTEND;
76
77 return ExtendKind;
78}
79
Hans Wennborgacb842d2014-03-05 02:43:26 +000080void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
81 SelectionDAG *DAG) {
Dan Gohmana3624b62009-11-23 17:16:22 +000082 Fn = &fn;
83 MF = &mf;
Eric Christopher2ae2de72014-10-09 00:57:31 +000084 TLI = MF->getSubtarget().getTargetLowering();
Dan Gohmana3624b62009-11-23 17:16:22 +000085 RegInfo = &MF->getRegInfo();
Jonas Paulssonf12b9252015-11-28 11:02:32 +000086 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
David Majnemer1ef65402016-03-03 00:01:25 +000087 unsigned StackAlign = TFI->getStackAlignment();
Dan Gohmana3624b62009-11-23 17:16:22 +000088
Dan Gohmand7b5ce32010-07-10 09:00:22 +000089 // Check whether the function can return without sret-demotion.
90 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +000091 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
92 mf.getDataLayout());
Bill Wendling8db01cb2013-06-06 00:11:39 +000093 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
Eric Christopher2ae2de72014-10-09 00:57:31 +000094 Fn->isVarArg(), Outs, Fn->getContext());
Dan Gohmand7b5ce32010-07-10 09:00:22 +000095
David Majnemer1ef65402016-03-03 00:01:25 +000096 // If this personality uses funclets, we need to do a bit more work.
Reid Klecknerf7ad5342016-10-19 17:08:23 +000097 DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects;
David Majnemer1ef65402016-03-03 00:01:25 +000098 EHPersonality Personality = classifyEHPersonality(
99 Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr);
100 if (isFuncletEHPersonality(Personality)) {
101 // Calculate state numbers if we haven't already.
102 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
103 if (Personality == EHPersonality::MSVC_CXX)
104 calculateWinCXXEHStateNumbers(&fn, EHInfo);
105 else if (isAsynchronousEHPersonality(Personality))
106 calculateSEHStateNumbers(&fn, EHInfo);
107 else if (Personality == EHPersonality::CoreCLR)
108 calculateClrEHStateNumbers(&fn, EHInfo);
109
110 // Map all BB references in the WinEH data to MBBs.
111 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
112 for (WinEHHandlerType &H : TBME.HandlerArray) {
113 if (const AllocaInst *AI = H.CatchObj.Alloca)
Reid Klecknerf7ad5342016-10-19 17:08:23 +0000114 CatchObjects.insert({AI, {}}).first->second.push_back(
115 &H.CatchObj.FrameIndex);
David Majnemer1ef65402016-03-03 00:01:25 +0000116 else
117 H.CatchObj.FrameIndex = INT_MAX;
118 }
119 }
120 }
121
Dan Gohmana3624b62009-11-23 17:16:22 +0000122 // Initialize the mapping of values to registers. This is only set up for
123 // instruction values that are used outside of the block that defines
124 // them.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000125 for (const BasicBlock &BB : *Fn) {
126 for (const Instruction &I : BB) {
127 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000128 Type *Ty = AI->getAllocatedType();
129 unsigned Align =
130 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
131 AI->getAlignment());
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000132
133 // Static allocas can be folded into the initial stack frame
134 // adjustment. For targets that don't realign the stack, don't
135 // do this if there is an extra alignment requirement.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000136 if (AI->isStaticAlloca() &&
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000137 (TFI->isStackRealignable() || (Align <= StackAlign))) {
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000138 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000139 uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty);
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000140
141 TySize *= CUI->getZExtValue(); // Get total allocated size.
142 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
David Majnemer1ef65402016-03-03 00:01:25 +0000143 int FrameIndex = INT_MAX;
144 auto Iter = CatchObjects.find(AI);
145 if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000146 FrameIndex = MF->getFrameInfo().CreateFixedObject(
David Majnemer1ef65402016-03-03 00:01:25 +0000147 TySize, 0, /*Immutable=*/false, /*isAliased=*/true);
Matthias Braun941a7052016-07-28 18:40:00 +0000148 MF->getFrameInfo().setObjectAlignment(FrameIndex, Align);
David Majnemer1ef65402016-03-03 00:01:25 +0000149 } else {
150 FrameIndex =
Matthias Braun941a7052016-07-28 18:40:00 +0000151 MF->getFrameInfo().CreateStackObject(TySize, Align, false, AI);
David Majnemer1ef65402016-03-03 00:01:25 +0000152 }
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000153
David Majnemer1ef65402016-03-03 00:01:25 +0000154 StaticAllocaMap[AI] = FrameIndex;
155 // Update the catch handler information.
Reid Klecknerf7ad5342016-10-19 17:08:23 +0000156 if (Iter != CatchObjects.end()) {
157 for (int *CatchObjPtr : Iter->second)
158 *CatchObjPtr = FrameIndex;
159 }
Reid Kleckner0b2bccc2014-09-02 18:42:44 +0000160 } else {
Jonas Paulssonf12b9252015-11-28 11:02:32 +0000161 // FIXME: Overaligned static allocas should be grouped into
162 // a single dynamic allocation instead of using a separate
163 // stack allocation for each one.
Hans Wennborgacb842d2014-03-05 02:43:26 +0000164 if (Align <= StackAlign)
165 Align = 0;
166 // Inform the Frame Information that we have variable-sized objects.
Matthias Braun941a7052016-07-28 18:40:00 +0000167 MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, AI);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000168 }
169 }
170
171 // Look for inline asm that clobbers the SP register.
172 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000173 ImmutableCallSite CS(&I);
Hans Wennborg0c72fd22014-03-05 03:21:23 +0000174 if (isa<InlineAsm>(CS.getCalledValue())) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000175 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Eric Christopher11e4df72015-02-26 22:38:43 +0000176 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Hans Wennborgacb842d2014-03-05 02:43:26 +0000177 std::vector<TargetLowering::AsmOperandInfo> Ops =
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000178 TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS);
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000179 for (TargetLowering::AsmOperandInfo &Op : Ops) {
Hans Wennborgacb842d2014-03-05 02:43:26 +0000180 if (Op.Type == InlineAsm::isClobber) {
181 // Clobbers don't have SDValue operands, hence SDValue().
182 TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
Eric Christopher2ae2de72014-10-09 00:57:31 +0000183 std::pair<unsigned, const TargetRegisterClass *> PhysReg =
Eric Christopher11e4df72015-02-26 22:38:43 +0000184 TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
185 Op.ConstraintVT);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000186 if (PhysReg.first == SP)
Matthias Braun941a7052016-07-28 18:40:00 +0000187 MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
Hans Wennborgacb842d2014-03-05 02:43:26 +0000188 }
189 }
190 }
191 }
192
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000193 // Look for calls to the @llvm.va_start intrinsic. We can omit some
194 // prologue boilerplate for variadic functions that don't examine their
195 // arguments.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000196 if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000197 if (II->getIntrinsicID() == Intrinsic::vastart)
Matthias Braun941a7052016-07-28 18:40:00 +0000198 MF->getFrameInfo().setHasVAStart(true);
Reid Kleckner2d9bb652014-08-22 21:59:26 +0000199 }
200
Eric Christopherbfba5722015-12-16 23:10:53 +0000201 // If we have a musttail call in a variadic function, we need to ensure we
Reid Kleckner16e55412014-08-29 21:42:08 +0000202 // forward implicit register parameters.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000203 if (const auto *CI = dyn_cast<CallInst>(&I)) {
Reid Kleckner16e55412014-08-29 21:42:08 +0000204 if (CI->isMustTailCall() && Fn->isVarArg())
Matthias Braun941a7052016-07-28 18:40:00 +0000205 MF->getFrameInfo().setHasMustTailInVarArgFunc(true);
Reid Kleckner16e55412014-08-29 21:42:08 +0000206 }
207
Dan Gohman1e9362772010-07-16 17:54:27 +0000208 // Mark values used outside their block as exported, by allocating
209 // a virtual register for them.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000210 if (isUsedOutsideOfDefiningBlock(&I))
211 if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I)))
212 InitializeRegForValue(&I);
Dan Gohmana3624b62009-11-23 17:16:22 +0000213
Jiangning Liuffbc6902014-09-19 05:30:35 +0000214 // Decide the preferred extend type for a value.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000215 PreferredExtendType[&I] = getPreferredExtendForValue(&I);
Dan Gohman1e9362772010-07-16 17:54:27 +0000216 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000217 }
Dan Gohman1e9362772010-07-16 17:54:27 +0000218
Dan Gohmana3624b62009-11-23 17:16:22 +0000219 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
220 // also creates the initial PHI MachineInstrs, though none of the input
221 // operands are populated.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000222 for (const BasicBlock &BB : *Fn) {
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000223 // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
224 // are really data, and no instructions can live here.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000225 if (BB.isEHPad()) {
226 const Instruction *PadInst = BB.getFirstNonPHI();
Reid Kleckner6ddae312015-11-05 21:09:49 +0000227 // If this is a non-landingpad EH pad, mark this function as using
David Majnemer7735a6d2015-10-06 23:31:59 +0000228 // funclets.
Reid Kleckner6ddae312015-11-05 21:09:49 +0000229 // FIXME: SEH catchpads do not create funclets, so we could avoid setting
230 // this in such cases in order to improve frame layout.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000231 if (!isa<LandingPadInst>(PadInst)) {
Matthias Braund0ee66c2016-12-01 19:32:15 +0000232 MF->setHasEHFunclets(true);
Matthias Braun941a7052016-07-28 18:40:00 +0000233 MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
Reid Kleckner6ddae312015-11-05 21:09:49 +0000234 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000235 if (isa<CatchSwitchInst>(PadInst)) {
236 assert(&*BB.begin() == PadInst &&
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000237 "WinEHPrepare failed to remove PHIs from imaginary BBs");
238 continue;
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000239 }
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000240 if (isa<FuncletPadInst>(PadInst))
241 assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000242 }
243
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000244 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);
245 MBBMap[&BB] = MBB;
Dan Gohmana3624b62009-11-23 17:16:22 +0000246 MF->push_back(MBB);
247
248 // Transfer the address-taken flag. This is necessary because there could
249 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
250 // the first one should be marked.
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000251 if (BB.hasAddressTaken())
Dan Gohmana3624b62009-11-23 17:16:22 +0000252 MBB->setHasAddressTaken();
253
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000254 // Mark landing pad blocks.
255 if (BB.isEHPad())
256 MBB->setIsEHPad();
257
Dan Gohmana3624b62009-11-23 17:16:22 +0000258 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
259 // appropriate.
Benjamin Kramerc7fc81e2017-12-30 15:27:33 +0000260 for (const PHINode &PN : BB.phis()) {
261 if (PN.use_empty())
Rafael Espindolae53b7d12011-05-13 15:18:06 +0000262 continue;
263
Benjamin Kramerc7fc81e2017-12-30 15:27:33 +0000264 // Skip empty types
265 if (PN.getType()->isEmptyTy())
266 continue;
267
268 DebugLoc DL = PN.getDebugLoc();
269 unsigned PHIReg = ValueMap[&PN];
Dan Gohmana3624b62009-11-23 17:16:22 +0000270 assert(PHIReg && "PHI node does not have an assigned virtual register!");
271
272 SmallVector<EVT, 4> ValueVTs;
Benjamin Kramerc7fc81e2017-12-30 15:27:33 +0000273 ComputeValueVTs(*TLI, MF->getDataLayout(), PN.getType(), ValueVTs);
Reid Kleckner0e7c84c2016-12-30 00:21:38 +0000274 for (EVT VT : ValueVTs) {
Bill Wendling8db01cb2013-06-06 00:11:39 +0000275 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
Eric Christopherfc6de422014-08-05 02:39:49 +0000276 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Dan Gohmana3624b62009-11-23 17:16:22 +0000277 for (unsigned i = 0; i != NumRegisters; ++i)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000278 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
Dan Gohmana3624b62009-11-23 17:16:22 +0000279 PHIReg += NumRegisters;
280 }
281 }
282 }
Dan Gohman69e8e322010-04-14 16:32:56 +0000283
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000284 if (!isFuncletEHPersonality(Personality))
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000285 return;
286
David Majnemer0e90f462016-01-07 04:31:35 +0000287 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
Reid Kleckner78783912015-09-10 00:25:23 +0000288
289 // Map all BB references in the WinEH data to MBBs.
Reid Klecknerb005d282015-09-16 20:16:27 +0000290 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
291 for (WinEHHandlerType &H : TBME.HandlerArray) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000292 if (H.Handler)
293 H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()];
Reid Klecknerb005d282015-09-16 20:16:27 +0000294 }
295 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000296 for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
Reid Kleckner78783912015-09-10 00:25:23 +0000297 if (UME.Cleanup)
David Majnemerbfa5b982015-10-10 00:04:29 +0000298 UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()];
Reid Kleckner78783912015-09-10 00:25:23 +0000299 for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) {
300 const BasicBlock *BB = UME.Handler.get<const BasicBlock *>();
301 UME.Handler = MBBMap[BB];
Reid Kleckner94b704c2015-09-09 21:10:03 +0000302 }
Joseph Tremoulet7f8c1162015-10-06 20:30:33 +0000303 for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
304 const BasicBlock *BB = CME.Handler.get<const BasicBlock *>();
305 CME.Handler = MBBMap[BB];
306 }
David Majnemercde33032015-03-30 22:58:10 +0000307}
308
Dan Gohmana3624b62009-11-23 17:16:22 +0000309/// clear - Clear out all the function-specific state. This returns this
310/// FunctionLoweringInfo to an empty state, ready to be used for a
311/// different function.
312void FunctionLoweringInfo::clear() {
313 MBBMap.clear();
314 ValueMap.clear();
315 StaticAllocaMap.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000316 LiveOutRegInfo.clear();
Cameron Zwarich988faf92011-02-24 10:00:13 +0000317 VisitedBBs.clear();
Evan Cheng6e822452010-04-28 23:08:54 +0000318 ArgDbgValues.clear();
Devang Patel86ec8b32010-08-31 22:22:42 +0000319 ByValArgFrameIndexMap.clear();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000320 RegFixups.clear();
Reid Kleckner3a7a2e42018-03-14 21:54:21 +0000321 RegsWithFixups.clear();
Philip Reames1a1bdb22014-12-02 18:50:36 +0000322 StatepointStackSlots.clear();
Sanjoy Dasc0c59fe2016-03-24 18:57:39 +0000323 StatepointSpillMaps.clear();
Jiangning Liu3b096172014-09-24 03:22:56 +0000324 PreferredExtendType.clear();
Dan Gohmana3624b62009-11-23 17:16:22 +0000325}
326
Dan Gohman93f59202010-07-02 00:10:16 +0000327/// CreateReg - Allocate a single virtual register for the given type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000328unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
Eric Christopherd9134482014-08-04 21:25:23 +0000329 return RegInfo->createVirtualRegister(
Eric Christopher2ae2de72014-10-09 00:57:31 +0000330 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
Dan Gohmana3624b62009-11-23 17:16:22 +0000331}
332
Dan Gohman93f59202010-07-02 00:10:16 +0000333/// CreateRegs - Allocate the appropriate number of virtual registers of
Dan Gohmana3624b62009-11-23 17:16:22 +0000334/// the correctly promoted or expanded types. Assign these registers
335/// consecutive vreg numbers and return the first assigned number.
336///
337/// In the case that the given value has struct or array type, this function
338/// will assign registers for each member or element.
339///
Chris Lattner229907c2011-07-18 04:54:35 +0000340unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
Eric Christopher2ae2de72014-10-09 00:57:31 +0000341 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Bill Wendling0ccf3102013-06-19 20:32:16 +0000342
Dan Gohmana3624b62009-11-23 17:16:22 +0000343 SmallVector<EVT, 4> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +0000344 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
Dan Gohmana3624b62009-11-23 17:16:22 +0000345
346 unsigned FirstReg = 0;
347 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
348 EVT ValueVT = ValueVTs[Value];
Bill Wendling8db01cb2013-06-06 00:11:39 +0000349 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000350
Bill Wendling8db01cb2013-06-06 00:11:39 +0000351 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000352 for (unsigned i = 0; i != NumRegs; ++i) {
Dan Gohman93f59202010-07-02 00:10:16 +0000353 unsigned R = CreateReg(RegisterVT);
Dan Gohmana3624b62009-11-23 17:16:22 +0000354 if (!FirstReg) FirstReg = R;
355 }
356 }
357 return FirstReg;
358}
Dan Gohmanad97b3d2009-11-23 17:42:46 +0000359
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000360/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
361/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
362/// the register's LiveOutInfo is for a smaller bit width, it is extended to
363/// the larger bit width by zero extension. The bit width must be no smaller
364/// than the LiveOutInfo's existing bit width.
365const FunctionLoweringInfo::LiveOutInfo *
366FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
367 if (!LiveOutRegInfo.inBounds(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +0000368 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000369
370 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
371 if (!LOI->IsValid)
Craig Topperc0196b12014-04-14 00:51:57 +0000372 return nullptr;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000373
Craig Topperd0af7e82017-04-28 05:31:46 +0000374 if (BitWidth > LOI->Known.getBitWidth()) {
Cameron Zwarich4c82cd22011-02-25 01:11:01 +0000375 LOI->NumSignBits = 1;
Craig Topperd938fd12017-05-03 22:07:25 +0000376 LOI->Known = LOI->Known.zextOrTrunc(BitWidth);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000377 }
378
379 return LOI;
380}
381
382/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
383/// register based on the LiveOutInfo of its operands.
384void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
Chris Lattner229907c2011-07-18 04:54:35 +0000385 Type *Ty = PN->getType();
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000386 if (!Ty->isIntegerTy() || Ty->isVectorTy())
387 return;
388
389 SmallVector<EVT, 1> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +0000390 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000391 assert(ValueVTs.size() == 1 &&
392 "PHIs with non-vector integer types should have a single VT.");
393 EVT IntVT = ValueVTs[0];
394
Bill Wendling8db01cb2013-06-06 00:11:39 +0000395 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000396 return;
Bill Wendling8db01cb2013-06-06 00:11:39 +0000397 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000398 unsigned BitWidth = IntVT.getSizeInBits();
399
400 unsigned DestReg = ValueMap[PN];
401 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
402 return;
403 LiveOutRegInfo.grow(DestReg);
404 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
405
406 Value *V = PN->getIncomingValue(0);
407 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
408 DestLOI.NumSignBits = 1;
Craig Topperd0af7e82017-04-28 05:31:46 +0000409 DestLOI.Known = KnownBits(BitWidth);
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000410 return;
411 }
412
413 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
414 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
415 DestLOI.NumSignBits = Val.getNumSignBits();
Craig Topperd0af7e82017-04-28 05:31:46 +0000416 DestLOI.Known.Zero = ~Val;
417 DestLOI.Known.One = Val;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000418 } else {
419 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
420 "CopyToReg node was created.");
421 unsigned SrcReg = ValueMap[V];
422 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
423 DestLOI.IsValid = false;
424 return;
425 }
426 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
427 if (!SrcLOI) {
428 DestLOI.IsValid = false;
429 return;
430 }
431 DestLOI = *SrcLOI;
432 }
433
Craig Topperd0af7e82017-04-28 05:31:46 +0000434 assert(DestLOI.Known.Zero.getBitWidth() == BitWidth &&
435 DestLOI.Known.One.getBitWidth() == BitWidth &&
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000436 "Masks should have the same bit width as the type.");
437
438 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
439 Value *V = PN->getIncomingValue(i);
440 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
441 DestLOI.NumSignBits = 1;
Craig Topperd0af7e82017-04-28 05:31:46 +0000442 DestLOI.Known = KnownBits(BitWidth);
Eric Christopher0713a9d2011-06-08 23:55:35 +0000443 return;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000444 }
445
446 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
447 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
448 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
Craig Topperd0af7e82017-04-28 05:31:46 +0000449 DestLOI.Known.Zero &= ~Val;
450 DestLOI.Known.One &= Val;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000451 continue;
452 }
453
454 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
455 "its CopyToReg node was created.");
456 unsigned SrcReg = ValueMap[V];
457 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
458 DestLOI.IsValid = false;
459 return;
460 }
461 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
462 if (!SrcLOI) {
463 DestLOI.IsValid = false;
464 return;
465 }
466 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
Craig Topperd0af7e82017-04-28 05:31:46 +0000467 DestLOI.Known.Zero &= SrcLOI->Known.Zero;
468 DestLOI.Known.One &= SrcLOI->Known.One;
Cameron Zwaricha62fc892011-02-24 10:00:25 +0000469 }
470}
471
Devang Patel9d904e12011-09-08 22:59:09 +0000472/// setArgumentFrameIndex - Record frame index for the byval
Devang Patel86ec8b32010-08-31 22:22:42 +0000473/// argument. This overrides previous frame index entry for this argument,
474/// if any.
Devang Patel9d904e12011-09-08 22:59:09 +0000475void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
Eric Christopher219d51d2012-02-24 01:59:01 +0000476 int FI) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000477 ByValArgFrameIndexMap[A] = FI;
478}
Eric Christopher0713a9d2011-06-08 23:55:35 +0000479
Devang Patel9d904e12011-09-08 22:59:09 +0000480/// getArgumentFrameIndex - Get frame index for the byval argument.
Devang Patel86ec8b32010-08-31 22:22:42 +0000481/// If the argument does not have any assigned frame index then 0 is
482/// returned.
Devang Patel9d904e12011-09-08 22:59:09 +0000483int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
Reid Kleckner3a363ff2017-05-09 16:02:20 +0000484 auto I = ByValArgFrameIndexMap.find(A);
Devang Patel86ec8b32010-08-31 22:22:42 +0000485 if (I != ByValArgFrameIndexMap.end())
486 return I->second;
Eric Christopher18c6be72012-02-23 03:39:43 +0000487 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
Reid Kleckner3a363ff2017-05-09 16:02:20 +0000488 return INT_MAX;
Devang Patel86ec8b32010-08-31 22:22:42 +0000489}
490
Reid Kleckner72ba7042015-10-07 00:27:33 +0000491unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg(
492 const Value *CPI, const TargetRegisterClass *RC) {
493 MachineRegisterInfo &MRI = MF->getRegInfo();
494 auto I = CatchPadExceptionPointers.insert({CPI, 0});
495 unsigned &VReg = I.first->second;
496 if (I.second)
497 VReg = MRI.createVirtualRegister(RC);
498 assert(VReg && "null vreg in exception pointer table!");
499 return VReg;
500}
501
Arnold Schwaighofer3f256582016-10-07 22:06:55 +0000502unsigned
503FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB,
504 const Value *Val) {
505 auto Key = std::make_pair(MBB, Val);
506 auto It = SwiftErrorVRegDefMap.find(Key);
507 // If this is the first use of this swifterror value in this basic block,
508 // create a new virtual register.
509 // After we processed all basic blocks we will satisfy this "upwards exposed
510 // use" by inserting a copy or phi at the beginning of this block.
511 if (It == SwiftErrorVRegDefMap.end()) {
512 auto &DL = MF->getDataLayout();
513 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
514 auto VReg = MF->getRegInfo().createVirtualRegister(RC);
515 SwiftErrorVRegDefMap[Key] = VReg;
516 SwiftErrorVRegUpwardsUse[Key] = VReg;
517 return VReg;
518 } else return It->second;
Manman Rene221a872016-04-05 18:13:16 +0000519}
520
Arnold Schwaighofer3f256582016-10-07 22:06:55 +0000521void FunctionLoweringInfo::setCurrentSwiftErrorVReg(
522 const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) {
523 SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg;
Manman Rene221a872016-04-05 18:13:16 +0000524}
Arnold Schwaighoferae9312c2017-06-15 17:34:42 +0000525
526std::pair<unsigned, bool>
527FunctionLoweringInfo::getOrCreateSwiftErrorVRegDefAt(const Instruction *I) {
528 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, true);
529 auto It = SwiftErrorVRegDefUses.find(Key);
530 if (It == SwiftErrorVRegDefUses.end()) {
531 auto &DL = MF->getDataLayout();
532 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
533 unsigned VReg = MF->getRegInfo().createVirtualRegister(RC);
534 SwiftErrorVRegDefUses[Key] = VReg;
535 return std::make_pair(VReg, true);
536 }
537 return std::make_pair(It->second, false);
538}
539
540std::pair<unsigned, bool>
541FunctionLoweringInfo::getOrCreateSwiftErrorVRegUseAt(const Instruction *I, const MachineBasicBlock *MBB, const Value *Val) {
542 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, false);
543 auto It = SwiftErrorVRegDefUses.find(Key);
544 if (It == SwiftErrorVRegDefUses.end()) {
545 unsigned VReg = getOrCreateSwiftErrorVReg(MBB, Val);
546 SwiftErrorVRegDefUses[Key] = VReg;
547 return std::make_pair(VReg, true);
548 }
549 return std::make_pair(It->second, false);
550}
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000551
552const Value *
553FunctionLoweringInfo::getValueFromVirtualReg(unsigned Vreg) {
554 if (VirtReg2Value.empty()) {
555 for (auto &P : ValueMap) {
556 VirtReg2Value[P.second] = P.first;
557 }
558 }
559 return VirtReg2Value[Vreg];
560}