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