LLVM 19.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 // If OldValue is used as the address part of a dbg.assign intrinsic replace
124 // it with NewValue and return true.
125 auto ReplaceDbgAssignAddress = [this, OldValue, NewValue]() -> bool {
126 auto *DAI = dyn_cast<DbgAssignIntrinsic>(this);
127 if (!DAI || OldValue != DAI->getAddress())
128 return false;
129 DAI->setAddress(NewValue);
130 return true;
131 };
132 bool DbgAssignAddrReplaced = ReplaceDbgAssignAddress();
133 (void)DbgAssignAddrReplaced;
134
135 assert(NewValue && "Values must be non-null");
136 auto Locations = location_ops();
137 auto OldIt = find(Locations, OldValue);
138 if (OldIt == Locations.end()) {
139 assert(DbgAssignAddrReplaced &&
140 "OldValue must be dbg.assign addr if unused in DIArgList");
141 return;
142 }
143
144 assert(OldIt != Locations.end() && "OldValue must be a current location");
145 if (!hasArgList()) {
146 Value *NewOperand = isa<MetadataAsValue>(NewValue)
147 ? NewValue
149 getContext(), ValueAsMetadata::get(NewValue));
150 return setArgOperand(0, NewOperand);
151 }
153 ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
154 for (auto *VMD : Locations)
155 MDs.push_back(VMD == *OldIt ? NewOperand : getAsMetadata(VMD));
158}
160 Value *NewValue) {
161 assert(OpIdx < getNumVariableLocationOps() && "Invalid Operand Index");
162 if (!hasArgList()) {
163 Value *NewOperand = isa<MetadataAsValue>(NewValue)
164 ? NewValue
166 getContext(), ValueAsMetadata::get(NewValue));
167 return setArgOperand(0, NewOperand);
168 }
170 ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
171 for (unsigned Idx = 0; Idx < getNumVariableLocationOps(); ++Idx)
172 MDs.push_back(Idx == OpIdx ? NewOperand
176}
177
180 assert(NewExpr->hasAllLocationOps(getNumVariableLocationOps() +
181 NewValues.size()) &&
182 "NewExpr for debug variable intrinsic does not reference every "
183 "location operand.");
184 assert(!is_contained(NewValues, nullptr) && "New values must be non-null");
187 for (auto *VMD : location_ops())
188 MDs.push_back(getAsMetadata(VMD));
189 for (auto *VMD : NewValues)
190 MDs.push_back(getAsMetadata(VMD));
193}
194
195std::optional<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
196 if (auto Fragment = getExpression()->getFragmentInfo())
197 return Fragment->SizeInBits;
198 return getVariable()->getSizeInBits();
199}
200
202 auto *MD = getRawAddress();
203 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
204 return V->getValue();
205
206 // When the value goes to null, it gets replaced by an empty MDNode.
207 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
208 return nullptr;
209}
210
212 setOperand(OpAssignID, MetadataAsValue::get(getContext(), New));
213}
214
216 setOperand(OpAddress,
218}
219
221 if (isKillAddress())
222 return;
224}
225
227 Value *Addr = getAddress();
228 return !Addr || isa<UndefValue>(Addr);
229}
230
232 setOperand(OpValue,
234}
235
237 StringRef Name) {
238 assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
239
240 // Do successive binary searches of the dotted name components. For
241 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
242 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
243 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
244 // size 1. During the search, we can skip the prefix that we already know is
245 // identical. By using strncmp we consider names with differing suffixes to
246 // be part of the equal range.
247 size_t CmpEnd = 4; // Skip the "llvm" component.
248 const char *const *Low = NameTable.begin();
249 const char *const *High = NameTable.end();
250 const char *const *LastLow = Low;
251 while (CmpEnd < Name.size() && High - Low > 0) {
252 size_t CmpStart = CmpEnd;
253 CmpEnd = Name.find('.', CmpStart + 1);
254 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
255 auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
256 return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
257 };
258 LastLow = Low;
259 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
260 }
261 if (High - Low > 0)
262 LastLow = Low;
263
264 if (LastLow == NameTable.end())
265 return -1;
266 StringRef NameFound = *LastLow;
267 if (Name == NameFound ||
268 (Name.starts_with(NameFound) && Name[NameFound.size()] == '.'))
269 return LastLow - NameTable.begin();
270 return -1;
271}
272
275 llvm_unreachable("InstrProfValueProfileInst does not have counters!");
276 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
277}
278
281 llvm_unreachable("Please use InstrProfValueProfileInst::getIndex()");
282 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
283}
284
287 return const_cast<Value *>(getArgOperand(4));
288 }
289 const Module *M = getModule();
290 LLVMContext &Context = M->getContext();
291 return ConstantInt::get(Type::getInt64Ty(Context), 1);
292}
293
295 if (isa<InstrProfCallsite>(this))
296 return getArgOperand(4);
297 return nullptr;
298}
299
300std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
301 unsigned NumOperands = arg_size();
302 Metadata *MD = nullptr;
303 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2));
304 if (MAV)
305 MD = MAV->getMetadata();
306 if (!MD || !isa<MDString>(MD))
307 return std::nullopt;
308 return convertStrToRoundingMode(cast<MDString>(MD)->getString());
309}
310
311std::optional<fp::ExceptionBehavior>
313 unsigned NumOperands = arg_size();
314 Metadata *MD = nullptr;
315 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1));
316 if (MAV)
317 MD = MAV->getMetadata();
318 if (!MD || !isa<MDString>(MD))
319 return std::nullopt;
320 return convertStrToExceptionBehavior(cast<MDString>(MD)->getString());
321}
322
324 std::optional<fp::ExceptionBehavior> Except = getExceptionBehavior();
325 if (Except) {
326 if (*Except != fp::ebIgnore)
327 return false;
328 }
329
330 std::optional<RoundingMode> Rounding = getRoundingMode();
331 if (Rounding) {
332 if (*Rounding != RoundingMode::NearestTiesToEven)
333 return false;
334 }
335
336 return true;
337}
338
340 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
341 if (!MD || !isa<MDString>(MD))
343 return StringSwitch<FCmpInst::Predicate>(cast<MDString>(MD)->getString())
344 .Case("oeq", FCmpInst::FCMP_OEQ)
345 .Case("ogt", FCmpInst::FCMP_OGT)
346 .Case("oge", FCmpInst::FCMP_OGE)
347 .Case("olt", FCmpInst::FCMP_OLT)
348 .Case("ole", FCmpInst::FCMP_OLE)
349 .Case("one", FCmpInst::FCMP_ONE)
350 .Case("ord", FCmpInst::FCMP_ORD)
351 .Case("uno", FCmpInst::FCMP_UNO)
352 .Case("ueq", FCmpInst::FCMP_UEQ)
353 .Case("ugt", FCmpInst::FCMP_UGT)
354 .Case("uge", FCmpInst::FCMP_UGE)
355 .Case("ult", FCmpInst::FCMP_ULT)
356 .Case("ule", FCmpInst::FCMP_ULE)
357 .Case("une", FCmpInst::FCMP_UNE)
359}
360
363}
364
366 switch (getIntrinsicID()) {
367 default:
368 return false;
369#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
370 case Intrinsic::INTRINSIC: \
371 return NARG == 1;
372#include "llvm/IR/ConstrainedOps.def"
373 }
374}
375
377 switch (getIntrinsicID()) {
378 default:
379 return false;
380#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
381 case Intrinsic::INTRINSIC: \
382 return NARG == 3;
383#include "llvm/IR/ConstrainedOps.def"
384 }
385}
386
388 switch (I->getIntrinsicID()) {
389#define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
390 case Intrinsic::INTRINSIC:
391#include "llvm/IR/ConstrainedOps.def"
392 return true;
393 default:
394 return false;
395 }
396}
397
399 auto GetVectorLengthOfType = [](const Type *T) -> ElementCount {
400 const auto *VT = cast<VectorType>(T);
401 auto ElemCount = VT->getElementCount();
402 return ElemCount;
403 };
404
405 Value *VPMask = getMaskParam();
406 if (!VPMask) {
407 assert((getIntrinsicID() == Intrinsic::vp_merge ||
408 getIntrinsicID() == Intrinsic::vp_select) &&
409 "Unexpected VP intrinsic without mask operand");
410 return GetVectorLengthOfType(getType());
411 }
412 return GetVectorLengthOfType(VPMask->getType());
413}
414
416 if (auto MaskPos = getMaskParamPos(getIntrinsicID()))
417 return getArgOperand(*MaskPos);
418 return nullptr;
419}
420
422 auto MaskPos = getMaskParamPos(getIntrinsicID());
423 setArgOperand(*MaskPos, NewMask);
424}
425
427 if (auto EVLPos = getVectorLengthParamPos(getIntrinsicID()))
428 return getArgOperand(*EVLPos);
429 return nullptr;
430}
431
434 setArgOperand(*EVLPos, NewEVL);
435}
436
437std::optional<unsigned>
439 switch (IntrinsicID) {
440 default:
441 return std::nullopt;
442
443#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
444 case Intrinsic::VPID: \
445 return MASKPOS;
446#include "llvm/IR/VPIntrinsics.def"
447 }
448}
449
450std::optional<unsigned>
452 switch (IntrinsicID) {
453 default:
454 return std::nullopt;
455
456#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
457 case Intrinsic::VPID: \
458 return VLENPOS;
459#include "llvm/IR/VPIntrinsics.def"
460 }
461}
462
463/// \return the alignment of the pointer used by this load/store/gather or
464/// scatter.
466 std::optional<unsigned> PtrParamOpt =
468 assert(PtrParamOpt && "no pointer argument!");
469 return getParamAlign(*PtrParamOpt);
470}
471
472/// \return The pointer operand of this load,store, gather or scatter.
474 if (auto PtrParamOpt = getMemoryPointerParamPos(getIntrinsicID()))
475 return getArgOperand(*PtrParamOpt);
476 return nullptr;
477}
478
479std::optional<unsigned>
481 switch (VPID) {
482 default:
483 break;
484#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
485#define VP_PROPERTY_MEMOP(POINTERPOS, ...) return POINTERPOS;
486#define END_REGISTER_VP_INTRINSIC(VPID) break;
487#include "llvm/IR/VPIntrinsics.def"
488 }
489 return std::nullopt;
490}
491
492/// \return The data (payload) operand of this store or scatter.
494 auto DataParamOpt = getMemoryDataParamPos(getIntrinsicID());
495 if (!DataParamOpt)
496 return nullptr;
497 return getArgOperand(*DataParamOpt);
498}
499
501 switch (VPID) {
502 default:
503 break;
504#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
505#define VP_PROPERTY_MEMOP(POINTERPOS, DATAPOS) return DATAPOS;
506#define END_REGISTER_VP_INTRINSIC(VPID) break;
507#include "llvm/IR/VPIntrinsics.def"
508 }
509 return std::nullopt;
510}
511
513 switch (ID) {
514 default:
515 break;
516#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
517 case Intrinsic::VPID: \
518 return true;
519#include "llvm/IR/VPIntrinsics.def"
520 }
521 return false;
522}
523
525 return ::isVPIntrinsic(ID);
526}
527
528// Equivalent non-predicated opcode
529constexpr static std::optional<unsigned>
531 switch (ID) {
532 default:
533 break;
534#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
535#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) return Instruction::OPC;
536#define END_REGISTER_VP_INTRINSIC(VPID) break;
537#include "llvm/IR/VPIntrinsics.def"
538 }
539 return std::nullopt;
540}
541
542std::optional<unsigned>
544 return ::getFunctionalOpcodeForVP(ID);
545}
546
547// Equivalent non-predicated intrinsic ID
548constexpr static std::optional<Intrinsic::ID>
550 switch (ID) {
551 default:
552 break;
553#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
554#define VP_PROPERTY_FUNCTIONAL_INTRINSIC(INTRIN) return Intrinsic::INTRIN;
555#define END_REGISTER_VP_INTRINSIC(VPID) break;
556#include "llvm/IR/VPIntrinsics.def"
557 }
558 return std::nullopt;
559}
560
561std::optional<Intrinsic::ID>
563 return ::getFunctionalIntrinsicIDForVP(ID);
564}
565
567 switch (ID) {
568 default:
569 break;
570#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
571#define VP_PROPERTY_NO_FUNCTIONAL return true;
572#define END_REGISTER_VP_INTRINSIC(VPID) break;
573#include "llvm/IR/VPIntrinsics.def"
574 }
575 return false;
576}
577
578// All VP intrinsics should have an equivalent non-VP opcode or intrinsic
579// defined, or be marked that they don't have one.
580#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) \
581 static_assert(doesVPHaveNoFunctionalEquivalent(Intrinsic::VPID) || \
582 getFunctionalOpcodeForVP(Intrinsic::VPID) || \
583 getFunctionalIntrinsicIDForVP(Intrinsic::VPID));
584#include "llvm/IR/VPIntrinsics.def"
585
586// Equivalent non-predicated constrained intrinsic
587std::optional<Intrinsic::ID>
589 switch (ID) {
590 default:
591 break;
592#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
593#define VP_PROPERTY_CONSTRAINEDFP(HASRND, HASEXCEPT, CID) return Intrinsic::CID;
594#define END_REGISTER_VP_INTRINSIC(VPID) break;
595#include "llvm/IR/VPIntrinsics.def"
596 }
597 return std::nullopt;
598}
599
601 switch (IROPC) {
602 default:
603 break;
604
605#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) break;
606#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) case Instruction::OPC:
607#define END_REGISTER_VP_INTRINSIC(VPID) return Intrinsic::VPID;
608#include "llvm/IR/VPIntrinsics.def"
609 }
611}
612
614 using namespace PatternMatch;
615
617
618 // No vlen param - no lanes masked-off by it.
619 auto *VLParam = getVectorLengthParam();
620 if (!VLParam)
621 return true;
622
623 // Note that the VP intrinsic causes undefined behavior if the Explicit Vector
624 // Length parameter is strictly greater-than the number of vector elements of
625 // the operation. This function returns true when this is detected statically
626 // in the IR.
627
628 // Check whether "W == vscale * EC.getKnownMinValue()"
629 if (EC.isScalable()) {
630 // Compare vscale patterns
631 uint64_t VScaleFactor;
632 if (match(VLParam, m_Mul(m_VScale(), m_ConstantInt(VScaleFactor))))
633 return VScaleFactor >= EC.getKnownMinValue();
634 return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale());
635 }
636
637 // standard SIMD operation
638 const auto *VLConst = dyn_cast<ConstantInt>(VLParam);
639 if (!VLConst)
640 return false;
641
642 uint64_t VLNum = VLConst->getZExtValue();
643 if (VLNum >= EC.getKnownMinValue())
644 return true;
645
646 return false;
647}
648
650 Type *ReturnType,
651 ArrayRef<Value *> Params) {
652 assert(isVPIntrinsic(VPID) && "not a VP intrinsic");
653 Function *VPFunc;
654 switch (VPID) {
655 default: {
656 Type *OverloadTy = Params[0]->getType();
658 OverloadTy =
659 Params[*VPReductionIntrinsic::getVectorParamPos(VPID)]->getType();
660
661 VPFunc = Intrinsic::getDeclaration(M, VPID, OverloadTy);
662 break;
663 }
664 case Intrinsic::vp_trunc:
665 case Intrinsic::vp_sext:
666 case Intrinsic::vp_zext:
667 case Intrinsic::vp_fptoui:
668 case Intrinsic::vp_fptosi:
669 case Intrinsic::vp_uitofp:
670 case Intrinsic::vp_sitofp:
671 case Intrinsic::vp_fptrunc:
672 case Intrinsic::vp_fpext:
673 case Intrinsic::vp_ptrtoint:
674 case Intrinsic::vp_inttoptr:
675 case Intrinsic::vp_lrint:
676 case Intrinsic::vp_llrint:
677 VPFunc =
678 Intrinsic::getDeclaration(M, VPID, {ReturnType, Params[0]->getType()});
679 break;
680 case Intrinsic::vp_is_fpclass:
681 VPFunc = Intrinsic::getDeclaration(M, VPID, {Params[0]->getType()});
682 break;
683 case Intrinsic::vp_merge:
684 case Intrinsic::vp_select:
685 VPFunc = Intrinsic::getDeclaration(M, VPID, {Params[1]->getType()});
686 break;
687 case Intrinsic::vp_load:
689 M, VPID, {ReturnType, Params[0]->getType()});
690 break;
691 case Intrinsic::experimental_vp_strided_load:
693 M, VPID, {ReturnType, Params[0]->getType(), Params[1]->getType()});
694 break;
695 case Intrinsic::vp_gather:
697 M, VPID, {ReturnType, Params[0]->getType()});
698 break;
699 case Intrinsic::vp_store:
701 M, VPID, {Params[0]->getType(), Params[1]->getType()});
702 break;
703 case Intrinsic::experimental_vp_strided_store:
705 M, VPID,
706 {Params[0]->getType(), Params[1]->getType(), Params[2]->getType()});
707 break;
708 case Intrinsic::vp_scatter:
710 M, VPID, {Params[0]->getType(), Params[1]->getType()});
711 break;
712 }
713 assert(VPFunc && "Could not declare VP intrinsic");
714 return VPFunc;
715}
716
718 switch (ID) {
719 default:
720 break;
721#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
722#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
723#define END_REGISTER_VP_INTRINSIC(VPID) break;
724#include "llvm/IR/VPIntrinsics.def"
725 }
726 return false;
727}
728
730 switch (ID) {
731 default:
732 break;
733#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
734#define VP_PROPERTY_CASTOP return true;
735#define END_REGISTER_VP_INTRINSIC(VPID) break;
736#include "llvm/IR/VPIntrinsics.def"
737 }
738 return false;
739}
740
742 switch (ID) {
743 default:
744 break;
745#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
746#define VP_PROPERTY_CMP(CCPOS, ...) return true;
747#define END_REGISTER_VP_INTRINSIC(VPID) break;
748#include "llvm/IR/VPIntrinsics.def"
749 }
750 return false;
751}
752
754 switch (ID) {
755 default:
756 break;
757#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
758#define VP_PROPERTY_BINARYOP return true;
759#define END_REGISTER_VP_INTRINSIC(VPID) break;
760#include "llvm/IR/VPIntrinsics.def"
761 }
762 return false;
763}
764
766 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
767 if (!MD || !isa<MDString>(MD))
769 return StringSwitch<ICmpInst::Predicate>(cast<MDString>(MD)->getString())
770 .Case("eq", ICmpInst::ICMP_EQ)
771 .Case("ne", ICmpInst::ICMP_NE)
772 .Case("ugt", ICmpInst::ICMP_UGT)
773 .Case("uge", ICmpInst::ICMP_UGE)
774 .Case("ult", ICmpInst::ICMP_ULT)
775 .Case("ule", ICmpInst::ICMP_ULE)
776 .Case("sgt", ICmpInst::ICMP_SGT)
777 .Case("sge", ICmpInst::ICMP_SGE)
778 .Case("slt", ICmpInst::ICMP_SLT)
779 .Case("sle", ICmpInst::ICMP_SLE)
781}
782
784 bool IsFP = true;
785 std::optional<unsigned> CCArgIdx;
786 switch (getIntrinsicID()) {
787 default:
788 break;
789#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
790#define VP_PROPERTY_CMP(CCPOS, ISFP) \
791 CCArgIdx = CCPOS; \
792 IsFP = ISFP; \
793 break;
794#define END_REGISTER_VP_INTRINSIC(VPID) break;
795#include "llvm/IR/VPIntrinsics.def"
796 }
797 assert(CCArgIdx && "Unexpected vector-predicated comparison");
798 return IsFP ? getFPPredicateFromMD(getArgOperand(*CCArgIdx))
800}
801
804}
805
808}
809
810std::optional<unsigned>
812 switch (ID) {
813#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
814#define VP_PROPERTY_REDUCTION(STARTPOS, VECTORPOS) return VECTORPOS;
815#define END_REGISTER_VP_INTRINSIC(VPID) break;
816#include "llvm/IR/VPIntrinsics.def"
817 default:
818 break;
819 }
820 return std::nullopt;
821}
822
823std::optional<unsigned>
825 switch (ID) {
826#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
827#define VP_PROPERTY_REDUCTION(STARTPOS, VECTORPOS) return STARTPOS;
828#define END_REGISTER_VP_INTRINSIC(VPID) break;
829#include "llvm/IR/VPIntrinsics.def"
830 default:
831 break;
832 }
833 return std::nullopt;
834}
835
837 switch (getIntrinsicID()) {
838 case Intrinsic::uadd_with_overflow:
839 case Intrinsic::sadd_with_overflow:
840 case Intrinsic::uadd_sat:
841 case Intrinsic::sadd_sat:
842 return Instruction::Add;
843 case Intrinsic::usub_with_overflow:
844 case Intrinsic::ssub_with_overflow:
845 case Intrinsic::usub_sat:
846 case Intrinsic::ssub_sat:
847 return Instruction::Sub;
848 case Intrinsic::umul_with_overflow:
849 case Intrinsic::smul_with_overflow:
850 return Instruction::Mul;
851 default:
852 llvm_unreachable("Invalid intrinsic");
853 }
854}
855
857 switch (getIntrinsicID()) {
858 case Intrinsic::sadd_with_overflow:
859 case Intrinsic::ssub_with_overflow:
860 case Intrinsic::smul_with_overflow:
861 case Intrinsic::sadd_sat:
862 case Intrinsic::ssub_sat:
863 return true;
864 default:
865 return false;
866 }
867}
868
870 if (isSigned())
872 else
874}
875
877 const Value *Token = getArgOperand(0);
878 if (isa<UndefValue>(Token))
879 return Token;
880
881 // Treat none token as if it was undef here
882 if (isa<ConstantTokenNone>(Token))
883 return UndefValue::get(Token->getType());
884
885 // This takes care both of relocates for call statepoints and relocates
886 // on normal path of invoke statepoint.
887 if (!isa<LandingPadInst>(Token))
888 return cast<GCStatepointInst>(Token);
889
890 // This relocate is on exceptional path of an invoke statepoint
891 const BasicBlock *InvokeBB =
892 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
893
894 assert(InvokeBB && "safepoints should have unique landingpads");
895 assert(InvokeBB->getTerminator() &&
896 "safepoint block should be well formed");
897
898 return cast<GCStatepointInst>(InvokeBB->getTerminator());
899}
900
902 auto Statepoint = getStatepoint();
903 if (isa<UndefValue>(Statepoint))
904 return UndefValue::get(Statepoint->getType());
905
906 auto *GCInst = cast<GCStatepointInst>(Statepoint);
907 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
908 return *(Opt->Inputs.begin() + getBasePtrIndex());
909 return *(GCInst->arg_begin() + getBasePtrIndex());
910}
911
913 auto *Statepoint = getStatepoint();
914 if (isa<UndefValue>(Statepoint))
915 return UndefValue::get(Statepoint->getType());
916
917 auto *GCInst = cast<GCStatepointInst>(Statepoint);
918 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
919 return *(Opt->Inputs.begin() + getDerivedPtrIndex());
920 return *(GCInst->arg_begin() + getDerivedPtrIndex());
921}
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
std::string Name
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 FCmpInst::Predicate getFPPredicateFromMD(const Value *Op)
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
uint64_t High
LLVMContext & Context
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:206
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:221
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
bool isSigned() const
Whether the intrinsic is signed or unsigned.
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:2109
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1692
unsigned arg_size() const
Definition: InstrTypes.h:1685
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:996
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:1001
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:1004
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:1002
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_NE
not equal
Definition: InstrTypes.h:1015
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:1009
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:1003
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
FCmpInst::Predicate getPredicate() const
std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
std::optional< RoundingMode > getRoundingMode() const
static bool classof(const IntrinsicInst *I)
static DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
Assignment ID.
DWARF expression.
std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
This class represents an Operation in the Expression.
void setAssignId(DIAssignID *New)
void setKillAddress()
Kill the address component.
bool isKillAddress() const
Check whether this kills the address component.
Metadata * getRawAddress() const
Value * getAddress() const
iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
void addVariableLocationOps(ArrayRef< Value * > NewValues, DIExpression *NewExpr)
Adding a new location operand will always result in this intrinsic using an ArgList,...
void replaceVariableLocationOp(Value *OldValue, Value *NewValue)
Value * getVariableLocationOp(unsigned OpIdx) const
DILocalVariable * getVariable() const
unsigned getNumVariableLocationOps() const
void setOperand(unsigned i, Value *v)
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
const Value * getStatepoint() const
The statepoint with which this gc.relocate is associated.
Value * getBasePtr() const
unsigned getBasePtrIndex() const
The index into the associate statepoint's argument list which contains the base pointer of the pointe...
Value * getDerivedPtr() const
unsigned getDerivedPtrIndex() const
The index into the associate statepoint's argument list which contains the pointer whose relocation t...
Value * getCallee() const
ConstantInt * getIndex() const
ConstantInt * getNumCounters() const
static bool classof(const IntrinsicInst *I)
static bool classof(const IntrinsicInst *I)
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:83
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
static 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:54
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Metadata * getRawLocation() const
iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
Value * getVariableLocationOp(unsigned OpIdx) const
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
static constexpr size_t npos
Definition: StringRef.h:52
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt64Ty(LLVMContext &C)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
unsigned getNumOperands() const
Definition: User.h:191
static bool isVPBinOp(Intrinsic::ID ID)
static bool isVPCast(Intrinsic::ID ID)
static bool isVPCmp(Intrinsic::ID ID)
CmpInst::Predicate getPredicate() const
static Function * getDeclarationForParams(Module *M, Intrinsic::ID, Type *ReturnType, ArrayRef< Value * > Params)
Declares a llvm.vp.
static std::optional< unsigned > getMaskParamPos(Intrinsic::ID IntrinsicID)
bool canIgnoreVectorLengthParam() const
void setMaskParam(Value *)
static std::optional< unsigned > getFunctionalOpcodeForVP(Intrinsic::ID ID)
static std::optional< unsigned > getMemoryDataParamPos(Intrinsic::ID)
Value * getVectorLengthParam() const
static std::optional< Intrinsic::ID > getFunctionalIntrinsicIDForVP(Intrinsic::ID ID)
void setVectorLengthParam(Value *)
static std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
static Intrinsic::ID getForOpcode(unsigned OC)
The llvm.vp.* intrinsics for this instruction Opcode.
static std::optional< unsigned > getMemoryPointerParamPos(Intrinsic::ID)
static bool isVPIntrinsic(Intrinsic::ID)
Value * getMemoryDataParam() const
Value * getMemoryPointerParam() const
MaybeAlign getPointerAlignment() const
Value * getMaskParam() const
ElementCount getStaticVectorLength() const
static std::optional< Intrinsic::ID > getConstrainedIntrinsicIDForVP(Intrinsic::ID ID)
static bool isVPReduction(Intrinsic::ID ID)
unsigned getStartParamPos() const
unsigned getVectorParamPos() const
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:450
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:495
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
A range adaptor for a pair of iterators.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
int lookupLLVMIntrinsicByName(ArrayRef< const char * > NameTable, StringRef Name)
Looks up Name in NameTable via binary search.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1469
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
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
VScaleVal_match m_VScale()
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition: FPEnv.h:39
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
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:1742
static ValueAsMetadata * getAsMetadata(Value *V)
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.
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:1879
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117