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