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 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
240 StringRef Name) {
241 assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
242
243 // Do successive binary searches of the dotted name components. For
244 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
245 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
246 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
247 // size 1. During the search, we can skip the prefix that we already know is
248 // identical. By using strncmp we consider names with differing suffixes to
249 // be part of the equal range.
250 size_t CmpEnd = 4; // Skip the "llvm" component.
251 const char *const *Low = NameTable.begin();
252 const char *const *High = NameTable.end();
253 const char *const *LastLow = Low;
254 while (CmpEnd < Name.size() && High - Low > 0) {
255 size_t CmpStart = CmpEnd;
256 CmpEnd = Name.find('.', CmpStart + 1);
257 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
258 auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
259 return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
260 };
261 LastLow = Low;
262 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
263 }
264 if (High - Low > 0)
265 LastLow = Low;
266
267 if (LastLow == NameTable.end())
268 return -1;
269 StringRef NameFound = *LastLow;
270 if (Name == NameFound ||
271 (Name.starts_with(NameFound) && Name[NameFound.size()] == '.'))
272 return LastLow - NameTable.begin();
273 return -1;
274}
275
278 llvm_unreachable("InstrProfValueProfileInst does not have counters!");
279 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
280}
281
284 llvm_unreachable("Please use InstrProfValueProfileInst::getIndex()");
285 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
286}
287
290 return const_cast<Value *>(getArgOperand(4));
291 }
292 const Module *M = getModule();
293 LLVMContext &Context = M->getContext();
294 return ConstantInt::get(Type::getInt64Ty(Context), 1);
295}
296
298 if (isa<InstrProfCallsite>(this))
299 return getArgOperand(4);
300 return nullptr;
301}
302
303std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
304 unsigned NumOperands = arg_size();
305 Metadata *MD = nullptr;
306 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2));
307 if (MAV)
308 MD = MAV->getMetadata();
309 if (!MD || !isa<MDString>(MD))
310 return std::nullopt;
311 return convertStrToRoundingMode(cast<MDString>(MD)->getString());
312}
313
314std::optional<fp::ExceptionBehavior>
316 unsigned NumOperands = arg_size();
317 Metadata *MD = nullptr;
318 auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1));
319 if (MAV)
320 MD = MAV->getMetadata();
321 if (!MD || !isa<MDString>(MD))
322 return std::nullopt;
323 return convertStrToExceptionBehavior(cast<MDString>(MD)->getString());
324}
325
327 std::optional<fp::ExceptionBehavior> Except = getExceptionBehavior();
328 if (Except) {
329 if (*Except != fp::ebIgnore)
330 return false;
331 }
332
333 std::optional<RoundingMode> Rounding = getRoundingMode();
334 if (Rounding) {
335 if (*Rounding != RoundingMode::NearestTiesToEven)
336 return false;
337 }
338
339 return true;
340}
341
343 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
344 if (!MD || !isa<MDString>(MD))
346 return StringSwitch<FCmpInst::Predicate>(cast<MDString>(MD)->getString())
347 .Case("oeq", FCmpInst::FCMP_OEQ)
348 .Case("ogt", FCmpInst::FCMP_OGT)
349 .Case("oge", FCmpInst::FCMP_OGE)
350 .Case("olt", FCmpInst::FCMP_OLT)
351 .Case("ole", FCmpInst::FCMP_OLE)
352 .Case("one", FCmpInst::FCMP_ONE)
353 .Case("ord", FCmpInst::FCMP_ORD)
354 .Case("uno", FCmpInst::FCMP_UNO)
355 .Case("ueq", FCmpInst::FCMP_UEQ)
356 .Case("ugt", FCmpInst::FCMP_UGT)
357 .Case("uge", FCmpInst::FCMP_UGE)
358 .Case("ult", FCmpInst::FCMP_ULT)
359 .Case("ule", FCmpInst::FCMP_ULE)
360 .Case("une", FCmpInst::FCMP_UNE)
362}
363
366}
367
369 switch (getIntrinsicID()) {
370 default:
371 return false;
372#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
373 case Intrinsic::INTRINSIC: \
374 return NARG == 1;
375#include "llvm/IR/ConstrainedOps.def"
376 }
377}
378
380 switch (getIntrinsicID()) {
381 default:
382 return false;
383#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
384 case Intrinsic::INTRINSIC: \
385 return NARG == 3;
386#include "llvm/IR/ConstrainedOps.def"
387 }
388}
389
391 switch (I->getIntrinsicID()) {
392#define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
393 case Intrinsic::INTRINSIC:
394#include "llvm/IR/ConstrainedOps.def"
395 return true;
396 default:
397 return false;
398 }
399}
400
402 auto GetVectorLengthOfType = [](const Type *T) -> ElementCount {
403 const auto *VT = cast<VectorType>(T);
404 auto ElemCount = VT->getElementCount();
405 return ElemCount;
406 };
407
408 Value *VPMask = getMaskParam();
409 if (!VPMask) {
410 assert((getIntrinsicID() == Intrinsic::vp_merge ||
411 getIntrinsicID() == Intrinsic::vp_select) &&
412 "Unexpected VP intrinsic without mask operand");
413 return GetVectorLengthOfType(getType());
414 }
415 return GetVectorLengthOfType(VPMask->getType());
416}
417
419 if (auto MaskPos = getMaskParamPos(getIntrinsicID()))
420 return getArgOperand(*MaskPos);
421 return nullptr;
422}
423
425 auto MaskPos = getMaskParamPos(getIntrinsicID());
426 setArgOperand(*MaskPos, NewMask);
427}
428
430 if (auto EVLPos = getVectorLengthParamPos(getIntrinsicID()))
431 return getArgOperand(*EVLPos);
432 return nullptr;
433}
434
437 setArgOperand(*EVLPos, NewEVL);
438}
439
440std::optional<unsigned>
442 switch (IntrinsicID) {
443 default:
444 return std::nullopt;
445
446#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
447 case Intrinsic::VPID: \
448 return MASKPOS;
449#include "llvm/IR/VPIntrinsics.def"
450 }
451}
452
453std::optional<unsigned>
455 switch (IntrinsicID) {
456 default:
457 return std::nullopt;
458
459#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
460 case Intrinsic::VPID: \
461 return VLENPOS;
462#include "llvm/IR/VPIntrinsics.def"
463 }
464}
465
466/// \return the alignment of the pointer used by this load/store/gather or
467/// scatter.
469 std::optional<unsigned> PtrParamOpt =
471 assert(PtrParamOpt && "no pointer argument!");
472 return getParamAlign(*PtrParamOpt);
473}
474
475/// \return The pointer operand of this load,store, gather or scatter.
477 if (auto PtrParamOpt = getMemoryPointerParamPos(getIntrinsicID()))
478 return getArgOperand(*PtrParamOpt);
479 return nullptr;
480}
481
482std::optional<unsigned>
484 switch (VPID) {
485 default:
486 break;
487#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
488#define VP_PROPERTY_MEMOP(POINTERPOS, ...) return POINTERPOS;
489#define END_REGISTER_VP_INTRINSIC(VPID) break;
490#include "llvm/IR/VPIntrinsics.def"
491 }
492 return std::nullopt;
493}
494
495/// \return The data (payload) operand of this store or scatter.
497 auto DataParamOpt = getMemoryDataParamPos(getIntrinsicID());
498 if (!DataParamOpt)
499 return nullptr;
500 return getArgOperand(*DataParamOpt);
501}
502
504 switch (VPID) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
508#define VP_PROPERTY_MEMOP(POINTERPOS, DATAPOS) return DATAPOS;
509#define END_REGISTER_VP_INTRINSIC(VPID) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return std::nullopt;
513}
514
516 switch (ID) {
517 default:
518 break;
519#define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS) \
520 case Intrinsic::VPID: \
521 return true;
522#include "llvm/IR/VPIntrinsics.def"
523 }
524 return false;
525}
526
528 return ::isVPIntrinsic(ID);
529}
530
531// Equivalent non-predicated opcode
532constexpr static std::optional<unsigned>
534 switch (ID) {
535 default:
536 break;
537#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
538#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) return Instruction::OPC;
539#define END_REGISTER_VP_INTRINSIC(VPID) break;
540#include "llvm/IR/VPIntrinsics.def"
541 }
542 return std::nullopt;
543}
544
545std::optional<unsigned>
547 return ::getFunctionalOpcodeForVP(ID);
548}
549
550// Equivalent non-predicated intrinsic ID
551constexpr static std::optional<Intrinsic::ID>
553 switch (ID) {
554 default:
555 break;
556#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
557#define VP_PROPERTY_FUNCTIONAL_INTRINSIC(INTRIN) return Intrinsic::INTRIN;
558#define END_REGISTER_VP_INTRINSIC(VPID) break;
559#include "llvm/IR/VPIntrinsics.def"
560 }
561 return std::nullopt;
562}
563
564std::optional<Intrinsic::ID>
566 return ::getFunctionalIntrinsicIDForVP(ID);
567}
568
570 switch (ID) {
571 default:
572 break;
573#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
574#define VP_PROPERTY_NO_FUNCTIONAL return true;
575#define END_REGISTER_VP_INTRINSIC(VPID) break;
576#include "llvm/IR/VPIntrinsics.def"
577 }
578 return false;
579}
580
581// All VP intrinsics should have an equivalent non-VP opcode or intrinsic
582// defined, or be marked that they don't have one.
583#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) \
584 static_assert(doesVPHaveNoFunctionalEquivalent(Intrinsic::VPID) || \
585 getFunctionalOpcodeForVP(Intrinsic::VPID) || \
586 getFunctionalIntrinsicIDForVP(Intrinsic::VPID));
587#include "llvm/IR/VPIntrinsics.def"
588
589// Equivalent non-predicated constrained intrinsic
590std::optional<Intrinsic::ID>
592 switch (ID) {
593 default:
594 break;
595#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
596#define VP_PROPERTY_CONSTRAINEDFP(HASRND, HASEXCEPT, CID) return Intrinsic::CID;
597#define END_REGISTER_VP_INTRINSIC(VPID) break;
598#include "llvm/IR/VPIntrinsics.def"
599 }
600 return std::nullopt;
601}
602
604 switch (IROPC) {
605 default:
606 break;
607
608#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) break;
609#define VP_PROPERTY_FUNCTIONAL_OPC(OPC) case Instruction::OPC:
610#define END_REGISTER_VP_INTRINSIC(VPID) return Intrinsic::VPID;
611#include "llvm/IR/VPIntrinsics.def"
612 }
614}
615
617 using namespace PatternMatch;
618
620
621 // No vlen param - no lanes masked-off by it.
622 auto *VLParam = getVectorLengthParam();
623 if (!VLParam)
624 return true;
625
626 // Note that the VP intrinsic causes undefined behavior if the Explicit Vector
627 // Length parameter is strictly greater-than the number of vector elements of
628 // the operation. This function returns true when this is detected statically
629 // in the IR.
630
631 // Check whether "W == vscale * EC.getKnownMinValue()"
632 if (EC.isScalable()) {
633 // Compare vscale patterns
634 uint64_t VScaleFactor;
635 if (match(VLParam, m_Mul(m_VScale(), m_ConstantInt(VScaleFactor))))
636 return VScaleFactor >= EC.getKnownMinValue();
637 return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale());
638 }
639
640 // standard SIMD operation
641 const auto *VLConst = dyn_cast<ConstantInt>(VLParam);
642 if (!VLConst)
643 return false;
644
645 uint64_t VLNum = VLConst->getZExtValue();
646 if (VLNum >= EC.getKnownMinValue())
647 return true;
648
649 return false;
650}
651
653 Type *ReturnType,
654 ArrayRef<Value *> Params) {
655 assert(isVPIntrinsic(VPID) && "not a VP intrinsic");
656 Function *VPFunc;
657 switch (VPID) {
658 default: {
659 Type *OverloadTy = Params[0]->getType();
661 OverloadTy =
662 Params[*VPReductionIntrinsic::getVectorParamPos(VPID)]->getType();
663
664 VPFunc = Intrinsic::getDeclaration(M, VPID, OverloadTy);
665 break;
666 }
667 case Intrinsic::vp_trunc:
668 case Intrinsic::vp_sext:
669 case Intrinsic::vp_zext:
670 case Intrinsic::vp_fptoui:
671 case Intrinsic::vp_fptosi:
672 case Intrinsic::vp_uitofp:
673 case Intrinsic::vp_sitofp:
674 case Intrinsic::vp_fptrunc:
675 case Intrinsic::vp_fpext:
676 case Intrinsic::vp_ptrtoint:
677 case Intrinsic::vp_inttoptr:
678 case Intrinsic::vp_lrint:
679 case Intrinsic::vp_llrint:
680 VPFunc =
681 Intrinsic::getDeclaration(M, VPID, {ReturnType, Params[0]->getType()});
682 break;
683 case Intrinsic::vp_is_fpclass:
684 VPFunc = Intrinsic::getDeclaration(M, VPID, {Params[0]->getType()});
685 break;
686 case Intrinsic::vp_merge:
687 case Intrinsic::vp_select:
688 VPFunc = Intrinsic::getDeclaration(M, VPID, {Params[1]->getType()});
689 break;
690 case Intrinsic::vp_load:
692 M, VPID, {ReturnType, Params[0]->getType()});
693 break;
694 case Intrinsic::experimental_vp_strided_load:
696 M, VPID, {ReturnType, Params[0]->getType(), Params[1]->getType()});
697 break;
698 case Intrinsic::vp_gather:
700 M, VPID, {ReturnType, Params[0]->getType()});
701 break;
702 case Intrinsic::vp_store:
704 M, VPID, {Params[0]->getType(), Params[1]->getType()});
705 break;
706 case Intrinsic::experimental_vp_strided_store:
708 M, VPID,
709 {Params[0]->getType(), Params[1]->getType(), Params[2]->getType()});
710 break;
711 case Intrinsic::vp_scatter:
713 M, VPID, {Params[0]->getType(), Params[1]->getType()});
714 break;
715 }
716 assert(VPFunc && "Could not declare VP intrinsic");
717 return VPFunc;
718}
719
721 switch (ID) {
722 default:
723 break;
724#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
725#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
726#define END_REGISTER_VP_INTRINSIC(VPID) break;
727#include "llvm/IR/VPIntrinsics.def"
728 }
729 return false;
730}
731
733 switch (ID) {
734 default:
735 break;
736#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
737#define VP_PROPERTY_CASTOP return true;
738#define END_REGISTER_VP_INTRINSIC(VPID) break;
739#include "llvm/IR/VPIntrinsics.def"
740 }
741 return false;
742}
743
745 switch (ID) {
746 default:
747 break;
748#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
749#define VP_PROPERTY_CMP(CCPOS, ...) return true;
750#define END_REGISTER_VP_INTRINSIC(VPID) break;
751#include "llvm/IR/VPIntrinsics.def"
752 }
753 return false;
754}
755
757 switch (ID) {
758 default:
759 break;
760#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
761#define VP_PROPERTY_BINARYOP return true;
762#define END_REGISTER_VP_INTRINSIC(VPID) break;
763#include "llvm/IR/VPIntrinsics.def"
764 }
765 return false;
766}
767
769 Metadata *MD = cast<MetadataAsValue>(Op)->getMetadata();
770 if (!MD || !isa<MDString>(MD))
772 return StringSwitch<ICmpInst::Predicate>(cast<MDString>(MD)->getString())
773 .Case("eq", ICmpInst::ICMP_EQ)
774 .Case("ne", ICmpInst::ICMP_NE)
775 .Case("ugt", ICmpInst::ICMP_UGT)
776 .Case("uge", ICmpInst::ICMP_UGE)
777 .Case("ult", ICmpInst::ICMP_ULT)
778 .Case("ule", ICmpInst::ICMP_ULE)
779 .Case("sgt", ICmpInst::ICMP_SGT)
780 .Case("sge", ICmpInst::ICMP_SGE)
781 .Case("slt", ICmpInst::ICMP_SLT)
782 .Case("sle", ICmpInst::ICMP_SLE)
784}
785
787 bool IsFP = true;
788 std::optional<unsigned> CCArgIdx;
789 switch (getIntrinsicID()) {
790 default:
791 break;
792#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
793#define VP_PROPERTY_CMP(CCPOS, ISFP) \
794 CCArgIdx = CCPOS; \
795 IsFP = ISFP; \
796 break;
797#define END_REGISTER_VP_INTRINSIC(VPID) break;
798#include "llvm/IR/VPIntrinsics.def"
799 }
800 assert(CCArgIdx && "Unexpected vector-predicated comparison");
801 return IsFP ? getFPPredicateFromMD(getArgOperand(*CCArgIdx))
803}
804
807}
808
811}
812
813std::optional<unsigned>
815 switch (ID) {
816#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
817#define VP_PROPERTY_REDUCTION(STARTPOS, VECTORPOS) return VECTORPOS;
818#define END_REGISTER_VP_INTRINSIC(VPID) break;
819#include "llvm/IR/VPIntrinsics.def"
820 default:
821 break;
822 }
823 return std::nullopt;
824}
825
826std::optional<unsigned>
828 switch (ID) {
829#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
830#define VP_PROPERTY_REDUCTION(STARTPOS, VECTORPOS) return STARTPOS;
831#define END_REGISTER_VP_INTRINSIC(VPID) break;
832#include "llvm/IR/VPIntrinsics.def"
833 default:
834 break;
835 }
836 return std::nullopt;
837}
838
840 switch (getIntrinsicID()) {
841 case Intrinsic::uadd_with_overflow:
842 case Intrinsic::sadd_with_overflow:
843 case Intrinsic::uadd_sat:
844 case Intrinsic::sadd_sat:
845 return Instruction::Add;
846 case Intrinsic::usub_with_overflow:
847 case Intrinsic::ssub_with_overflow:
848 case Intrinsic::usub_sat:
849 case Intrinsic::ssub_sat:
850 return Instruction::Sub;
851 case Intrinsic::umul_with_overflow:
852 case Intrinsic::smul_with_overflow:
853 return Instruction::Mul;
854 default:
855 llvm_unreachable("Invalid intrinsic");
856 }
857}
858
860 switch (getIntrinsicID()) {
861 case Intrinsic::sadd_with_overflow:
862 case Intrinsic::ssub_with_overflow:
863 case Intrinsic::smul_with_overflow:
864 case Intrinsic::sadd_sat:
865 case Intrinsic::ssub_sat:
866 return true;
867 default:
868 return false;
869 }
870}
871
873 if (isSigned())
875 else
877}
878
880 const Value *Token = getArgOperand(0);
881 if (isa<UndefValue>(Token))
882 return Token;
883
884 // Treat none token as if it was undef here
885 if (isa<ConstantTokenNone>(Token))
886 return UndefValue::get(Token->getType());
887
888 // This takes care both of relocates for call statepoints and relocates
889 // on normal path of invoke statepoint.
890 if (!isa<LandingPadInst>(Token))
891 return cast<GCStatepointInst>(Token);
892
893 // This relocate is on exceptional path of an invoke statepoint
894 const BasicBlock *InvokeBB =
895 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
896
897 assert(InvokeBB && "safepoints should have unique landingpads");
898 assert(InvokeBB->getTerminator() &&
899 "safepoint block should be well formed");
900
901 return cast<GCStatepointInst>(InvokeBB->getTerminator());
902}
903
905 auto Statepoint = getStatepoint();
906 if (isa<UndefValue>(Statepoint))
907 return UndefValue::get(Statepoint->getType());
908
909 auto *GCInst = cast<GCStatepointInst>(Statepoint);
910 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
911 return *(Opt->Inputs.begin() + getBasePtrIndex());
912 return *(GCInst->arg_begin() + getBasePtrIndex());
913}
914
916 auto *Statepoint = getStatepoint();
917 if (isa<UndefValue>(Statepoint))
918 return UndefValue::get(Statepoint->getType());
919
920 auto *GCInst = cast<GCStatepointInst>(Statepoint);
921 if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
922 return *(Opt->Inputs.begin() + getDerivedPtrIndex());
923 return *(GCInst->arg_begin() + getDerivedPtrIndex());
924}
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, bool AllowEmpty=false)
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