blob: c8d6b0776fbf43289fed39af2d1c0942b4d29aa2 [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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100144 /// Returns the number of non-debug IR instructions in this function.
145 /// This is equivalent to the sum of the sizes of each basic block contained
146 /// within this function.
147 unsigned getInstructionCount();
148
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100149 /// Returns the FunctionType for me.
150 FunctionType *getFunctionType() const {
151 return cast<FunctionType>(getValueType());
152 }
153
154 /// Returns the type of the ret val.
155 Type *getReturnType() const { return getFunctionType()->getReturnType(); }
156
157 /// getContext - Return a reference to the LLVMContext associated with this
158 /// function.
159 LLVMContext &getContext() const;
160
161 /// isVarArg - Return true if this function takes a variable number of
162 /// arguments.
163 bool isVarArg() const { return getFunctionType()->isVarArg(); }
164
165 bool isMaterializable() const {
166 return getGlobalObjectSubClassData() & (1 << IsMaterializableBit);
167 }
168 void setIsMaterializable(bool V) {
169 unsigned Mask = 1 << IsMaterializableBit;
170 setGlobalObjectSubClassData((~Mask & getGlobalObjectSubClassData()) |
171 (V ? Mask : 0u));
172 }
173
174 /// getIntrinsicID - This method returns the ID number of the specified
175 /// function, or Intrinsic::not_intrinsic if the function is not an
176 /// intrinsic, or if the pointer is null. This value is always defined to be
177 /// zero to allow easy checking for whether a function is intrinsic or not.
178 /// The particular intrinsic functions which correspond to this value are
179 /// defined in llvm/Intrinsics.h.
180 Intrinsic::ID getIntrinsicID() const LLVM_READONLY { return IntID; }
181
182 /// isIntrinsic - Returns true if the function's name starts with "llvm.".
183 /// It's possible for this function to return true while getIntrinsicID()
184 /// returns Intrinsic::not_intrinsic!
185 bool isIntrinsic() const { return HasLLVMReservedName; }
186
187 static Intrinsic::ID lookupIntrinsicID(StringRef Name);
188
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100189 /// Recalculate the ID for this function if it is an Intrinsic defined
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100190 /// in llvm/Intrinsics.h. Sets the intrinsic ID to Intrinsic::not_intrinsic
191 /// if the name of this function does not match an intrinsic in that header.
192 /// Note, this method does not need to be called directly, as it is called
193 /// from Value::setName() whenever the name of this function changes.
194 void recalculateIntrinsicID();
195
196 /// getCallingConv()/setCallingConv(CC) - These method get and set the
197 /// calling convention of this function. The enum values for the known
198 /// calling conventions are defined in CallingConv.h.
199 CallingConv::ID getCallingConv() const {
200 return static_cast<CallingConv::ID>((getSubclassDataFromValue() >> 4) &
201 CallingConv::MaxID);
202 }
203 void setCallingConv(CallingConv::ID CC) {
204 auto ID = static_cast<unsigned>(CC);
205 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
206 setValueSubclassData((getSubclassDataFromValue() & 0xc00f) | (ID << 4));
207 }
208
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100209 /// Return the attribute list for this Function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100210 AttributeList getAttributes() const { return AttributeSets; }
211
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100212 /// Set the attribute list for this Function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213 void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
214
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100215 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216 void addFnAttr(Attribute::AttrKind Kind) {
217 addAttribute(AttributeList::FunctionIndex, Kind);
218 }
219
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100220 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100221 void addFnAttr(StringRef Kind, StringRef Val = StringRef()) {
222 addAttribute(AttributeList::FunctionIndex,
223 Attribute::get(getContext(), Kind, Val));
224 }
225
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100226 /// Add function attributes to this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 void addFnAttr(Attribute Attr) {
228 addAttribute(AttributeList::FunctionIndex, Attr);
229 }
230
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100231 /// Remove function attributes from this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100232 void removeFnAttr(Attribute::AttrKind Kind) {
233 removeAttribute(AttributeList::FunctionIndex, Kind);
234 }
235
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100236 /// Remove function attribute from this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100237 void removeFnAttr(StringRef Kind) {
238 setAttributes(getAttributes().removeAttribute(
239 getContext(), AttributeList::FunctionIndex, Kind));
240 }
241
242 enum ProfileCountType { PCT_Invalid, PCT_Real, PCT_Synthetic };
243
244 /// Class to represent profile counts.
245 ///
246 /// This class represents both real and synthetic profile counts.
247 class ProfileCount {
248 private:
249 uint64_t Count;
250 ProfileCountType PCT;
251 static ProfileCount Invalid;
252
253 public:
254 ProfileCount() : Count(-1), PCT(PCT_Invalid) {}
255 ProfileCount(uint64_t Count, ProfileCountType PCT)
256 : Count(Count), PCT(PCT) {}
257 bool hasValue() const { return PCT != PCT_Invalid; }
258 uint64_t getCount() const { return Count; }
259 ProfileCountType getType() const { return PCT; }
260 bool isSynthetic() const { return PCT == PCT_Synthetic; }
261 explicit operator bool() { return hasValue(); }
262 bool operator!() const { return !hasValue(); }
263 // Update the count retaining the same profile count type.
264 ProfileCount &setCount(uint64_t C) {
265 Count = C;
266 return *this;
267 }
268 static ProfileCount getInvalid() { return ProfileCount(-1, PCT_Invalid); }
269 };
270
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100271 /// Set the entry count for this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100272 ///
273 /// Entry count is the number of times this function was executed based on
274 /// pgo data. \p Imports points to a set of GUIDs that needs to
275 /// be imported by the function for sample PGO, to enable the same inlines as
276 /// the profiled optimized binary.
277 void setEntryCount(ProfileCount Count,
278 const DenseSet<GlobalValue::GUID> *Imports = nullptr);
279
280 /// A convenience wrapper for setting entry count
281 void setEntryCount(uint64_t Count, ProfileCountType Type = PCT_Real,
282 const DenseSet<GlobalValue::GUID> *Imports = nullptr);
283
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100284 /// Get the entry count for this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285 ///
286 /// Entry count is the number of times the function was executed based on
287 /// pgo data.
288 ProfileCount getEntryCount() const;
289
290 /// Return true if the function is annotated with profile data.
291 ///
292 /// Presence of entry counts from a profile run implies the function has
293 /// profile annotations.
294 bool hasProfileData() const { return getEntryCount().hasValue(); }
295
296 /// Returns the set of GUIDs that needs to be imported to the function for
297 /// sample PGO, to enable the same inlines as the profiled optimized binary.
298 DenseSet<GlobalValue::GUID> getImportGUIDs() const;
299
300 /// Set the section prefix for this function.
301 void setSectionPrefix(StringRef Prefix);
302
303 /// Get the section prefix for this function.
304 Optional<StringRef> getSectionPrefix() const;
305
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100306 /// Return true if the function has the attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100307 bool hasFnAttribute(Attribute::AttrKind Kind) const {
308 return AttributeSets.hasFnAttribute(Kind);
309 }
310
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100311 /// Return true if the function has the attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100312 bool hasFnAttribute(StringRef Kind) const {
313 return AttributeSets.hasFnAttribute(Kind);
314 }
315
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100316 /// Return the attribute for the given attribute kind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100317 Attribute getFnAttribute(Attribute::AttrKind Kind) const {
318 return getAttribute(AttributeList::FunctionIndex, Kind);
319 }
320
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100321 /// Return the attribute for the given attribute kind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100322 Attribute getFnAttribute(StringRef Kind) const {
323 return getAttribute(AttributeList::FunctionIndex, Kind);
324 }
325
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100326 /// Return the stack alignment for the function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100327 unsigned getFnStackAlignment() const {
328 if (!hasFnAttribute(Attribute::StackAlignment))
329 return 0;
330 return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
331 }
332
333 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
334 /// to use during code generation.
335 bool hasGC() const {
336 return getSubclassDataFromValue() & (1<<14);
337 }
338 const std::string &getGC() const;
339 void setGC(std::string Str);
340 void clearGC();
341
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100342 /// adds the attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100343 void addAttribute(unsigned i, Attribute::AttrKind Kind);
344
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100345 /// adds the attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100346 void addAttribute(unsigned i, Attribute Attr);
347
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100348 /// adds the attributes to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100349 void addAttributes(unsigned i, const AttrBuilder &Attrs);
350
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100351 /// adds the attribute to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100352 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
353
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100354 /// adds the attribute to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100355 void addParamAttr(unsigned ArgNo, Attribute Attr);
356
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100357 /// adds the attributes to the list of attributes for the given arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100358 void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
359
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100360 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100361 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
362
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100363 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100364 void removeAttribute(unsigned i, StringRef Kind);
365
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100366 /// removes the attributes from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100367 void removeAttributes(unsigned i, const AttrBuilder &Attrs);
368
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100369 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100370 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
371
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100372 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100373 void removeParamAttr(unsigned ArgNo, StringRef Kind);
374
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100375 /// removes the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100376 void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
377
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100378 /// check if an attributes is in the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100379 bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {
380 return getAttributes().hasAttribute(i, Kind);
381 }
382
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100383 /// check if an attributes is in the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100384 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
385 return getAttributes().hasParamAttribute(ArgNo, Kind);
386 }
387
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100388 /// gets the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
390 return AttributeSets.getAttribute(i, Kind);
391 }
392
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100393 /// gets the attribute from the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100394 Attribute getAttribute(unsigned i, StringRef Kind) const {
395 return AttributeSets.getAttribute(i, Kind);
396 }
397
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100398 /// adds the dereferenceable attribute to the list of attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100399 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
400
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100401 /// adds the dereferenceable attribute to the list of attributes for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100402 /// the given arg.
403 void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
404
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100405 /// adds the dereferenceable_or_null attribute to the list of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100406 /// attributes.
407 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
408
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100409 /// adds the dereferenceable_or_null attribute to the list of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100410 /// attributes for the given arg.
411 void addDereferenceableOrNullParamAttr(unsigned ArgNo, uint64_t Bytes);
412
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100413 /// Extract the alignment for a call or parameter (0=unknown).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100414 unsigned getParamAlignment(unsigned ArgNo) const {
415 return AttributeSets.getParamAlignment(ArgNo);
416 }
417
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100418 /// Extract the number of dereferenceable bytes for a call or
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100419 /// parameter (0=unknown).
420 /// @param i AttributeList index, referring to a return value or argument.
421 uint64_t getDereferenceableBytes(unsigned i) const {
422 return AttributeSets.getDereferenceableBytes(i);
423 }
424
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100425 /// Extract the number of dereferenceable bytes for a parameter.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100426 /// @param ArgNo Index of an argument, with 0 being the first function arg.
427 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
428 return AttributeSets.getParamDereferenceableBytes(ArgNo);
429 }
430
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100431 /// Extract the number of dereferenceable_or_null 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 getDereferenceableOrNullBytes(unsigned i) const {
435 return AttributeSets.getDereferenceableOrNullBytes(i);
436 }
437
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100438 /// Extract the number of dereferenceable_or_null bytes for a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100439 /// parameter.
440 /// @param ArgNo AttributeList ArgNo, referring to an argument.
441 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
442 return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
443 }
444
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100445 /// Determine if the function does not access memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100446 bool doesNotAccessMemory() const {
447 return hasFnAttribute(Attribute::ReadNone);
448 }
449 void setDoesNotAccessMemory() {
450 addFnAttr(Attribute::ReadNone);
451 }
452
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100453 /// Determine if the function does not access or only reads memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100454 bool onlyReadsMemory() const {
455 return doesNotAccessMemory() || hasFnAttribute(Attribute::ReadOnly);
456 }
457 void setOnlyReadsMemory() {
458 addFnAttr(Attribute::ReadOnly);
459 }
460
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100461 /// Determine if the function does not access or only writes memory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100462 bool doesNotReadMemory() const {
463 return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly);
464 }
465 void setDoesNotReadMemory() {
466 addFnAttr(Attribute::WriteOnly);
467 }
468
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100469 /// Determine if the call can access memmory only using pointers based
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100470 /// on its arguments.
471 bool onlyAccessesArgMemory() const {
472 return hasFnAttribute(Attribute::ArgMemOnly);
473 }
474 void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly); }
475
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100476 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100477 /// inaccessible from the IR.
478 bool onlyAccessesInaccessibleMemory() const {
479 return hasFnAttribute(Attribute::InaccessibleMemOnly);
480 }
481 void setOnlyAccessesInaccessibleMemory() {
482 addFnAttr(Attribute::InaccessibleMemOnly);
483 }
484
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100485 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100486 /// either inaccessible from the IR or pointed to by its arguments.
487 bool onlyAccessesInaccessibleMemOrArgMem() const {
488 return hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly);
489 }
490 void setOnlyAccessesInaccessibleMemOrArgMem() {
491 addFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
492 }
493
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100494 /// Determine if the function cannot return.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100495 bool doesNotReturn() const {
496 return hasFnAttribute(Attribute::NoReturn);
497 }
498 void setDoesNotReturn() {
499 addFnAttr(Attribute::NoReturn);
500 }
501
502 /// Determine if the function should not perform indirect branch tracking.
503 bool doesNoCfCheck() const { return hasFnAttribute(Attribute::NoCfCheck); }
504
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100505 /// Determine if the function cannot unwind.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100506 bool doesNotThrow() const {
507 return hasFnAttribute(Attribute::NoUnwind);
508 }
509 void setDoesNotThrow() {
510 addFnAttr(Attribute::NoUnwind);
511 }
512
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100513 /// Determine if the call cannot be duplicated.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100514 bool cannotDuplicate() const {
515 return hasFnAttribute(Attribute::NoDuplicate);
516 }
517 void setCannotDuplicate() {
518 addFnAttr(Attribute::NoDuplicate);
519 }
520
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100521 /// Determine if the call is convergent.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100522 bool isConvergent() const {
523 return hasFnAttribute(Attribute::Convergent);
524 }
525 void setConvergent() {
526 addFnAttr(Attribute::Convergent);
527 }
528 void setNotConvergent() {
529 removeFnAttr(Attribute::Convergent);
530 }
531
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100532 /// Determine if the call has sideeffects.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100533 bool isSpeculatable() const {
534 return hasFnAttribute(Attribute::Speculatable);
535 }
536 void setSpeculatable() {
537 addFnAttr(Attribute::Speculatable);
538 }
539
540 /// Determine if the function is known not to recurse, directly or
541 /// indirectly.
542 bool doesNotRecurse() const {
543 return hasFnAttribute(Attribute::NoRecurse);
544 }
545 void setDoesNotRecurse() {
546 addFnAttr(Attribute::NoRecurse);
547 }
548
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100549 /// True if the ABI mandates (or the user requested) that this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100550 /// function be in a unwind table.
551 bool hasUWTable() const {
552 return hasFnAttribute(Attribute::UWTable);
553 }
554 void setHasUWTable() {
555 addFnAttr(Attribute::UWTable);
556 }
557
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100558 /// True if this function needs an unwind table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100559 bool needsUnwindTableEntry() const {
560 return hasUWTable() || !doesNotThrow();
561 }
562
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100563 /// Determine if the function returns a structure through first
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100564 /// or second pointer argument.
565 bool hasStructRetAttr() const {
566 return AttributeSets.hasParamAttribute(0, Attribute::StructRet) ||
567 AttributeSets.hasParamAttribute(1, Attribute::StructRet);
568 }
569
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100570 /// Determine if the parameter or return value is marked with NoAlias
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100571 /// attribute.
572 bool returnDoesNotAlias() const {
573 return AttributeSets.hasAttribute(AttributeList::ReturnIndex,
574 Attribute::NoAlias);
575 }
576 void setReturnDoesNotAlias() {
577 addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
578 }
579
580 /// Optimize this function for minimum size (-Oz).
581 bool optForMinSize() const { return hasFnAttribute(Attribute::MinSize); }
582
583 /// Optimize this function for size (-Os) or minimum size (-Oz).
584 bool optForSize() const {
585 return hasFnAttribute(Attribute::OptimizeForSize) || optForMinSize();
586 }
587
588 /// copyAttributesFrom - copy all additional attributes (those not needed to
589 /// create a Function) from the Function Src to this one.
590 void copyAttributesFrom(const Function *Src);
591
592 /// deleteBody - This method deletes the body of the function, and converts
593 /// the linkage to external.
594 ///
595 void deleteBody() {
596 dropAllReferences();
597 setLinkage(ExternalLinkage);
598 }
599
600 /// removeFromParent - This method unlinks 'this' from the containing module,
601 /// but does not delete it.
602 ///
603 void removeFromParent();
604
605 /// eraseFromParent - This method unlinks 'this' from the containing module
606 /// and deletes it.
607 ///
608 void eraseFromParent();
609
610 /// Steal arguments from another function.
611 ///
612 /// Drop this function's arguments and splice in the ones from \c Src.
613 /// Requires that this has no function body.
614 void stealArgumentListFrom(Function &Src);
615
616 /// Get the underlying elements of the Function... the basic block list is
617 /// empty for external functions.
618 ///
619 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
620 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
621
622 static BasicBlockListType Function::*getSublistAccess(BasicBlock*) {
623 return &Function::BasicBlocks;
624 }
625
626 const BasicBlock &getEntryBlock() const { return front(); }
627 BasicBlock &getEntryBlock() { return front(); }
628
629 //===--------------------------------------------------------------------===//
630 // Symbol Table Accessing functions...
631
632 /// getSymbolTable() - Return the symbol table if any, otherwise nullptr.
633 ///
634 inline ValueSymbolTable *getValueSymbolTable() { return SymTab.get(); }
635 inline const ValueSymbolTable *getValueSymbolTable() const {
636 return SymTab.get();
637 }
638
639 //===--------------------------------------------------------------------===//
640 // BasicBlock iterator forwarding functions
641 //
642 iterator begin() { return BasicBlocks.begin(); }
643 const_iterator begin() const { return BasicBlocks.begin(); }
644 iterator end () { return BasicBlocks.end(); }
645 const_iterator end () const { return BasicBlocks.end(); }
646
647 size_t size() const { return BasicBlocks.size(); }
648 bool empty() const { return BasicBlocks.empty(); }
649 const BasicBlock &front() const { return BasicBlocks.front(); }
650 BasicBlock &front() { return BasicBlocks.front(); }
651 const BasicBlock &back() const { return BasicBlocks.back(); }
652 BasicBlock &back() { return BasicBlocks.back(); }
653
654/// @name Function Argument Iteration
655/// @{
656
657 arg_iterator arg_begin() {
658 CheckLazyArguments();
659 return Arguments;
660 }
661 const_arg_iterator arg_begin() const {
662 CheckLazyArguments();
663 return Arguments;
664 }
665
666 arg_iterator arg_end() {
667 CheckLazyArguments();
668 return Arguments + NumArgs;
669 }
670 const_arg_iterator arg_end() const {
671 CheckLazyArguments();
672 return Arguments + NumArgs;
673 }
674
675 iterator_range<arg_iterator> args() {
676 return make_range(arg_begin(), arg_end());
677 }
678 iterator_range<const_arg_iterator> args() const {
679 return make_range(arg_begin(), arg_end());
680 }
681
682/// @}
683
684 size_t arg_size() const { return NumArgs; }
685 bool arg_empty() const { return arg_size() == 0; }
686
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100687 /// Check whether this function has a personality function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100688 bool hasPersonalityFn() const {
689 return getSubclassDataFromValue() & (1<<3);
690 }
691
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100692 /// Get the personality function associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100693 Constant *getPersonalityFn() const;
694 void setPersonalityFn(Constant *Fn);
695
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100696 /// Check whether this function has prefix data.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100697 bool hasPrefixData() const {
698 return getSubclassDataFromValue() & (1<<1);
699 }
700
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100701 /// Get the prefix data associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100702 Constant *getPrefixData() const;
703 void setPrefixData(Constant *PrefixData);
704
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100705 /// Check whether this function has prologue data.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100706 bool hasPrologueData() const {
707 return getSubclassDataFromValue() & (1<<2);
708 }
709
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100710 /// Get the prologue data associated with this function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100711 Constant *getPrologueData() const;
712 void setPrologueData(Constant *PrologueData);
713
714 /// Print the function to an output stream with an optional
715 /// AssemblyAnnotationWriter.
716 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
717 bool ShouldPreserveUseListOrder = false,
718 bool IsForDebug = false) const;
719
720 /// viewCFG - This function is meant for use from the debugger. You can just
721 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
722 /// program, displaying the CFG of the current function with the code for each
723 /// basic block inside. This depends on there being a 'dot' and 'gv' program
724 /// in your path.
725 ///
726 void viewCFG() const;
727
728 /// viewCFGOnly - This function is meant for use from the debugger. It works
729 /// just like viewCFG, but it does not include the contents of basic blocks
730 /// into the nodes, just the label. If you are only interested in the CFG
731 /// this can make the graph smaller.
732 ///
733 void viewCFGOnly() const;
734
735 /// Methods for support type inquiry through isa, cast, and dyn_cast:
736 static bool classof(const Value *V) {
737 return V->getValueID() == Value::FunctionVal;
738 }
739
740 /// dropAllReferences() - This method causes all the subinstructions to "let
741 /// go" of all references that they are maintaining. This allows one to
742 /// 'delete' a whole module at a time, even though there may be circular
743 /// references... first all references are dropped, and all use counts go to
744 /// zero. Then everything is deleted for real. Note that no operations are
745 /// valid on an object that has "dropped all references", except operator
746 /// delete.
747 ///
748 /// Since no other object in the module can have references into the body of a
749 /// function, dropping all references deletes the entire body of the function,
750 /// including any contained basic blocks.
751 ///
752 void dropAllReferences();
753
754 /// hasAddressTaken - returns true if there are any uses of this function
755 /// other than direct calls or invokes to it, or blockaddress expressions.
756 /// Optionally passes back an offending user for diagnostic purposes.
757 ///
758 bool hasAddressTaken(const User** = nullptr) const;
759
760 /// isDefTriviallyDead - Return true if it is trivially safe to remove
761 /// this function definition from the module (because it isn't externally
762 /// visible, does not have its address taken, and has no callers). To make
763 /// this more accurate, call removeDeadConstantUsers first.
764 bool isDefTriviallyDead() const;
765
766 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
767 /// setjmp or other function that gcc recognizes as "returning twice".
768 bool callsFunctionThatReturnsTwice() const;
769
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100770 /// Set the attached subprogram.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100771 ///
772 /// Calls \a setMetadata() with \a LLVMContext::MD_dbg.
773 void setSubprogram(DISubprogram *SP);
774
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100775 /// Get the attached subprogram.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100776 ///
777 /// Calls \a getMetadata() with \a LLVMContext::MD_dbg and casts the result
778 /// to \a DISubprogram.
779 DISubprogram *getSubprogram() const;
780
781 /// Returns true if we should emit debug info for profiling.
782 bool isDebugInfoForProfiling() const;
783
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100784 /// Check if null pointer dereferencing is considered undefined behavior for
785 /// the function.
786 /// Return value: false => null pointer dereference is undefined.
787 /// Return value: true => null pointer dereference is not undefined.
788 bool nullPointerIsDefined() const;
789
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100790private:
791 void allocHungoffUselist();
792 template<int Idx> void setHungoffOperand(Constant *C);
793
794 /// Shadow Value::setValueSubclassData with a private forwarding method so
795 /// that subclasses cannot accidentally use it.
796 void setValueSubclassData(unsigned short D) {
797 Value::setValueSubclassData(D);
798 }
799 void setValueSubclassDataBit(unsigned Bit, bool On);
800};
801
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100802/// Check whether null pointer dereferencing is considered undefined behavior
803/// for a given function or an address space.
804/// Null pointer access in non-zero address space is not considered undefined.
805/// Return value: false => null pointer dereference is undefined.
806/// Return value: true => null pointer dereference is not undefined.
807bool NullPointerIsDefined(const Function *F, unsigned AS = 0);
808
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100809template <>
810struct OperandTraits<Function> : public HungoffOperandTraits<3> {};
811
812DEFINE_TRANSPARENT_OPERAND_ACCESSORS(Function, Value)
813
814} // end namespace llvm
815
816#endif // LLVM_IR_FUNCTION_H