blob: 7184cb49af97c83ea6510a394a24b33580332e3f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Function.h - Class to represent a single function ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
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"
37#include <cassert>
38#include <cstddef>
39#include <cstdint>
40#include <memory>
41#include <string>
42
43namespace llvm {
44
45namespace Intrinsic {
46enum ID : unsigned;
47}
48
49class AssemblyAnnotationWriter;
50class Constant;
51class DISubprogram;
52class LLVMContext;
53class Module;
54template <typename T> class Optional;
55class raw_ostream;
56class Type;
57class User;
58
59class Function : public GlobalObject, public ilist_node<Function> {
60public:
61 using BasicBlockListType = SymbolTableList<BasicBlock>;
62
63 // BasicBlock iterators...
64 using iterator = BasicBlockListType::iterator;
65 using const_iterator = BasicBlockListType::const_iterator;
66
67 using arg_iterator = Argument *;
68 using const_arg_iterator = const Argument *;
69
70private:
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
97 friend class SymbolTableListTraits<Function>;
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.
103public:
104 bool hasLazyArguments() const {
105 return getSubclassDataFromValue() & (1<<0);
106 }
107
108private:
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 ///
Andrew Scull0372a572018-11-16 15:47:06 +0000122 Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100123 const Twine &N = "", Module *M = nullptr);
124
125public:
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,
Andrew Scull0372a572018-11-16 15:47:06 +0000136 unsigned AddrSpace, const Twine &N = "",
137 Module *M = nullptr) {
138 return new Function(Ty, Linkage, AddrSpace, N, M);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100139 }
140
Andrew Scull0372a572018-11-16 15:47:06 +0000141 // 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100154 // Provide fast operand accessors.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
156
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100157 /// 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.
Andrew Walbran16937d02019-10-22 13:54:20 +0100160 unsigned getInstructionCount() const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100161
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100162 /// Returns the FunctionType for me.
163 FunctionType *getFunctionType() const {
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.
193 Intrinsic::ID getIntrinsicID() const LLVM_READONLY { return IntID; }
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100202 /// Recalculate the ID for this function if it is an Intrinsic defined
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100203 /// 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.
212 CallingConv::ID getCallingConv() const {
213 return static_cast<CallingConv::ID>((getSubclassDataFromValue() >> 4) &
214 CallingConv::MaxID);
215 }
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));
220 }
221
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100222 /// Return the attribute list for this Function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100223 AttributeList getAttributes() const { return AttributeSets; }
224
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100225 /// Set the attribute list for this Function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100226 void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
227
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100228 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 void addFnAttr(Attribute::AttrKind Kind) {
230 addAttribute(AttributeList::FunctionIndex, Kind);
231 }
232
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100233 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100234 void addFnAttr(StringRef Kind, StringRef Val = StringRef()) {
235 addAttribute(AttributeList::FunctionIndex,
236 Attribute::get(getContext(), Kind, Val));
237 }
238
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100239 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100240 void addFnAttr(Attribute Attr) {
241 addAttribute(AttributeList::FunctionIndex, Attr);
242 }
243
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100244 /// Remove function attributes from this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245 void removeFnAttr(Attribute::AttrKind Kind) {
246 removeAttribute(AttributeList::FunctionIndex, Kind);
247 }
248
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100249 /// Remove function attribute from this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100250 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100284 /// Set the entry count for this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285 ///
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100297 /// Get the entry count for this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298 ///
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100319 /// Return true if the function has the attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100320 bool hasFnAttribute(Attribute::AttrKind Kind) const {
321 return AttributeSets.hasFnAttribute(Kind);
322 }
323
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100324 /// Return true if the function has the attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100325 bool hasFnAttribute(StringRef Kind) const {
326 return AttributeSets.hasFnAttribute(Kind);
327 }
328
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100329 /// Return the attribute for the given attribute kind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100330 Attribute getFnAttribute(Attribute::AttrKind Kind) const {
331 return getAttribute(AttributeList::FunctionIndex, Kind);
332 }
333
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100334 /// Return the attribute for the given attribute kind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100335 Attribute getFnAttribute(StringRef Kind) const {
336 return getAttribute(AttributeList::FunctionIndex, Kind);
337 }
338
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100339 /// Return the stack alignment for the function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100340 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100355 /// adds the attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100356 void addAttribute(unsigned i, Attribute::AttrKind Kind);
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358 /// adds the attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359 void addAttribute(unsigned i, Attribute Attr);
360
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100361 /// adds the attributes to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100362 void addAttributes(unsigned i, const AttrBuilder &Attrs);
363
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100364 /// adds the attribute to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100365 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
366
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100367 /// adds the attribute to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100368 void addParamAttr(unsigned ArgNo, Attribute Attr);
369
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100370 /// adds the attributes to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100371 void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
372
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100373 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100374 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
375
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100376 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100377 void removeAttribute(unsigned i, StringRef Kind);
378
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100379 /// removes the attributes from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100380 void removeAttributes(unsigned i, const AttrBuilder &Attrs);
381
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100382 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100383 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
384
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100385 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100386 void removeParamAttr(unsigned ArgNo, StringRef Kind);
387
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100388 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389 void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
390
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100391 /// check if an attributes is in the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100392 bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {
393 return getAttributes().hasAttribute(i, Kind);
394 }
395
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100396 /// check if an attributes is in the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100397 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
398 return getAttributes().hasParamAttribute(ArgNo, Kind);
399 }
400
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100401 /// gets the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100402 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
403 return AttributeSets.getAttribute(i, Kind);
404 }
405
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100406 /// gets the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100407 Attribute getAttribute(unsigned i, StringRef Kind) const {
408 return AttributeSets.getAttribute(i, Kind);
409 }
410
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100411 /// adds the dereferenceable attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100412 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
413
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100414 /// adds the dereferenceable attribute to the list of attributes for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415 /// the given arg.
416 void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
417
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100418 /// adds the dereferenceable_or_null attribute to the list of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100419 /// attributes.
420 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
421
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100422 /// adds the dereferenceable_or_null attribute to the list of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100423 /// attributes for the given arg.
424 void addDereferenceableOrNullParamAttr(unsigned ArgNo, uint64_t Bytes);
425
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100426 /// Extract the alignment for a call or parameter (0=unknown).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100427 unsigned getParamAlignment(unsigned ArgNo) const {
428 return AttributeSets.getParamAlignment(ArgNo);
429 }
430
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100431 /// Extract the number of dereferenceable bytes for a call or
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100432 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100438 /// Extract the number of dereferenceable bytes for a parameter.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100439 /// @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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100444 /// Extract the number of dereferenceable_or_null bytes for a call or
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100445 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100451 /// Extract the number of dereferenceable_or_null bytes for a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100452 /// parameter.
453 /// @param ArgNo AttributeList ArgNo, referring to an argument.
454 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
455 return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
456 }
457
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100458 /// Determine if the function does not access memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100459 bool doesNotAccessMemory() const {
460 return hasFnAttribute(Attribute::ReadNone);
461 }
462 void setDoesNotAccessMemory() {
463 addFnAttr(Attribute::ReadNone);
464 }
465
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100466 /// Determine if the function does not access or only reads memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100467 bool onlyReadsMemory() const {
468 return doesNotAccessMemory() || hasFnAttribute(Attribute::ReadOnly);
469 }
470 void setOnlyReadsMemory() {
471 addFnAttr(Attribute::ReadOnly);
472 }
473
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100474 /// Determine if the function does not access or only writes memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100475 bool doesNotReadMemory() const {
476 return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly);
477 }
478 void setDoesNotReadMemory() {
479 addFnAttr(Attribute::WriteOnly);
480 }
481
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100482 /// Determine if the call can access memmory only using pointers based
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100483 /// on its arguments.
484 bool onlyAccessesArgMemory() const {
485 return hasFnAttribute(Attribute::ArgMemOnly);
486 }
487 void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly); }
488
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100489 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100490 /// inaccessible from the IR.
491 bool onlyAccessesInaccessibleMemory() const {
492 return hasFnAttribute(Attribute::InaccessibleMemOnly);
493 }
494 void setOnlyAccessesInaccessibleMemory() {
495 addFnAttr(Attribute::InaccessibleMemOnly);
496 }
497
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100498 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100499 /// either inaccessible from the IR or pointed to by its arguments.
500 bool onlyAccessesInaccessibleMemOrArgMem() const {
501 return hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly);
502 }
503 void setOnlyAccessesInaccessibleMemOrArgMem() {
504 addFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
505 }
506
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100507 /// Determine if the function cannot return.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100508 bool doesNotReturn() const {
509 return hasFnAttribute(Attribute::NoReturn);
510 }
511 void setDoesNotReturn() {
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100518 /// Determine if the function cannot unwind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100519 bool doesNotThrow() const {
520 return hasFnAttribute(Attribute::NoUnwind);
521 }
522 void setDoesNotThrow() {
523 addFnAttr(Attribute::NoUnwind);
524 }
525
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100526 /// Determine if the call cannot be duplicated.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100527 bool cannotDuplicate() const {
528 return hasFnAttribute(Attribute::NoDuplicate);
529 }
530 void setCannotDuplicate() {
531 addFnAttr(Attribute::NoDuplicate);
532 }
533
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100534 /// Determine if the call is convergent.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100535 bool isConvergent() const {
536 return hasFnAttribute(Attribute::Convergent);
537 }
538 void setConvergent() {
539 addFnAttr(Attribute::Convergent);
540 }
541 void setNotConvergent() {
542 removeFnAttr(Attribute::Convergent);
543 }
544
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100545 /// Determine if the call has sideeffects.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546 bool isSpeculatable() const {
547 return hasFnAttribute(Attribute::Speculatable);
548 }
549 void setSpeculatable() {
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 }
558 void setDoesNotRecurse() {
559 addFnAttr(Attribute::NoRecurse);
560 }
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// True if the ABI mandates (or the user requested) that this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100571 /// True if this function needs an unwind table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100572 bool needsUnwindTableEntry() const {
Andrew Scull0372a572018-11-16 15:47:06 +0000573 return hasUWTable() || !doesNotThrow() || hasPersonalityFn();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100574 }
575
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100576 /// Determine if the function returns a structure through first
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100577 /// or second pointer argument.
578 bool hasStructRetAttr() const {
579 return AttributeSets.hasParamAttribute(0, Attribute::StructRet) ||
580 AttributeSets.hasParamAttribute(1, Attribute::StructRet);
581 }
582
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100583 /// Determine if the parameter or return value is marked with NoAlias
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100584 /// attribute.
585 bool returnDoesNotAlias() const {
586 return AttributeSets.hasAttribute(AttributeList::ReturnIndex,
587 Attribute::NoAlias);
588 }
589 void setReturnDoesNotAlias() {
590 addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
591 }
592
593 /// Optimize this function for minimum size (-Oz).
594 bool optForMinSize() const { return hasFnAttribute(Attribute::MinSize); }
595
596 /// Optimize this function for size (-Os) or minimum size (-Oz).
597 bool optForSize() const {
598 return hasFnAttribute(Attribute::OptimizeForSize) || optForMinSize();
599 }
600
601 /// copyAttributesFrom - copy all additional attributes (those not needed to
602 /// create a Function) from the Function Src to this one.
603 void copyAttributesFrom(const Function *Src);
604
605 /// deleteBody - This method deletes the body of the function, and converts
606 /// the linkage to external.
607 ///
608 void deleteBody() {
609 dropAllReferences();
610 setLinkage(ExternalLinkage);
611 }
612
613 /// removeFromParent - This method unlinks 'this' from the containing module,
614 /// but does not delete it.
615 ///
616 void removeFromParent();
617
618 /// eraseFromParent - This method unlinks 'this' from the containing module
619 /// and deletes it.
620 ///
621 void eraseFromParent();
622
623 /// Steal arguments from another function.
624 ///
625 /// Drop this function's arguments and splice in the ones from \c Src.
626 /// Requires that this has no function body.
627 void stealArgumentListFrom(Function &Src);
628
629 /// Get the underlying elements of the Function... the basic block list is
630 /// empty for external functions.
631 ///
632 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
633 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
634
635 static BasicBlockListType Function::*getSublistAccess(BasicBlock*) {
636 return &Function::BasicBlocks;
637 }
638
639 const BasicBlock &getEntryBlock() const { return front(); }
640 BasicBlock &getEntryBlock() { return front(); }
641
642 //===--------------------------------------------------------------------===//
643 // Symbol Table Accessing functions...
644
645 /// getSymbolTable() - Return the symbol table if any, otherwise nullptr.
646 ///
647 inline ValueSymbolTable *getValueSymbolTable() { return SymTab.get(); }
648 inline const ValueSymbolTable *getValueSymbolTable() const {
649 return SymTab.get();
650 }
651
652 //===--------------------------------------------------------------------===//
653 // BasicBlock iterator forwarding functions
654 //
655 iterator begin() { return BasicBlocks.begin(); }
656 const_iterator begin() const { return BasicBlocks.begin(); }
657 iterator end () { return BasicBlocks.end(); }
658 const_iterator end () const { return BasicBlocks.end(); }
659
660 size_t size() const { return BasicBlocks.size(); }
661 bool empty() const { return BasicBlocks.empty(); }
662 const BasicBlock &front() const { return BasicBlocks.front(); }
663 BasicBlock &front() { return BasicBlocks.front(); }
664 const BasicBlock &back() const { return BasicBlocks.back(); }
665 BasicBlock &back() { return BasicBlocks.back(); }
666
667/// @name Function Argument Iteration
668/// @{
669
670 arg_iterator arg_begin() {
671 CheckLazyArguments();
672 return Arguments;
673 }
674 const_arg_iterator arg_begin() const {
675 CheckLazyArguments();
676 return Arguments;
677 }
678
679 arg_iterator arg_end() {
680 CheckLazyArguments();
681 return Arguments + NumArgs;
682 }
683 const_arg_iterator arg_end() const {
684 CheckLazyArguments();
685 return Arguments + NumArgs;
686 }
687
688 iterator_range<arg_iterator> args() {
689 return make_range(arg_begin(), arg_end());
690 }
691 iterator_range<const_arg_iterator> args() const {
692 return make_range(arg_begin(), arg_end());
693 }
694
695/// @}
696
697 size_t arg_size() const { return NumArgs; }
698 bool arg_empty() const { return arg_size() == 0; }
699
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100700 /// Check whether this function has a personality function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100701 bool hasPersonalityFn() const {
702 return getSubclassDataFromValue() & (1<<3);
703 }
704
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100705 /// Get the personality function associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100706 Constant *getPersonalityFn() const;
707 void setPersonalityFn(Constant *Fn);
708
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100709 /// Check whether this function has prefix data.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100710 bool hasPrefixData() const {
711 return getSubclassDataFromValue() & (1<<1);
712 }
713
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100714 /// Get the prefix data associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100715 Constant *getPrefixData() const;
716 void setPrefixData(Constant *PrefixData);
717
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100718 /// Check whether this function has prologue data.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100719 bool hasPrologueData() const {
720 return getSubclassDataFromValue() & (1<<2);
721 }
722
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100723 /// Get the prologue data associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100724 Constant *getPrologueData() const;
725 void setPrologueData(Constant *PrologueData);
726
727 /// Print the function to an output stream with an optional
728 /// AssemblyAnnotationWriter.
729 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
730 bool ShouldPreserveUseListOrder = false,
731 bool IsForDebug = false) const;
732
733 /// viewCFG - This function is meant for use from the debugger. You can just
734 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
735 /// program, displaying the CFG of the current function with the code for each
736 /// basic block inside. This depends on there being a 'dot' and 'gv' program
737 /// in your path.
738 ///
739 void viewCFG() const;
740
741 /// viewCFGOnly - This function is meant for use from the debugger. It works
742 /// just like viewCFG, but it does not include the contents of basic blocks
743 /// into the nodes, just the label. If you are only interested in the CFG
744 /// this can make the graph smaller.
745 ///
746 void viewCFGOnly() const;
747
748 /// Methods for support type inquiry through isa, cast, and dyn_cast:
749 static bool classof(const Value *V) {
750 return V->getValueID() == Value::FunctionVal;
751 }
752
753 /// dropAllReferences() - This method causes all the subinstructions to "let
754 /// go" of all references that they are maintaining. This allows one to
755 /// 'delete' a whole module at a time, even though there may be circular
756 /// references... first all references are dropped, and all use counts go to
757 /// zero. Then everything is deleted for real. Note that no operations are
758 /// valid on an object that has "dropped all references", except operator
759 /// delete.
760 ///
761 /// Since no other object in the module can have references into the body of a
762 /// function, dropping all references deletes the entire body of the function,
763 /// including any contained basic blocks.
764 ///
765 void dropAllReferences();
766
767 /// hasAddressTaken - returns true if there are any uses of this function
768 /// other than direct calls or invokes to it, or blockaddress expressions.
769 /// Optionally passes back an offending user for diagnostic purposes.
770 ///
771 bool hasAddressTaken(const User** = nullptr) const;
772
773 /// isDefTriviallyDead - Return true if it is trivially safe to remove
774 /// this function definition from the module (because it isn't externally
775 /// visible, does not have its address taken, and has no callers). To make
776 /// this more accurate, call removeDeadConstantUsers first.
777 bool isDefTriviallyDead() const;
778
779 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
780 /// setjmp or other function that gcc recognizes as "returning twice".
781 bool callsFunctionThatReturnsTwice() const;
782
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100783 /// Set the attached subprogram.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100784 ///
785 /// Calls \a setMetadata() with \a LLVMContext::MD_dbg.
786 void setSubprogram(DISubprogram *SP);
787
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100788 /// Get the attached subprogram.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100789 ///
790 /// Calls \a getMetadata() with \a LLVMContext::MD_dbg and casts the result
791 /// to \a DISubprogram.
792 DISubprogram *getSubprogram() const;
793
794 /// Returns true if we should emit debug info for profiling.
795 bool isDebugInfoForProfiling() const;
796
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100797 /// Check if null pointer dereferencing is considered undefined behavior for
798 /// the function.
799 /// Return value: false => null pointer dereference is undefined.
800 /// Return value: true => null pointer dereference is not undefined.
801 bool nullPointerIsDefined() const;
802
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100803private:
804 void allocHungoffUselist();
805 template<int Idx> void setHungoffOperand(Constant *C);
806
807 /// Shadow Value::setValueSubclassData with a private forwarding method so
808 /// that subclasses cannot accidentally use it.
809 void setValueSubclassData(unsigned short D) {
810 Value::setValueSubclassData(D);
811 }
812 void setValueSubclassDataBit(unsigned Bit, bool On);
813};
814
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100815/// Check whether null pointer dereferencing is considered undefined behavior
816/// for a given function or an address space.
817/// Null pointer access in non-zero address space is not considered undefined.
818/// Return value: false => null pointer dereference is undefined.
819/// Return value: true => null pointer dereference is not undefined.
820bool NullPointerIsDefined(const Function *F, unsigned AS = 0);
821
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100822template <>
823struct OperandTraits<Function> : public HungoffOperandTraits<3> {};
824
825DEFINE_TRANSPARENT_OPERAND_ACCESSORS(Function, Value)
826
827} // end namespace llvm
828
829#endif // LLVM_IR_FUNCTION_H