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