xref: /llvm-project/llvm/lib/IR/Function.cpp (revision 8c24f33158d81d5f4b0c5d27c2f07396f0f1484b)
1 //===- Function.cpp - Implement the Global object classes -----------------===//
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 the Function class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Function.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/Constant.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/GlobalValue.h"
30 #include "llvm/IR/InstIterator.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/IntrinsicsAArch64.h"
35 #include "llvm/IR/IntrinsicsAMDGPU.h"
36 #include "llvm/IR/IntrinsicsARM.h"
37 #include "llvm/IR/IntrinsicsBPF.h"
38 #include "llvm/IR/IntrinsicsHexagon.h"
39 #include "llvm/IR/IntrinsicsMips.h"
40 #include "llvm/IR/IntrinsicsNVPTX.h"
41 #include "llvm/IR/IntrinsicsPowerPC.h"
42 #include "llvm/IR/IntrinsicsR600.h"
43 #include "llvm/IR/IntrinsicsRISCV.h"
44 #include "llvm/IR/IntrinsicsS390.h"
45 #include "llvm/IR/IntrinsicsWebAssembly.h"
46 #include "llvm/IR/IntrinsicsX86.h"
47 #include "llvm/IR/IntrinsicsXCore.h"
48 #include "llvm/IR/LLVMContext.h"
49 #include "llvm/IR/MDBuilder.h"
50 #include "llvm/IR/Metadata.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/IR/SymbolTableListTraits.h"
53 #include "llvm/IR/Type.h"
54 #include "llvm/IR/Use.h"
55 #include "llvm/IR/User.h"
56 #include "llvm/IR/Value.h"
57 #include "llvm/IR/ValueSymbolTable.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/Compiler.h"
60 #include "llvm/Support/ErrorHandling.h"
61 #include <algorithm>
62 #include <cassert>
63 #include <cstddef>
64 #include <cstdint>
65 #include <cstring>
66 #include <string>
67 
68 using namespace llvm;
69 using ProfileCount = Function::ProfileCount;
70 
71 // Explicit instantiations of SymbolTableListTraits since some of the methods
72 // are not in the public header file...
73 template class llvm::SymbolTableListTraits<BasicBlock>;
74 
75 //===----------------------------------------------------------------------===//
76 // Argument Implementation
77 //===----------------------------------------------------------------------===//
78 
79 Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
80     : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
81   setName(Name);
82 }
83 
84 void Argument::setParent(Function *parent) {
85   Parent = parent;
86 }
87 
88 bool Argument::hasNonNullAttr() const {
89   if (!getType()->isPointerTy()) return false;
90   if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
91     return true;
92   else if (getDereferenceableBytes() > 0 &&
93            !NullPointerIsDefined(getParent(),
94                                  getType()->getPointerAddressSpace()))
95     return true;
96   return false;
97 }
98 
99 bool Argument::hasByValAttr() const {
100   if (!getType()->isPointerTy()) return false;
101   return hasAttribute(Attribute::ByVal);
102 }
103 
104 bool Argument::hasSwiftSelfAttr() const {
105   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
106 }
107 
108 bool Argument::hasSwiftErrorAttr() const {
109   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
110 }
111 
112 bool Argument::hasInAllocaAttr() const {
113   if (!getType()->isPointerTy()) return false;
114   return hasAttribute(Attribute::InAlloca);
115 }
116 
117 bool Argument::hasPassPointeeByValueAttr() const {
118   if (!getType()->isPointerTy()) return false;
119   AttributeList Attrs = getParent()->getAttributes();
120   return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
121          Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca) ||
122          Attrs.hasParamAttribute(getArgNo(), Attribute::Preallocated);
123 }
124 
125 unsigned Argument::getParamAlignment() const {
126   assert(getType()->isPointerTy() && "Only pointers have alignments");
127   return getParent()->getParamAlignment(getArgNo());
128 }
129 
130 MaybeAlign Argument::getParamAlign() const {
131   assert(getType()->isPointerTy() && "Only pointers have alignments");
132   return getParent()->getParamAlign(getArgNo());
133 }
134 
135 Type *Argument::getParamByValType() const {
136   assert(getType()->isPointerTy() && "Only pointers have byval types");
137   return getParent()->getParamByValType(getArgNo());
138 }
139 
140 uint64_t Argument::getDereferenceableBytes() const {
141   assert(getType()->isPointerTy() &&
142          "Only pointers have dereferenceable bytes");
143   return getParent()->getParamDereferenceableBytes(getArgNo());
144 }
145 
146 uint64_t Argument::getDereferenceableOrNullBytes() const {
147   assert(getType()->isPointerTy() &&
148          "Only pointers have dereferenceable bytes");
149   return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
150 }
151 
152 bool Argument::hasNestAttr() const {
153   if (!getType()->isPointerTy()) return false;
154   return hasAttribute(Attribute::Nest);
155 }
156 
157 bool Argument::hasNoAliasAttr() const {
158   if (!getType()->isPointerTy()) return false;
159   return hasAttribute(Attribute::NoAlias);
160 }
161 
162 bool Argument::hasNoCaptureAttr() const {
163   if (!getType()->isPointerTy()) return false;
164   return hasAttribute(Attribute::NoCapture);
165 }
166 
167 bool Argument::hasStructRetAttr() const {
168   if (!getType()->isPointerTy()) return false;
169   return hasAttribute(Attribute::StructRet);
170 }
171 
172 bool Argument::hasInRegAttr() const {
173   return hasAttribute(Attribute::InReg);
174 }
175 
176 bool Argument::hasReturnedAttr() const {
177   return hasAttribute(Attribute::Returned);
178 }
179 
180 bool Argument::hasZExtAttr() const {
181   return hasAttribute(Attribute::ZExt);
182 }
183 
184 bool Argument::hasSExtAttr() const {
185   return hasAttribute(Attribute::SExt);
186 }
187 
188 bool Argument::onlyReadsMemory() const {
189   AttributeList Attrs = getParent()->getAttributes();
190   return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
191          Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
192 }
193 
194 void Argument::addAttrs(AttrBuilder &B) {
195   AttributeList AL = getParent()->getAttributes();
196   AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
197   getParent()->setAttributes(AL);
198 }
199 
200 void Argument::addAttr(Attribute::AttrKind Kind) {
201   getParent()->addParamAttr(getArgNo(), Kind);
202 }
203 
204 void Argument::addAttr(Attribute Attr) {
205   getParent()->addParamAttr(getArgNo(), Attr);
206 }
207 
208 void Argument::removeAttr(Attribute::AttrKind Kind) {
209   getParent()->removeParamAttr(getArgNo(), Kind);
210 }
211 
212 bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
213   return getParent()->hasParamAttribute(getArgNo(), Kind);
214 }
215 
216 Attribute Argument::getAttribute(Attribute::AttrKind Kind) const {
217   return getParent()->getParamAttribute(getArgNo(), Kind);
218 }
219 
220 //===----------------------------------------------------------------------===//
221 // Helper Methods in Function
222 //===----------------------------------------------------------------------===//
223 
224 LLVMContext &Function::getContext() const {
225   return getType()->getContext();
226 }
227 
228 unsigned Function::getInstructionCount() const {
229   unsigned NumInstrs = 0;
230   for (const BasicBlock &BB : BasicBlocks)
231     NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
232                                BB.instructionsWithoutDebug().end());
233   return NumInstrs;
234 }
235 
236 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
237                            const Twine &N, Module &M) {
238   return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
239 }
240 
241 void Function::removeFromParent() {
242   getParent()->getFunctionList().remove(getIterator());
243 }
244 
245 void Function::eraseFromParent() {
246   getParent()->getFunctionList().erase(getIterator());
247 }
248 
249 //===----------------------------------------------------------------------===//
250 // Function Implementation
251 //===----------------------------------------------------------------------===//
252 
253 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
254   // If AS == -1 and we are passed a valid module pointer we place the function
255   // in the program address space. Otherwise we default to AS0.
256   if (AddrSpace == static_cast<unsigned>(-1))
257     return M ? M->getDataLayout().getProgramAddressSpace() : 0;
258   return AddrSpace;
259 }
260 
261 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
262                    const Twine &name, Module *ParentModule)
263     : GlobalObject(Ty, Value::FunctionVal,
264                    OperandTraits<Function>::op_begin(this), 0, Linkage, name,
265                    computeAddrSpace(AddrSpace, ParentModule)),
266       NumArgs(Ty->getNumParams()) {
267   assert(FunctionType::isValidReturnType(getReturnType()) &&
268          "invalid return type");
269   setGlobalObjectSubClassData(0);
270 
271   // We only need a symbol table for a function if the context keeps value names
272   if (!getContext().shouldDiscardValueNames())
273     SymTab = std::make_unique<ValueSymbolTable>();
274 
275   // If the function has arguments, mark them as lazily built.
276   if (Ty->getNumParams())
277     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
278 
279   if (ParentModule)
280     ParentModule->getFunctionList().push_back(this);
281 
282   HasLLVMReservedName = getName().startswith("llvm.");
283   // Ensure intrinsics have the right parameter attributes.
284   // Note, the IntID field will have been set in Value::setName if this function
285   // name is a valid intrinsic ID.
286   if (IntID)
287     setAttributes(Intrinsic::getAttributes(getContext(), IntID));
288 }
289 
290 Function::~Function() {
291   dropAllReferences();    // After this it is safe to delete instructions.
292 
293   // Delete all of the method arguments and unlink from symbol table...
294   if (Arguments)
295     clearArguments();
296 
297   // Remove the function from the on-the-side GC table.
298   clearGC();
299 }
300 
301 void Function::BuildLazyArguments() const {
302   // Create the arguments vector, all arguments start out unnamed.
303   auto *FT = getFunctionType();
304   if (NumArgs > 0) {
305     Arguments = std::allocator<Argument>().allocate(NumArgs);
306     for (unsigned i = 0, e = NumArgs; i != e; ++i) {
307       Type *ArgTy = FT->getParamType(i);
308       assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
309       new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
310     }
311   }
312 
313   // Clear the lazy arguments bit.
314   unsigned SDC = getSubclassDataFromValue();
315   SDC &= ~(1 << 0);
316   const_cast<Function*>(this)->setValueSubclassData(SDC);
317   assert(!hasLazyArguments());
318 }
319 
320 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
321   return MutableArrayRef<Argument>(Args, Count);
322 }
323 
324 bool Function::isConstrainedFPIntrinsic() const {
325   switch (getIntrinsicID()) {
326 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
327   case Intrinsic::INTRINSIC:
328 #include "llvm/IR/ConstrainedOps.def"
329     return true;
330 #undef INSTRUCTION
331   default:
332     return false;
333   }
334 }
335 
336 void Function::clearArguments() {
337   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
338     A.setName("");
339     A.~Argument();
340   }
341   std::allocator<Argument>().deallocate(Arguments, NumArgs);
342   Arguments = nullptr;
343 }
344 
345 void Function::stealArgumentListFrom(Function &Src) {
346   assert(isDeclaration() && "Expected no references to current arguments");
347 
348   // Drop the current arguments, if any, and set the lazy argument bit.
349   if (!hasLazyArguments()) {
350     assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
351                         [](const Argument &A) { return A.use_empty(); }) &&
352            "Expected arguments to be unused in declaration");
353     clearArguments();
354     setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
355   }
356 
357   // Nothing to steal if Src has lazy arguments.
358   if (Src.hasLazyArguments())
359     return;
360 
361   // Steal arguments from Src, and fix the lazy argument bits.
362   assert(arg_size() == Src.arg_size());
363   Arguments = Src.Arguments;
364   Src.Arguments = nullptr;
365   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
366     // FIXME: This does the work of transferNodesFromList inefficiently.
367     SmallString<128> Name;
368     if (A.hasName())
369       Name = A.getName();
370     if (!Name.empty())
371       A.setName("");
372     A.setParent(this);
373     if (!Name.empty())
374       A.setName(Name);
375   }
376 
377   setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
378   assert(!hasLazyArguments());
379   Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
380 }
381 
382 // dropAllReferences() - This function causes all the subinstructions to "let
383 // go" of all references that they are maintaining.  This allows one to
384 // 'delete' a whole class at a time, even though there may be circular
385 // references... first all references are dropped, and all use counts go to
386 // zero.  Then everything is deleted for real.  Note that no operations are
387 // valid on an object that has "dropped all references", except operator
388 // delete.
389 //
390 void Function::dropAllReferences() {
391   setIsMaterializable(false);
392 
393   for (BasicBlock &BB : *this)
394     BB.dropAllReferences();
395 
396   // Delete all basic blocks. They are now unused, except possibly by
397   // blockaddresses, but BasicBlock's destructor takes care of those.
398   while (!BasicBlocks.empty())
399     BasicBlocks.begin()->eraseFromParent();
400 
401   // Drop uses of any optional data (real or placeholder).
402   if (getNumOperands()) {
403     User::dropAllReferences();
404     setNumHungOffUseOperands(0);
405     setValueSubclassData(getSubclassDataFromValue() & ~0xe);
406   }
407 
408   // Metadata is stored in a side-table.
409   clearMetadata();
410 }
411 
412 void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
413   AttributeList PAL = getAttributes();
414   PAL = PAL.addAttribute(getContext(), i, Kind);
415   setAttributes(PAL);
416 }
417 
418 void Function::addAttribute(unsigned i, Attribute Attr) {
419   AttributeList PAL = getAttributes();
420   PAL = PAL.addAttribute(getContext(), i, Attr);
421   setAttributes(PAL);
422 }
423 
424 void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
425   AttributeList PAL = getAttributes();
426   PAL = PAL.addAttributes(getContext(), i, Attrs);
427   setAttributes(PAL);
428 }
429 
430 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
431   AttributeList PAL = getAttributes();
432   PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
433   setAttributes(PAL);
434 }
435 
436 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
437   AttributeList PAL = getAttributes();
438   PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
439   setAttributes(PAL);
440 }
441 
442 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
443   AttributeList PAL = getAttributes();
444   PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs);
445   setAttributes(PAL);
446 }
447 
448 void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
449   AttributeList PAL = getAttributes();
450   PAL = PAL.removeAttribute(getContext(), i, Kind);
451   setAttributes(PAL);
452 }
453 
454 void Function::removeAttribute(unsigned i, StringRef Kind) {
455   AttributeList PAL = getAttributes();
456   PAL = PAL.removeAttribute(getContext(), i, Kind);
457   setAttributes(PAL);
458 }
459 
460 void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
461   AttributeList PAL = getAttributes();
462   PAL = PAL.removeAttributes(getContext(), i, Attrs);
463   setAttributes(PAL);
464 }
465 
466 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
467   AttributeList PAL = getAttributes();
468   PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
469   setAttributes(PAL);
470 }
471 
472 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
473   AttributeList PAL = getAttributes();
474   PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
475   setAttributes(PAL);
476 }
477 
478 void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
479   AttributeList PAL = getAttributes();
480   PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs);
481   setAttributes(PAL);
482 }
483 
484 void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
485   AttributeList PAL = getAttributes();
486   PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
487   setAttributes(PAL);
488 }
489 
490 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
491   AttributeList PAL = getAttributes();
492   PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
493   setAttributes(PAL);
494 }
495 
496 void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
497   AttributeList PAL = getAttributes();
498   PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
499   setAttributes(PAL);
500 }
501 
502 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
503                                                  uint64_t Bytes) {
504   AttributeList PAL = getAttributes();
505   PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes);
506   setAttributes(PAL);
507 }
508 
509 const std::string &Function::getGC() const {
510   assert(hasGC() && "Function has no collector");
511   return getContext().getGC(*this);
512 }
513 
514 void Function::setGC(std::string Str) {
515   setValueSubclassDataBit(14, !Str.empty());
516   getContext().setGC(*this, std::move(Str));
517 }
518 
519 void Function::clearGC() {
520   if (!hasGC())
521     return;
522   getContext().deleteGC(*this);
523   setValueSubclassDataBit(14, false);
524 }
525 
526 /// Copy all additional attributes (those not needed to create a Function) from
527 /// the Function Src to this one.
528 void Function::copyAttributesFrom(const Function *Src) {
529   GlobalObject::copyAttributesFrom(Src);
530   setCallingConv(Src->getCallingConv());
531   setAttributes(Src->getAttributes());
532   if (Src->hasGC())
533     setGC(Src->getGC());
534   else
535     clearGC();
536   if (Src->hasPersonalityFn())
537     setPersonalityFn(Src->getPersonalityFn());
538   if (Src->hasPrefixData())
539     setPrefixData(Src->getPrefixData());
540   if (Src->hasPrologueData())
541     setPrologueData(Src->getPrologueData());
542 }
543 
544 /// Table of string intrinsic names indexed by enum value.
545 static const char * const IntrinsicNameTable[] = {
546   "not_intrinsic",
547 #define GET_INTRINSIC_NAME_TABLE
548 #include "llvm/IR/IntrinsicImpl.inc"
549 #undef GET_INTRINSIC_NAME_TABLE
550 };
551 
552 /// Table of per-target intrinsic name tables.
553 #define GET_INTRINSIC_TARGET_DATA
554 #include "llvm/IR/IntrinsicImpl.inc"
555 #undef GET_INTRINSIC_TARGET_DATA
556 
557 /// Find the segment of \c IntrinsicNameTable for intrinsics with the same
558 /// target as \c Name, or the generic table if \c Name is not target specific.
559 ///
560 /// Returns the relevant slice of \c IntrinsicNameTable
561 static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
562   assert(Name.startswith("llvm."));
563 
564   ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
565   // Drop "llvm." and take the first dotted component. That will be the target
566   // if this is target specific.
567   StringRef Target = Name.drop_front(5).split('.').first;
568   auto It = partition_point(
569       Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
570   // We've either found the target or just fall back to the generic set, which
571   // is always first.
572   const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
573   return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
574 }
575 
576 /// This does the actual lookup of an intrinsic ID which
577 /// matches the given function name.
578 Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
579   ArrayRef<const char *> NameTable = findTargetSubtable(Name);
580   int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
581   if (Idx == -1)
582     return Intrinsic::not_intrinsic;
583 
584   // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
585   // an index into a sub-table.
586   int Adjust = NameTable.data() - IntrinsicNameTable;
587   Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
588 
589   // If the intrinsic is not overloaded, require an exact match. If it is
590   // overloaded, require either exact or prefix match.
591   const auto MatchSize = strlen(NameTable[Idx]);
592   assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
593   bool IsExactMatch = Name.size() == MatchSize;
594   return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID
595                                                      : Intrinsic::not_intrinsic;
596 }
597 
598 void Function::recalculateIntrinsicID() {
599   StringRef Name = getName();
600   if (!Name.startswith("llvm.")) {
601     HasLLVMReservedName = false;
602     IntID = Intrinsic::not_intrinsic;
603     return;
604   }
605   HasLLVMReservedName = true;
606   IntID = lookupIntrinsicID(Name);
607 }
608 
609 /// Returns a stable mangling for the type specified for use in the name
610 /// mangling scheme used by 'any' types in intrinsic signatures.  The mangling
611 /// of named types is simply their name.  Manglings for unnamed types consist
612 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
613 /// combined with the mangling of their component types.  A vararg function
614 /// type will have a suffix of 'vararg'.  Since function types can contain
615 /// other function types, we close a function type mangling with suffix 'f'
616 /// which can't be confused with it's prefix.  This ensures we don't have
617 /// collisions between two unrelated function types. Otherwise, you might
618 /// parse ffXX as f(fXX) or f(fX)X.  (X is a placeholder for any other type.)
619 ///
620 static std::string getMangledTypeStr(Type* Ty) {
621   std::string Result;
622   if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
623     Result += "p" + utostr(PTyp->getAddressSpace()) +
624       getMangledTypeStr(PTyp->getElementType());
625   } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
626     Result += "a" + utostr(ATyp->getNumElements()) +
627       getMangledTypeStr(ATyp->getElementType());
628   } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
629     if (!STyp->isLiteral()) {
630       Result += "s_";
631       Result += STyp->getName();
632     } else {
633       Result += "sl_";
634       for (auto Elem : STyp->elements())
635         Result += getMangledTypeStr(Elem);
636     }
637     // Ensure nested structs are distinguishable.
638     Result += "s";
639   } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
640     Result += "f_" + getMangledTypeStr(FT->getReturnType());
641     for (size_t i = 0; i < FT->getNumParams(); i++)
642       Result += getMangledTypeStr(FT->getParamType(i));
643     if (FT->isVarArg())
644       Result += "vararg";
645     // Ensure nested function types are distinguishable.
646     Result += "f";
647   } else if (VectorType* VTy = dyn_cast<VectorType>(Ty)) {
648     ElementCount EC = VTy->getElementCount();
649     if (EC.Scalable)
650       Result += "nx";
651     Result += "v" + utostr(EC.Min) + getMangledTypeStr(VTy->getElementType());
652   } else if (Ty) {
653     switch (Ty->getTypeID()) {
654     default: llvm_unreachable("Unhandled type");
655     case Type::VoidTyID:      Result += "isVoid";   break;
656     case Type::MetadataTyID:  Result += "Metadata"; break;
657     case Type::HalfTyID:      Result += "f16";      break;
658     case Type::BFloatTyID:    Result += "bf16";     break;
659     case Type::FloatTyID:     Result += "f32";      break;
660     case Type::DoubleTyID:    Result += "f64";      break;
661     case Type::X86_FP80TyID:  Result += "f80";      break;
662     case Type::FP128TyID:     Result += "f128";     break;
663     case Type::PPC_FP128TyID: Result += "ppcf128";  break;
664     case Type::X86_MMXTyID:   Result += "x86mmx";   break;
665     case Type::IntegerTyID:
666       Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
667       break;
668     }
669   }
670   return Result;
671 }
672 
673 StringRef Intrinsic::getName(ID id) {
674   assert(id < num_intrinsics && "Invalid intrinsic ID!");
675   assert(!Intrinsic::isOverloaded(id) &&
676          "This version of getName does not support overloading");
677   return IntrinsicNameTable[id];
678 }
679 
680 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
681   assert(id < num_intrinsics && "Invalid intrinsic ID!");
682   std::string Result(IntrinsicNameTable[id]);
683   for (Type *Ty : Tys) {
684     Result += "." + getMangledTypeStr(Ty);
685   }
686   return Result;
687 }
688 
689 /// IIT_Info - These are enumerators that describe the entries returned by the
690 /// getIntrinsicInfoTableEntries function.
691 ///
692 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
693 enum IIT_Info {
694   // Common values should be encoded with 0-15.
695   IIT_Done = 0,
696   IIT_I1   = 1,
697   IIT_I8   = 2,
698   IIT_I16  = 3,
699   IIT_I32  = 4,
700   IIT_I64  = 5,
701   IIT_F16  = 6,
702   IIT_F32  = 7,
703   IIT_F64  = 8,
704   IIT_V2   = 9,
705   IIT_V4   = 10,
706   IIT_V8   = 11,
707   IIT_V16  = 12,
708   IIT_V32  = 13,
709   IIT_PTR  = 14,
710   IIT_ARG  = 15,
711 
712   // Values from 16+ are only encodable with the inefficient encoding.
713   IIT_V64  = 16,
714   IIT_MMX  = 17,
715   IIT_TOKEN = 18,
716   IIT_METADATA = 19,
717   IIT_EMPTYSTRUCT = 20,
718   IIT_STRUCT2 = 21,
719   IIT_STRUCT3 = 22,
720   IIT_STRUCT4 = 23,
721   IIT_STRUCT5 = 24,
722   IIT_EXTEND_ARG = 25,
723   IIT_TRUNC_ARG = 26,
724   IIT_ANYPTR = 27,
725   IIT_V1   = 28,
726   IIT_VARARG = 29,
727   IIT_HALF_VEC_ARG = 30,
728   IIT_SAME_VEC_WIDTH_ARG = 31,
729   IIT_PTR_TO_ARG = 32,
730   IIT_PTR_TO_ELT = 33,
731   IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
732   IIT_I128 = 35,
733   IIT_V512 = 36,
734   IIT_V1024 = 37,
735   IIT_STRUCT6 = 38,
736   IIT_STRUCT7 = 39,
737   IIT_STRUCT8 = 40,
738   IIT_F128 = 41,
739   IIT_VEC_ELEMENT = 42,
740   IIT_SCALABLE_VEC = 43,
741   IIT_SUBDIVIDE2_ARG = 44,
742   IIT_SUBDIVIDE4_ARG = 45,
743   IIT_VEC_OF_BITCASTS_TO_INT = 46,
744   IIT_V128  = 47
745 };
746 
747 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
748                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
749   using namespace Intrinsic;
750 
751   IIT_Info Info = IIT_Info(Infos[NextElt++]);
752   unsigned StructElts = 2;
753 
754   switch (Info) {
755   case IIT_Done:
756     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
757     return;
758   case IIT_VARARG:
759     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
760     return;
761   case IIT_MMX:
762     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
763     return;
764   case IIT_TOKEN:
765     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
766     return;
767   case IIT_METADATA:
768     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
769     return;
770   case IIT_F16:
771     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
772     return;
773   case IIT_F32:
774     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
775     return;
776   case IIT_F64:
777     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
778     return;
779   case IIT_F128:
780     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
781     return;
782   case IIT_I1:
783     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
784     return;
785   case IIT_I8:
786     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
787     return;
788   case IIT_I16:
789     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
790     return;
791   case IIT_I32:
792     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
793     return;
794   case IIT_I64:
795     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
796     return;
797   case IIT_I128:
798     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
799     return;
800   case IIT_V1:
801     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
802     DecodeIITType(NextElt, Infos, OutputTable);
803     return;
804   case IIT_V2:
805     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
806     DecodeIITType(NextElt, Infos, OutputTable);
807     return;
808   case IIT_V4:
809     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
810     DecodeIITType(NextElt, Infos, OutputTable);
811     return;
812   case IIT_V8:
813     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
814     DecodeIITType(NextElt, Infos, OutputTable);
815     return;
816   case IIT_V16:
817     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
818     DecodeIITType(NextElt, Infos, OutputTable);
819     return;
820   case IIT_V32:
821     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
822     DecodeIITType(NextElt, Infos, OutputTable);
823     return;
824   case IIT_V64:
825     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
826     DecodeIITType(NextElt, Infos, OutputTable);
827     return;
828   case IIT_V128:
829     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 128));
830     DecodeIITType(NextElt, Infos, OutputTable);
831     return;
832   case IIT_V512:
833     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
834     DecodeIITType(NextElt, Infos, OutputTable);
835     return;
836   case IIT_V1024:
837     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
838     DecodeIITType(NextElt, Infos, OutputTable);
839     return;
840   case IIT_PTR:
841     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
842     DecodeIITType(NextElt, Infos, OutputTable);
843     return;
844   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
845     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
846                                              Infos[NextElt++]));
847     DecodeIITType(NextElt, Infos, OutputTable);
848     return;
849   }
850   case IIT_ARG: {
851     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
852     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
853     return;
854   }
855   case IIT_EXTEND_ARG: {
856     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
857     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
858                                              ArgInfo));
859     return;
860   }
861   case IIT_TRUNC_ARG: {
862     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
863     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
864                                              ArgInfo));
865     return;
866   }
867   case IIT_HALF_VEC_ARG: {
868     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
869     OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
870                                              ArgInfo));
871     return;
872   }
873   case IIT_SAME_VEC_WIDTH_ARG: {
874     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
875     OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
876                                              ArgInfo));
877     return;
878   }
879   case IIT_PTR_TO_ARG: {
880     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
881     OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
882                                              ArgInfo));
883     return;
884   }
885   case IIT_PTR_TO_ELT: {
886     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
887     OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
888     return;
889   }
890   case IIT_VEC_OF_ANYPTRS_TO_ELT: {
891     unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
892     unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
893     OutputTable.push_back(
894         IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
895     return;
896   }
897   case IIT_EMPTYSTRUCT:
898     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
899     return;
900   case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH;
901   case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH;
902   case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH;
903   case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
904   case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
905   case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
906   case IIT_STRUCT2: {
907     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
908 
909     for (unsigned i = 0; i != StructElts; ++i)
910       DecodeIITType(NextElt, Infos, OutputTable);
911     return;
912   }
913   case IIT_SUBDIVIDE2_ARG: {
914     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
915     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide2Argument,
916                                              ArgInfo));
917     return;
918   }
919   case IIT_SUBDIVIDE4_ARG: {
920     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
921     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide4Argument,
922                                              ArgInfo));
923     return;
924   }
925   case IIT_VEC_ELEMENT: {
926     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
927     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecElementArgument,
928                                              ArgInfo));
929     return;
930   }
931   case IIT_SCALABLE_VEC: {
932     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ScalableVecArgument,
933                                              0));
934     DecodeIITType(NextElt, Infos, OutputTable);
935     return;
936   }
937   case IIT_VEC_OF_BITCASTS_TO_INT: {
938     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
939     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt,
940                                              ArgInfo));
941     return;
942   }
943   }
944   llvm_unreachable("unhandled");
945 }
946 
947 #define GET_INTRINSIC_GENERATOR_GLOBAL
948 #include "llvm/IR/IntrinsicImpl.inc"
949 #undef GET_INTRINSIC_GENERATOR_GLOBAL
950 
951 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
952                                              SmallVectorImpl<IITDescriptor> &T){
953   // Check to see if the intrinsic's type was expressible by the table.
954   unsigned TableVal = IIT_Table[id-1];
955 
956   // Decode the TableVal into an array of IITValues.
957   SmallVector<unsigned char, 8> IITValues;
958   ArrayRef<unsigned char> IITEntries;
959   unsigned NextElt = 0;
960   if ((TableVal >> 31) != 0) {
961     // This is an offset into the IIT_LongEncodingTable.
962     IITEntries = IIT_LongEncodingTable;
963 
964     // Strip sentinel bit.
965     NextElt = (TableVal << 1) >> 1;
966   } else {
967     // Decode the TableVal into an array of IITValues.  If the entry was encoded
968     // into a single word in the table itself, decode it now.
969     do {
970       IITValues.push_back(TableVal & 0xF);
971       TableVal >>= 4;
972     } while (TableVal);
973 
974     IITEntries = IITValues;
975     NextElt = 0;
976   }
977 
978   // Okay, decode the table into the output vector of IITDescriptors.
979   DecodeIITType(NextElt, IITEntries, T);
980   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
981     DecodeIITType(NextElt, IITEntries, T);
982 }
983 
984 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
985                              ArrayRef<Type*> Tys, LLVMContext &Context) {
986   using namespace Intrinsic;
987 
988   IITDescriptor D = Infos.front();
989   Infos = Infos.slice(1);
990 
991   switch (D.Kind) {
992   case IITDescriptor::Void: return Type::getVoidTy(Context);
993   case IITDescriptor::VarArg: return Type::getVoidTy(Context);
994   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
995   case IITDescriptor::Token: return Type::getTokenTy(Context);
996   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
997   case IITDescriptor::Half: return Type::getHalfTy(Context);
998   case IITDescriptor::Float: return Type::getFloatTy(Context);
999   case IITDescriptor::Double: return Type::getDoubleTy(Context);
1000   case IITDescriptor::Quad: return Type::getFP128Ty(Context);
1001 
1002   case IITDescriptor::Integer:
1003     return IntegerType::get(Context, D.Integer_Width);
1004   case IITDescriptor::Vector:
1005     return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
1006   case IITDescriptor::Pointer:
1007     return PointerType::get(DecodeFixedType(Infos, Tys, Context),
1008                             D.Pointer_AddressSpace);
1009   case IITDescriptor::Struct: {
1010     SmallVector<Type *, 8> Elts;
1011     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1012       Elts.push_back(DecodeFixedType(Infos, Tys, Context));
1013     return StructType::get(Context, Elts);
1014   }
1015   case IITDescriptor::Argument:
1016     return Tys[D.getArgumentNumber()];
1017   case IITDescriptor::ExtendArgument: {
1018     Type *Ty = Tys[D.getArgumentNumber()];
1019     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1020       return VectorType::getExtendedElementVectorType(VTy);
1021 
1022     return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
1023   }
1024   case IITDescriptor::TruncArgument: {
1025     Type *Ty = Tys[D.getArgumentNumber()];
1026     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1027       return VectorType::getTruncatedElementVectorType(VTy);
1028 
1029     IntegerType *ITy = cast<IntegerType>(Ty);
1030     assert(ITy->getBitWidth() % 2 == 0);
1031     return IntegerType::get(Context, ITy->getBitWidth() / 2);
1032   }
1033   case IITDescriptor::Subdivide2Argument:
1034   case IITDescriptor::Subdivide4Argument: {
1035     Type *Ty = Tys[D.getArgumentNumber()];
1036     VectorType *VTy = dyn_cast<VectorType>(Ty);
1037     assert(VTy && "Expected an argument of Vector Type");
1038     int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
1039     return VectorType::getSubdividedVectorType(VTy, SubDivs);
1040   }
1041   case IITDescriptor::HalfVecArgument:
1042     return VectorType::getHalfElementsVectorType(cast<VectorType>(
1043                                                   Tys[D.getArgumentNumber()]));
1044   case IITDescriptor::SameVecWidthArgument: {
1045     Type *EltTy = DecodeFixedType(Infos, Tys, Context);
1046     Type *Ty = Tys[D.getArgumentNumber()];
1047     if (auto *VTy = dyn_cast<VectorType>(Ty))
1048       return VectorType::get(EltTy, VTy->getElementCount());
1049     return EltTy;
1050   }
1051   case IITDescriptor::PtrToArgument: {
1052     Type *Ty = Tys[D.getArgumentNumber()];
1053     return PointerType::getUnqual(Ty);
1054   }
1055   case IITDescriptor::PtrToElt: {
1056     Type *Ty = Tys[D.getArgumentNumber()];
1057     VectorType *VTy = dyn_cast<VectorType>(Ty);
1058     if (!VTy)
1059       llvm_unreachable("Expected an argument of Vector Type");
1060     Type *EltTy = VTy->getElementType();
1061     return PointerType::getUnqual(EltTy);
1062   }
1063   case IITDescriptor::VecElementArgument: {
1064     Type *Ty = Tys[D.getArgumentNumber()];
1065     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1066       return VTy->getElementType();
1067     llvm_unreachable("Expected an argument of Vector Type");
1068   }
1069   case IITDescriptor::VecOfBitcastsToInt: {
1070     Type *Ty = Tys[D.getArgumentNumber()];
1071     VectorType *VTy = dyn_cast<VectorType>(Ty);
1072     assert(VTy && "Expected an argument of Vector Type");
1073     return VectorType::getInteger(VTy);
1074   }
1075   case IITDescriptor::VecOfAnyPtrsToElt:
1076     // Return the overloaded type (which determines the pointers address space)
1077     return Tys[D.getOverloadArgNumber()];
1078   case IITDescriptor::ScalableVecArgument: {
1079     auto *Ty = cast<FixedVectorType>(DecodeFixedType(Infos, Tys, Context));
1080     return ScalableVectorType::get(Ty->getElementType(), Ty->getNumElements());
1081   }
1082   }
1083   llvm_unreachable("unhandled");
1084 }
1085 
1086 FunctionType *Intrinsic::getType(LLVMContext &Context,
1087                                  ID id, ArrayRef<Type*> Tys) {
1088   SmallVector<IITDescriptor, 8> Table;
1089   getIntrinsicInfoTableEntries(id, Table);
1090 
1091   ArrayRef<IITDescriptor> TableRef = Table;
1092   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
1093 
1094   SmallVector<Type*, 8> ArgTys;
1095   while (!TableRef.empty())
1096     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
1097 
1098   // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
1099   // If we see void type as the type of the last argument, it is vararg intrinsic
1100   if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
1101     ArgTys.pop_back();
1102     return FunctionType::get(ResultTy, ArgTys, true);
1103   }
1104   return FunctionType::get(ResultTy, ArgTys, false);
1105 }
1106 
1107 bool Intrinsic::isOverloaded(ID id) {
1108 #define GET_INTRINSIC_OVERLOAD_TABLE
1109 #include "llvm/IR/IntrinsicImpl.inc"
1110 #undef GET_INTRINSIC_OVERLOAD_TABLE
1111 }
1112 
1113 bool Intrinsic::isLeaf(ID id) {
1114   switch (id) {
1115   default:
1116     return true;
1117 
1118   case Intrinsic::experimental_gc_statepoint:
1119   case Intrinsic::experimental_patchpoint_void:
1120   case Intrinsic::experimental_patchpoint_i64:
1121     return false;
1122   }
1123 }
1124 
1125 /// This defines the "Intrinsic::getAttributes(ID id)" method.
1126 #define GET_INTRINSIC_ATTRIBUTES
1127 #include "llvm/IR/IntrinsicImpl.inc"
1128 #undef GET_INTRINSIC_ATTRIBUTES
1129 
1130 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
1131   // There can never be multiple globals with the same name of different types,
1132   // because intrinsics must be a specific type.
1133   return cast<Function>(
1134       M->getOrInsertFunction(getName(id, Tys),
1135                              getType(M->getContext(), id, Tys))
1136           .getCallee());
1137 }
1138 
1139 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
1140 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
1141 #include "llvm/IR/IntrinsicImpl.inc"
1142 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
1143 
1144 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
1145 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1146 #include "llvm/IR/IntrinsicImpl.inc"
1147 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1148 
1149 using DeferredIntrinsicMatchPair =
1150     std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>;
1151 
1152 static bool matchIntrinsicType(
1153     Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
1154     SmallVectorImpl<Type *> &ArgTys,
1155     SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks,
1156     bool IsDeferredCheck) {
1157   using namespace Intrinsic;
1158 
1159   // If we ran out of descriptors, there are too many arguments.
1160   if (Infos.empty()) return true;
1161 
1162   // Do this before slicing off the 'front' part
1163   auto InfosRef = Infos;
1164   auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) {
1165     DeferredChecks.emplace_back(T, InfosRef);
1166     return false;
1167   };
1168 
1169   IITDescriptor D = Infos.front();
1170   Infos = Infos.slice(1);
1171 
1172   switch (D.Kind) {
1173     case IITDescriptor::Void: return !Ty->isVoidTy();
1174     case IITDescriptor::VarArg: return true;
1175     case IITDescriptor::MMX:  return !Ty->isX86_MMXTy();
1176     case IITDescriptor::Token: return !Ty->isTokenTy();
1177     case IITDescriptor::Metadata: return !Ty->isMetadataTy();
1178     case IITDescriptor::Half: return !Ty->isHalfTy();
1179     case IITDescriptor::Float: return !Ty->isFloatTy();
1180     case IITDescriptor::Double: return !Ty->isDoubleTy();
1181     case IITDescriptor::Quad: return !Ty->isFP128Ty();
1182     case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1183     case IITDescriptor::Vector: {
1184       // FIXME: We shouldn't be assuming all Vector types are fixed width.
1185       // This will be fixed soon in another future patch.
1186       FixedVectorType *VT = dyn_cast<FixedVectorType>(Ty);
1187       return !VT || VT->getNumElements() != D.Vector_Width ||
1188              matchIntrinsicType(VT->getElementType(), Infos, ArgTys,
1189                                 DeferredChecks, IsDeferredCheck);
1190     }
1191     case IITDescriptor::Pointer: {
1192       PointerType *PT = dyn_cast<PointerType>(Ty);
1193       return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
1194              matchIntrinsicType(PT->getElementType(), Infos, ArgTys,
1195                                 DeferredChecks, IsDeferredCheck);
1196     }
1197 
1198     case IITDescriptor::Struct: {
1199       StructType *ST = dyn_cast<StructType>(Ty);
1200       if (!ST || ST->getNumElements() != D.Struct_NumElements)
1201         return true;
1202 
1203       for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1204         if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys,
1205                                DeferredChecks, IsDeferredCheck))
1206           return true;
1207       return false;
1208     }
1209 
1210     case IITDescriptor::Argument:
1211       // If this is the second occurrence of an argument,
1212       // verify that the later instance matches the previous instance.
1213       if (D.getArgumentNumber() < ArgTys.size())
1214         return Ty != ArgTys[D.getArgumentNumber()];
1215 
1216       if (D.getArgumentNumber() > ArgTys.size() ||
1217           D.getArgumentKind() == IITDescriptor::AK_MatchType)
1218         return IsDeferredCheck || DeferCheck(Ty);
1219 
1220       assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck &&
1221              "Table consistency error");
1222       ArgTys.push_back(Ty);
1223 
1224       switch (D.getArgumentKind()) {
1225         case IITDescriptor::AK_Any:        return false; // Success
1226         case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1227         case IITDescriptor::AK_AnyFloat:   return !Ty->isFPOrFPVectorTy();
1228         case IITDescriptor::AK_AnyVector:  return !isa<VectorType>(Ty);
1229         case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
1230         default:                           break;
1231       }
1232       llvm_unreachable("all argument kinds not covered");
1233 
1234     case IITDescriptor::ExtendArgument: {
1235       // If this is a forward reference, defer the check for later.
1236       if (D.getArgumentNumber() >= ArgTys.size())
1237         return IsDeferredCheck || DeferCheck(Ty);
1238 
1239       Type *NewTy = ArgTys[D.getArgumentNumber()];
1240       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1241         NewTy = VectorType::getExtendedElementVectorType(VTy);
1242       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1243         NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1244       else
1245         return true;
1246 
1247       return Ty != NewTy;
1248     }
1249     case IITDescriptor::TruncArgument: {
1250       // If this is a forward reference, defer the check for later.
1251       if (D.getArgumentNumber() >= ArgTys.size())
1252         return IsDeferredCheck || DeferCheck(Ty);
1253 
1254       Type *NewTy = ArgTys[D.getArgumentNumber()];
1255       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1256         NewTy = VectorType::getTruncatedElementVectorType(VTy);
1257       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1258         NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1259       else
1260         return true;
1261 
1262       return Ty != NewTy;
1263     }
1264     case IITDescriptor::HalfVecArgument:
1265       // If this is a forward reference, defer the check for later.
1266       if (D.getArgumentNumber() >= ArgTys.size())
1267         return IsDeferredCheck || DeferCheck(Ty);
1268       return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1269              VectorType::getHalfElementsVectorType(
1270                      cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1271     case IITDescriptor::SameVecWidthArgument: {
1272       if (D.getArgumentNumber() >= ArgTys.size()) {
1273         // Defer check and subsequent check for the vector element type.
1274         Infos = Infos.slice(1);
1275         return IsDeferredCheck || DeferCheck(Ty);
1276       }
1277       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1278       auto *ThisArgType = dyn_cast<VectorType>(Ty);
1279       // Both must be vectors of the same number of elements or neither.
1280       if ((ReferenceType != nullptr) != (ThisArgType != nullptr))
1281         return true;
1282       Type *EltTy = Ty;
1283       if (ThisArgType) {
1284         if (ReferenceType->getElementCount() !=
1285             ThisArgType->getElementCount())
1286           return true;
1287         EltTy = ThisArgType->getElementType();
1288       }
1289       return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks,
1290                                 IsDeferredCheck);
1291     }
1292     case IITDescriptor::PtrToArgument: {
1293       if (D.getArgumentNumber() >= ArgTys.size())
1294         return IsDeferredCheck || DeferCheck(Ty);
1295       Type * ReferenceType = ArgTys[D.getArgumentNumber()];
1296       PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1297       return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
1298     }
1299     case IITDescriptor::PtrToElt: {
1300       if (D.getArgumentNumber() >= ArgTys.size())
1301         return IsDeferredCheck || DeferCheck(Ty);
1302       VectorType * ReferenceType =
1303         dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1304       PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1305 
1306       return (!ThisArgType || !ReferenceType ||
1307               ThisArgType->getElementType() != ReferenceType->getElementType());
1308     }
1309     case IITDescriptor::VecOfAnyPtrsToElt: {
1310       unsigned RefArgNumber = D.getRefArgNumber();
1311       if (RefArgNumber >= ArgTys.size()) {
1312         if (IsDeferredCheck)
1313           return true;
1314         // If forward referencing, already add the pointer-vector type and
1315         // defer the checks for later.
1316         ArgTys.push_back(Ty);
1317         return DeferCheck(Ty);
1318       }
1319 
1320       if (!IsDeferredCheck){
1321         assert(D.getOverloadArgNumber() == ArgTys.size() &&
1322                "Table consistency error");
1323         ArgTys.push_back(Ty);
1324       }
1325 
1326       // Verify the overloaded type "matches" the Ref type.
1327       // i.e. Ty is a vector with the same width as Ref.
1328       // Composed of pointers to the same element type as Ref.
1329       VectorType *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
1330       VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1331       if (!ThisArgVecTy || !ReferenceType ||
1332           (ReferenceType->getNumElements() != ThisArgVecTy->getNumElements()))
1333         return true;
1334       PointerType *ThisArgEltTy =
1335           dyn_cast<PointerType>(ThisArgVecTy->getElementType());
1336       if (!ThisArgEltTy)
1337         return true;
1338       return ThisArgEltTy->getElementType() != ReferenceType->getElementType();
1339     }
1340     case IITDescriptor::VecElementArgument: {
1341       if (D.getArgumentNumber() >= ArgTys.size())
1342         return IsDeferredCheck ? true : DeferCheck(Ty);
1343       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1344       return !ReferenceType || Ty != ReferenceType->getElementType();
1345     }
1346     case IITDescriptor::Subdivide2Argument:
1347     case IITDescriptor::Subdivide4Argument: {
1348       // If this is a forward reference, defer the check for later.
1349       if (D.getArgumentNumber() >= ArgTys.size())
1350         return IsDeferredCheck || DeferCheck(Ty);
1351 
1352       Type *NewTy = ArgTys[D.getArgumentNumber()];
1353       if (auto *VTy = dyn_cast<VectorType>(NewTy)) {
1354         int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
1355         NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs);
1356         return Ty != NewTy;
1357       }
1358       return true;
1359     }
1360     case IITDescriptor::ScalableVecArgument: {
1361       if (!isa<ScalableVectorType>(Ty))
1362         return true;
1363       ScalableVectorType *STy = cast<ScalableVectorType>(Ty);
1364       unsigned MinElts = STy->getMinNumElements();
1365       FixedVectorType *FVTy =
1366           FixedVectorType::get(STy->getElementType(), MinElts);
1367       return matchIntrinsicType(FVTy, Infos, ArgTys, DeferredChecks,
1368                                 IsDeferredCheck);
1369     }
1370     case IITDescriptor::VecOfBitcastsToInt: {
1371       if (D.getArgumentNumber() >= ArgTys.size())
1372         return IsDeferredCheck || DeferCheck(Ty);
1373       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1374       auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1375       if (!ThisArgVecTy || !ReferenceType)
1376         return true;
1377       return ThisArgVecTy != VectorType::getInteger(ReferenceType);
1378     }
1379   }
1380   llvm_unreachable("unhandled");
1381 }
1382 
1383 Intrinsic::MatchIntrinsicTypesResult
1384 Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
1385                                    ArrayRef<Intrinsic::IITDescriptor> &Infos,
1386                                    SmallVectorImpl<Type *> &ArgTys) {
1387   SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks;
1388   if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks,
1389                          false))
1390     return MatchIntrinsicTypes_NoMatchRet;
1391 
1392   unsigned NumDeferredReturnChecks = DeferredChecks.size();
1393 
1394   for (auto Ty : FTy->params())
1395     if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false))
1396       return MatchIntrinsicTypes_NoMatchArg;
1397 
1398   for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) {
1399     DeferredIntrinsicMatchPair &Check = DeferredChecks[I];
1400     if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks,
1401                            true))
1402       return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet
1403                                          : MatchIntrinsicTypes_NoMatchArg;
1404   }
1405 
1406   return MatchIntrinsicTypes_Match;
1407 }
1408 
1409 bool
1410 Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1411                                 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1412   // If there are no descriptors left, then it can't be a vararg.
1413   if (Infos.empty())
1414     return isVarArg;
1415 
1416   // There should be only one descriptor remaining at this point.
1417   if (Infos.size() != 1)
1418     return true;
1419 
1420   // Check and verify the descriptor.
1421   IITDescriptor D = Infos.front();
1422   Infos = Infos.slice(1);
1423   if (D.Kind == IITDescriptor::VarArg)
1424     return !isVarArg;
1425 
1426   return true;
1427 }
1428 
1429 Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
1430   Intrinsic::ID ID = F->getIntrinsicID();
1431   if (!ID)
1432     return None;
1433 
1434   FunctionType *FTy = F->getFunctionType();
1435   // Accumulate an array of overloaded types for the given intrinsic
1436   SmallVector<Type *, 4> ArgTys;
1437   {
1438     SmallVector<Intrinsic::IITDescriptor, 8> Table;
1439     getIntrinsicInfoTableEntries(ID, Table);
1440     ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1441 
1442     if (Intrinsic::matchIntrinsicSignature(FTy, TableRef, ArgTys))
1443       return None;
1444     if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
1445       return None;
1446   }
1447 
1448   StringRef Name = F->getName();
1449   if (Name == Intrinsic::getName(ID, ArgTys))
1450     return None;
1451 
1452   auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1453   NewDecl->setCallingConv(F->getCallingConv());
1454   assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
1455   return NewDecl;
1456 }
1457 
1458 /// hasAddressTaken - returns true if there are any uses of this function
1459 /// other than direct calls or invokes to it.
1460 bool Function::hasAddressTaken(const User* *PutOffender) const {
1461   for (const Use &U : uses()) {
1462     const User *FU = U.getUser();
1463     if (isa<BlockAddress>(FU))
1464       continue;
1465     const auto *Call = dyn_cast<CallBase>(FU);
1466     if (!Call) {
1467       if (PutOffender)
1468         *PutOffender = FU;
1469       return true;
1470     }
1471     if (!Call->isCallee(&U)) {
1472       if (PutOffender)
1473         *PutOffender = FU;
1474       return true;
1475     }
1476   }
1477   return false;
1478 }
1479 
1480 bool Function::isDefTriviallyDead() const {
1481   // Check the linkage
1482   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1483       !hasAvailableExternallyLinkage())
1484     return false;
1485 
1486   // Check if the function is used by anything other than a blockaddress.
1487   for (const User *U : users())
1488     if (!isa<BlockAddress>(U))
1489       return false;
1490 
1491   return true;
1492 }
1493 
1494 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
1495 /// setjmp or other function that gcc recognizes as "returning twice".
1496 bool Function::callsFunctionThatReturnsTwice() const {
1497   for (const Instruction &I : instructions(this))
1498     if (const auto *Call = dyn_cast<CallBase>(&I))
1499       if (Call->hasFnAttr(Attribute::ReturnsTwice))
1500         return true;
1501 
1502   return false;
1503 }
1504 
1505 Constant *Function::getPersonalityFn() const {
1506   assert(hasPersonalityFn() && getNumOperands());
1507   return cast<Constant>(Op<0>());
1508 }
1509 
1510 void Function::setPersonalityFn(Constant *Fn) {
1511   setHungoffOperand<0>(Fn);
1512   setValueSubclassDataBit(3, Fn != nullptr);
1513 }
1514 
1515 Constant *Function::getPrefixData() const {
1516   assert(hasPrefixData() && getNumOperands());
1517   return cast<Constant>(Op<1>());
1518 }
1519 
1520 void Function::setPrefixData(Constant *PrefixData) {
1521   setHungoffOperand<1>(PrefixData);
1522   setValueSubclassDataBit(1, PrefixData != nullptr);
1523 }
1524 
1525 Constant *Function::getPrologueData() const {
1526   assert(hasPrologueData() && getNumOperands());
1527   return cast<Constant>(Op<2>());
1528 }
1529 
1530 void Function::setPrologueData(Constant *PrologueData) {
1531   setHungoffOperand<2>(PrologueData);
1532   setValueSubclassDataBit(2, PrologueData != nullptr);
1533 }
1534 
1535 void Function::allocHungoffUselist() {
1536   // If we've already allocated a uselist, stop here.
1537   if (getNumOperands())
1538     return;
1539 
1540   allocHungoffUses(3, /*IsPhi=*/ false);
1541   setNumHungOffUseOperands(3);
1542 
1543   // Initialize the uselist with placeholder operands to allow traversal.
1544   auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
1545   Op<0>().set(CPN);
1546   Op<1>().set(CPN);
1547   Op<2>().set(CPN);
1548 }
1549 
1550 template <int Idx>
1551 void Function::setHungoffOperand(Constant *C) {
1552   if (C) {
1553     allocHungoffUselist();
1554     Op<Idx>().set(C);
1555   } else if (getNumOperands()) {
1556     Op<Idx>().set(
1557         ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
1558   }
1559 }
1560 
1561 void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
1562   assert(Bit < 16 && "SubclassData contains only 16 bits");
1563   if (On)
1564     setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
1565   else
1566     setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
1567 }
1568 
1569 void Function::setEntryCount(ProfileCount Count,
1570                              const DenseSet<GlobalValue::GUID> *S) {
1571   assert(Count.hasValue());
1572 #if !defined(NDEBUG)
1573   auto PrevCount = getEntryCount();
1574   assert(!PrevCount.hasValue() || PrevCount.getType() == Count.getType());
1575 #endif
1576 
1577   auto ImportGUIDs = getImportGUIDs();
1578   if (S == nullptr && ImportGUIDs.size())
1579     S = &ImportGUIDs;
1580 
1581   MDBuilder MDB(getContext());
1582   setMetadata(
1583       LLVMContext::MD_prof,
1584       MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
1585 }
1586 
1587 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
1588                              const DenseSet<GlobalValue::GUID> *Imports) {
1589   setEntryCount(ProfileCount(Count, Type), Imports);
1590 }
1591 
1592 ProfileCount Function::getEntryCount(bool AllowSynthetic) const {
1593   MDNode *MD = getMetadata(LLVMContext::MD_prof);
1594   if (MD && MD->getOperand(0))
1595     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
1596       if (MDS->getString().equals("function_entry_count")) {
1597         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
1598         uint64_t Count = CI->getValue().getZExtValue();
1599         // A value of -1 is used for SamplePGO when there were no samples.
1600         // Treat this the same as unknown.
1601         if (Count == (uint64_t)-1)
1602           return ProfileCount::getInvalid();
1603         return ProfileCount(Count, PCT_Real);
1604       } else if (AllowSynthetic &&
1605                  MDS->getString().equals("synthetic_function_entry_count")) {
1606         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
1607         uint64_t Count = CI->getValue().getZExtValue();
1608         return ProfileCount(Count, PCT_Synthetic);
1609       }
1610     }
1611   return ProfileCount::getInvalid();
1612 }
1613 
1614 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
1615   DenseSet<GlobalValue::GUID> R;
1616   if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
1617     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1618       if (MDS->getString().equals("function_entry_count"))
1619         for (unsigned i = 2; i < MD->getNumOperands(); i++)
1620           R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
1621                        ->getValue()
1622                        .getZExtValue());
1623   return R;
1624 }
1625 
1626 void Function::setSectionPrefix(StringRef Prefix) {
1627   MDBuilder MDB(getContext());
1628   setMetadata(LLVMContext::MD_section_prefix,
1629               MDB.createFunctionSectionPrefix(Prefix));
1630 }
1631 
1632 Optional<StringRef> Function::getSectionPrefix() const {
1633   if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
1634     assert(cast<MDString>(MD->getOperand(0))
1635                ->getString()
1636                .equals("function_section_prefix") &&
1637            "Metadata not match");
1638     return cast<MDString>(MD->getOperand(1))->getString();
1639   }
1640   return None;
1641 }
1642 
1643 bool Function::nullPointerIsDefined() const {
1644   return getFnAttribute("null-pointer-is-valid")
1645           .getValueAsString()
1646           .equals("true");
1647 }
1648 
1649 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
1650   if (F && F->nullPointerIsDefined())
1651     return true;
1652 
1653   if (AS != 0)
1654     return true;
1655 
1656   return false;
1657 }
1658