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