LLVM 23.0.0git
CallLowering.cpp
Go to the documentation of this file.
1//===-- lib/CodeGen/GlobalISel/CallLowering.cpp - Call lowering -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements some simple delegations needed for call lowering.
11///
12//===----------------------------------------------------------------------===//
13
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
27
28#define DEBUG_TYPE "call-lowering"
29
30using namespace llvm;
31
32void CallLowering::anchor() {}
33
34/// Helper function which updates \p Flags when \p AttrFn returns true.
35static void
37 const std::function<bool(Attribute::AttrKind)> &AttrFn) {
38 // TODO: There are missing flags. Add them here.
39 if (AttrFn(Attribute::SExt))
40 Flags.setSExt();
41 if (AttrFn(Attribute::ZExt))
42 Flags.setZExt();
43 if (AttrFn(Attribute::InReg))
44 Flags.setInReg();
45 if (AttrFn(Attribute::StructRet))
46 Flags.setSRet();
47 if (AttrFn(Attribute::Nest))
48 Flags.setNest();
49 if (AttrFn(Attribute::ByVal))
50 Flags.setByVal();
51 if (AttrFn(Attribute::ByRef))
52 Flags.setByRef();
53 if (AttrFn(Attribute::Preallocated))
54 Flags.setPreallocated();
55 if (AttrFn(Attribute::InAlloca))
56 Flags.setInAlloca();
57 if (AttrFn(Attribute::Returned))
58 Flags.setReturned();
59 if (AttrFn(Attribute::SwiftSelf))
60 Flags.setSwiftSelf();
61 if (AttrFn(Attribute::SwiftAsync))
62 Flags.setSwiftAsync();
63 if (AttrFn(Attribute::SwiftError))
64 Flags.setSwiftError();
65}
66
68 unsigned ArgIdx) const {
69 ISD::ArgFlagsTy Flags;
70 addFlagsUsingAttrFn(Flags, [&Call, &ArgIdx](Attribute::AttrKind Attr) {
71 return Call.paramHasAttr(ArgIdx, Attr);
72 });
73 return Flags;
74}
75
78 ISD::ArgFlagsTy Flags;
80 return Call.hasRetAttr(Attr);
81 });
82 return Flags;
83}
84
86 const AttributeList &Attrs,
87 unsigned OpIdx) const {
88 addFlagsUsingAttrFn(Flags, [&Attrs, &OpIdx](Attribute::AttrKind Attr) {
89 return Attrs.hasAttributeAtIndex(OpIdx, Attr);
90 });
91}
92
94 ArrayRef<Register> ResRegs,
96 Register SwiftErrorVReg,
97 std::optional<PtrAuthInfo> PAI,
98 Register ConvergenceCtrlToken,
99 std::function<Register()> GetCalleeReg) const {
100 CallLoweringInfo Info;
101 const DataLayout &DL = MIRBuilder.getDataLayout();
102 MachineFunction &MF = MIRBuilder.getMF();
104 bool CanBeTailCalled = CB.isTailCall() &&
106 (MF.getFunction()
107 .getFnAttribute("disable-tail-calls")
108 .getValueAsString() != "true");
109
110 CallingConv::ID CallConv = CB.getCallingConv();
111 Type *RetTy = CB.getType();
112 bool IsVarArg = CB.getFunctionType()->isVarArg();
113
115 getReturnInfo(CallConv, RetTy, CB.getAttributes(), SplitArgs, DL);
116 Info.CanLowerReturn = canLowerReturn(MF, CallConv, SplitArgs, IsVarArg);
117
118 Info.IsConvergent = CB.isConvergent();
119
120 if (!Info.CanLowerReturn) {
121 // Callee requires sret demotion.
122 insertSRetOutgoingArgument(MIRBuilder, CB, Info);
123
124 // The sret demotion isn't compatible with tail-calls, since the sret
125 // argument points into the caller's stack frame.
126 CanBeTailCalled = false;
127 }
128
129 // First step is to marshall all the function's parameters into the correct
130 // physregs and memory locations. Gather the sequence of argument types that
131 // we'll pass to the assigner function.
132 unsigned i = 0;
133 unsigned NumFixedArgs = CB.getFunctionType()->getNumParams();
134 for (const auto &Arg : CB.args()) {
135 ArgInfo OrigArg{ArgRegs[i], *Arg.get(), i, getAttributesForArgIdx(CB, i)};
136 setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CB);
137 if (i >= NumFixedArgs)
138 OrigArg.Flags[0].setVarArg();
139
140 // If we have an explicit sret argument that is an Instruction, (i.e., it
141 // might point to function-local memory), we can't meaningfully tail-call.
142 if (OrigArg.Flags[0].isSRet() && isa<Instruction>(&Arg))
143 CanBeTailCalled = false;
144
145 Info.OrigArgs.push_back(OrigArg);
146 ++i;
147 }
148
149 // Try looking through a bitcast from one function type to another.
150 // Commonly happens with calls to objc_msgSend().
151 const Value *CalleeV = CB.getCalledOperand()->stripPointerCasts();
152
153 // If IRTranslator chose to drop the ptrauth info, we can turn this into
154 // a direct call.
156 CalleeV = cast<ConstantPtrAuth>(CalleeV)->getPointer();
157 assert(isa<Function>(CalleeV));
158 }
159
160 if (const Function *F = dyn_cast<Function>(CalleeV)) {
161 if (F->hasFnAttribute(Attribute::NonLazyBind)) {
162 LLT Ty = getLLTForType(*F->getType(), DL);
163 Register Reg = MIRBuilder.buildGlobalValue(Ty, F).getReg(0);
164 Info.Callee = MachineOperand::CreateReg(Reg, false);
165 } else {
166 Info.Callee = MachineOperand::CreateGA(F, 0);
167 }
168 } else if (isa<GlobalIFunc>(CalleeV) || isa<GlobalAlias>(CalleeV)) {
169 // IR IFuncs and Aliases can't be forward declared (only defined), so the
170 // callee must be in the same TU and therefore we can direct-call it without
171 // worrying about it being out of range.
172 Info.Callee = MachineOperand::CreateGA(cast<GlobalValue>(CalleeV), 0);
173 } else
174 Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
175
176 Register ReturnHintAlignReg;
177 Align ReturnHintAlign;
178
179 Info.OrigRet = ArgInfo{ResRegs, RetTy, 0, getAttributesForReturn(CB)};
180
181 if (!Info.OrigRet.Ty->isVoidTy()) {
182 setArgFlags(Info.OrigRet, AttributeList::ReturnIndex, DL, CB);
183
184 if (MaybeAlign Alignment = CB.getRetAlign()) {
185 if (*Alignment > Align(1)) {
186 ReturnHintAlignReg = MRI.cloneVirtualRegister(ResRegs[0]);
187 Info.OrigRet.Regs[0] = ReturnHintAlignReg;
188 ReturnHintAlign = *Alignment;
189 }
190 }
191 }
192
193 auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi);
194 if (Bundle && CB.isIndirectCall()) {
195 Info.CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
196 assert(Info.CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
197 }
198
200 Info.DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0]);
201 }
202
203 Info.CB = &CB;
204 Info.KnownCallees = CB.getMetadata(LLVMContext::MD_callees);
205 Info.CallConv = CallConv;
206 Info.SwiftErrorVReg = SwiftErrorVReg;
207 Info.PAI = PAI;
208 Info.ConvergenceCtrlToken = ConvergenceCtrlToken;
209 Info.IsMustTailCall = CB.isMustTailCall();
210 Info.IsTailCall = CanBeTailCalled;
211 Info.IsVarArg = IsVarArg;
212 if (!lowerCall(MIRBuilder, Info))
213 return false;
214
215 if (ReturnHintAlignReg && !Info.LoweredTailCall) {
216 MIRBuilder.buildAssertAlign(ResRegs[0], ReturnHintAlignReg,
217 ReturnHintAlign);
218 }
219
220 return true;
221}
222
223template <typename FuncInfoTy>
225 const DataLayout &DL,
226 const FuncInfoTy &FuncInfo) const {
227 auto &Flags = Arg.Flags[0];
228 const AttributeList &Attrs = FuncInfo.getAttributes();
229 addArgFlagsFromAttributes(Flags, Attrs, OpIdx);
230
232 if (PtrTy) {
233 Flags.setPointer();
234 Flags.setPointerAddrSpace(PtrTy->getPointerAddressSpace());
235 }
236
237 Align MemAlign = DL.getABITypeAlign(Arg.Ty);
238 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
239 Flags.isByRef()) {
240 assert(OpIdx >= AttributeList::FirstArgIndex);
241 unsigned ParamIdx = OpIdx - AttributeList::FirstArgIndex;
242
243 Type *ElementTy = FuncInfo.getParamByValType(ParamIdx);
244 if (!ElementTy)
245 ElementTy = FuncInfo.getParamByRefType(ParamIdx);
246 if (!ElementTy)
247 ElementTy = FuncInfo.getParamInAllocaType(ParamIdx);
248 if (!ElementTy)
249 ElementTy = FuncInfo.getParamPreallocatedType(ParamIdx);
250
251 assert(ElementTy && "Must have byval, inalloca or preallocated type");
252
253 uint64_t MemSize = DL.getTypeAllocSize(ElementTy);
254 if (Flags.isByRef())
255 Flags.setByRefSize(MemSize);
256 else
257 Flags.setByValSize(MemSize);
258
259 // For ByVal, alignment should be passed from FE. BE will guess if
260 // this info is not there but there are cases it cannot get right.
261 if (auto ParamAlign = FuncInfo.getParamStackAlign(ParamIdx))
262 MemAlign = *ParamAlign;
263 else if ((ParamAlign = FuncInfo.getParamAlign(ParamIdx)))
264 MemAlign = *ParamAlign;
265 else
266 MemAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
267 } else if (OpIdx >= AttributeList::FirstArgIndex) {
268 if (auto ParamAlign =
269 FuncInfo.getParamStackAlign(OpIdx - AttributeList::FirstArgIndex))
270 MemAlign = *ParamAlign;
271 }
272 Flags.setMemAlign(MemAlign);
273 Flags.setOrigAlign(DL.getABITypeAlign(Arg.Ty));
274
275 // Don't try to use the returned attribute if the argument is marked as
276 // swiftself, since it won't be passed in x0.
277 if (Flags.isSwiftSelf())
278 Flags.setReturned(false);
279}
280
281template void
283 const DataLayout &DL,
284 const Function &FuncInfo) const;
285
286template void
288 const DataLayout &DL,
289 const CallBase &FuncInfo) const;
290
292 SmallVectorImpl<ArgInfo> &SplitArgs,
293 const DataLayout &DL,
294 CallingConv::ID CallConv,
295 SmallVectorImpl<uint64_t> *Offsets) const {
296 LLVMContext &Ctx = OrigArg.Ty->getContext();
297
298 SmallVector<EVT, 4> SplitVTs;
299 ComputeValueVTs(*TLI, DL, OrigArg.Ty, SplitVTs, /*MemVTs=*/nullptr, Offsets,
300 0);
301
302 if (SplitVTs.size() == 0)
303 return;
304
305 if (SplitVTs.size() == 1) {
306 // No splitting to do, but we want to replace the original type (e.g. [1 x
307 // double] -> double).
308 SplitArgs.emplace_back(OrigArg.Regs[0], SplitVTs[0].getTypeForEVT(Ctx),
309 OrigArg.OrigArgIndex, OrigArg.Flags[0],
310 OrigArg.OrigValue);
311 return;
312 }
313
314 // Create one ArgInfo for each virtual register in the original ArgInfo.
315 assert(OrigArg.Regs.size() == SplitVTs.size() && "Regs / types mismatch");
316
317 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
318 OrigArg.Ty, CallConv, false, DL);
319 for (unsigned i = 0, e = SplitVTs.size(); i < e; ++i) {
320 Type *SplitTy = SplitVTs[i].getTypeForEVT(Ctx);
321 SplitArgs.emplace_back(OrigArg.Regs[i], SplitTy, OrigArg.OrigArgIndex,
322 OrigArg.Flags[0]);
323 if (NeedsRegBlock)
324 SplitArgs.back().Flags[0].setInConsecutiveRegs();
325 }
326
327 SplitArgs.back().Flags[0].setInConsecutiveRegsLast();
328}
329
330/// Pack values \p SrcRegs to cover the vector type result \p DstRegs.
333 ArrayRef<Register> SrcRegs) {
334 MachineRegisterInfo &MRI = *B.getMRI();
335 LLT LLTy = MRI.getType(DstRegs[0]);
336 LLT PartLLT = MRI.getType(SrcRegs[0]);
337
338 // Deal with v3s16 split into v2s16
339 LLT LCMTy = getCoverTy(LLTy, PartLLT);
340 if (LCMTy == LLTy) {
341 // Common case where no padding is needed.
342 assert(DstRegs.size() == 1);
343 return B.buildConcatVectors(DstRegs[0], SrcRegs);
344 }
345
346 // We need to create an unmerge to the result registers, which may require
347 // widening the original value.
348 Register UnmergeSrcReg;
349 if (LCMTy != PartLLT) {
350 assert(DstRegs.size() == 1);
351 return B.buildDeleteTrailingVectorElements(
352 DstRegs[0], B.buildMergeLikeInstr(LCMTy, SrcRegs));
353 } else {
354 // We don't need to widen anything if we're extracting a scalar which was
355 // promoted to a vector e.g. s8 -> v4s8 -> s8
356 assert(SrcRegs.size() == 1);
357 UnmergeSrcReg = SrcRegs[0];
358 }
359
360 int NumDst = LCMTy.getSizeInBits() / LLTy.getSizeInBits();
361
362 SmallVector<Register, 8> PadDstRegs(NumDst);
363 llvm::copy(DstRegs, PadDstRegs.begin());
364
365 // Create the excess dead defs for the unmerge.
366 for (int I = DstRegs.size(); I != NumDst; ++I)
367 PadDstRegs[I] = MRI.createGenericVirtualRegister(LLTy);
368
369 if (PadDstRegs.size() == 1)
370 return B.buildDeleteTrailingVectorElements(DstRegs[0], UnmergeSrcReg);
371 return B.buildUnmerge(PadDstRegs, UnmergeSrcReg);
372}
373
374/// Create a sequence of instructions to combine pieces split into register
375/// typed values to the original IR value. \p OrigRegs contains the destination
376/// value registers of type \p LLTy, and \p Regs contains the legalized pieces
377/// with type \p PartLLT. This is used for incoming values (physregs to vregs).
379 ArrayRef<Register> Regs, LLT LLTy, LLT PartLLT,
380 const ISD::ArgFlagsTy Flags) {
381 MachineRegisterInfo &MRI = *B.getMRI();
382
383 if (PartLLT == LLTy) {
384 // We should have avoided introducing a new virtual register, and just
385 // directly assigned here.
386 assert(OrigRegs[0] == Regs[0]);
387 return;
388 }
389
390 if (PartLLT.getSizeInBits() == LLTy.getSizeInBits() && OrigRegs.size() == 1 &&
391 Regs.size() == 1) {
392 B.buildBitcast(OrigRegs[0], Regs[0]);
393 return;
394 }
395
396 // A vector PartLLT needs extending to LLTy's element size.
397 // E.g. <2 x s64> = G_SEXT <2 x s32>.
398 if (PartLLT.isVector() == LLTy.isVector() &&
399 PartLLT.getScalarSizeInBits() > LLTy.getScalarSizeInBits() &&
400 (!PartLLT.isVector() ||
401 PartLLT.getElementCount() == LLTy.getElementCount()) &&
402 OrigRegs.size() == 1 && Regs.size() == 1) {
403 Register SrcReg = Regs[0];
404
405 LLT LocTy = MRI.getType(SrcReg);
406
407 if (Flags.isSExt()) {
408 SrcReg = B.buildAssertSExt(LocTy, SrcReg, LLTy.getScalarSizeInBits())
409 .getReg(0);
410 } else if (Flags.isZExt()) {
411 SrcReg = B.buildAssertZExt(LocTy, SrcReg, LLTy.getScalarSizeInBits())
412 .getReg(0);
413 }
414
415 // Sometimes pointers are passed zero extended.
416 LLT OrigTy = MRI.getType(OrigRegs[0]);
417 if (OrigTy.isPointer()) {
418 LLT IntPtrTy = LLT::scalar(OrigTy.getSizeInBits());
419 B.buildIntToPtr(OrigRegs[0], B.buildTrunc(IntPtrTy, SrcReg));
420 return;
421 }
422
423 B.buildTrunc(OrigRegs[0], SrcReg);
424 return;
425 }
426
427 if (!LLTy.isVector() && !PartLLT.isVector()) {
428 assert(OrigRegs.size() == 1);
429 LLT OrigTy = MRI.getType(OrigRegs[0]);
430
431 unsigned SrcSize = PartLLT.getSizeInBits().getFixedValue() * Regs.size();
432 if (SrcSize == OrigTy.getSizeInBits())
433 B.buildMergeValues(OrigRegs[0], Regs);
434 else {
435 auto Widened = B.buildMergeLikeInstr(LLT::scalar(SrcSize), Regs);
436 B.buildTrunc(OrigRegs[0], Widened);
437 }
438
439 return;
440 }
441
442 if (PartLLT.isVector()) {
443 assert(OrigRegs.size() == 1);
444 SmallVector<Register> CastRegs(Regs);
445
446 // If PartLLT is a mismatched vector in both number of elements and element
447 // size, e.g. PartLLT == v2s64 and LLTy is v3s32, then first coerce it to
448 // have the same elt type, i.e. v4s32.
449 // TODO: Extend this coersion to element multiples other than just 2.
450 if (TypeSize::isKnownGT(PartLLT.getSizeInBits(), LLTy.getSizeInBits()) &&
451 PartLLT.getScalarSizeInBits() == LLTy.getScalarSizeInBits() * 2 &&
452 Regs.size() == 1) {
453 LLT NewTy = PartLLT.changeElementType(LLTy.getElementType())
454 .changeElementCount(PartLLT.getElementCount() * 2);
455 CastRegs[0] = B.buildBitcast(NewTy, Regs[0]).getReg(0);
456 PartLLT = NewTy;
457 }
458
459 if (LLTy.getScalarType() == PartLLT.getElementType()) {
460 mergeVectorRegsToResultRegs(B, OrigRegs, CastRegs);
461 } else {
462 unsigned I = 0;
463 LLT GCDTy = getGCDType(LLTy, PartLLT);
464
465 // We are both splitting a vector, and bitcasting its element types. Cast
466 // the source pieces into the appropriate number of pieces with the result
467 // element type.
468 for (Register SrcReg : CastRegs)
469 CastRegs[I++] = B.buildBitcast(GCDTy, SrcReg).getReg(0);
470 mergeVectorRegsToResultRegs(B, OrigRegs, CastRegs);
471 }
472
473 return;
474 }
475
476 assert(LLTy.isVector() && !PartLLT.isVector());
477
478 LLT DstEltTy = LLTy.getElementType();
479
480 // Pointer information was discarded. We'll need to coerce some register types
481 // to avoid violating type constraints.
482 LLT RealDstEltTy = MRI.getType(OrigRegs[0]).getElementType();
483
484 assert(DstEltTy.getSizeInBits() == RealDstEltTy.getSizeInBits());
485
486 if (DstEltTy == PartLLT) {
487 // Vector was trivially scalarized.
488
489 if (RealDstEltTy.isPointer()) {
490 for (Register Reg : Regs)
491 MRI.setType(Reg, RealDstEltTy);
492 }
493
494 B.buildBuildVector(OrigRegs[0], Regs);
495 } else if (DstEltTy.getSizeInBits() > PartLLT.getSizeInBits()) {
496 // Deal with vector with 64-bit elements decomposed to 32-bit
497 // registers. Need to create intermediate 64-bit elements.
498 SmallVector<Register, 8> EltMerges;
499 int PartsPerElt =
500 divideCeil(DstEltTy.getSizeInBits(), PartLLT.getSizeInBits());
501 LLT ExtendedPartTy = LLT::scalar(PartLLT.getSizeInBits() * PartsPerElt);
502
503 for (int I = 0, NumElts = LLTy.getNumElements(); I != NumElts; ++I) {
504 auto Merge =
505 B.buildMergeLikeInstr(ExtendedPartTy, Regs.take_front(PartsPerElt));
506 if (ExtendedPartTy.getSizeInBits() > RealDstEltTy.getSizeInBits())
507 Merge = B.buildTrunc(RealDstEltTy, Merge);
508 // Fix the type in case this is really a vector of pointers.
509 MRI.setType(Merge.getReg(0), RealDstEltTy);
510 EltMerges.push_back(Merge.getReg(0));
511 Regs = Regs.drop_front(PartsPerElt);
512 }
513
514 B.buildBuildVector(OrigRegs[0], EltMerges);
515 } else {
516 // Vector was split, and elements promoted to a wider type.
517 // FIXME: Should handle floating point promotions.
518 unsigned NumElts = LLTy.getNumElements();
519 LLT BVType = LLT::fixed_vector(NumElts, PartLLT);
520
521 Register BuildVec;
522 if (NumElts == Regs.size())
523 BuildVec = B.buildBuildVector(BVType, Regs).getReg(0);
524 else {
525 // Vector elements are packed in the inputs.
526 // e.g. we have a <4 x s16> but 2 x s32 in regs.
527 assert(NumElts > Regs.size());
528 LLT SrcEltTy = MRI.getType(Regs[0]);
529
530 LLT OriginalEltTy = MRI.getType(OrigRegs[0]).getElementType();
531
532 // Input registers contain packed elements.
533 // Determine how many elements per reg.
534 assert((SrcEltTy.getSizeInBits() % OriginalEltTy.getSizeInBits()) == 0);
535 unsigned EltPerReg =
536 (SrcEltTy.getSizeInBits() / OriginalEltTy.getSizeInBits());
537
539 BVRegs.reserve(Regs.size() * EltPerReg);
540 for (Register R : Regs) {
541 auto Unmerge = B.buildUnmerge(OriginalEltTy, R);
542 for (unsigned K = 0; K < EltPerReg; ++K)
543 BVRegs.push_back(B.buildAnyExt(PartLLT, Unmerge.getReg(K)).getReg(0));
544 }
545
546 // We may have some more elements in BVRegs, e.g. if we have 2 s32 pieces
547 // for a <3 x s16> vector. We should have less than EltPerReg extra items.
548 if (BVRegs.size() > NumElts) {
549 assert((BVRegs.size() - NumElts) < EltPerReg);
550 BVRegs.truncate(NumElts);
551 }
552 BuildVec = B.buildBuildVector(BVType, BVRegs).getReg(0);
553 }
554 B.buildTrunc(OrigRegs[0], BuildVec);
555 }
556}
557
558/// Create a sequence of instructions to expand the value in \p SrcReg (of type
559/// \p SrcTy) to the types in \p DstRegs (of type \p PartTy). \p ExtendOp should
560/// contain the type of scalar value extension if necessary.
561///
562/// This is used for outgoing values (vregs to physregs)
564 Register SrcReg, LLT SrcTy, LLT PartTy,
565 unsigned ExtendOp = TargetOpcode::G_ANYEXT) {
566 // We could just insert a regular copy, but this is unreachable at the moment.
567 assert(SrcTy != PartTy && "identical part types shouldn't reach here");
568
569 const TypeSize PartSize = PartTy.getSizeInBits();
570
571 if (PartSize == SrcTy.getSizeInBits() && DstRegs.size() == 1) {
572 // TODO: Handle int<->ptr casts. It just happens the ABI lowering
573 // assignments are not pointer aware.
574 B.buildBitcast(DstRegs[0], SrcReg);
575 return;
576 }
577
578 if (PartTy.isVector() == SrcTy.isVector() &&
579 PartTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits()) {
580 assert(DstRegs.size() == 1);
581 B.buildInstr(ExtendOp, {DstRegs[0]}, {SrcReg});
582 return;
583 }
584
585 if (SrcTy.isVector() && !PartTy.isVector() &&
586 TypeSize::isKnownGT(PartSize, SrcTy.getElementType().getSizeInBits()) &&
587 SrcTy.getElementCount() == ElementCount::getFixed(DstRegs.size())) {
588 // Vector was scalarized, and the elements extended.
589 auto UnmergeToEltTy = B.buildUnmerge(SrcTy.getElementType(), SrcReg);
590 for (int i = 0, e = DstRegs.size(); i != e; ++i)
591 B.buildAnyExt(DstRegs[i], UnmergeToEltTy.getReg(i));
592 return;
593 }
594
595 if (SrcTy.isVector() && PartTy.isVector() &&
596 PartTy.getSizeInBits() == SrcTy.getSizeInBits() &&
597 ElementCount::isKnownLT(SrcTy.getElementCount(),
598 PartTy.getElementCount())) {
599 // A coercion like: v2f32 -> v4f32 or nxv2f32 -> nxv4f32
600 Register DstReg = DstRegs.front();
601 B.buildPadVectorWithUndefElements(DstReg, SrcReg);
602 return;
603 }
604
605 LLT GCDTy = getGCDType(SrcTy, PartTy);
606 if (GCDTy == PartTy) {
607 // If this already evenly divisible, we can create a simple unmerge.
608 B.buildUnmerge(DstRegs, SrcReg);
609 return;
610 }
611
612 if (SrcTy.isVector() && !PartTy.isVector() &&
613 SrcTy.getScalarSizeInBits() > PartTy.getSizeInBits()) {
614 LLT ExtTy =
615 LLT::vector(SrcTy.getElementCount(),
616 LLT::scalar(PartTy.getScalarSizeInBits() * DstRegs.size() /
617 SrcTy.getNumElements()));
618 auto Ext = B.buildAnyExt(ExtTy, SrcReg);
619 B.buildUnmerge(DstRegs, Ext);
620 return;
621 }
622
623 MachineRegisterInfo &MRI = *B.getMRI();
624 LLT DstTy = MRI.getType(DstRegs[0]);
625 LLT CoverTy = getCoverTy(SrcTy, PartTy);
626 if (SrcTy.isVector() && DstRegs.size() > 1) {
627 TypeSize FullCoverSize =
628 DstTy.getSizeInBits().multiplyCoefficientBy(DstRegs.size());
629
630 LLT EltTy = SrcTy.getElementType();
631 TypeSize EltSize = EltTy.getSizeInBits();
632 if (FullCoverSize.isKnownMultipleOf(EltSize)) {
633 TypeSize VecSize = FullCoverSize.divideCoefficientBy(EltSize);
634 CoverTy =
635 LLT::vector(ElementCount::get(VecSize, VecSize.isScalable()), EltTy);
636 }
637 }
638
639 if (PartTy.isVector() && CoverTy == PartTy) {
640 assert(DstRegs.size() == 1);
641 B.buildPadVectorWithUndefElements(DstRegs[0], SrcReg);
642 return;
643 }
644
645 const unsigned DstSize = DstTy.getSizeInBits();
646 const unsigned SrcSize = SrcTy.getSizeInBits();
647 unsigned CoveringSize = CoverTy.getSizeInBits();
648
649 Register UnmergeSrc = SrcReg;
650
651 if (!CoverTy.isVector() && CoveringSize != SrcSize) {
652 // For scalars, it's common to be able to use a simple extension.
653 if (SrcTy.isScalar() && DstTy.isScalar()) {
654 CoveringSize = alignTo(SrcSize, DstSize);
655 LLT CoverTy = LLT::scalar(CoveringSize);
656 UnmergeSrc = B.buildInstr(ExtendOp, {CoverTy}, {SrcReg}).getReg(0);
657 } else {
658 // Widen to the common type.
659 // FIXME: This should respect the extend type
660 Register Undef = B.buildUndef(SrcTy).getReg(0);
661 SmallVector<Register, 8> MergeParts(1, SrcReg);
662 for (unsigned Size = SrcSize; Size != CoveringSize; Size += SrcSize)
663 MergeParts.push_back(Undef);
664 UnmergeSrc = B.buildMergeLikeInstr(CoverTy, MergeParts).getReg(0);
665 }
666 }
667
668 if (CoverTy.isVector() && CoveringSize != SrcSize)
669 UnmergeSrc = B.buildPadVectorWithUndefElements(CoverTy, SrcReg).getReg(0);
670
671 B.buildUnmerge(DstRegs, UnmergeSrc);
672}
673
675 ValueHandler &Handler, ValueAssigner &Assigner,
677 CallingConv::ID CallConv, bool IsVarArg,
678 ArrayRef<Register> ThisReturnRegs) const {
679 MachineFunction &MF = MIRBuilder.getMF();
680 const Function &F = MF.getFunction();
682
683 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, F.getContext());
684 if (!determineAssignments(Assigner, Args, CCInfo))
685 return false;
686
687 return handleAssignments(Handler, Args, CCInfo, ArgLocs, MIRBuilder,
688 ThisReturnRegs);
689}
690
692 if (Flags.isSExt())
693 return TargetOpcode::G_SEXT;
694 if (Flags.isZExt())
695 return TargetOpcode::G_ZEXT;
696 return TargetOpcode::G_ANYEXT;
697}
698
701 CCState &CCInfo) const {
702 LLVMContext &Ctx = CCInfo.getContext();
703 const CallingConv::ID CallConv = CCInfo.getCallingConv();
704
705 unsigned NumArgs = Args.size();
706 for (unsigned i = 0; i != NumArgs; ++i) {
707 EVT CurVT = EVT::getEVT(Args[i].Ty);
708
709 MVT NewVT = TLI->getRegisterTypeForCallingConv(Ctx, CallConv, CurVT);
710
711 // If we need to split the type over multiple regs, check it's a scenario
712 // we currently support.
713 unsigned NumParts =
714 TLI->getNumRegistersForCallingConv(Ctx, CallConv, CurVT);
715
716 if (NumParts == 1) {
717 // Try to use the register type if we couldn't assign the VT.
718 if (Assigner.assignArg(i, CurVT, NewVT, NewVT, CCValAssign::Full, Args[i],
719 Args[i].Flags[0], CCInfo))
720 return false;
721 continue;
722 }
723
724 // For incoming arguments (physregs to vregs), we could have values in
725 // physregs (or memlocs) which we want to extract and copy to vregs.
726 // During this, we might have to deal with the LLT being split across
727 // multiple regs, so we have to record this information for later.
728 //
729 // If we have outgoing args, then we have the opposite case. We have a
730 // vreg with an LLT which we want to assign to a physical location, and
731 // we might have to record that the value has to be split later.
732
733 // We're handling an incoming arg which is split over multiple regs.
734 // E.g. passing an s128 on AArch64.
735 ISD::ArgFlagsTy OrigFlags = Args[i].Flags[0];
736 Args[i].Flags.clear();
737
738 for (unsigned Part = 0; Part < NumParts; ++Part) {
739 ISD::ArgFlagsTy Flags = OrigFlags;
740 if (Part == 0) {
741 Flags.setSplit();
742 } else {
743 Flags.setOrigAlign(Align(1));
744 if (Part == NumParts - 1)
745 Flags.setSplitEnd();
746 }
747
748 Args[i].Flags.push_back(Flags);
749 if (Assigner.assignArg(i, CurVT, NewVT, NewVT, CCValAssign::Full, Args[i],
750 Args[i].Flags[Part], CCInfo)) {
751 // Still couldn't assign this smaller part type for some reason.
752 return false;
753 }
754 }
755 }
756
757 return true;
758}
759
762 CCState &CCInfo,
764 MachineIRBuilder &MIRBuilder,
765 ArrayRef<Register> ThisReturnRegs) const {
766 MachineFunction &MF = MIRBuilder.getMF();
768 const Function &F = MF.getFunction();
769 const DataLayout &DL = F.getDataLayout();
770
771 const unsigned NumArgs = Args.size();
772
773 // Stores thunks for outgoing register assignments. This is used so we delay
774 // generating register copies until mem loc assignments are done. We do this
775 // so that if the target is using the delayed stack protector feature, we can
776 // find the split point of the block accurately. E.g. if we have:
777 // G_STORE %val, %memloc
778 // $x0 = COPY %foo
779 // $x1 = COPY %bar
780 // CALL func
781 // ... then the split point for the block will correctly be at, and including,
782 // the copy to $x0. If instead the G_STORE instruction immediately precedes
783 // the CALL, then we'd prematurely choose the CALL as the split point, thus
784 // generating a split block with a CALL that uses undefined physregs.
785 SmallVector<std::function<void()>> DelayedOutgoingRegAssignments;
786
787 for (unsigned i = 0, j = 0; i != NumArgs; ++i, ++j) {
788 assert(j < ArgLocs.size() && "Skipped too many arg locs");
789 CCValAssign &VA = ArgLocs[j];
790 assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
791
792 if (VA.needsCustom()) {
793 std::function<void()> Thunk;
794 unsigned NumArgRegs = Handler.assignCustomValue(
795 Args[i], ArrayRef(ArgLocs).slice(j), &Thunk);
796 if (Thunk)
797 DelayedOutgoingRegAssignments.emplace_back(Thunk);
798 if (!NumArgRegs)
799 return false;
800 j += (NumArgRegs - 1);
801 continue;
802 }
803
804 auto AllocaAddressSpace = MF.getDataLayout().getAllocaAddrSpace();
805
806 const MVT ValVT = VA.getValVT();
807 const MVT LocVT = VA.getLocVT();
808
809 const LLT LocTy(LocVT);
810 const LLT ValTy(ValVT);
811 const LLT NewLLT = Handler.isIncomingArgumentHandler() ? LocTy : ValTy;
812 const EVT OrigVT = EVT::getEVT(Args[i].Ty);
813 const LLT OrigTy = getLLTForType(*Args[i].Ty, DL);
814 const LLT PointerTy = LLT::pointer(
815 AllocaAddressSpace, DL.getPointerSizeInBits(AllocaAddressSpace));
816
817 // Expected to be multiple regs for a single incoming arg.
818 // There should be Regs.size() ArgLocs per argument.
819 // This should be the same as getNumRegistersForCallingConv
820 const unsigned NumParts = Args[i].Flags.size();
821
822 // Now split the registers into the assigned types.
823 Args[i].OrigRegs.assign(Args[i].Regs.begin(), Args[i].Regs.end());
824
825 if (NumParts != 1 || NewLLT != OrigTy) {
826 // If we can't directly assign the register, we need one or more
827 // intermediate values.
828 Args[i].Regs.resize(NumParts);
829
830 // When we have indirect parameter passing we are receiving a pointer,
831 // that points to the actual value, so we need one "temporary" pointer.
832 if (VA.getLocInfo() == CCValAssign::Indirect) {
833 if (Handler.isIncomingArgumentHandler())
834 Args[i].Regs[0] = MRI.createGenericVirtualRegister(PointerTy);
835 } else {
836 // For each split register, create and assign a vreg that will store
837 // the incoming component of the larger value. These will later be
838 // merged to form the final vreg.
839 for (unsigned Part = 0; Part < NumParts; ++Part)
840 Args[i].Regs[Part] = MRI.createGenericVirtualRegister(NewLLT);
841 }
842 }
843
844 assert((j + (NumParts - 1)) < ArgLocs.size() &&
845 "Too many regs for number of args");
846
847 // Coerce into outgoing value types before register assignment.
848 if (!Handler.isIncomingArgumentHandler() && OrigTy != ValTy &&
850 assert(Args[i].OrigRegs.size() == 1);
851 buildCopyToRegs(MIRBuilder, Args[i].Regs, Args[i].OrigRegs[0], OrigTy,
852 ValTy, extendOpFromFlags(Args[i].Flags[0]));
853 }
854
855 bool IndirectParameterPassingHandled = false;
856 bool BigEndianPartOrdering = TLI->hasBigEndianPartOrdering(OrigVT, DL);
857 for (unsigned Part = 0; Part < NumParts; ++Part) {
858 assert((VA.getLocInfo() != CCValAssign::Indirect || Part == 0) &&
859 "Only the first parameter should be processed when "
860 "handling indirect passing!");
861 Register ArgReg = Args[i].Regs[Part];
862 // There should be Regs.size() ArgLocs per argument.
863 unsigned Idx = BigEndianPartOrdering ? NumParts - 1 - Part : Part;
864 CCValAssign &VA = ArgLocs[j + Idx];
865 const ISD::ArgFlagsTy Flags = Args[i].Flags[Part];
866
867 // We found an indirect parameter passing, and we have an
868 // OutgoingValueHandler as our handler (so we are at the call site or the
869 // return value). In this case, start the construction of the following
870 // GMIR, that is responsible for the preparation of indirect parameter
871 // passing:
872 //
873 // %1(indirectly passed type) = The value to pass
874 // %3(pointer) = G_FRAME_INDEX %stack.0
875 // G_STORE %1, %3 :: (store (s128), align 8)
876 //
877 // After this GMIR, the remaining part of the loop body will decide how
878 // to get the value to the caller and we break out of the loop.
879 if (VA.getLocInfo() == CCValAssign::Indirect &&
880 !Handler.isIncomingArgumentHandler()) {
881 Align AlignmentForStored = DL.getPrefTypeAlign(Args[i].Ty);
882 MachineFrameInfo &MFI = MF.getFrameInfo();
883 // Get some space on the stack for the value, so later we can pass it
884 // as a reference.
885 int FrameIdx = MFI.CreateStackObject(OrigTy.getScalarSizeInBits(),
886 AlignmentForStored, false);
887 Register PointerToStackReg =
888 MIRBuilder.buildFrameIndex(PointerTy, FrameIdx).getReg(0);
889 MachinePointerInfo StackPointerMPO =
891 // Store the value in the previously created stack space.
892 MIRBuilder.buildStore(Args[i].OrigRegs[Part], PointerToStackReg,
893 StackPointerMPO,
894 inferAlignFromPtrInfo(MF, StackPointerMPO));
895
896 ArgReg = PointerToStackReg;
897 IndirectParameterPassingHandled = true;
898 }
899
900 if (VA.isMemLoc() && !Flags.isByVal()) {
901 // Individual pieces may have been spilled to the stack and others
902 // passed in registers.
903
904 // TODO: The memory size may be larger than the value we need to
905 // store. We may need to adjust the offset for big endian targets.
906 LLT MemTy = Handler.getStackValueStoreType(DL, VA, Flags);
907
909 Register StackAddr =
911 ? PointerTy.getSizeInBytes()
912 : MemTy.getSizeInBytes(),
913 VA.getLocMemOffset(), MPO, Flags);
914
915 // Finish the handling of indirect passing from the passers
916 // (OutgoingParameterHandler) side.
917 // This branch is needed, so the pointer to the value is loaded onto the
918 // stack.
920 Handler.assignValueToAddress(ArgReg, StackAddr, PointerTy, MPO, VA);
921 else
922 Handler.assignValueToAddress(Args[i], Part, StackAddr, MemTy, MPO,
923 VA);
924 } else if (VA.isMemLoc() && Flags.isByVal()) {
925 assert(Args[i].Regs.size() == 1 && "didn't expect split byval pointer");
926
927 if (Handler.isIncomingArgumentHandler()) {
928 // We just need to copy the frame index value to the pointer.
930 Register StackAddr = Handler.getStackAddress(
931 Flags.getByValSize(), VA.getLocMemOffset(), MPO, Flags);
932 MIRBuilder.buildCopy(Args[i].Regs[0], StackAddr);
933 } else {
934 // For outgoing byval arguments, insert the implicit copy byval
935 // implies, such that writes in the callee do not modify the caller's
936 // value.
937 uint64_t MemSize = Flags.getByValSize();
938 int64_t Offset = VA.getLocMemOffset();
939
940 MachinePointerInfo DstMPO;
941 Register StackAddr =
942 Handler.getStackAddress(MemSize, Offset, DstMPO, Flags);
943
944 MachinePointerInfo SrcMPO(Args[i].OrigValue);
945 if (!Args[i].OrigValue) {
946 // We still need to accurately track the stack address space if we
947 // don't know the underlying value.
948 const LLT PtrTy = MRI.getType(StackAddr);
949 SrcMPO = MachinePointerInfo(PtrTy.getAddressSpace());
950 }
951
952 Align DstAlign = std::max(Flags.getNonZeroByValAlign(),
953 inferAlignFromPtrInfo(MF, DstMPO));
954
955 Align SrcAlign = std::max(Flags.getNonZeroByValAlign(),
956 inferAlignFromPtrInfo(MF, SrcMPO));
957
958 Handler.copyArgumentMemory(Args[i], StackAddr, Args[i].Regs[0],
959 DstMPO, DstAlign, SrcMPO, SrcAlign,
960 MemSize, VA);
961 }
962 } else if (i == 0 && !ThisReturnRegs.empty() &&
963 Handler.isIncomingArgumentHandler() &&
965 Handler.assignValueToReg(ArgReg, ThisReturnRegs[Part], VA);
966 } else if (Handler.isIncomingArgumentHandler()) {
967 Handler.assignValueToReg(ArgReg, VA.getLocReg(), VA);
968 } else {
969 DelayedOutgoingRegAssignments.emplace_back([=, &Handler]() {
970 Handler.assignValueToReg(ArgReg, VA.getLocReg(), VA);
971 });
972 }
973
974 // Finish the handling of indirect parameter passing when receiving
975 // the value (we are in the called function or the caller when receiving
976 // the return value).
977 if (VA.getLocInfo() == CCValAssign::Indirect &&
978 Handler.isIncomingArgumentHandler()) {
979 Align Alignment = DL.getABITypeAlign(Args[i].Ty);
981
982 // Since we are doing indirect parameter passing, we know that the value
983 // in the temporary register is not the value passed to the function,
984 // but rather a pointer to that value. Let's load that value into the
985 // virtual register where the parameter should go.
986 MIRBuilder.buildLoad(Args[i].OrigRegs[0], Args[i].Regs[0], MPO,
987 Alignment);
988
989 IndirectParameterPassingHandled = true;
990 }
991
992 if (IndirectParameterPassingHandled)
993 break;
994 }
995
996 // Now that all pieces have been assigned, re-pack the register typed values
997 // into the original value typed registers. This is only necessary, when
998 // the value was passed in multiple registers, not indirectly.
999 if (Handler.isIncomingArgumentHandler() && OrigVT != LocVT &&
1000 !IndirectParameterPassingHandled) {
1001 // Merge the split registers into the expected larger result vregs of
1002 // the original call.
1003 buildCopyFromRegs(MIRBuilder, Args[i].OrigRegs, Args[i].Regs, OrigTy,
1004 LocTy, Args[i].Flags[0]);
1005 }
1006
1007 j += NumParts - 1;
1008 }
1009 for (auto &Fn : DelayedOutgoingRegAssignments)
1010 Fn();
1011
1012 return true;
1013}
1014
1016 ArrayRef<Register> VRegs, Register DemoteReg,
1017 int FI) const {
1018 MachineFunction &MF = MIRBuilder.getMF();
1020 const DataLayout &DL = MF.getDataLayout();
1021
1022 SmallVector<EVT, 4> SplitVTs;
1024 ComputeValueVTs(*TLI, DL, RetTy, SplitVTs, /*MemVTs=*/nullptr, &Offsets, 0);
1025
1026 assert(VRegs.size() == SplitVTs.size());
1027
1028 unsigned NumValues = SplitVTs.size();
1029 Align BaseAlign = DL.getPrefTypeAlign(RetTy);
1030 Type *RetPtrTy =
1031 PointerType::get(RetTy->getContext(), DL.getAllocaAddrSpace());
1032 LLT OffsetLLTy = getLLTForType(*DL.getIndexType(RetPtrTy), DL);
1033
1035
1036 for (unsigned I = 0; I < NumValues; ++I) {
1037 Register Addr;
1038 MIRBuilder.materializeObjectPtrOffset(Addr, DemoteReg, OffsetLLTy,
1039 Offsets[I]);
1040 auto *MMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad,
1041 MRI.getType(VRegs[I]),
1042 commonAlignment(BaseAlign, Offsets[I]));
1043 MIRBuilder.buildLoad(VRegs[I], Addr, *MMO);
1044 }
1045}
1046
1048 ArrayRef<Register> VRegs,
1049 Register DemoteReg) const {
1050 MachineFunction &MF = MIRBuilder.getMF();
1052 const DataLayout &DL = MF.getDataLayout();
1053
1054 SmallVector<EVT, 4> SplitVTs;
1056 ComputeValueVTs(*TLI, DL, RetTy, SplitVTs, /*MemVTs=*/nullptr, &Offsets, 0);
1057
1058 assert(VRegs.size() == SplitVTs.size());
1059
1060 unsigned NumValues = SplitVTs.size();
1061 Align BaseAlign = DL.getPrefTypeAlign(RetTy);
1062 unsigned AS = DL.getAllocaAddrSpace();
1063 LLT OffsetLLTy = getLLTForType(*DL.getIndexType(RetTy->getContext(), AS), DL);
1064
1065 MachinePointerInfo PtrInfo(AS);
1066
1067 for (unsigned I = 0; I < NumValues; ++I) {
1068 Register Addr;
1069 MIRBuilder.materializeObjectPtrOffset(Addr, DemoteReg, OffsetLLTy,
1070 Offsets[I]);
1071 auto *MMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
1072 MRI.getType(VRegs[I]),
1073 commonAlignment(BaseAlign, Offsets[I]));
1074 MIRBuilder.buildStore(VRegs[I], Addr, *MMO);
1075 }
1076}
1077
1079 const Function &F, SmallVectorImpl<ArgInfo> &SplitArgs, Register &DemoteReg,
1080 MachineRegisterInfo &MRI, const DataLayout &DL) const {
1081 unsigned AS = DL.getAllocaAddrSpace();
1082 DemoteReg = MRI.createGenericVirtualRegister(
1083 LLT::pointer(AS, DL.getPointerSizeInBits(AS)));
1084
1085 Type *PtrTy = PointerType::get(F.getContext(), AS);
1086
1087 SmallVector<EVT, 1> ValueVTs;
1088 ComputeValueVTs(*TLI, DL, PtrTy, ValueVTs);
1089
1090 // NOTE: Assume that a pointer won't get split into more than one VT.
1091 assert(ValueVTs.size() == 1);
1092
1093 ArgInfo DemoteArg(DemoteReg, ValueVTs[0].getTypeForEVT(PtrTy->getContext()),
1095 setArgFlags(DemoteArg, AttributeList::ReturnIndex, DL, F);
1096 DemoteArg.Flags[0].setSRet();
1097 SplitArgs.insert(SplitArgs.begin(), DemoteArg);
1098}
1099
1101 const CallBase &CB,
1102 CallLoweringInfo &Info) const {
1103 const DataLayout &DL = MIRBuilder.getDataLayout();
1104 Type *RetTy = CB.getType();
1105 unsigned AS = DL.getAllocaAddrSpace();
1106 LLT FramePtrTy = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1107
1108 int FI = MIRBuilder.getMF().getFrameInfo().CreateStackObject(
1109 DL.getTypeAllocSize(RetTy), DL.getPrefTypeAlign(RetTy), false);
1110
1111 Register DemoteReg = MIRBuilder.buildFrameIndex(FramePtrTy, FI).getReg(0);
1112 ArgInfo DemoteArg(DemoteReg, PointerType::get(RetTy->getContext(), AS),
1114 setArgFlags(DemoteArg, AttributeList::ReturnIndex, DL, CB);
1115 DemoteArg.Flags[0].setSRet();
1116
1117 Info.OrigArgs.insert(Info.OrigArgs.begin(), DemoteArg);
1118 Info.DemoteStackIndex = FI;
1119 Info.DemoteRegister = DemoteReg;
1120}
1121
1124 CCAssignFn *Fn) const {
1125 for (unsigned I = 0, E = Outs.size(); I < E; ++I) {
1126 MVT VT = MVT::getVT(Outs[I].Ty);
1127 if (Fn(I, VT, VT, CCValAssign::Full, Outs[I].Flags[0], Outs[I].Ty, CCInfo))
1128 return false;
1129 }
1130 return true;
1131}
1132
1134 AttributeList Attrs,
1136 const DataLayout &DL) const {
1137 LLVMContext &Context = RetTy->getContext();
1139
1140 SmallVector<EVT, 4> SplitVTs;
1141 ComputeValueVTs(*TLI, DL, RetTy, SplitVTs);
1142 addArgFlagsFromAttributes(Flags, Attrs, AttributeList::ReturnIndex);
1143
1144 for (EVT VT : SplitVTs) {
1145 unsigned NumParts =
1146 TLI->getNumRegistersForCallingConv(Context, CallConv, VT);
1147 MVT RegVT = TLI->getRegisterTypeForCallingConv(Context, CallConv, VT);
1148 Type *PartTy = EVT(RegVT).getTypeForEVT(Context);
1149
1150 for (unsigned I = 0; I < NumParts; ++I) {
1151 Outs.emplace_back(PartTy, Flags);
1152 }
1153 }
1154}
1155
1157 const auto &F = MF.getFunction();
1158 Type *ReturnType = F.getReturnType();
1159 CallingConv::ID CallConv = F.getCallingConv();
1160
1162 getReturnInfo(CallConv, ReturnType, F.getAttributes(), SplitArgs,
1163 MF.getDataLayout());
1164 return canLowerReturn(MF, CallConv, SplitArgs, F.isVarArg());
1165}
1166
1168 const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask,
1169 const SmallVectorImpl<CCValAssign> &OutLocs,
1170 const SmallVectorImpl<ArgInfo> &OutArgs) const {
1171 for (unsigned i = 0; i < OutLocs.size(); ++i) {
1172 const auto &ArgLoc = OutLocs[i];
1173 // If it's not a register, it's fine.
1174 if (!ArgLoc.isRegLoc())
1175 continue;
1176
1177 MCRegister PhysReg = ArgLoc.getLocReg();
1178
1179 // Only look at callee-saved registers.
1180 if (MachineOperand::clobbersPhysReg(CallerPreservedMask, PhysReg))
1181 continue;
1182
1183 LLVM_DEBUG(
1184 dbgs()
1185 << "... Call has an argument passed in a callee-saved register.\n");
1186
1187 // Check if it was copied from.
1188 const ArgInfo &OutInfo = OutArgs[i];
1189
1190 if (OutInfo.Regs.size() > 1) {
1191 LLVM_DEBUG(
1192 dbgs() << "... Cannot handle arguments in multiple registers.\n");
1193 return false;
1194 }
1195
1196 // Check if we copy the register, walking through copies from virtual
1197 // registers. Note that getDefIgnoringCopies does not ignore copies from
1198 // physical registers.
1199 MachineInstr *RegDef = getDefIgnoringCopies(OutInfo.Regs[0], MRI);
1200 if (!RegDef || RegDef->getOpcode() != TargetOpcode::COPY) {
1201 LLVM_DEBUG(
1202 dbgs()
1203 << "... Parameter was not copied into a VReg, cannot tail call.\n");
1204 return false;
1205 }
1206
1207 // Got a copy. Verify that it's the same as the register we want.
1208 Register CopyRHS = RegDef->getOperand(1).getReg();
1209 if (CopyRHS != PhysReg) {
1210 LLVM_DEBUG(dbgs() << "... Callee-saved register was not copied into "
1211 "VReg, cannot tail call.\n");
1212 return false;
1213 }
1214 }
1215
1216 return true;
1217}
1218
1220 MachineFunction &MF,
1222 ValueAssigner &CalleeAssigner,
1223 ValueAssigner &CallerAssigner) const {
1224 const Function &F = MF.getFunction();
1225 CallingConv::ID CalleeCC = Info.CallConv;
1226 CallingConv::ID CallerCC = F.getCallingConv();
1227
1228 if (CallerCC == CalleeCC)
1229 return true;
1230
1232 CCState CCInfo1(CalleeCC, Info.IsVarArg, MF, ArgLocs1, F.getContext());
1233 if (!determineAssignments(CalleeAssigner, InArgs, CCInfo1))
1234 return false;
1235
1237 CCState CCInfo2(CallerCC, F.isVarArg(), MF, ArgLocs2, F.getContext());
1238 if (!determineAssignments(CallerAssigner, InArgs, CCInfo2))
1239 return false;
1240
1241 // We need the argument locations to match up exactly. If there's more in
1242 // one than the other, then we are done.
1243 if (ArgLocs1.size() != ArgLocs2.size())
1244 return false;
1245
1246 // Make sure that each location is passed in exactly the same way.
1247 for (unsigned i = 0, e = ArgLocs1.size(); i < e; ++i) {
1248 const CCValAssign &Loc1 = ArgLocs1[i];
1249 const CCValAssign &Loc2 = ArgLocs2[i];
1250
1251 // We need both of them to be the same. So if one is a register and one
1252 // isn't, we're done.
1253 if (Loc1.isRegLoc() != Loc2.isRegLoc())
1254 return false;
1255
1256 if (Loc1.isRegLoc()) {
1257 // If they don't have the same register location, we're done.
1258 if (Loc1.getLocReg() != Loc2.getLocReg())
1259 return false;
1260
1261 // They matched, so we can move to the next ArgLoc.
1262 continue;
1263 }
1264
1265 // Loc1 wasn't a RegLoc, so they both must be MemLocs. Check if they match.
1266 if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset())
1267 return false;
1268 }
1269
1270 return true;
1271}
1272
1274 const DataLayout &DL, const CCValAssign &VA, ISD::ArgFlagsTy Flags) const {
1275 const MVT ValVT = VA.getValVT();
1276 if (ValVT != MVT::iPTR) {
1277 LLT ValTy(ValVT);
1278
1279 // We lost the pointeriness going through CCValAssign, so try to restore it
1280 // based on the flags.
1281 if (Flags.isPointer()) {
1282 LLT PtrTy = LLT::pointer(Flags.getPointerAddrSpace(),
1283 ValTy.getScalarSizeInBits());
1284 if (ValVT.isVector() && ValVT.getVectorNumElements() != 1)
1285 return LLT::vector(ValTy.getElementCount(), PtrTy);
1286 return PtrTy;
1287 }
1288
1289 return ValTy;
1290 }
1291
1292 unsigned AddrSpace = Flags.getPointerAddrSpace();
1293 return LLT::pointer(AddrSpace, DL.getPointerSize(AddrSpace));
1294}
1295
1297 const ArgInfo &Arg, Register DstPtr, Register SrcPtr,
1298 const MachinePointerInfo &DstPtrInfo, Align DstAlign,
1299 const MachinePointerInfo &SrcPtrInfo, Align SrcAlign, uint64_t MemSize,
1300 CCValAssign &VA) const {
1301 MachineFunction &MF = MIRBuilder.getMF();
1303 SrcPtrInfo,
1305 SrcAlign);
1306
1308 DstPtrInfo,
1310 MemSize, DstAlign);
1311
1312 const LLT PtrTy = MRI.getType(DstPtr);
1313 const LLT SizeTy = LLT::scalar(PtrTy.getSizeInBits());
1314
1315 auto SizeConst = MIRBuilder.buildConstant(SizeTy, MemSize);
1316 MIRBuilder.buildMemCpy(DstPtr, SrcPtr, SizeConst, *DstMMO, *SrcMMO);
1317}
1318
1320 const CCValAssign &VA,
1321 unsigned MaxSizeBits) {
1322 LLT LocTy{VA.getLocVT()};
1323 LLT ValTy{VA.getValVT()};
1324
1325 if (LocTy.getSizeInBits() == ValTy.getSizeInBits())
1326 return ValReg;
1327
1328 if (LocTy.isScalar() && MaxSizeBits && MaxSizeBits < LocTy.getSizeInBits()) {
1329 if (MaxSizeBits <= ValTy.getSizeInBits())
1330 return ValReg;
1331 LocTy = LLT::scalar(MaxSizeBits);
1332 }
1333
1334 const LLT ValRegTy = MRI.getType(ValReg);
1335 if (ValRegTy.isPointer()) {
1336 // The x32 ABI wants to zero extend 32-bit pointers to 64-bit registers, so
1337 // we have to cast to do the extension.
1338 LLT IntPtrTy = LLT::scalar(ValRegTy.getSizeInBits());
1339 ValReg = MIRBuilder.buildPtrToInt(IntPtrTy, ValReg).getReg(0);
1340 }
1341
1342 switch (VA.getLocInfo()) {
1343 default:
1344 break;
1345 case CCValAssign::Full:
1346 case CCValAssign::BCvt:
1347 // FIXME: bitconverting between vector types may or may not be a
1348 // nop in big-endian situations.
1349 return ValReg;
1350 case CCValAssign::AExt: {
1351 auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
1352 return MIB.getReg(0);
1353 }
1354 case CCValAssign::SExt: {
1355 Register NewReg = MRI.createGenericVirtualRegister(LocTy);
1356 MIRBuilder.buildSExt(NewReg, ValReg);
1357 return NewReg;
1358 }
1359 case CCValAssign::ZExt: {
1360 Register NewReg = MRI.createGenericVirtualRegister(LocTy);
1361 MIRBuilder.buildZExt(NewReg, ValReg);
1362 return NewReg;
1363 }
1364 }
1365 llvm_unreachable("unable to extend register");
1366}
1367
1368void CallLowering::ValueAssigner::anchor() {}
1369
1371 const CCValAssign &VA, Register SrcReg, LLT NarrowTy) {
1372 switch (VA.getLocInfo()) {
1374 return MIRBuilder
1375 .buildAssertZExt(MRI.cloneVirtualRegister(SrcReg), SrcReg,
1376 NarrowTy.getScalarSizeInBits())
1377 .getReg(0);
1378 }
1380 return MIRBuilder
1381 .buildAssertSExt(MRI.cloneVirtualRegister(SrcReg), SrcReg,
1382 NarrowTy.getScalarSizeInBits())
1383 .getReg(0);
1384 break;
1385 }
1386 default:
1387 return SrcReg;
1388 }
1389}
1390
1391/// Check if we can use a basic COPY instruction between the two types.
1392///
1393/// We're currently building on top of the infrastructure using MVT, which loses
1394/// pointer information in the CCValAssign. We accept copies from physical
1395/// registers that have been reported as integers if it's to an equivalent sized
1396/// pointer LLT.
1397static bool isCopyCompatibleType(LLT SrcTy, LLT DstTy) {
1398 if (SrcTy == DstTy)
1399 return true;
1400
1401 if (SrcTy.getSizeInBits() != DstTy.getSizeInBits())
1402 return false;
1403
1404 SrcTy = SrcTy.getScalarType();
1405 DstTy = DstTy.getScalarType();
1406
1407 return (SrcTy.isPointer() && DstTy.isScalar()) ||
1408 (DstTy.isPointer() && SrcTy.isScalar());
1409}
1410
1412 Register ValVReg, Register PhysReg, const CCValAssign &VA) {
1413 const MVT LocVT = VA.getLocVT();
1414 const LLT LocTy(LocVT);
1415 const LLT RegTy = MRI.getType(ValVReg);
1416
1417 if (isCopyCompatibleType(RegTy, LocTy)) {
1418 MIRBuilder.buildCopy(ValVReg, PhysReg);
1419 return;
1420 }
1421
1422 auto Copy = MIRBuilder.buildCopy(LocTy, PhysReg);
1423 auto Hint = buildExtensionHint(VA, Copy.getReg(0), RegTy);
1424 MIRBuilder.buildTrunc(ValVReg, Hint);
1425}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void addFlagsUsingAttrFn(ISD::ArgFlagsTy &Flags, const std::function< bool(Attribute::AttrKind)> &AttrFn)
Helper function which updates Flags when AttrFn returns true.
static void buildCopyToRegs(MachineIRBuilder &B, ArrayRef< Register > DstRegs, Register SrcReg, LLT SrcTy, LLT PartTy, unsigned ExtendOp=TargetOpcode::G_ANYEXT)
Create a sequence of instructions to expand the value in SrcReg (of type SrcTy) to the types in DstRe...
static MachineInstrBuilder mergeVectorRegsToResultRegs(MachineIRBuilder &B, ArrayRef< Register > DstRegs, ArrayRef< Register > SrcRegs)
Pack values SrcRegs to cover the vector type result DstRegs.
static void buildCopyFromRegs(MachineIRBuilder &B, ArrayRef< Register > OrigRegs, ArrayRef< Register > Regs, LLT LLTy, LLT PartLLT, const ISD::ArgFlagsTy Flags)
Create a sequence of instructions to combine pieces split into register typed values to the original ...
static bool isCopyCompatibleType(LLT SrcTy, LLT DstTy)
Check if we can use a basic COPY instruction between the two types.
static unsigned extendOpFromFlags(llvm::ISD::ArgFlagsTy Flags)
This file describes how to lower LLVM calls to machine code calls.
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
MachineInstr unsigned OpIdx
R600 Clause Merge
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file describes how to lower LLVM code to machine code.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition ArrayRef.h:219
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:195
const T & front() const
front - Get the first element.
Definition ArrayRef.h:145
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:122
CCState - This class holds information needed while lowering arguments and return values.
CallingConv::ID getCallingConv() const
LLVMContext & getContext() const
CCValAssign - Represent assignment of one arg/retval to a location.
Register getLocReg() const
LocInfo getLocInfo() const
bool needsCustom() const
int64_t getLocMemOffset() const
unsigned getValNo() const
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
MaybeAlign getRetAlign() const
Extract the alignment of the return value.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
void insertSRetOutgoingArgument(MachineIRBuilder &MIRBuilder, const CallBase &CB, CallLoweringInfo &Info) const
For the call-base described by CB, insert the hidden sret ArgInfo to the OrigArgs field of Info.
void insertSRetLoads(MachineIRBuilder &MIRBuilder, Type *RetTy, ArrayRef< Register > VRegs, Register DemoteReg, int FI) const
Load the returned value from the stack into virtual registers in VRegs.
bool checkReturnTypeForCallConv(MachineFunction &MF) const
Toplevel function to check the return type based on the target calling convention.
bool handleAssignments(ValueHandler &Handler, SmallVectorImpl< ArgInfo > &Args, CCState &CCState, SmallVectorImpl< CCValAssign > &ArgLocs, MachineIRBuilder &MIRBuilder, ArrayRef< Register > ThisReturnRegs={}) const
Use Handler to insert code to handle the argument/return values represented by Args.
bool resultsCompatible(CallLoweringInfo &Info, MachineFunction &MF, SmallVectorImpl< ArgInfo > &InArgs, ValueAssigner &CalleeAssigner, ValueAssigner &CallerAssigner) const
void splitToValueTypes(const ArgInfo &OrigArgInfo, SmallVectorImpl< ArgInfo > &SplitArgs, const DataLayout &DL, CallingConv::ID CallConv, SmallVectorImpl< uint64_t > *Offsets=nullptr) const
Break OrigArgInfo into one or more pieces the calling convention can process, returned in SplitArgs.
virtual bool canLowerReturn(MachineFunction &MF, CallingConv::ID CallConv, SmallVectorImpl< BaseArgInfo > &Outs, bool IsVarArg) const
This hook must be implemented to check whether the return values described by Outs can fit into the r...
virtual bool isTypeIsValidForThisReturn(EVT Ty) const
For targets which support the "returned" parameter attribute, returns true if the given type is a val...
void insertSRetIncomingArgument(const Function &F, SmallVectorImpl< ArgInfo > &SplitArgs, Register &DemoteReg, MachineRegisterInfo &MRI, const DataLayout &DL) const
Insert the hidden sret ArgInfo to the beginning of SplitArgs.
ISD::ArgFlagsTy getAttributesForArgIdx(const CallBase &Call, unsigned ArgIdx) const
bool determineAndHandleAssignments(ValueHandler &Handler, ValueAssigner &Assigner, SmallVectorImpl< ArgInfo > &Args, MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, bool IsVarArg, ArrayRef< Register > ThisReturnRegs={}) const
Invoke ValueAssigner::assignArg on each of the given Args and then use Handler to move them to the as...
void insertSRetStores(MachineIRBuilder &MIRBuilder, Type *RetTy, ArrayRef< Register > VRegs, Register DemoteReg) const
Store the return value given by VRegs into stack starting at the offset specified in DemoteReg.
void addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags, const AttributeList &Attrs, unsigned OpIdx) const
Adds flags to Flags based off of the attributes in Attrs.
bool parametersInCSRMatch(const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< ArgInfo > &OutVals) const
Check whether parameters to a call that are passed in callee saved registers are the same as from the...
void getReturnInfo(CallingConv::ID CallConv, Type *RetTy, AttributeList Attrs, SmallVectorImpl< BaseArgInfo > &Outs, const DataLayout &DL) const
Get the type and the ArgFlags for the split components of RetTy as returned by ComputeValueVTs.
bool determineAssignments(ValueAssigner &Assigner, SmallVectorImpl< ArgInfo > &Args, CCState &CCInfo) const
Analyze the argument list in Args, using Assigner to populate CCInfo.
bool checkReturn(CCState &CCInfo, SmallVectorImpl< BaseArgInfo > &Outs, CCAssignFn *Fn) const
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
ISD::ArgFlagsTy getAttributesForReturn(const CallBase &Call) const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
unsigned getAllocaAddrSpace() const
Definition DataLayout.h:246
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
bool isVarArg() const
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
constexpr LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type.
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
constexpr ElementCount getElementCount() const
constexpr unsigned getAddressSpace() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr LLT changeElementCount(ElementCount EC) const
Return a vector or scalar with the same element type and the new element count.
constexpr LLT getScalarType() const
constexpr TypeSize getSizeInBytes() const
Returns the total size of the type in bytes, i.e.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Machine Value Type.
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
static LLVM_ABI MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Helper class to build MachineInstr.
MachineInstrBuilder buildGlobalValue(const DstOp &Res, const GlobalValue *GV)
Build and insert Res = G_GLOBAL_VALUE GV.
std::optional< MachineInstrBuilder > materializeObjectPtrOffset(Register &Res, Register Op0, const LLT ValueTy, uint64_t Value)
Materialize and insert an instruction with appropriate flags for addressing some offset of an object,...
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildAssertAlign(const DstOp &Res, const SrcOp &Op, Align AlignVal)
Build and insert Res = G_ASSERT_ALIGN Op, AlignVal.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
const DataLayout & getDataLayout() const
Register getReg(unsigned Idx) const
Get the register for the operand index.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Class to represent pointers.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator insert(iterator I, T &&Elt)
void truncate(size_type N)
Like resize, but requires that N is less than size().
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition TypeSize.h:180
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:216
constexpr LeafTy multiplyCoefficientBy(ScalarTy RHS) const
Definition TypeSize.h:256
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:223
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
@ Undef
Value of the register doesn't matter.
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
void * PointerTy
LLVM_ABI MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
Definition Utils.cpp:495
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
LLVM_ABI LLVM_READNONE LLT getCoverTy(LLT OrigTy, LLT TargetTy)
Return smallest type that covers both OrigTy and TargetTy and is multiple of TargetTy.
Definition Utils.cpp:1262
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1883
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy)
Return a type where the total size is the greatest common divisor of OrigTy and TargetTy.
Definition Utils.cpp:1283
LLVM_ABI LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
LLVM_ABI Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition Utils.cpp:905
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
const Value * OrigValue
Optionally track the original IR value for the argument.
SmallVector< Register, 4 > Regs
unsigned OrigArgIndex
Index original Function's argument.
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
SmallVector< ISD::ArgFlagsTy, 4 > Flags
void assignValueToReg(Register ValVReg, Register PhysReg, const CCValAssign &VA) override
Provides a default implementation for argument handling.
Register buildExtensionHint(const CCValAssign &VA, Register SrcReg, LLT NarrowTy)
Insert G_ASSERT_ZEXT/G_ASSERT_SEXT or other hint instruction based on VA, returning the new register ...
Argument handling is mostly uniform between the four places that make these decisions: function forma...
virtual bool assignArg(unsigned ValNo, EVT OrigVT, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, ISD::ArgFlagsTy Flags, CCState &State)
Wrap call to (typically tablegenerated CCAssignFn).
void copyArgumentMemory(const ArgInfo &Arg, Register DstPtr, Register SrcPtr, const MachinePointerInfo &DstPtrInfo, Align DstAlign, const MachinePointerInfo &SrcPtrInfo, Align SrcAlign, uint64_t MemSize, CCValAssign &VA) const
Do a memory copy of MemSize bytes from SrcPtr to DstPtr.
virtual Register getStackAddress(uint64_t MemSize, int64_t Offset, MachinePointerInfo &MPO, ISD::ArgFlagsTy Flags)=0
Materialize a VReg containing the address of the specified stack-based object.
virtual LLT getStackValueStoreType(const DataLayout &DL, const CCValAssign &VA, ISD::ArgFlagsTy Flags) const
Return the in-memory size to write for the argument at VA.
bool isIncomingArgumentHandler() const
Returns true if the handler is dealing with incoming arguments, i.e.
virtual void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy, const MachinePointerInfo &MPO, const CCValAssign &VA)=0
The specified value has been assigned to a stack location.
Register extendRegister(Register ValReg, const CCValAssign &VA, unsigned MaxSizeBits=0)
Extend a register to the location type given in VA, capped at extending to at most MaxSize bits.
virtual unsigned assignCustomValue(ArgInfo &Arg, ArrayRef< CCValAssign > VAs, std::function< void()> *Thunk=nullptr)
Handle custom values, which may be passed into one or more of VAs.
virtual void assignValueToReg(Register ValVReg, Register PhysReg, const CCValAssign &VA)=0
The specified value has been assigned to a physical register, handle the appropriate COPY (either to ...
Extended Value Type.
Definition ValueTypes.h:35
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106