1 //===- llvm/Function.h - Class to represent a single function ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the declaration of the Function class, which represents a
10 // single function/procedure in LLVM.
12 // A function basically consists of a list of basic blocks, a list of arguments,
13 // and a symbol table.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_FUNCTION_H
18 #define LLVM_IR_FUNCTION_H
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"
24 #include "llvm/ADT/iterator_range.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"
33 #include "llvm/IR/SymbolTableListTraits.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Compiler.h"
49 class AssemblyAnnotationWriter
;
54 template <typename T
> class Optional
;
59 class Function
: public GlobalObject
, public ilist_node
<Function
> {
61 using BasicBlockListType
= SymbolTableList
<BasicBlock
>;
63 // BasicBlock iterators...
64 using iterator
= BasicBlockListType::iterator
;
65 using const_iterator
= BasicBlockListType::const_iterator
;
67 using arg_iterator
= Argument
*;
68 using const_arg_iterator
= const Argument
*;
71 // Important things that make up a function!
72 BasicBlockListType BasicBlocks
; ///< The basic blocks
73 mutable Argument
*Arguments
= nullptr; ///< The formal arguments
75 std::unique_ptr
<ValueSymbolTable
>
76 SymTab
; ///< Symbol table of args/instructions
77 AttributeList AttributeSets
; ///< Parameter attributes
82 * bit 0 : HasLazyArguments
83 * bit 1 : HasPrefixData
84 * bit 2 : HasPrologueData
85 * bit 3 : HasPersonalityFn
86 * bits 4-13 : CallingConvention
88 * bits 15 : [reserved]
91 /// Bits from GlobalObject::GlobalObjectSubclassData.
93 /// Whether this function is materializable.
94 IsMaterializableBit
= 0,
97 friend class SymbolTableListTraits
<Function
>;
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.
104 bool hasLazyArguments() const {
105 return getSubclassDataFromValue() & (1<<0);
109 void CheckLazyArguments() const {
110 if (hasLazyArguments())
111 BuildLazyArguments();
114 void BuildLazyArguments() const;
116 void clearArguments();
118 /// Function ctor - If the (optional) Module argument is specified, the
119 /// function is automatically inserted into the end of the function list for
122 Function(FunctionType
*Ty
, LinkageTypes Linkage
, unsigned AddrSpace
,
123 const Twine
&N
= "", Module
*M
= nullptr);
126 Function(const Function
&) = delete;
127 void operator=(const Function
&) = delete;
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; }
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
);
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
);
147 /// Creates a new function and attaches it to a module.
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
);
154 // Provide fast operand accessors.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
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;
162 /// Returns the FunctionType for me.
163 FunctionType
*getFunctionType() const {
164 return cast
<FunctionType
>(getValueType());
167 /// Returns the type of the ret val.
168 Type
*getReturnType() const { return getFunctionType()->getReturnType(); }
170 /// getContext - Return a reference to the LLVMContext associated with this
172 LLVMContext
&getContext() const;
174 /// isVarArg - Return true if this function takes a variable number of
176 bool isVarArg() const { return getFunctionType()->isVarArg(); }
178 bool isMaterializable() const {
179 return getGlobalObjectSubClassData() & (1 << IsMaterializableBit
);
181 void setIsMaterializable(bool V
) {
182 unsigned Mask
= 1 << IsMaterializableBit
;
183 setGlobalObjectSubClassData((~Mask
& getGlobalObjectSubClassData()) |
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.
193 Intrinsic::ID
getIntrinsicID() const LLVM_READONLY
{ return IntID
; }
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
; }
200 static Intrinsic::ID
lookupIntrinsicID(StringRef Name
);
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();
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.
212 CallingConv::ID
getCallingConv() const {
213 return static_cast<CallingConv::ID
>((getSubclassDataFromValue() >> 4) &
216 void setCallingConv(CallingConv::ID CC
) {
217 auto ID
= static_cast<unsigned>(CC
);
218 assert(!(ID
& ~CallingConv::MaxID
) && "Unsupported calling convention");
219 setValueSubclassData((getSubclassDataFromValue() & 0xc00f) | (ID
<< 4));
222 /// Return the attribute list for this Function.
223 AttributeList
getAttributes() const { return AttributeSets
; }
225 /// Set the attribute list for this Function.
226 void setAttributes(AttributeList Attrs
) { AttributeSets
= Attrs
; }
228 /// Add function attributes to this function.
229 void addFnAttr(Attribute::AttrKind Kind
) {
230 addAttribute(AttributeList::FunctionIndex
, Kind
);
233 /// Add function attributes to this function.
234 void addFnAttr(StringRef Kind
, StringRef Val
= StringRef()) {
235 addAttribute(AttributeList::FunctionIndex
,
236 Attribute::get(getContext(), Kind
, Val
));
239 /// Add function attributes to this function.
240 void addFnAttr(Attribute Attr
) {
241 addAttribute(AttributeList::FunctionIndex
, Attr
);
244 /// Remove function attributes from this function.
245 void removeFnAttr(Attribute::AttrKind Kind
) {
246 removeAttribute(AttributeList::FunctionIndex
, Kind
);
249 /// Remove function attribute from this function.
250 void removeFnAttr(StringRef Kind
) {
251 setAttributes(getAttributes().removeAttribute(
252 getContext(), AttributeList::FunctionIndex
, Kind
));
255 enum ProfileCountType
{ PCT_Invalid
, PCT_Real
, PCT_Synthetic
};
257 /// Class to represent profile counts.
259 /// This class represents both real and synthetic profile counts.
263 ProfileCountType PCT
;
264 static ProfileCount Invalid
;
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
) {
281 static ProfileCount
getInvalid() { return ProfileCount(-1, PCT_Invalid
); }
284 /// Set the entry count for this function.
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);
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);
297 /// Get the entry count for this function.
299 /// Entry count is the number of times the function was executed.
300 /// When AllowSynthetic is false, only pgo_data will be returned.
301 ProfileCount
getEntryCount(bool AllowSynthetic
= false) const;
303 /// Return true if the function is annotated with profile data.
305 /// Presence of entry counts from a profile run implies the function has
306 /// profile annotations. If IncludeSynthetic is false, only return true
307 /// when the profile data is real.
308 bool hasProfileData(bool IncludeSynthetic
= false) const {
309 return getEntryCount(IncludeSynthetic
).hasValue();
312 /// Returns the set of GUIDs that needs to be imported to the function for
313 /// sample PGO, to enable the same inlines as the profiled optimized binary.
314 DenseSet
<GlobalValue::GUID
> getImportGUIDs() const;
316 /// Set the section prefix for this function.
317 void setSectionPrefix(StringRef Prefix
);
319 /// Get the section prefix for this function.
320 Optional
<StringRef
> getSectionPrefix() const;
322 /// Return true if the function has the attribute.
323 bool hasFnAttribute(Attribute::AttrKind Kind
) const {
324 return AttributeSets
.hasFnAttribute(Kind
);
327 /// Return true if the function has the attribute.
328 bool hasFnAttribute(StringRef Kind
) const {
329 return AttributeSets
.hasFnAttribute(Kind
);
332 /// Return the attribute for the given attribute kind.
333 Attribute
getFnAttribute(Attribute::AttrKind Kind
) const {
334 return getAttribute(AttributeList::FunctionIndex
, Kind
);
337 /// Return the attribute for the given attribute kind.
338 Attribute
getFnAttribute(StringRef Kind
) const {
339 return getAttribute(AttributeList::FunctionIndex
, Kind
);
342 /// Return the stack alignment for the function.
343 unsigned getFnStackAlignment() const {
344 if (!hasFnAttribute(Attribute::StackAlignment
))
347 AttributeSets
.getStackAlignment(AttributeList::FunctionIndex
))
352 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
353 /// to use during code generation.
355 return getSubclassDataFromValue() & (1<<14);
357 const std::string
&getGC() const;
358 void setGC(std::string Str
);
361 /// adds the attribute to the list of attributes.
362 void addAttribute(unsigned i
, Attribute::AttrKind Kind
);
364 /// adds the attribute to the list of attributes.
365 void addAttribute(unsigned i
, Attribute Attr
);
367 /// adds the attributes to the list of attributes.
368 void addAttributes(unsigned i
, const AttrBuilder
&Attrs
);
370 /// adds the attribute to the list of attributes for the given arg.
371 void addParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
);
373 /// adds the attribute to the list of attributes for the given arg.
374 void addParamAttr(unsigned ArgNo
, Attribute Attr
);
376 /// adds the attributes to the list of attributes for the given arg.
377 void addParamAttrs(unsigned ArgNo
, const AttrBuilder
&Attrs
);
379 /// removes the attribute from the list of attributes.
380 void removeAttribute(unsigned i
, Attribute::AttrKind Kind
);
382 /// removes the attribute from the list of attributes.
383 void removeAttribute(unsigned i
, StringRef Kind
);
385 /// removes the attributes from the list of attributes.
386 void removeAttributes(unsigned i
, const AttrBuilder
&Attrs
);
388 /// removes the attribute from the list of attributes.
389 void removeParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
);
391 /// removes the attribute from the list of attributes.
392 void removeParamAttr(unsigned ArgNo
, StringRef Kind
);
394 /// removes the attribute from the list of attributes.
395 void removeParamAttrs(unsigned ArgNo
, const AttrBuilder
&Attrs
);
397 /// check if an attributes is in the list of attributes.
398 bool hasAttribute(unsigned i
, Attribute::AttrKind Kind
) const {
399 return getAttributes().hasAttribute(i
, Kind
);
402 /// check if an attributes is in the list of attributes.
403 bool hasParamAttribute(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
404 return getAttributes().hasParamAttribute(ArgNo
, Kind
);
407 /// gets the specified attribute from the list of attributes.
408 Attribute
getParamAttribute(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
409 return getAttributes().getParamAttr(ArgNo
, Kind
);
412 /// gets the attribute from the list of attributes.
413 Attribute
getAttribute(unsigned i
, Attribute::AttrKind Kind
) const {
414 return AttributeSets
.getAttribute(i
, Kind
);
417 /// gets the attribute from the list of attributes.
418 Attribute
getAttribute(unsigned i
, StringRef Kind
) const {
419 return AttributeSets
.getAttribute(i
, Kind
);
422 /// adds the dereferenceable attribute to the list of attributes.
423 void addDereferenceableAttr(unsigned i
, uint64_t Bytes
);
425 /// adds the dereferenceable attribute to the list of attributes for
427 void addDereferenceableParamAttr(unsigned ArgNo
, uint64_t Bytes
);
429 /// adds the dereferenceable_or_null attribute to the list of
431 void addDereferenceableOrNullAttr(unsigned i
, uint64_t Bytes
);
433 /// adds the dereferenceable_or_null attribute to the list of
434 /// attributes for the given arg.
435 void addDereferenceableOrNullParamAttr(unsigned ArgNo
, uint64_t Bytes
);
437 /// Extract the alignment for a call or parameter (0=unknown).
438 unsigned getParamAlignment(unsigned ArgNo
) const {
439 if (const auto MA
= AttributeSets
.getParamAlignment(ArgNo
))
444 /// Extract the byval type for a parameter.
445 Type
*getParamByValType(unsigned ArgNo
) const {
446 Type
*Ty
= AttributeSets
.getParamByValType(ArgNo
);
447 return Ty
? Ty
: (arg_begin() + ArgNo
)->getType()->getPointerElementType();
450 /// Extract the number of dereferenceable bytes for a call or
451 /// parameter (0=unknown).
452 /// @param i AttributeList index, referring to a return value or argument.
453 uint64_t getDereferenceableBytes(unsigned i
) const {
454 return AttributeSets
.getDereferenceableBytes(i
);
457 /// Extract the number of dereferenceable bytes for a parameter.
458 /// @param ArgNo Index of an argument, with 0 being the first function arg.
459 uint64_t getParamDereferenceableBytes(unsigned ArgNo
) const {
460 return AttributeSets
.getParamDereferenceableBytes(ArgNo
);
463 /// Extract the number of dereferenceable_or_null bytes for a call or
464 /// parameter (0=unknown).
465 /// @param i AttributeList index, referring to a return value or argument.
466 uint64_t getDereferenceableOrNullBytes(unsigned i
) const {
467 return AttributeSets
.getDereferenceableOrNullBytes(i
);
470 /// Extract the number of dereferenceable_or_null bytes for a
472 /// @param ArgNo AttributeList ArgNo, referring to an argument.
473 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo
) const {
474 return AttributeSets
.getParamDereferenceableOrNullBytes(ArgNo
);
477 /// Determine if the function does not access memory.
478 bool doesNotAccessMemory() const {
479 return hasFnAttribute(Attribute::ReadNone
);
481 void setDoesNotAccessMemory() {
482 addFnAttr(Attribute::ReadNone
);
485 /// Determine if the function does not access or only reads memory.
486 bool onlyReadsMemory() const {
487 return doesNotAccessMemory() || hasFnAttribute(Attribute::ReadOnly
);
489 void setOnlyReadsMemory() {
490 addFnAttr(Attribute::ReadOnly
);
493 /// Determine if the function does not access or only writes memory.
494 bool doesNotReadMemory() const {
495 return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly
);
497 void setDoesNotReadMemory() {
498 addFnAttr(Attribute::WriteOnly
);
501 /// Determine if the call can access memmory only using pointers based
502 /// on its arguments.
503 bool onlyAccessesArgMemory() const {
504 return hasFnAttribute(Attribute::ArgMemOnly
);
506 void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly
); }
508 /// Determine if the function may only access memory that is
509 /// inaccessible from the IR.
510 bool onlyAccessesInaccessibleMemory() const {
511 return hasFnAttribute(Attribute::InaccessibleMemOnly
);
513 void setOnlyAccessesInaccessibleMemory() {
514 addFnAttr(Attribute::InaccessibleMemOnly
);
517 /// Determine if the function may only access memory that is
518 /// either inaccessible from the IR or pointed to by its arguments.
519 bool onlyAccessesInaccessibleMemOrArgMem() const {
520 return hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly
);
522 void setOnlyAccessesInaccessibleMemOrArgMem() {
523 addFnAttr(Attribute::InaccessibleMemOrArgMemOnly
);
526 /// Determine if the function cannot return.
527 bool doesNotReturn() const {
528 return hasFnAttribute(Attribute::NoReturn
);
530 void setDoesNotReturn() {
531 addFnAttr(Attribute::NoReturn
);
534 /// Determine if the function should not perform indirect branch tracking.
535 bool doesNoCfCheck() const { return hasFnAttribute(Attribute::NoCfCheck
); }
537 /// Determine if the function cannot unwind.
538 bool doesNotThrow() const {
539 return hasFnAttribute(Attribute::NoUnwind
);
541 void setDoesNotThrow() {
542 addFnAttr(Attribute::NoUnwind
);
545 /// Determine if the call cannot be duplicated.
546 bool cannotDuplicate() const {
547 return hasFnAttribute(Attribute::NoDuplicate
);
549 void setCannotDuplicate() {
550 addFnAttr(Attribute::NoDuplicate
);
553 /// Determine if the call is convergent.
554 bool isConvergent() const {
555 return hasFnAttribute(Attribute::Convergent
);
557 void setConvergent() {
558 addFnAttr(Attribute::Convergent
);
560 void setNotConvergent() {
561 removeFnAttr(Attribute::Convergent
);
564 /// Determine if the call has sideeffects.
565 bool isSpeculatable() const {
566 return hasFnAttribute(Attribute::Speculatable
);
568 void setSpeculatable() {
569 addFnAttr(Attribute::Speculatable
);
572 /// Determine if the call might deallocate memory.
573 bool doesNotFreeMemory() const {
574 return onlyReadsMemory() || hasFnAttribute(Attribute::NoFree
);
576 void setDoesNotFreeMemory() {
577 addFnAttr(Attribute::NoFree
);
580 /// Determine if the function is known not to recurse, directly or
582 bool doesNotRecurse() const {
583 return hasFnAttribute(Attribute::NoRecurse
);
585 void setDoesNotRecurse() {
586 addFnAttr(Attribute::NoRecurse
);
589 /// True if the ABI mandates (or the user requested) that this
590 /// function be in a unwind table.
591 bool hasUWTable() const {
592 return hasFnAttribute(Attribute::UWTable
);
594 void setHasUWTable() {
595 addFnAttr(Attribute::UWTable
);
598 /// True if this function needs an unwind table.
599 bool needsUnwindTableEntry() const {
600 return hasUWTable() || !doesNotThrow() || hasPersonalityFn();
603 /// Determine if the function returns a structure through first
604 /// or second pointer argument.
605 bool hasStructRetAttr() const {
606 return AttributeSets
.hasParamAttribute(0, Attribute::StructRet
) ||
607 AttributeSets
.hasParamAttribute(1, Attribute::StructRet
);
610 /// Determine if the parameter or return value is marked with NoAlias
612 bool returnDoesNotAlias() const {
613 return AttributeSets
.hasAttribute(AttributeList::ReturnIndex
,
616 void setReturnDoesNotAlias() {
617 addAttribute(AttributeList::ReturnIndex
, Attribute::NoAlias
);
620 /// Do not optimize this function (-O0).
621 bool hasOptNone() const { return hasFnAttribute(Attribute::OptimizeNone
); }
623 /// Optimize this function for minimum size (-Oz).
624 bool hasMinSize() const { return hasFnAttribute(Attribute::MinSize
); }
626 /// Optimize this function for size (-Os) or minimum size (-Oz).
627 bool hasOptSize() const {
628 return hasFnAttribute(Attribute::OptimizeForSize
) || hasMinSize();
631 /// copyAttributesFrom - copy all additional attributes (those not needed to
632 /// create a Function) from the Function Src to this one.
633 void copyAttributesFrom(const Function
*Src
);
635 /// deleteBody - This method deletes the body of the function, and converts
636 /// the linkage to external.
640 setLinkage(ExternalLinkage
);
643 /// removeFromParent - This method unlinks 'this' from the containing module,
644 /// but does not delete it.
646 void removeFromParent();
648 /// eraseFromParent - This method unlinks 'this' from the containing module
651 void eraseFromParent();
653 /// Steal arguments from another function.
655 /// Drop this function's arguments and splice in the ones from \c Src.
656 /// Requires that this has no function body.
657 void stealArgumentListFrom(Function
&Src
);
659 /// Get the underlying elements of the Function... the basic block list is
660 /// empty for external functions.
662 const BasicBlockListType
&getBasicBlockList() const { return BasicBlocks
; }
663 BasicBlockListType
&getBasicBlockList() { return BasicBlocks
; }
665 static BasicBlockListType
Function::*getSublistAccess(BasicBlock
*) {
666 return &Function::BasicBlocks
;
669 const BasicBlock
&getEntryBlock() const { return front(); }
670 BasicBlock
&getEntryBlock() { return front(); }
672 //===--------------------------------------------------------------------===//
673 // Symbol Table Accessing functions...
675 /// getSymbolTable() - Return the symbol table if any, otherwise nullptr.
677 inline ValueSymbolTable
*getValueSymbolTable() { return SymTab
.get(); }
678 inline const ValueSymbolTable
*getValueSymbolTable() const {
682 //===--------------------------------------------------------------------===//
683 // BasicBlock iterator forwarding functions
685 iterator
begin() { return BasicBlocks
.begin(); }
686 const_iterator
begin() const { return BasicBlocks
.begin(); }
687 iterator
end () { return BasicBlocks
.end(); }
688 const_iterator
end () const { return BasicBlocks
.end(); }
690 size_t size() const { return BasicBlocks
.size(); }
691 bool empty() const { return BasicBlocks
.empty(); }
692 const BasicBlock
&front() const { return BasicBlocks
.front(); }
693 BasicBlock
&front() { return BasicBlocks
.front(); }
694 const BasicBlock
&back() const { return BasicBlocks
.back(); }
695 BasicBlock
&back() { return BasicBlocks
.back(); }
697 /// @name Function Argument Iteration
700 arg_iterator
arg_begin() {
701 CheckLazyArguments();
704 const_arg_iterator
arg_begin() const {
705 CheckLazyArguments();
709 arg_iterator
arg_end() {
710 CheckLazyArguments();
711 return Arguments
+ NumArgs
;
713 const_arg_iterator
arg_end() const {
714 CheckLazyArguments();
715 return Arguments
+ NumArgs
;
718 Argument
* getArg(unsigned i
) const {
719 assert (i
< NumArgs
&& "getArg() out of range!");
720 CheckLazyArguments();
721 return Arguments
+ i
;
724 iterator_range
<arg_iterator
> args() {
725 return make_range(arg_begin(), arg_end());
727 iterator_range
<const_arg_iterator
> args() const {
728 return make_range(arg_begin(), arg_end());
733 size_t arg_size() const { return NumArgs
; }
734 bool arg_empty() const { return arg_size() == 0; }
736 /// Check whether this function has a personality function.
737 bool hasPersonalityFn() const {
738 return getSubclassDataFromValue() & (1<<3);
741 /// Get the personality function associated with this function.
742 Constant
*getPersonalityFn() const;
743 void setPersonalityFn(Constant
*Fn
);
745 /// Check whether this function has prefix data.
746 bool hasPrefixData() const {
747 return getSubclassDataFromValue() & (1<<1);
750 /// Get the prefix data associated with this function.
751 Constant
*getPrefixData() const;
752 void setPrefixData(Constant
*PrefixData
);
754 /// Check whether this function has prologue data.
755 bool hasPrologueData() const {
756 return getSubclassDataFromValue() & (1<<2);
759 /// Get the prologue data associated with this function.
760 Constant
*getPrologueData() const;
761 void setPrologueData(Constant
*PrologueData
);
763 /// Print the function to an output stream with an optional
764 /// AssemblyAnnotationWriter.
765 void print(raw_ostream
&OS
, AssemblyAnnotationWriter
*AAW
= nullptr,
766 bool ShouldPreserveUseListOrder
= false,
767 bool IsForDebug
= false) const;
769 /// viewCFG - This function is meant for use from the debugger. You can just
770 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
771 /// program, displaying the CFG of the current function with the code for each
772 /// basic block inside. This depends on there being a 'dot' and 'gv' program
775 void viewCFG() const;
777 /// viewCFGOnly - This function is meant for use from the debugger. It works
778 /// just like viewCFG, but it does not include the contents of basic blocks
779 /// into the nodes, just the label. If you are only interested in the CFG
780 /// this can make the graph smaller.
782 void viewCFGOnly() const;
784 /// Methods for support type inquiry through isa, cast, and dyn_cast:
785 static bool classof(const Value
*V
) {
786 return V
->getValueID() == Value::FunctionVal
;
789 /// dropAllReferences() - This method causes all the subinstructions to "let
790 /// go" of all references that they are maintaining. This allows one to
791 /// 'delete' a whole module at a time, even though there may be circular
792 /// references... first all references are dropped, and all use counts go to
793 /// zero. Then everything is deleted for real. Note that no operations are
794 /// valid on an object that has "dropped all references", except operator
797 /// Since no other object in the module can have references into the body of a
798 /// function, dropping all references deletes the entire body of the function,
799 /// including any contained basic blocks.
801 void dropAllReferences();
803 /// hasAddressTaken - returns true if there are any uses of this function
804 /// other than direct calls or invokes to it, or blockaddress expressions.
805 /// Optionally passes back an offending user for diagnostic purposes.
807 bool hasAddressTaken(const User
** = nullptr) const;
809 /// isDefTriviallyDead - Return true if it is trivially safe to remove
810 /// this function definition from the module (because it isn't externally
811 /// visible, does not have its address taken, and has no callers). To make
812 /// this more accurate, call removeDeadConstantUsers first.
813 bool isDefTriviallyDead() const;
815 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
816 /// setjmp or other function that gcc recognizes as "returning twice".
817 bool callsFunctionThatReturnsTwice() const;
819 /// Set the attached subprogram.
821 /// Calls \a setMetadata() with \a LLVMContext::MD_dbg.
822 void setSubprogram(DISubprogram
*SP
);
824 /// Get the attached subprogram.
826 /// Calls \a getMetadata() with \a LLVMContext::MD_dbg and casts the result
827 /// to \a DISubprogram.
828 DISubprogram
*getSubprogram() const;
830 /// Returns true if we should emit debug info for profiling.
831 bool isDebugInfoForProfiling() const;
833 /// Check if null pointer dereferencing is considered undefined behavior for
835 /// Return value: false => null pointer dereference is undefined.
836 /// Return value: true => null pointer dereference is not undefined.
837 bool nullPointerIsDefined() const;
840 void allocHungoffUselist();
841 template<int Idx
> void setHungoffOperand(Constant
*C
);
843 /// Shadow Value::setValueSubclassData with a private forwarding method so
844 /// that subclasses cannot accidentally use it.
845 void setValueSubclassData(unsigned short D
) {
846 Value::setValueSubclassData(D
);
848 void setValueSubclassDataBit(unsigned Bit
, bool On
);
851 /// Check whether null pointer dereferencing is considered undefined behavior
852 /// for a given function or an address space.
853 /// Null pointer access in non-zero address space is not considered undefined.
854 /// Return value: false => null pointer dereference is undefined.
855 /// Return value: true => null pointer dereference is not undefined.
856 bool NullPointerIsDefined(const Function
*F
, unsigned AS
= 0);
859 struct OperandTraits
<Function
> : public HungoffOperandTraits
<3> {};
861 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(Function
, Value
)
863 } // end namespace llvm
865 #endif // LLVM_IR_FUNCTION_H