LLVM 22.0.0git
IntrinsicInst.cpp
Go to the documentation of this file.
1//===-- IntrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
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// This file implements methods that make it really easy to deal with intrinsic
10// functions.
11//
12// All intrinsic function calls are instances of the call instruction, so these
13// are all subclasses of the CallInst class. Note that none of these classes
14// has state or virtual methods, which is an important part of this gross/neat
15// hack working.
16//
17// In some cases, arguments to intrinsics need to be generic and are defined as
18// type pointer to empty struct { }*. To access the real item of interest the
19// cast instruction needs to be stripped away.
20//
21//===----------------------------------------------------------------------===//
22
25#include "llvm/IR/Constants.h"
27#include "llvm/IR/Metadata.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Operator.h"
31#include "llvm/IR/Statepoint.h"
32#include <optional>
33
34using namespace llvm;
35
37 switch (IID) {
38 case Intrinsic::objc_autorelease:
39 case Intrinsic::objc_autoreleasePoolPop:
40 case Intrinsic::objc_autoreleasePoolPush:
41 case Intrinsic::objc_autoreleaseReturnValue:
42 case Intrinsic::objc_copyWeak:
43 case Intrinsic::objc_destroyWeak:
44 case Intrinsic::objc_initWeak:
45 case Intrinsic::objc_loadWeak:
46 case Intrinsic::objc_loadWeakRetained:
47 case Intrinsic::objc_moveWeak:
48 case Intrinsic::objc_release:
49 case Intrinsic::objc_retain:
50 case Intrinsic::objc_retainAutorelease:
51 case Intrinsic::objc_retainAutoreleaseReturnValue:
52 case Intrinsic::objc_retainAutoreleasedReturnValue:
53 case Intrinsic::objc_retainBlock:
54 case Intrinsic::objc_storeStrong:
55 case Intrinsic::objc_storeWeak:
56 case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
57 case Intrinsic::objc_retainedObject:
58 case Intrinsic::objc_unretainedObject:
59 case Intrinsic::objc_unretainedPointer:
60 case Intrinsic::objc_retain_autorelease:
61 case Intrinsic::objc_sync_enter:
62 case Intrinsic::objc_sync_exit:
63 return true;
64 default:
65 return false;
66 }
67}
68
69//===----------------------------------------------------------------------===//
70/// DbgVariableIntrinsic - This is the common base class for debug info
71/// intrinsics for variables.
72///
73
76 assert(MD && "First operand of DbgVariableIntrinsic should be non-null.");
77 // If operand is ValueAsMetadata, return a range over just that operand.
78 if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
79 return {location_op_iterator(VAM), location_op_iterator(VAM + 1)};
80 }
81 // If operand is DIArgList, return a range over its args.
82 if (auto *AL = dyn_cast<DIArgList>(MD))
83 return {location_op_iterator(AL->args_begin()),
84 location_op_iterator(AL->args_end())};
85 // Operand must be an empty metadata tuple, so return empty iterator.
86 return {location_op_iterator(static_cast<ValueAsMetadata *>(nullptr)),
87 location_op_iterator(static_cast<ValueAsMetadata *>(nullptr))};
88}
89
93}
94
97}
98
100 Metadata *MD = getRawLocation();
101 assert(MD && "First operand of DbgVariableIntrinsic should be non-null.");
102 if (auto *AL = dyn_cast<DIArgList>(MD))
103 return AL->getArgs()[OpIdx]->getValue();
104 if (isa<MDNode>(MD))
105 return nullptr;
106 assert(
107 isa<ValueAsMetadata>(MD) &&
108 "Attempted to get location operand from DbgVariableIntrinsic with none.");
109 auto *V = cast<ValueAsMetadata>(MD);
110 assert(OpIdx == 0 && "Operand Index must be 0 for a debug intrinsic with a "
111 "single location operand.");
112 return V->getValue();
113}
114
116 return isa<MetadataAsValue>(V) ? dyn_cast<ValueAsMetadata>(
117 cast<MetadataAsValue>(V)->getMetadata())
119}
120
122 Value *NewValue,
123 bool AllowEmpty) {
124 // If OldValue is used as the address part of a dbg.assign intrinsic replace
125 // it with NewValue and return true.
126 auto ReplaceDbgAssignAddress = [this, OldValue, NewValue]() -> bool {
127 auto *DAI = dyn_cast<DbgAssignIntrinsic>(this);
128 if (!DAI || OldValue != DAI->getAddress())
129 return false;
130 DAI->setAddress(NewValue);
131 return true;
132 };
133 bool DbgAssignAddrReplaced = ReplaceDbgAssignAddress();
134 (void)DbgAssignAddrReplaced;
135
136 assert(NewValue && "Values must be non-null");
137 auto Locations = location_ops();
138 auto OldIt = find(Locations, OldValue);
139 if (OldIt == Locations.end()) {
140 if (AllowEmpty || DbgAssignAddrReplaced)
141 return;
142 assert(DbgAssignAddrReplaced &&
143 "OldValue must be dbg.assign addr if unused in DIArgList");
144 return;
145 }
146
147 assert(OldIt != Locations.end() && "OldValue must be a current location");
148 if (!hasArgList()) {
149 Value *NewOperand = isa<MetadataAsValue>(NewValue)
150 ? NewValue
152 getContext(), ValueAsMetadata::get(NewValue));
153 return setArgOperand(0, NewOperand);
154 }
156 ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
157 for (auto *VMD : Locations)
158 MDs.push_back(VMD == *OldIt ? NewOperand : getAsMetadata(VMD));
161}
163 Value *NewValue) {
164 assert(OpIdx < getNumVariableLocationOps() && "Invalid Operand Index");
165 if (!hasArgList()) {
166 Value *NewOperand = isa<MetadataAsValue>(NewValue)
167 ? NewValue
169 getContext(), ValueAsMetadata::get(NewValue));
170 return setArgOperand(0, NewOperand);
171 }
173 ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
174 for (unsigned Idx = 0; Idx < getNumVariableLocationOps(); ++Idx)
175 MDs.push_back(Idx == OpIdx ? NewOperand
179}
180
183 assert(NewExpr->hasAllLocationOps(getNumVariableLocationOps() +
184 NewValues.size()) &&
185 "NewExpr for debug variable intrinsic does not reference every "
186 "location operand.");
187 assert(!is_contained(NewValues, nullptr) && "New values must be non-null");
190 for (auto *VMD : location_ops())
191 MDs.push_back(getAsMetadata(VMD));
192 for (auto *VMD : NewValues)
193 MDs.push_back(getAsMetadata(VMD));
196}
197
198std::optional<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
199 if (auto Fragment = getExpression()->getFragmentInfo())
200 return Fragment->SizeInBits;
201 return getVariable()->getSizeInBits();
202}
203
205 auto *MD = getRawAddress();
206 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
207 return V->getValue();
208
209 // When the value goes to null, it gets replaced by an empty MDNode.
210 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
211 return nullptr;
212}
213
215 setOperand(OpAssignID, MetadataAsValue::get(getContext(), New));
216}
217
219 setOperand(OpAddress,
221}
222
224 if (isKillAddress())
225 return;
227}
228
230 Value *Addr = getAddress();
231 return !Addr || isa<UndefValue>(Addr);
232}
233
235 setOperand(OpValue,
237}
238
241 llvm_unreachable("InstrProfValueProfileInst does not have counters!");
242 return cast<ConstantInt>(getArgOperand(2));
243}
244
247 llvm_unreachable("Please use InstrProfValueProfileInst::getIndex()");
248 return cast<ConstantInt>(getArgOperand(3));
249}
250
252 assert(isa<InstrProfCntrInstBase>(this));
253 setArgOperand(3, ConstantInt::get(Type::getInt32Ty(getContext()), Idx));
254}
255
258 return getArgOperand(4);
259 }
260 const Module *M = getModule();
261 LLVMContext &Context = M->getContext();
262 return ConstantInt::get(Type::getInt64Ty(Context), 1);
263}
264
266 if (isa<InstrProfCallsite>(this))
267 return getArgOperand(4);
268 return nullptr;
269}
270
272 assert(isa<InstrProfCallsite>(this));
273 setArgOperand(4, Callee);
274}
275
276std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
277 unsigned NumOperands = arg_size();
278 Metadata *MD = nullptr;
279 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2));
280 if (MAV)
281 MD = MAV->getMetadata();
282 if (!MD || !isa<MDString>(MD))
283 return std::nullopt;
284 return convertStrToRoundingMode(cast<MDString>(MD)->getString());
285}
286
287std::optional<fp::ExceptionBehavior>
289 unsigned NumOperands = arg_size();
290 Metadata *MD = nullptr;
291 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1));
292 if (MAV)
293 MD = MAV->getMetadata();
294 if (!MD || !isa<MDString>(MD))
295 return std::nullopt;
296 return convertStrToExceptionBehavior(cast<MDString>(MD)->getString());
297}
298
300 std::optional<fp::ExceptionBehavior> Except = getExceptionBehavior();
301 if (Except) {
302 if (*Except != fp::ebIgnore)
303 return false;
304 }
305
306 std::optional<RoundingMode> Rounding = getRoundingMode();
307 if (Rounding) {
308 if (*Rounding != RoundingMode::NearestTiesToEven)
309 return false;
310 }
311
312 return true;
313}
314
316 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
317 if (!MD || !isa<MDString>(MD))
319 return StringSwitch<FCmpInst::Predicate>(cast<MDString>(MD)->getString())
320 .Case("oeq", FCmpInst::FCMP_OEQ)
321 .Case("ogt", FCmpInst::FCMP_OGT)
322 .Case("oge", FCmpInst::FCMP_OGE)
323 .Case("olt", FCmpInst::FCMP_OLT)
324 .Case("ole", FCmpInst::FCMP_OLE)
325 .Case("one", FCmpInst::FCMP_ONE)
326 .Case("ord", FCmpInst::FCMP_ORD)
327 .Case("uno", FCmpInst::FCMP_UNO)
328 .Case("ueq", FCmpInst::FCMP_UEQ)
329 .Case("ugt", FCmpInst::FCMP_UGT)
330 .Case("uge", FCmpInst::FCMP_UGE)
331 .Case("ult", FCmpInst::FCMP_ULT)
332 .Case("ule", FCmpInst::FCMP_ULE)
333 .Case("une", FCmpInst::FCMP_UNE)
335}
336
339}
340
342 // All constrained fp intrinsics have "fpexcept" metadata.
343 unsigned NumArgs = arg_size() - 1;
344
345 // Some intrinsics have "round" metadata.
347 NumArgs -= 1;
348
349 // Compare intrinsics take their predicate as metadata.
350 if (isa<ConstrainedFPCmpIntrinsic>(this))
351 NumArgs -= 1;
352
353 return NumArgs;
354}
355
357 return Intrinsic::isConstrainedFPIntrinsic(I->getIntrinsicID());
358}
359
361 auto GetVectorLengthOfType = [](const Type *T) -> ElementCount {
362 const auto *VT = cast<VectorType>(T);
363 auto ElemCount = VT->getElementCount();
364 return ElemCount;
365 };
366
367 Value *VPMask = getMaskParam();
368 if (!VPMask) {
369 assert((getIntrinsicID() == Intrinsic::vp_merge ||
370 getIntrinsicID() == Intrinsic::vp_select) &&
371 "Unexpected VP intrinsic without mask operand");
372 return GetVectorLengthOfType(getType());
373 }
374 return GetVectorLengthOfType(VPMask->getType());
375}
376
378 if (auto MaskPos = getMaskParamPos(getIntrinsicID()))
379 return getArgOperand(*MaskPos);
380 return nullptr;
381}
382
384 auto MaskPos = getMaskParamPos(getIntrinsicID());
385 setArgOperand(*MaskPos, NewMask);
386}
387
389 if (auto EVLPos = getVectorLengthParamPos(getIntrinsicID()))
390 return getArgOperand(*EVLPos);
391 return nullptr;
392}
393
396 setArgOperand(*EVLPos, NewEVL);
397}
398
399std::optional<unsigned>
401 switch (IntrinsicID) {
402 default:
403 return std::nullopt;
404
405#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
406 case Intrinsic::VPID: \
407 return MASKPOS;
408#include "llvm/IR/VPIntrinsics.def"
409 }
410}
411
412std::optional<unsigned>
414 switch (IntrinsicID) {
415 default:
416 return std::nullopt;
417
418#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
419 case Intrinsic::VPID: \
420 return VLENPOS;
421#include "llvm/IR/VPIntrinsics.def"
422 }
423}
424
425/// \return the alignment of the pointer used by this load/store/gather or
426/// scatter.
428 std::optional<unsigned> PtrParamOpt =
430 assert(PtrParamOpt && "no pointer argument!");
431 return getParamAlign(*PtrParamOpt);
432}
433
434/// \return The pointer operand of this load,store, gather or scatter.
436 if (auto PtrParamOpt = getMemoryPointerParamPos(getIntrinsicID()))
437 return getArgOperand(*PtrParamOpt);
438 return nullptr;
439}
440
441std::optional<unsigned>
443 switch (VPID) {
444 default:
445 return std::nullopt;
446 case Intrinsic::vp_store:
447 case Intrinsic::vp_scatter:
448 case Intrinsic::experimental_vp_strided_store:
449 return 1;
450 case Intrinsic::vp_load:
451 case Intrinsic::vp_load_ff:
452 case Intrinsic::vp_gather:
453 case Intrinsic::experimental_vp_strided_load:
454 return 0;
455 }
456}
457
458/// \return The data (payload) operand of this store or scatter.
460 auto DataParamOpt = getMemoryDataParamPos(getIntrinsicID());
461 if (!DataParamOpt)
462 return nullptr;
463 return getArgOperand(*DataParamOpt);
464}
465
467 switch (VPID) {
468 default:
469 return std::nullopt;
470 case Intrinsic::vp_store:
471 case Intrinsic::vp_scatter:
472 case Intrinsic::experimental_vp_strided_store:
473 return 0;
474 }
475}
476
478 switch (ID) {
479 default:
480 break;
481#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
482 case Intrinsic::VPID: \
483 return true;
484#include "llvm/IR/VPIntrinsics.def"
485 }
486 return false;
487}
488
490 return ::isVPIntrinsic(ID);
491}
492
493// Equivalent non-predicated opcode
494constexpr static std::optional<unsigned>
496 switch (ID) {
497 default:
498 break;
499#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
500#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) return Instruction::OPC;
501#define END_REGISTER_VP_INTRINSIC(VPID) break;
502#include "llvm/IR/VPIntrinsics.def"
503 }
504 return std::nullopt;
505}
506
507std::optional<unsigned>
509 return ::getFunctionalOpcodeForVP(ID);
510}
511
512// Equivalent non-predicated intrinsic ID
513constexpr static std::optional<Intrinsic::ID>
515 switch (ID) {
516 default:
517 break;
518#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
519#define VP_PROPERTY_FUNCTIONAL_INTRINSIC(INTRIN) return Intrinsic::INTRIN;
520#define END_REGISTER_VP_INTRINSIC(VPID) break;
521#include "llvm/IR/VPIntrinsics.def"
522 }
523 return std::nullopt;
524}
525
526std::optional<Intrinsic::ID>
528 return ::getFunctionalIntrinsicIDForVP(ID);
529}
530
532 switch (ID) {
533 default:
534 break;
535#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
536#define VP_PROPERTY_NO_FUNCTIONAL return true;
537#define END_REGISTER_VP_INTRINSIC(VPID) break;
538#include "llvm/IR/VPIntrinsics.def"
539 }
540 return false;
541}
542
543// All VP intrinsics should have an equivalent non-VP opcode or intrinsic
544// defined, or be marked that they don't have one.
545#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) \
546 static_assert(doesVPHaveNoFunctionalEquivalent(Intrinsic::VPID) || \
547 getFunctionalOpcodeForVP(Intrinsic::VPID) || \
548 getFunctionalIntrinsicIDForVP(Intrinsic::VPID));
549#include "llvm/IR/VPIntrinsics.def"
550
551// Equivalent non-predicated constrained intrinsic
552std::optional<Intrinsic::ID>
554 switch (ID) {
555 default:
556 break;
557#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
558#define VP_PROPERTY_CONSTRAINEDFP(CID) return Intrinsic::CID;
559#define END_REGISTER_VP_INTRINSIC(VPID) break;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562 return std::nullopt;
563}
564
566 switch (IROPC) {
567 default:
568 break;
569
570#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) break;
571#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) case Instruction::OPC:
572#define END_REGISTER_VP_INTRINSIC(VPID) return Intrinsic::VPID;
573#include "llvm/IR/VPIntrinsics.def"
574 }
576}
577
579 if (::isVPIntrinsic(Id))
580 return Id;
581
582 switch (Id) {
583 default:
584 break;
585#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) break;
586#define VP_PROPERTY_FUNCTIONAL_INTRINSIC(INTRIN) case Intrinsic::INTRIN:
587#define END_REGISTER_VP_INTRINSIC(VPID) return Intrinsic::VPID;
588#include "llvm/IR/VPIntrinsics.def"
589 }
591}
592
594 return ::getForIntrinsic(Id);
595}
596
598 using namespace PatternMatch;
599
601
602 // No vlen param - no lanes masked-off by it.
603 auto *VLParam = getVectorLengthParam();
604 if (!VLParam)
605 return true;
606
607 // Note that the VP intrinsic causes undefined behavior if the Explicit Vector
608 // Length parameter is strictly greater-than the number of vector elements of
609 // the operation. This function returns true when this is detected statically
610 // in the IR.
611
612 // Check whether "W == vscale * EC.getKnownMinValue()"
613 if (EC.isScalable()) {
614 // Compare vscale patterns
615 uint64_t VScaleFactor;
616 if (match(VLParam, m_Mul(m_VScale(), m_ConstantInt(VScaleFactor))))
617 return VScaleFactor >= EC.getKnownMinValue();
618 return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale());
619 }
620
621 // standard SIMD operation
622 const auto *VLConst = dyn_cast<ConstantInt>(VLParam);
623 if (!VLConst)
624 return false;
625
626 uint64_t VLNum = VLConst->getZExtValue();
627 if (VLNum >= EC.getKnownMinValue())
628 return true;
629
630 return false;
631}
632
634 Module *M, Intrinsic::ID VPID, Type *ReturnType, ArrayRef<Value *> Params) {
635 assert(isVPIntrinsic(VPID) && "not a VP intrinsic");
636 Function *VPFunc;
637 switch (VPID) {
638 default: {
639 Type *OverloadTy = Params[0]->getType();
641 OverloadTy =
642 Params[*VPReductionIntrinsic::getVectorParamPos(VPID)]->getType();
643
644 VPFunc = Intrinsic::getOrInsertDeclaration(M, VPID, OverloadTy);
645 break;
646 }
647 case Intrinsic::vp_trunc:
648 case Intrinsic::vp_sext:
649 case Intrinsic::vp_zext:
650 case Intrinsic::vp_fptoui:
651 case Intrinsic::vp_fptosi:
652 case Intrinsic::vp_uitofp:
653 case Intrinsic::vp_sitofp:
654 case Intrinsic::vp_fptrunc:
655 case Intrinsic::vp_fpext:
656 case Intrinsic::vp_ptrtoint:
657 case Intrinsic::vp_inttoptr:
658 case Intrinsic::vp_lrint:
659 case Intrinsic::vp_llrint:
660 case Intrinsic::vp_cttz_elts:
662 M, VPID, {ReturnType, Params[0]->getType()});
663 break;
664 case Intrinsic::vp_is_fpclass:
665 VPFunc = Intrinsic::getOrInsertDeclaration(M, VPID, {Params[0]->getType()});
666 break;
667 case Intrinsic::vp_merge:
668 case Intrinsic::vp_select:
669 VPFunc = Intrinsic::getOrInsertDeclaration(M, VPID, {Params[1]->getType()});
670 break;
671 case Intrinsic::vp_load:
673 M, VPID, {ReturnType, Params[0]->getType()});
674 break;
675 case Intrinsic::vp_load_ff:
677 M, VPID, {ReturnType->getStructElementType(0), Params[0]->getType()});
678 break;
679 case Intrinsic::experimental_vp_strided_load:
681 M, VPID, {ReturnType, Params[0]->getType(), Params[1]->getType()});
682 break;
683 case Intrinsic::vp_gather:
685 M, VPID, {ReturnType, Params[0]->getType()});
686 break;
687 case Intrinsic::vp_store:
689 M, VPID, {Params[0]->getType(), Params[1]->getType()});
690 break;
691 case Intrinsic::experimental_vp_strided_store:
693 M, VPID,
694 {Params[0]->getType(), Params[1]->getType(), Params[2]->getType()});
695 break;
696 case Intrinsic::vp_scatter:
698 M, VPID, {Params[0]->getType(), Params[1]->getType()});
699 break;
700 case Intrinsic::experimental_vp_splat:
701 VPFunc = Intrinsic::getOrInsertDeclaration(M, VPID, ReturnType);
702 break;
703 }
704 assert(VPFunc && "Could not declare VP intrinsic");
705 return VPFunc;
706}
707
709 switch (ID) {
710 case Intrinsic::vp_reduce_add:
711 case Intrinsic::vp_reduce_mul:
712 case Intrinsic::vp_reduce_and:
713 case Intrinsic::vp_reduce_or:
714 case Intrinsic::vp_reduce_xor:
715 case Intrinsic::vp_reduce_smax:
716 case Intrinsic::vp_reduce_smin:
717 case Intrinsic::vp_reduce_umax:
718 case Intrinsic::vp_reduce_umin:
719 case Intrinsic::vp_reduce_fmax:
720 case Intrinsic::vp_reduce_fmin:
721 case Intrinsic::vp_reduce_fmaximum:
722 case Intrinsic::vp_reduce_fminimum:
723 case Intrinsic::vp_reduce_fadd:
724 case Intrinsic::vp_reduce_fmul:
725 return true;
726 default:
727 return false;
728 }
729}
730
732 // All of the vp.casts correspond to instructions
733 if (std::optional<unsigned> Opc = getFunctionalOpcodeForVP(ID))
734 return Instruction::isCast(*Opc);
735 return false;
736}
737
739 switch (ID) {
740 default:
741 return false;
742 case Intrinsic::vp_fcmp:
743 case Intrinsic::vp_icmp:
744 return true;
745 }
746}
747
749 switch (ID) {
750 default:
751 break;
752#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
753#define VP_PROPERTY_BINARYOP return true;
754#define END_REGISTER_VP_INTRINSIC(VPID) break;
755#include "llvm/IR/VPIntrinsics.def"
756 }
757 return false;
758}
759
761 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
762 if (!MD || !isa<MDString>(MD))
764 return StringSwitch<ICmpInst::Predicate>(cast<MDString>(MD)->getString())
765 .Case("eq", ICmpInst::ICMP_EQ)
766 .Case("ne", ICmpInst::ICMP_NE)
767 .Case("ugt", ICmpInst::ICMP_UGT)
768 .Case("uge", ICmpInst::ICMP_UGE)
769 .Case("ult", ICmpInst::ICMP_ULT)
770 .Case("ule", ICmpInst::ICMP_ULE)
771 .Case("sgt", ICmpInst::ICMP_SGT)
772 .Case("sge", ICmpInst::ICMP_SGE)
773 .Case("slt", ICmpInst::ICMP_SLT)
774 .Case("sle", ICmpInst::ICMP_SLE)
776}
777
780 return getIntrinsicID() == Intrinsic::vp_fcmp
783}
784
787}
788
791}
792
793std::optional<unsigned>
795 if (isVPReduction(ID))
796 return 1;
797 return std::nullopt;
798}
799
800std::optional<unsigned>
802 if (isVPReduction(ID))
803 return 0;
804 return std::nullopt;
805}
806
808 switch (getIntrinsicID()) {
809 case Intrinsic::uadd_with_overflow:
810 case Intrinsic::sadd_with_overflow:
811 case Intrinsic::uadd_sat:
812 case Intrinsic::sadd_sat:
813 return Instruction::Add;
814 case Intrinsic::usub_with_overflow:
815 case Intrinsic::ssub_with_overflow:
816 case Intrinsic::usub_sat:
817 case Intrinsic::ssub_sat:
818 return Instruction::Sub;
819 case Intrinsic::umul_with_overflow:
820 case Intrinsic::smul_with_overflow:
821 return Instruction::Mul;
822 default:
823 llvm_unreachable("Invalid intrinsic");
824 }
825}
826
828 switch (getIntrinsicID()) {
829 case Intrinsic::sadd_with_overflow:
830 case Intrinsic::ssub_with_overflow:
831 case Intrinsic::smul_with_overflow:
832 case Intrinsic::sadd_sat:
833 case Intrinsic::ssub_sat:
834 return true;
835 default:
836 return false;
837 }
838}
839
841 if (isSigned())
843 else
845}
846
848 const Value *Token = getArgOperand(0);
849 if (isa<UndefValue>(Token))
850 return Token;
851
852 // Treat none token as if it was undef here
853 if (isa<ConstantTokenNone>(Token))
854 return UndefValue::get(Token->getType());
855
856 // This takes care both of relocates for call statepoints and relocates
857 // on normal path of invoke statepoint.
858 if (!isa<LandingPadInst>(Token))
859 return cast<GCStatepointInst>(Token);
860
861 // This relocate is on exceptional path of an invoke statepoint
862 const BasicBlock *InvokeBB =
863 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
864
865 assert(InvokeBB && "safepoints should have unique landingpads");
866 assert(InvokeBB->getTerminator() &&
867 "safepoint block should be well formed");
868
869 return cast<GCStatepointInst>(InvokeBB->getTerminator());
870}
871
873 auto Statepoint = getStatepoint();
874 if (isa<UndefValue>(Statepoint))
875 return UndefValue::get(Statepoint->getType());
876
877 auto *GCInst = cast<GCStatepointInst>(Statepoint);
878 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
879 return *(Opt->Inputs.begin() + getBasePtrIndex());
880 return *(GCInst->arg_begin() + getBasePtrIndex());
881}
882
884 auto *Statepoint = getStatepoint();
885 if (isa<UndefValue>(Statepoint))
886 return UndefValue::get(Statepoint->getType());
887
888 auto *GCInst = cast<GCStatepointInst>(Statepoint);
889 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
890 return *(Opt->Inputs.begin() + getDerivedPtrIndex());
891 return *(GCInst->arg_begin() + getDerivedPtrIndex());
892}
893
895 Module *M = BB.getModule();
897 M, llvm::Intrinsic::experimental_convergence_anchor);
898 auto *Call = CallInst::Create(Fn, "", BB.getFirstInsertionPt());
899 return cast<ConvergenceControlInst>(Call);
900}
901
903 Module *M = BB.getModule();
905 M, llvm::Intrinsic::experimental_convergence_entry);
906 auto *Call = CallInst::Create(Fn, "", BB.getFirstInsertionPt());
907 return cast<ConvergenceControlInst>(Call);
908}
909
912 ConvergenceControlInst *ParentToken) {
913 Module *M = BB.getModule();
915 M, llvm::Intrinsic::experimental_convergence_loop);
916 llvm::Value *BundleArgs[] = {ParentToken};
917 llvm::OperandBundleDef OB("convergencectrl", BundleArgs);
918 auto *Call = CallInst::Create(Fn, {}, {OB}, "", BB.getFirstInsertionPt());
919 return cast<ConvergenceControlInst>(Call);
920}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Addr
Module.h This file contains the declarations for the Module class.
static constexpr std::optional< Intrinsic::ID > getFunctionalIntrinsicIDForVP(Intrinsic::ID ID)
static constexpr std::optional< unsigned > getFunctionalOpcodeForVP(Intrinsic::ID ID)
static ICmpInst::Predicate getIntPredicateFromMD(const Value *Op)
static constexpr bool doesVPHaveNoFunctionalEquivalent(Intrinsic::ID ID)
constexpr bool isVPIntrinsic(Intrinsic::ID ID)
static constexpr Intrinsic::ID getForIntrinsic(Intrinsic::ID Id)
static FCmpInst::Predicate getFPPredicateFromMD(const Value *Op)
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
MachineInstr unsigned OpIdx
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:393
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:233
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:248
LLVM_ABI unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
LLVM_ABI bool isSigned() const
Whether the intrinsic is signed or unsigned.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Definition: InstrTypes.h:1778
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1297
unsigned arg_size() const
Definition: InstrTypes.h:1290
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:681
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:692
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:686
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:689
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:685
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:687
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:706
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:694
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:691
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:688
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
LLVM_ABI FCmpInst::Predicate getPredicate() const
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI std::optional< RoundingMode > getRoundingMode() const
LLVM_ABI unsigned getNonMetadataArgCount() const
static LLVM_ABI bool classof(const IntrinsicInst *I)
LLVM_ABI bool isDefaultFPEnvironment() const
Represents calls to the llvm.experimintal.convergence.* intrinsics.
static LLVM_ABI ConvergenceControlInst * CreateAnchor(BasicBlock &BB)
static LLVM_ABI ConvergenceControlInst * CreateLoop(BasicBlock &BB, ConvergenceControlInst *Parent)
static LLVM_ABI ConvergenceControlInst * CreateEntry(BasicBlock &BB)
static LLVM_ABI DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
Assignment ID.
DWARF expression.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
This class represents an Operation in the Expression.
LLVM_ABI void setValue(Value *V)
LLVM_ABI void setAssignId(DIAssignID *New)
LLVM_ABI void setKillAddress()
Kill the address component.
LLVM_ABI bool isKillAddress() const
Check whether this kills the address component.
Metadata * getRawAddress() const
LLVM_ABI Value * getAddress() const
LLVM_ABI void setAddress(Value *V)
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
LLVM_ABI void addVariableLocationOps(ArrayRef< Value * > NewValues, DIExpression *NewExpr)
Adding a new location operand will always result in this intrinsic using an ArgList,...
LLVM_ABI void replaceVariableLocationOp(Value *OldValue, Value *NewValue, bool AllowEmpty=false)
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DILocalVariable * getVariable() const
unsigned getNumVariableLocationOps() const
void setOperand(unsigned i, Value *v)
LLVM_ABI std::optional< uint64_t > getFragmentSizeInBits() const
Get the size (in bits) of the variable, or fragment of the variable that is described.
DIExpression * getExpression() const
void setArgOperand(unsigned i, Value *v)
RawLocationWrapper getWrappedLocation() const
LLVM_ABI const Value * getStatepoint() const
The statepoint with which this gc.relocate is associated.
LLVM_ABI Value * getBasePtr() const
unsigned getBasePtrIndex() const
The index into the associate statepoint's argument list which contains the base pointer of the pointe...
LLVM_ABI Value * getDerivedPtr() const
unsigned getDerivedPtrIndex() const
The index into the associate statepoint's argument list which contains the pointer whose relocation t...
LLVM_ABI void setCallee(Value *Callee)
LLVM_ABI Value * getCallee() const
LLVM_ABI ConstantInt * getIndex() const
LLVM_ABI void setIndex(uint32_t Idx)
LLVM_ABI ConstantInt * getNumCounters() const
static bool classof(const IntrinsicInst *I)
LLVM_ABI Value * getStep() const
static bool classof(const IntrinsicInst *I)
bool isCast() const
Definition: Instruction.h:321
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:78
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
static LLVM_ABI bool mayLowerToFunctionCall(Intrinsic::ID IID)
Check if the intrinsic might lower into a regular function call in the course of IR transformations.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
Root of the metadata hierarchy.
Definition: Metadata.h:63
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A container for an operand bundle being viewed as a set of values rather than a set of uses.
Definition: InstrTypes.h:1069
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
Metadata * getRawLocation() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
unsigned getNumOperands() const
Definition: User.h:254
static LLVM_ABI bool isVPBinOp(Intrinsic::ID ID)
static LLVM_ABI bool isVPCast(Intrinsic::ID ID)
static LLVM_ABI bool isVPCmp(Intrinsic::ID ID)
LLVM_ABI CmpInst::Predicate getPredicate() const
static LLVM_ABI std::optional< unsigned > getMaskParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI bool canIgnoreVectorLengthParam() const
LLVM_ABI void setMaskParam(Value *)
static LLVM_ABI std::optional< unsigned > getFunctionalOpcodeForVP(Intrinsic::ID ID)
static LLVM_ABI std::optional< unsigned > getMemoryDataParamPos(Intrinsic::ID)
LLVM_ABI Value * getVectorLengthParam() const
static LLVM_ABI std::optional< Intrinsic::ID > getFunctionalIntrinsicIDForVP(Intrinsic::ID ID)
LLVM_ABI void setVectorLengthParam(Value *)
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
static LLVM_ABI Intrinsic::ID getForOpcode(unsigned OC)
The llvm.vp.* intrinsics for this instruction Opcode.
static LLVM_ABI Function * getOrInsertDeclarationForParams(Module *M, Intrinsic::ID, Type *ReturnType, ArrayRef< Value * > Params)
Declares a llvm.vp.
static LLVM_ABI std::optional< unsigned > getMemoryPointerParamPos(Intrinsic::ID)
static LLVM_ABI bool isVPIntrinsic(Intrinsic::ID)
LLVM_ABI Value * getMemoryDataParam() const
static LLVM_ABI Intrinsic::ID getForIntrinsic(Intrinsic::ID Id)
The llvm.vp.
LLVM_ABI Value * getMemoryPointerParam() const
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM_ABI Value * getMaskParam() const
LLVM_ABI ElementCount getStaticVectorLength() const
static LLVM_ABI std::optional< Intrinsic::ID > getConstrainedIntrinsicIDForVP(Intrinsic::ID ID)
static LLVM_ABI bool isVPReduction(Intrinsic::ID ID)
LLVM_ABI unsigned getStartParamPos() const
LLVM_ABI unsigned getVectorParamPos() const
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:457
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:502
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 LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
A range adaptor for a pair of iterators.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:751
LLVM_ABI bool hasConstrainedFPRoundingModeOperand(ID QID)
Returns true if the intrinsic ID is for one of the "Constrained Floating-Point Intrinsics" that take ...
Definition: Intrinsics.cpp:794
LLVM_ABI bool isConstrainedFPIntrinsic(ID QID)
Returns true if the intrinsic ID is for one of the "Constrained Floating-Point Intrinsics".
Definition: Intrinsics.cpp:782
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition: FPEnv.h:40
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1770
static ValueAsMetadata * getAsMetadata(Value *V)
LLVM_ABI std::optional< fp::ExceptionBehavior > convertStrToExceptionBehavior(StringRef)
Returns a valid ExceptionBehavior enumerator when given a string valid as input in constrained intrin...
Definition: FPEnv.cpp:65
@ NearestTiesToEven
roundTiesToEven.
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition: FPEnv.cpp:24
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117