LLVM  9.0.0svn
Function.h
Go to the documentation of this file.
1 //===- llvm/Function.h - Class to represent a single function ---*- C++ -*-===//
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 contains the declaration of the Function class, which represents a
10 // single function/procedure in LLVM.
11 //
12 // A function basically consists of a list of basic blocks, a list of arguments,
13 // and a symbol table.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_IR_FUNCTION_H
18 #define LLVM_IR_FUNCTION_H
19 
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/IR/Argument.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/GlobalObject.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/OperandTraits.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Compiler.h"
37 #include <cassert>
38 #include <cstddef>
39 #include <cstdint>
40 #include <memory>
41 #include <string>
42 
43 namespace llvm {
44 
45 namespace Intrinsic {
46 enum ID : unsigned;
47 }
48 
49 class AssemblyAnnotationWriter;
50 class Constant;
51 class DISubprogram;
52 class LLVMContext;
53 class Module;
54 template <typename T> class Optional;
55 class raw_ostream;
56 class Type;
57 class User;
58 
59 class Function : public GlobalObject, public ilist_node<Function> {
60 public:
62 
63  // BasicBlock iterators...
66 
68  using const_arg_iterator = const Argument *;
69 
70 private:
71  // Important things that make up a function!
72  BasicBlockListType BasicBlocks; ///< The basic blocks
73  mutable Argument *Arguments = nullptr; ///< The formal arguments
74  size_t NumArgs;
75  std::unique_ptr<ValueSymbolTable>
76  SymTab; ///< Symbol table of args/instructions
77  AttributeList AttributeSets; ///< Parameter attributes
78 
79  /*
80  * Value::SubclassData
81  *
82  * bit 0 : HasLazyArguments
83  * bit 1 : HasPrefixData
84  * bit 2 : HasPrologueData
85  * bit 3 : HasPersonalityFn
86  * bits 4-13 : CallingConvention
87  * bits 14 : HasGC
88  * bits 15 : [reserved]
89  */
90 
91  /// Bits from GlobalObject::GlobalObjectSubclassData.
92  enum {
93  /// Whether this function is materializable.
94  IsMaterializableBit = 0,
95  };
96 
98 
99  /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
100  /// built on demand, so that the list isn't allocated until the first client
101  /// needs it. The hasLazyArguments predicate returns true if the arg list
102  /// hasn't been set up yet.
103 public:
104  bool hasLazyArguments() const {
105  return getSubclassDataFromValue() & (1<<0);
106  }
107 
108 private:
109  void CheckLazyArguments() const {
110  if (hasLazyArguments())
111  BuildLazyArguments();
112  }
113 
114  void BuildLazyArguments() const;
115 
116  void clearArguments();
117 
118  /// Function ctor - If the (optional) Module argument is specified, the
119  /// function is automatically inserted into the end of the function list for
120  /// the module.
121  ///
122  Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
123  const Twine &N = "", Module *M = nullptr);
124 
125 public:
126  Function(const Function&) = delete;
127  void operator=(const Function&) = delete;
128  ~Function();
129 
130  // This is here to help easily convert from FunctionT * (Function * or
131  // MachineFunction *) in BlockFrequencyInfoImpl to Function * by calling
132  // FunctionT->getFunction().
133  const Function &getFunction() const { return *this; }
134 
135  static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
136  unsigned AddrSpace, const Twine &N = "",
137  Module *M = nullptr) {
138  return new Function(Ty, Linkage, AddrSpace, N, M);
139  }
140 
141  // TODO: remove this once all users have been updated to pass an AddrSpace
142  static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
143  const Twine &N = "", Module *M = nullptr) {
144  return new Function(Ty, Linkage, static_cast<unsigned>(-1), N, M);
145  }
146 
147  /// Creates a new function and attaches it to a module.
148  ///
149  /// Places the function in the program address space as specified
150  /// by the module's data layout.
151  static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
152  const Twine &N, Module &M);
153 
154  // Provide fast operand accessors.
156 
157  /// Returns the number of non-debug IR instructions in this function.
158  /// This is equivalent to the sum of the sizes of each basic block contained
159  /// within this function.
160  unsigned getInstructionCount() const;
161 
162  /// Returns the FunctionType for me.
164  return cast<FunctionType>(getValueType());
165  }
166 
167  /// Returns the type of the ret val.
168  Type *getReturnType() const { return getFunctionType()->getReturnType(); }
169 
170  /// getContext - Return a reference to the LLVMContext associated with this
171  /// function.
172  LLVMContext &getContext() const;
173 
174  /// isVarArg - Return true if this function takes a variable number of
175  /// arguments.
176  bool isVarArg() const { return getFunctionType()->isVarArg(); }
177 
178  bool isMaterializable() const {
179  return getGlobalObjectSubClassData() & (1 << IsMaterializableBit);
180  }
181  void setIsMaterializable(bool V) {
182  unsigned Mask = 1 << IsMaterializableBit;
183  setGlobalObjectSubClassData((~Mask & getGlobalObjectSubClassData()) |
184  (V ? Mask : 0u));
185  }
186 
187  /// getIntrinsicID - This method returns the ID number of the specified
188  /// function, or Intrinsic::not_intrinsic if the function is not an
189  /// intrinsic, or if the pointer is null. This value is always defined to be
190  /// zero to allow easy checking for whether a function is intrinsic or not.
191  /// The particular intrinsic functions which correspond to this value are
192  /// defined in llvm/Intrinsics.h.
194 
195  /// isIntrinsic - Returns true if the function's name starts with "llvm.".
196  /// It's possible for this function to return true while getIntrinsicID()
197  /// returns Intrinsic::not_intrinsic!
198  bool isIntrinsic() const { return HasLLVMReservedName; }
199 
200  static Intrinsic::ID lookupIntrinsicID(StringRef Name);
201 
202  /// Recalculate the ID for this function if it is an Intrinsic defined
203  /// in llvm/Intrinsics.h. Sets the intrinsic ID to Intrinsic::not_intrinsic
204  /// if the name of this function does not match an intrinsic in that header.
205  /// Note, this method does not need to be called directly, as it is called
206  /// from Value::setName() whenever the name of this function changes.
207  void recalculateIntrinsicID();
208 
209  /// getCallingConv()/setCallingConv(CC) - These method get and set the
210  /// calling convention of this function. The enum values for the known
211  /// calling conventions are defined in CallingConv.h.
213  return static_cast<CallingConv::ID>((getSubclassDataFromValue() >> 4) &
215  }
217  auto ID = static_cast<unsigned>(CC);
218  assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
219  setValueSubclassData((getSubclassDataFromValue() & 0xc00f) | (ID << 4));
220  }
221 
222  /// Return the attribute list for this Function.
223  AttributeList getAttributes() const { return AttributeSets; }
224 
225  /// Set the attribute list for this Function.
226  void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
227 
228  /// Add function attributes to this function.
230  addAttribute(AttributeList::FunctionIndex, Kind);
231  }
232 
233  /// Add function attributes to this function.
235  addAttribute(AttributeList::FunctionIndex,
236  Attribute::get(getContext(), Kind, Val));
237  }
238 
239  /// Add function attributes to this function.
240  void addFnAttr(Attribute Attr) {
241  addAttribute(AttributeList::FunctionIndex, Attr);
242  }
243 
244  /// Remove function attributes from this function.
246  removeAttribute(AttributeList::FunctionIndex, Kind);
247  }
248 
249  /// Remove function attribute from this function.
250  void removeFnAttr(StringRef Kind) {
251  setAttributes(getAttributes().removeAttribute(
252  getContext(), AttributeList::FunctionIndex, Kind));
253  }
254 
255  enum ProfileCountType { PCT_Invalid, PCT_Real, PCT_Synthetic };
256 
257  /// Class to represent profile counts.
258  ///
259  /// This class represents both real and synthetic profile counts.
260  class ProfileCount {
261  private:
262  uint64_t Count;
263  ProfileCountType PCT;
264  static ProfileCount Invalid;
265 
266  public:
267  ProfileCount() : Count(-1), PCT(PCT_Invalid) {}
268  ProfileCount(uint64_t Count, ProfileCountType PCT)
269  : Count(Count), PCT(PCT) {}
270  bool hasValue() const { return PCT != PCT_Invalid; }
271  uint64_t getCount() const { return Count; }
272  ProfileCountType getType() const { return PCT; }
273  bool isSynthetic() const { return PCT == PCT_Synthetic; }
274  explicit operator bool() { return hasValue(); }
275  bool operator!() const { return !hasValue(); }
276  // Update the count retaining the same profile count type.
277  ProfileCount &setCount(uint64_t C) {
278  Count = C;
279  return *this;
280  }
281  static ProfileCount getInvalid() { return ProfileCount(-1, PCT_Invalid); }
282  };
283 
284  /// Set the entry count for this function.
285  ///
286  /// Entry count is the number of times this function was executed based on
287  /// pgo data. \p Imports points to a set of GUIDs that needs to
288  /// be imported by the function for sample PGO, to enable the same inlines as
289  /// the profiled optimized binary.
290  void setEntryCount(ProfileCount Count,
291  const DenseSet<GlobalValue::GUID> *Imports = nullptr);
292 
293  /// A convenience wrapper for setting entry count
294  void setEntryCount(uint64_t Count, ProfileCountType Type = PCT_Real,
295  const DenseSet<GlobalValue::GUID> *Imports = nullptr);
296 
297  /// Get the entry count for this function.
298  ///
299  /// Entry count is the number of times the function was executed based on
300  /// pgo data.
301  ProfileCount getEntryCount() const;
302 
303  /// Return true if the function is annotated with profile data.
304  ///
305  /// Presence of entry counts from a profile run implies the function has
306  /// profile annotations.
307  bool hasProfileData() const { return getEntryCount().hasValue(); }
308 
309  /// Returns the set of GUIDs that needs to be imported to the function for
310  /// sample PGO, to enable the same inlines as the profiled optimized binary.
311  DenseSet<GlobalValue::GUID> getImportGUIDs() const;
312 
313  /// Set the section prefix for this function.
314  void setSectionPrefix(StringRef Prefix);
315 
316  /// Get the section prefix for this function.
317  Optional<StringRef> getSectionPrefix() const;
318 
319  /// Return true if the function has the attribute.
321  return AttributeSets.hasFnAttribute(Kind);
322  }
323 
324  /// Return true if the function has the attribute.
325  bool hasFnAttribute(StringRef Kind) const {
326  return AttributeSets.hasFnAttribute(Kind);
327  }
328 
329  /// Return the attribute for the given attribute kind.
332  }
333 
334  /// Return the attribute for the given attribute kind.
337  }
338 
339  /// Return the stack alignment for the function.
340  unsigned getFnStackAlignment() const {
341  if (!hasFnAttribute(Attribute::StackAlignment))
342  return 0;
343  return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
344  }
345 
346  /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
347  /// to use during code generation.
348  bool hasGC() const {
349  return getSubclassDataFromValue() & (1<<14);
350  }
351  const std::string &getGC() const;
352  void setGC(std::string Str);
353  void clearGC();
354 
355  /// adds the attribute to the list of attributes.
356  void addAttribute(unsigned i, Attribute::AttrKind Kind);
357 
358  /// adds the attribute to the list of attributes.
359  void addAttribute(unsigned i, Attribute Attr);
360 
361  /// adds the attributes to the list of attributes.
362  void addAttributes(unsigned i, const AttrBuilder &Attrs);
363 
364  /// adds the attribute to the list of attributes for the given arg.
365  void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
366 
367  /// adds the attribute to the list of attributes for the given arg.
368  void addParamAttr(unsigned ArgNo, Attribute Attr);
369 
370  /// adds the attributes to the list of attributes for the given arg.
371  void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
372 
373  /// removes the attribute from the list of attributes.
374  void removeAttribute(unsigned i, Attribute::AttrKind Kind);
375 
376  /// removes the attribute from the list of attributes.
377  void removeAttribute(unsigned i, StringRef Kind);
378 
379  /// removes the attributes from the list of attributes.
380  void removeAttributes(unsigned i, const AttrBuilder &Attrs);
381 
382  /// removes the attribute from the list of attributes.
383  void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
384 
385  /// removes the attribute from the list of attributes.
386  void removeParamAttr(unsigned ArgNo, StringRef Kind);
387 
388  /// removes the attribute from the list of attributes.
389  void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
390 
391  /// check if an attributes is in the list of attributes.
392  bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {
393  return getAttributes().hasAttribute(i, Kind);
394  }
395 
396  /// check if an attributes is in the list of attributes.
397  bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
398  return getAttributes().hasParamAttribute(ArgNo, Kind);
399  }
400 
401  /// gets the attribute from the list of attributes.
402  Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
403  return AttributeSets.getAttribute(i, Kind);
404  }
405 
406  /// gets the attribute from the list of attributes.
407  Attribute getAttribute(unsigned i, StringRef Kind) const {
408  return AttributeSets.getAttribute(i, Kind);
409  }
410 
411  /// adds the dereferenceable attribute to the list of attributes.
412  void addDereferenceableAttr(unsigned i, uint64_t Bytes);
413 
414  /// adds the dereferenceable attribute to the list of attributes for
415  /// the given arg.
416  void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
417 
418  /// adds the dereferenceable_or_null attribute to the list of
419  /// attributes.
420  void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
421 
422  /// adds the dereferenceable_or_null attribute to the list of
423  /// attributes for the given arg.
424  void addDereferenceableOrNullParamAttr(unsigned ArgNo, uint64_t Bytes);
425 
426  /// Extract the alignment for a call or parameter (0=unknown).
427  unsigned getParamAlignment(unsigned ArgNo) const {
428  return AttributeSets.getParamAlignment(ArgNo);
429  }
430 
431  /// Extract the number of dereferenceable bytes for a call or
432  /// parameter (0=unknown).
433  /// @param i AttributeList index, referring to a return value or argument.
434  uint64_t getDereferenceableBytes(unsigned i) const {
435  return AttributeSets.getDereferenceableBytes(i);
436  }
437 
438  /// Extract the number of dereferenceable bytes for a parameter.
439  /// @param ArgNo Index of an argument, with 0 being the first function arg.
440  uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
441  return AttributeSets.getParamDereferenceableBytes(ArgNo);
442  }
443 
444  /// Extract the number of dereferenceable_or_null bytes for a call or
445  /// parameter (0=unknown).
446  /// @param i AttributeList index, referring to a return value or argument.
447  uint64_t getDereferenceableOrNullBytes(unsigned i) const {
448  return AttributeSets.getDereferenceableOrNullBytes(i);
449  }
450 
451  /// Extract the number of dereferenceable_or_null bytes for a
452  /// parameter.
453  /// @param ArgNo AttributeList ArgNo, referring to an argument.
454  uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
455  return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
456  }
457 
458  /// Determine if the function does not access memory.
459  bool doesNotAccessMemory() const {
460  return hasFnAttribute(Attribute::ReadNone);
461  }
463  addFnAttr(Attribute::ReadNone);
464  }
465 
466  /// Determine if the function does not access or only reads memory.
467  bool onlyReadsMemory() const {
468  return doesNotAccessMemory() || hasFnAttribute(Attribute::ReadOnly);
469  }
471  addFnAttr(Attribute::ReadOnly);
472  }
473 
474  /// Determine if the function does not access or only writes memory.
475  bool doesNotReadMemory() const {
476  return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly);
477  }
479  addFnAttr(Attribute::WriteOnly);
480  }
481 
482  /// Determine if the call can access memmory only using pointers based
483  /// on its arguments.
484  bool onlyAccessesArgMemory() const {
485  return hasFnAttribute(Attribute::ArgMemOnly);
486  }
487  void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly); }
488 
489  /// Determine if the function may only access memory that is
490  /// inaccessible from the IR.
492  return hasFnAttribute(Attribute::InaccessibleMemOnly);
493  }
495  addFnAttr(Attribute::InaccessibleMemOnly);
496  }
497 
498  /// Determine if the function may only access memory that is
499  /// either inaccessible from the IR or pointed to by its arguments.
501  return hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly);
502  }
504  addFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
505  }
506 
507  /// Determine if the function cannot return.
508  bool doesNotReturn() const {
509  return hasFnAttribute(Attribute::NoReturn);
510  }
512  addFnAttr(Attribute::NoReturn);
513  }
514 
515  /// Determine if the function should not perform indirect branch tracking.
516  bool doesNoCfCheck() const { return hasFnAttribute(Attribute::NoCfCheck); }
517 
518  /// Determine if the function cannot unwind.
519  bool doesNotThrow() const {
520  return hasFnAttribute(Attribute::NoUnwind);
521  }
523  addFnAttr(Attribute::NoUnwind);
524  }
525 
526  /// Determine if the call cannot be duplicated.
527  bool cannotDuplicate() const {
528  return hasFnAttribute(Attribute::NoDuplicate);
529  }
531  addFnAttr(Attribute::NoDuplicate);
532  }
533 
534  /// Determine if the call is convergent.
535  bool isConvergent() const {
536  return hasFnAttribute(Attribute::Convergent);
537  }
538  void setConvergent() {
539  addFnAttr(Attribute::Convergent);
540  }
542  removeFnAttr(Attribute::Convergent);
543  }
544 
545  /// Determine if the call has sideeffects.
546  bool isSpeculatable() const {
547  return hasFnAttribute(Attribute::Speculatable);
548  }
550  addFnAttr(Attribute::Speculatable);
551  }
552 
553  /// Determine if the function is known not to recurse, directly or
554  /// indirectly.
555  bool doesNotRecurse() const {
556  return hasFnAttribute(Attribute::NoRecurse);
557  }
559  addFnAttr(Attribute::NoRecurse);
560  }
561 
562  /// True if the ABI mandates (or the user requested) that this
563  /// function be in a unwind table.
564  bool hasUWTable() const {
565  return hasFnAttribute(Attribute::UWTable);
566  }
567  void setHasUWTable() {
568  addFnAttr(Attribute::UWTable);
569  }
570 
571  /// True if this function needs an unwind table.
572  bool needsUnwindTableEntry() const {
573  return hasUWTable() || !doesNotThrow() || hasPersonalityFn();
574  }
575 
576  /// Determine if the function returns a structure through first
577  /// or second pointer argument.
578  bool hasStructRetAttr() const {
579  return AttributeSets.hasParamAttribute(0, Attribute::StructRet) ||
580  AttributeSets.hasParamAttribute(1, Attribute::StructRet);
581  }
582 
583  /// Determine if the parameter or return value is marked with NoAlias
584  /// attribute.
585  bool returnDoesNotAlias() const {
586  return AttributeSets.hasAttribute(AttributeList::ReturnIndex,
588  }
591  }
592 
593  /// Do not optimize this function (-O0).
594  bool hasOptNone() const { return hasFnAttribute(Attribute::OptimizeNone); }
595 
596  /// Optimize this function for minimum size (-Oz).
597  bool hasMinSize() const { return hasFnAttribute(Attribute::MinSize); }
598 
599  /// Optimize this function for size (-Os) or minimum size (-Oz).
600  bool hasOptSize() const {
601  return hasFnAttribute(Attribute::OptimizeForSize) || hasMinSize();
602  }
603 
604  /// copyAttributesFrom - copy all additional attributes (those not needed to
605  /// create a Function) from the Function Src to this one.
606  void copyAttributesFrom(const Function *Src);
607 
608  /// deleteBody - This method deletes the body of the function, and converts
609  /// the linkage to external.
610  ///
611  void deleteBody() {
612  dropAllReferences();
613  setLinkage(ExternalLinkage);
614  }
615 
616  /// removeFromParent - This method unlinks 'this' from the containing module,
617  /// but does not delete it.
618  ///
619  void removeFromParent();
620 
621  /// eraseFromParent - This method unlinks 'this' from the containing module
622  /// and deletes it.
623  ///
624  void eraseFromParent();
625 
626  /// Steal arguments from another function.
627  ///
628  /// Drop this function's arguments and splice in the ones from \c Src.
629  /// Requires that this has no function body.
630  void stealArgumentListFrom(Function &Src);
631 
632  /// Get the underlying elements of the Function... the basic block list is
633  /// empty for external functions.
634  ///
635  const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
636  BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
637 
639  return &Function::BasicBlocks;
640  }
641 
642  const BasicBlock &getEntryBlock() const { return front(); }
643  BasicBlock &getEntryBlock() { return front(); }
644 
645  //===--------------------------------------------------------------------===//
646  // Symbol Table Accessing functions...
647 
648  /// getSymbolTable() - Return the symbol table if any, otherwise nullptr.
649  ///
650  inline ValueSymbolTable *getValueSymbolTable() { return SymTab.get(); }
651  inline const ValueSymbolTable *getValueSymbolTable() const {
652  return SymTab.get();
653  }
654 
655  //===--------------------------------------------------------------------===//
656  // BasicBlock iterator forwarding functions
657  //
658  iterator begin() { return BasicBlocks.begin(); }
659  const_iterator begin() const { return BasicBlocks.begin(); }
660  iterator end () { return BasicBlocks.end(); }
661  const_iterator end () const { return BasicBlocks.end(); }
662 
663  size_t size() const { return BasicBlocks.size(); }
664  bool empty() const { return BasicBlocks.empty(); }
665  const BasicBlock &front() const { return BasicBlocks.front(); }
666  BasicBlock &front() { return BasicBlocks.front(); }
667  const BasicBlock &back() const { return BasicBlocks.back(); }
668  BasicBlock &back() { return BasicBlocks.back(); }
669 
670 /// @name Function Argument Iteration
671 /// @{
672 
674  CheckLazyArguments();
675  return Arguments;
676  }
678  CheckLazyArguments();
679  return Arguments;
680  }
681 
683  CheckLazyArguments();
684  return Arguments + NumArgs;
685  }
687  CheckLazyArguments();
688  return Arguments + NumArgs;
689  }
690 
692  return make_range(arg_begin(), arg_end());
693  }
695  return make_range(arg_begin(), arg_end());
696  }
697 
698 /// @}
699 
700  size_t arg_size() const { return NumArgs; }
701  bool arg_empty() const { return arg_size() == 0; }
702 
703  /// Check whether this function has a personality function.
704  bool hasPersonalityFn() const {
705  return getSubclassDataFromValue() & (1<<3);
706  }
707 
708  /// Get the personality function associated with this function.
709  Constant *getPersonalityFn() const;
710  void setPersonalityFn(Constant *Fn);
711 
712  /// Check whether this function has prefix data.
713  bool hasPrefixData() const {
714  return getSubclassDataFromValue() & (1<<1);
715  }
716 
717  /// Get the prefix data associated with this function.
718  Constant *getPrefixData() const;
719  void setPrefixData(Constant *PrefixData);
720 
721  /// Check whether this function has prologue data.
722  bool hasPrologueData() const {
723  return getSubclassDataFromValue() & (1<<2);
724  }
725 
726  /// Get the prologue data associated with this function.
727  Constant *getPrologueData() const;
728  void setPrologueData(Constant *PrologueData);
729 
730  /// Print the function to an output stream with an optional
731  /// AssemblyAnnotationWriter.
732  void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
733  bool ShouldPreserveUseListOrder = false,
734  bool IsForDebug = false) const;
735 
736  /// viewCFG - This function is meant for use from the debugger. You can just
737  /// say 'call F->viewCFG()' and a ghostview window should pop up from the
738  /// program, displaying the CFG of the current function with the code for each
739  /// basic block inside. This depends on there being a 'dot' and 'gv' program
740  /// in your path.
741  ///
742  void viewCFG() const;
743 
744  /// viewCFGOnly - This function is meant for use from the debugger. It works
745  /// just like viewCFG, but it does not include the contents of basic blocks
746  /// into the nodes, just the label. If you are only interested in the CFG
747  /// this can make the graph smaller.
748  ///
749  void viewCFGOnly() const;
750 
751  /// Methods for support type inquiry through isa, cast, and dyn_cast:
752  static bool classof(const Value *V) {
753  return V->getValueID() == Value::FunctionVal;
754  }
755 
756  /// dropAllReferences() - This method causes all the subinstructions to "let
757  /// go" of all references that they are maintaining. This allows one to
758  /// 'delete' a whole module at a time, even though there may be circular
759  /// references... first all references are dropped, and all use counts go to
760  /// zero. Then everything is deleted for real. Note that no operations are
761  /// valid on an object that has "dropped all references", except operator
762  /// delete.
763  ///
764  /// Since no other object in the module can have references into the body of a
765  /// function, dropping all references deletes the entire body of the function,
766  /// including any contained basic blocks.
767  ///
768  void dropAllReferences();
769 
770  /// hasAddressTaken - returns true if there are any uses of this function
771  /// other than direct calls or invokes to it, or blockaddress expressions.
772  /// Optionally passes back an offending user for diagnostic purposes.
773  ///
774  bool hasAddressTaken(const User** = nullptr) const;
775 
776  /// isDefTriviallyDead - Return true if it is trivially safe to remove
777  /// this function definition from the module (because it isn't externally
778  /// visible, does not have its address taken, and has no callers). To make
779  /// this more accurate, call removeDeadConstantUsers first.
780  bool isDefTriviallyDead() const;
781 
782  /// callsFunctionThatReturnsTwice - Return true if the function has a call to
783  /// setjmp or other function that gcc recognizes as "returning twice".
784  bool callsFunctionThatReturnsTwice() const;
785 
786  /// Set the attached subprogram.
787  ///
788  /// Calls \a setMetadata() with \a LLVMContext::MD_dbg.
789  void setSubprogram(DISubprogram *SP);
790 
791  /// Get the attached subprogram.
792  ///
793  /// Calls \a getMetadata() with \a LLVMContext::MD_dbg and casts the result
794  /// to \a DISubprogram.
795  DISubprogram *getSubprogram() const;
796 
797  /// Returns true if we should emit debug info for profiling.
798  bool isDebugInfoForProfiling() const;
799 
800  /// Check if null pointer dereferencing is considered undefined behavior for
801  /// the function.
802  /// Return value: false => null pointer dereference is undefined.
803  /// Return value: true => null pointer dereference is not undefined.
804  bool nullPointerIsDefined() const;
805 
806 private:
807  void allocHungoffUselist();
808  template<int Idx> void setHungoffOperand(Constant *C);
809 
810  /// Shadow Value::setValueSubclassData with a private forwarding method so
811  /// that subclasses cannot accidentally use it.
812  void setValueSubclassData(unsigned short D) {
814  }
815  void setValueSubclassDataBit(unsigned Bit, bool On);
816 };
817 
818 /// Check whether null pointer dereferencing is considered undefined behavior
819 /// for a given function or an address space.
820 /// Null pointer access in non-zero address space is not considered undefined.
821 /// Return value: false => null pointer dereference is undefined.
822 /// Return value: true => null pointer dereference is not undefined.
823 bool NullPointerIsDefined(const Function *F, unsigned AS = 0);
824 
825 template <>
827 
829 
830 } // end namespace llvm
831 
832 #endif // LLVM_IR_FUNCTION_H
size_t size() const
Definition: Function.h:663
const Function & getFunction() const
Definition: Function.h:133
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:176
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
Definition: Function.h:467
uint64_t CallInst * C
bool isIntrinsic() const
isIntrinsic - Returns true if the function&#39;s name starts with "llvm.".
Definition: Function.h:198
void setConvergent()
Definition: Function.h:538
This class provides a symbol table of name/value pairs.
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
bool empty() const
Definition: Function.h:664
unsigned getFnStackAlignment() const
Return the stack alignment for the function.
Definition: Function.h:340
const_iterator begin() const
Definition: Function.h:659
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:463
bool isSpeculatable() const
Determine if the call has sideeffects.
Definition: Function.h:546
uint64_t getParamDereferenceableBytes(unsigned ArgNo) const
Extract the number of dereferenceable bytes for a parameter.
Definition: Function.h:440
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:594
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:600
iterator end()
Definition: Function.h:660
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned getParamAlignment(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Definition: Function.h:427
bool hasFnAttribute(StringRef Kind) const
Return true if the function has the attribute.
Definition: Function.h:325
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
const BasicBlock & back() const
Definition: Function.h:667
void setDoesNotRecurse()
Definition: Function.h:558
bool hasPrologueData() const
Check whether this function has prologue data.
Definition: Function.h:722
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:320
arg_iterator arg_end()
Definition: Function.h:682
F(f)
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
void setDoesNotReadMemory()
Definition: Function.h:478
void setDoesNotThrow()
Definition: Function.h:522
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition: Function.h:578
bool hasPrefixData() const
Check whether this function has prefix data.
Definition: Function.h:713
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
uint64_t getDereferenceableBytes(unsigned Index) const
Get the number of dereferenceable bytes (or zero if unknown).
uint64_t getDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
Definition: Function.h:434
bool hasUWTable() const
True if the ABI mandates (or the user requested) that this function be in a unwind table...
Definition: Function.h:564
This file contains the simple types necessary to represent the attributes associated with functions a...
Attribute getAttribute(unsigned i, StringRef Kind) const
gets the attribute from the list of attributes.
Definition: Function.h:407
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition: Function.h:397
bool onlyAccessesInaccessibleMemory() const
Determine if the function may only access memory that is inaccessible from the IR.
Definition: Function.h:491
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
Subprogram description.
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
Class to represent function types.
Definition: DerivedTypes.h:102
static BasicBlockListType Function::* getSublistAccess(BasicBlock *)
Definition: Function.h:638
bool arg_empty() const
Definition: Function.h:701
bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition: Function.h:392
uint64_t getDereferenceableOrNullBytes(unsigned Index) const
Get the number of dereferenceable_or_null bytes (or zero if unknown).
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:223
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:704
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
iterator begin()
Definition: Function.h:658
void setSpeculatable()
Definition: Function.h:549
uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const
Extract the number of dereferenceable_or_null bytes for a parameter.
Definition: Function.h:454
void setIsMaterializable(bool V)
Definition: Function.h:181
void setOnlyAccessesArgMemory()
Definition: Function.h:487
bool doesNotAccessMemory() const
Determine if the function does not access memory.
Definition: Function.h:459
static xmlAttrPtr getAttribute(xmlNodePtr Node, const unsigned char *AttributeName)
uint64_t getDereferenceableOrNullBytes(unsigned i) const
Extract the number of dereferenceable_or_null bytes for a call or parameter (0=unknown).
Definition: Function.h:447
const BasicBlock & getEntryBlock() const
Definition: Function.h:642
bool cannotDuplicate() const
Determine if the call cannot be duplicated.
Definition: Function.h:527
unsigned getStackAlignment(unsigned Index) const
Get the stack alignment.
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:216
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:135
void setOnlyReadsMemory()
Definition: Function.h:470
uint64_t getCount() const
Definition: Function.h:271
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external...
Definition: Function.h:611
This is an important base class in LLVM.
Definition: Constant.h:41
bool returnDoesNotAlias() const
Determine if the parameter or return value is marked with NoAlias attribute.
Definition: Function.h:585
BasicBlock & getEntryBlock()
Definition: Function.h:643
BasicBlock & front()
Definition: Function.h:666
void addFnAttr(StringRef Kind, StringRef Val=StringRef())
Add function attributes to this function.
Definition: Function.h:234
AMDGPU Lower Kernel Arguments
BasicBlock & back()
Definition: Function.h:668
bool doesNotReturn() const
Determine if the function cannot return.
Definition: Function.h:508
iterator_range< const_arg_iterator > args() const
Definition: Function.h:694
size_t arg_size() const
Definition: Function.h:700
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
arg_iterator arg_begin()
Definition: Function.h:673
void setHasUWTable()
Definition: Function.h:567
bool isMaterializable() const
Definition: Function.h:178
The highest possible calling convention ID. Must be some 2^k - 1.
Definition: CallingConv.h:226
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return the attribute object that exists at the given index.
static DISubprogram * getSubprogram(bool IsDistinct, Ts &&... Args)
Definition: DIBuilder.cpp:745
Class to represent profile counts.
Definition: Function.h:260
void setCannotDuplicate()
Definition: Function.h:530
void addFnAttr(Attribute Attr)
Add function attributes to this function.
Definition: Function.h:240
BasicBlockListType & getBasicBlockList()
Definition: Function.h:636
bool doesNotRecurse() const
Determine if the function is known not to recurse, directly or indirectly.
Definition: Function.h:555
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:519
ProfileCount & setCount(uint64_t C)
Definition: Function.h:277
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
void setDoesNotReturn()
Definition: Function.h:511
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
void setOnlyAccessesInaccessibleMemOrArgMem()
Definition: Function.h:503
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static ProfileCount getInvalid()
Definition: Function.h:281
void setNotConvergent()
Definition: Function.h:541
ProfileCount(uint64_t Count, ProfileCountType PCT)
Definition: Function.h:268
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1440
void setOnlyAccessesInaccessibleMemory()
Definition: Function.h:494
void setValueSubclassData(unsigned short D)
Definition: Value.h:655
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:226
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:193
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition: Function.h:650
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:572
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
A range adaptor for a pair of iterators.
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Function.h:752
uint64_t getParamDereferenceableBytes(unsigned ArgNo) const
Get the number of dereferenceable bytes (or zero if unknown) of an arg.
Definition: Attributes.h:609
const_arg_iterator arg_end() const
Definition: Function.h:686
could "use" a pointer
Function::ProfileCount ProfileCount
Definition: Function.cpp:56
unsigned getParamAlignment(unsigned ArgNo) const
Return the alignment for the specified function parameter.
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition: Function.h:348
uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const
Get the number of dereferenceable_or_null bytes (or zero if unknown) of an arg.
Definition: Attributes.h:619
void setDoesNotAccessMemory()
Definition: Function.h:462
#define N
bool onlyAccessesArgMemory() const
Determine if the call can access memmory only using pointers based on its arguments.
Definition: Function.h:484
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:597
bool doesNotReadMemory() const
Determine if the function does not access or only writes memory.
Definition: Function.h:475
Compile-time customization of User operands.
Definition: User.h:42
bool onlyAccessesInaccessibleMemOrArgMem() const
Determine if the function may only access memory that is either inaccessible from the IR or pointed t...
Definition: Function.h:500
const_arg_iterator arg_begin() const
Definition: Function.h:677
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
Definition: Function.h:635
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:80
#define LLVM_READONLY
Definition: Compiler.h:183
const ValueSymbolTable * getValueSymbolTable() const
Definition: Function.h:651
void removeFnAttr(Attribute::AttrKind Kind)
Remove function attributes from this function.
Definition: Function.h:245
bool hasFnAttribute(Attribute::AttrKind Kind) const
Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but may be faster. ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BasicBlock & front() const
Definition: Function.h:665
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:72
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:95
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
ProfileCountType getType() const
Definition: Function.h:272
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:330
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.h:229
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const
gets the attribute from the list of attributes.
Definition: Function.h:402
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:142
Attribute getFnAttribute(StringRef Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:335
bool doesNoCfCheck() const
Determine if the function should not perform indirect branch tracking.
Definition: Function.h:516
const_iterator end() const
Definition: Function.h:661
bool isConvergent() const
Determine if the call is convergent.
Definition: Function.h:535
bool hasProfileData() const
Return true if the function is annotated with profile data.
Definition: Function.h:307
void removeFnAttr(StringRef Kind)
Remove function attribute from this function.
Definition: Function.h:250
bool hasLazyArguments() const
hasLazyArguments/CheckLazyArguments - The argument list of a function is built on demand...
Definition: Function.h:104
iterator_range< arg_iterator > args()
Definition: Function.h:691
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:69
void setReturnDoesNotAlias()
Definition: Function.h:589