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