]> git.proxmox.com Git - rustc.git/blob - src/llvm/include/llvm/IR/Function.h
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / include / llvm / IR / Function.h
1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Function class, which represents a
11 // single function/procedure in LLVM.
12 //
13 // A function basically consists of a list of basic blocks, a list of arguments,
14 // and a symbol table.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_IR_FUNCTION_H
19 #define LLVM_IR_FUNCTION_H
20
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/Argument.h"
23 #include "llvm/IR/Attributes.h"
24 #include "llvm/IR/BasicBlock.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/Support/Compiler.h"
28
29 namespace llvm {
30
31 class FunctionType;
32 class LLVMContext;
33
34 // Traits for intrusive list of basic blocks...
35 template<> struct ilist_traits<BasicBlock>
36 : public SymbolTableListTraits<BasicBlock, Function> {
37
38 // createSentinel is used to get hold of the node that marks the end of the
39 // list... (same trick used here as in ilist_traits<Instruction>)
40 BasicBlock *createSentinel() const {
41 return static_cast<BasicBlock*>(&Sentinel);
42 }
43 static void destroySentinel(BasicBlock*) {}
44
45 BasicBlock *provideInitialHead() const { return createSentinel(); }
46 BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); }
47 static void noteHead(BasicBlock*, BasicBlock*) {}
48
49 static ValueSymbolTable *getSymTab(Function *ItemParent);
50 private:
51 mutable ilist_half_node<BasicBlock> Sentinel;
52 };
53
54 template<> struct ilist_traits<Argument>
55 : public SymbolTableListTraits<Argument, Function> {
56
57 Argument *createSentinel() const {
58 return static_cast<Argument*>(&Sentinel);
59 }
60 static void destroySentinel(Argument*) {}
61
62 Argument *provideInitialHead() const { return createSentinel(); }
63 Argument *ensureHead(Argument*) const { return createSentinel(); }
64 static void noteHead(Argument*, Argument*) {}
65
66 static ValueSymbolTable *getSymTab(Function *ItemParent);
67 private:
68 mutable ilist_half_node<Argument> Sentinel;
69 };
70
71 class Function : public GlobalObject, public ilist_node<Function> {
72 public:
73 typedef iplist<Argument> ArgumentListType;
74 typedef iplist<BasicBlock> BasicBlockListType;
75
76 // BasicBlock iterators...
77 typedef BasicBlockListType::iterator iterator;
78 typedef BasicBlockListType::const_iterator const_iterator;
79
80 typedef ArgumentListType::iterator arg_iterator;
81 typedef ArgumentListType::const_iterator const_arg_iterator;
82
83 private:
84 // Important things that make up a function!
85 BasicBlockListType BasicBlocks; ///< The basic blocks
86 mutable ArgumentListType ArgumentList; ///< The formal arguments
87 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
88 AttributeSet AttributeSets; ///< Parameter attributes
89
90 /*
91 * Value::SubclassData
92 *
93 * bit 0 : HasLazyArguments
94 * bit 1 : HasPrefixData
95 * bit 2 : HasPrologueData
96 * bit 3-6: CallingConvention
97 */
98
99 friend class SymbolTableListTraits<Function, Module>;
100
101 void setParent(Module *parent);
102
103 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
104 /// built on demand, so that the list isn't allocated until the first client
105 /// needs it. The hasLazyArguments predicate returns true if the arg list
106 /// hasn't been set up yet.
107 bool hasLazyArguments() const {
108 return getSubclassDataFromValue() & (1<<0);
109 }
110 void CheckLazyArguments() const {
111 if (hasLazyArguments())
112 BuildLazyArguments();
113 }
114 void BuildLazyArguments() const;
115
116 Function(const Function&) LLVM_DELETED_FUNCTION;
117 void operator=(const Function&) LLVM_DELETED_FUNCTION;
118
119 /// Do the actual lookup of an intrinsic ID when the query could not be
120 /// answered from the cache.
121 unsigned lookupIntrinsicID() const LLVM_READONLY;
122
123 /// Function ctor - If the (optional) Module argument is specified, the
124 /// function is automatically inserted into the end of the function list for
125 /// the module.
126 ///
127 Function(FunctionType *Ty, LinkageTypes Linkage,
128 const Twine &N = "", Module *M = nullptr);
129
130 public:
131 static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
132 const Twine &N = "", Module *M = nullptr) {
133 return new(0) Function(Ty, Linkage, N, M);
134 }
135
136 ~Function();
137
138 Type *getReturnType() const; // Return the type of the ret val
139 FunctionType *getFunctionType() const; // Return the FunctionType for me
140
141 /// getContext - Return a pointer to the LLVMContext associated with this
142 /// function, or NULL if this function is not bound to a context yet.
143 LLVMContext &getContext() const;
144
145 /// isVarArg - Return true if this function takes a variable number of
146 /// arguments.
147 bool isVarArg() const;
148
149 bool isMaterializable() const;
150 void setIsMaterializable(bool V);
151
152 /// getIntrinsicID - This method returns the ID number of the specified
153 /// function, or Intrinsic::not_intrinsic if the function is not an
154 /// intrinsic, or if the pointer is null. This value is always defined to be
155 /// zero to allow easy checking for whether a function is intrinsic or not.
156 /// The particular intrinsic functions which correspond to this value are
157 /// defined in llvm/Intrinsics.h. Results are cached in the LLVM context,
158 /// subsequent requests for the same ID return results much faster from the
159 /// cache.
160 ///
161 unsigned getIntrinsicID() const LLVM_READONLY;
162 bool isIntrinsic() const { return getName().startswith("llvm."); }
163
164 /// getCallingConv()/setCallingConv(CC) - These method get and set the
165 /// calling convention of this function. The enum values for the known
166 /// calling conventions are defined in CallingConv.h.
167 CallingConv::ID getCallingConv() const {
168 return static_cast<CallingConv::ID>(getSubclassDataFromValue() >> 3);
169 }
170 void setCallingConv(CallingConv::ID CC) {
171 setValueSubclassData((getSubclassDataFromValue() & 7) |
172 (static_cast<unsigned>(CC) << 3));
173 }
174
175 /// @brief Return the attribute list for this Function.
176 AttributeSet getAttributes() const { return AttributeSets; }
177
178 /// @brief Set the attribute list for this Function.
179 void setAttributes(AttributeSet attrs) { AttributeSets = attrs; }
180
181 /// @brief Add function attributes to this function.
182 void addFnAttr(Attribute::AttrKind N) {
183 setAttributes(AttributeSets.addAttribute(getContext(),
184 AttributeSet::FunctionIndex, N));
185 }
186
187 /// @brief Remove function attributes from this function.
188 void removeFnAttr(Attribute::AttrKind N) {
189 setAttributes(AttributeSets.removeAttribute(
190 getContext(), AttributeSet::FunctionIndex, N));
191 }
192
193 /// @brief Add function attributes to this function.
194 void addFnAttr(StringRef Kind) {
195 setAttributes(
196 AttributeSets.addAttribute(getContext(),
197 AttributeSet::FunctionIndex, Kind));
198 }
199 void addFnAttr(StringRef Kind, StringRef Value) {
200 setAttributes(
201 AttributeSets.addAttribute(getContext(),
202 AttributeSet::FunctionIndex, Kind, Value));
203 }
204
205 /// @brief Return true if the function has the attribute.
206 bool hasFnAttribute(Attribute::AttrKind Kind) const {
207 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind);
208 }
209 bool hasFnAttribute(StringRef Kind) const {
210 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind);
211 }
212
213 /// @brief Return the attribute for the given attribute kind.
214 Attribute getFnAttribute(Attribute::AttrKind Kind) const {
215 return AttributeSets.getAttribute(AttributeSet::FunctionIndex, Kind);
216 }
217 Attribute getFnAttribute(StringRef Kind) const {
218 return AttributeSets.getAttribute(AttributeSet::FunctionIndex, Kind);
219 }
220
221 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
222 /// to use during code generation.
223 bool hasGC() const;
224 const char *getGC() const;
225 void setGC(const char *Str);
226 void clearGC();
227
228 /// @brief adds the attribute to the list of attributes.
229 void addAttribute(unsigned i, Attribute::AttrKind attr);
230
231 /// @brief adds the attributes to the list of attributes.
232 void addAttributes(unsigned i, AttributeSet attrs);
233
234 /// @brief removes the attributes from the list of attributes.
235 void removeAttributes(unsigned i, AttributeSet attr);
236
237 /// @brief Extract the alignment for a call or parameter (0=unknown).
238 unsigned getParamAlignment(unsigned i) const {
239 return AttributeSets.getParamAlignment(i);
240 }
241
242 /// @brief Extract the number of dereferenceable bytes for a call or
243 /// parameter (0=unknown).
244 uint64_t getDereferenceableBytes(unsigned i) const {
245 return AttributeSets.getDereferenceableBytes(i);
246 }
247
248 /// @brief Determine if the function does not access memory.
249 bool doesNotAccessMemory() const {
250 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
251 Attribute::ReadNone);
252 }
253 void setDoesNotAccessMemory() {
254 addFnAttr(Attribute::ReadNone);
255 }
256
257 /// @brief Determine if the function does not access or only reads memory.
258 bool onlyReadsMemory() const {
259 return doesNotAccessMemory() ||
260 AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
261 Attribute::ReadOnly);
262 }
263 void setOnlyReadsMemory() {
264 addFnAttr(Attribute::ReadOnly);
265 }
266
267 /// @brief Determine if the function cannot return.
268 bool doesNotReturn() const {
269 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
270 Attribute::NoReturn);
271 }
272 void setDoesNotReturn() {
273 addFnAttr(Attribute::NoReturn);
274 }
275
276 /// @brief Determine if the function cannot unwind.
277 bool doesNotThrow() const {
278 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
279 Attribute::NoUnwind);
280 }
281 void setDoesNotThrow() {
282 addFnAttr(Attribute::NoUnwind);
283 }
284
285 /// @brief Determine if the call cannot be duplicated.
286 bool cannotDuplicate() const {
287 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
288 Attribute::NoDuplicate);
289 }
290 void setCannotDuplicate() {
291 addFnAttr(Attribute::NoDuplicate);
292 }
293
294 /// @brief True if the ABI mandates (or the user requested) that this
295 /// function be in a unwind table.
296 bool hasUWTable() const {
297 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
298 Attribute::UWTable);
299 }
300 void setHasUWTable() {
301 addFnAttr(Attribute::UWTable);
302 }
303
304 /// @brief True if this function needs an unwind table.
305 bool needsUnwindTableEntry() const {
306 return hasUWTable() || !doesNotThrow();
307 }
308
309 /// @brief Determine if the function returns a structure through first
310 /// pointer argument.
311 bool hasStructRetAttr() const {
312 return AttributeSets.hasAttribute(1, Attribute::StructRet) ||
313 AttributeSets.hasAttribute(2, Attribute::StructRet);
314 }
315
316 /// @brief Determine if the parameter does not alias other parameters.
317 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
318 bool doesNotAlias(unsigned n) const {
319 return AttributeSets.hasAttribute(n, Attribute::NoAlias);
320 }
321 void setDoesNotAlias(unsigned n) {
322 addAttribute(n, Attribute::NoAlias);
323 }
324
325 /// @brief Determine if the parameter can be captured.
326 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
327 bool doesNotCapture(unsigned n) const {
328 return AttributeSets.hasAttribute(n, Attribute::NoCapture);
329 }
330 void setDoesNotCapture(unsigned n) {
331 addAttribute(n, Attribute::NoCapture);
332 }
333
334 bool doesNotAccessMemory(unsigned n) const {
335 return AttributeSets.hasAttribute(n, Attribute::ReadNone);
336 }
337 void setDoesNotAccessMemory(unsigned n) {
338 addAttribute(n, Attribute::ReadNone);
339 }
340
341 bool onlyReadsMemory(unsigned n) const {
342 return doesNotAccessMemory(n) ||
343 AttributeSets.hasAttribute(n, Attribute::ReadOnly);
344 }
345 void setOnlyReadsMemory(unsigned n) {
346 addAttribute(n, Attribute::ReadOnly);
347 }
348
349 /// copyAttributesFrom - copy all additional attributes (those not needed to
350 /// create a Function) from the Function Src to this one.
351 void copyAttributesFrom(const GlobalValue *Src) override;
352
353 /// deleteBody - This method deletes the body of the function, and converts
354 /// the linkage to external.
355 ///
356 void deleteBody() {
357 dropAllReferences();
358 setLinkage(ExternalLinkage);
359 }
360
361 /// removeFromParent - This method unlinks 'this' from the containing module,
362 /// but does not delete it.
363 ///
364 void removeFromParent() override;
365
366 /// eraseFromParent - This method unlinks 'this' from the containing module
367 /// and deletes it.
368 ///
369 void eraseFromParent() override;
370
371
372 /// Get the underlying elements of the Function... the basic block list is
373 /// empty for external functions.
374 ///
375 const ArgumentListType &getArgumentList() const {
376 CheckLazyArguments();
377 return ArgumentList;
378 }
379 ArgumentListType &getArgumentList() {
380 CheckLazyArguments();
381 return ArgumentList;
382 }
383 static iplist<Argument> Function::*getSublistAccess(Argument*) {
384 return &Function::ArgumentList;
385 }
386
387 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
388 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
389 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
390 return &Function::BasicBlocks;
391 }
392
393 const BasicBlock &getEntryBlock() const { return front(); }
394 BasicBlock &getEntryBlock() { return front(); }
395
396 //===--------------------------------------------------------------------===//
397 // Symbol Table Accessing functions...
398
399 /// getSymbolTable() - Return the symbol table...
400 ///
401 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
402 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
403
404
405 //===--------------------------------------------------------------------===//
406 // BasicBlock iterator forwarding functions
407 //
408 iterator begin() { return BasicBlocks.begin(); }
409 const_iterator begin() const { return BasicBlocks.begin(); }
410 iterator end () { return BasicBlocks.end(); }
411 const_iterator end () const { return BasicBlocks.end(); }
412
413 size_t size() const { return BasicBlocks.size(); }
414 bool empty() const { return BasicBlocks.empty(); }
415 const BasicBlock &front() const { return BasicBlocks.front(); }
416 BasicBlock &front() { return BasicBlocks.front(); }
417 const BasicBlock &back() const { return BasicBlocks.back(); }
418 BasicBlock &back() { return BasicBlocks.back(); }
419
420 /// @name Function Argument Iteration
421 /// @{
422
423 arg_iterator arg_begin() {
424 CheckLazyArguments();
425 return ArgumentList.begin();
426 }
427 const_arg_iterator arg_begin() const {
428 CheckLazyArguments();
429 return ArgumentList.begin();
430 }
431 arg_iterator arg_end() {
432 CheckLazyArguments();
433 return ArgumentList.end();
434 }
435 const_arg_iterator arg_end() const {
436 CheckLazyArguments();
437 return ArgumentList.end();
438 }
439
440 iterator_range<arg_iterator> args() {
441 return iterator_range<arg_iterator>(arg_begin(), arg_end());
442 }
443
444 iterator_range<const_arg_iterator> args() const {
445 return iterator_range<const_arg_iterator>(arg_begin(), arg_end());
446 }
447
448 /// @}
449
450 size_t arg_size() const;
451 bool arg_empty() const;
452
453 bool hasPrefixData() const {
454 return getSubclassDataFromValue() & (1<<1);
455 }
456
457 Constant *getPrefixData() const;
458 void setPrefixData(Constant *PrefixData);
459
460 bool hasPrologueData() const {
461 return getSubclassDataFromValue() & (1<<2);
462 }
463
464 Constant *getPrologueData() const;
465 void setPrologueData(Constant *PrologueData);
466
467 /// viewCFG - This function is meant for use from the debugger. You can just
468 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
469 /// program, displaying the CFG of the current function with the code for each
470 /// basic block inside. This depends on there being a 'dot' and 'gv' program
471 /// in your path.
472 ///
473 void viewCFG() const;
474
475 /// viewCFGOnly - This function is meant for use from the debugger. It works
476 /// just like viewCFG, but it does not include the contents of basic blocks
477 /// into the nodes, just the label. If you are only interested in the CFG
478 /// this can make the graph smaller.
479 ///
480 void viewCFGOnly() const;
481
482 /// Methods for support type inquiry through isa, cast, and dyn_cast:
483 static inline bool classof(const Value *V) {
484 return V->getValueID() == Value::FunctionVal;
485 }
486
487 /// dropAllReferences() - This method causes all the subinstructions to "let
488 /// go" of all references that they are maintaining. This allows one to
489 /// 'delete' a whole module at a time, even though there may be circular
490 /// references... first all references are dropped, and all use counts go to
491 /// zero. Then everything is deleted for real. Note that no operations are
492 /// valid on an object that has "dropped all references", except operator
493 /// delete.
494 ///
495 /// Since no other object in the module can have references into the body of a
496 /// function, dropping all references deletes the entire body of the function,
497 /// including any contained basic blocks.
498 ///
499 void dropAllReferences();
500
501 /// hasAddressTaken - returns true if there are any uses of this function
502 /// other than direct calls or invokes to it, or blockaddress expressions.
503 /// Optionally passes back an offending user for diagnostic purposes.
504 ///
505 bool hasAddressTaken(const User** = nullptr) const;
506
507 /// isDefTriviallyDead - Return true if it is trivially safe to remove
508 /// this function definition from the module (because it isn't externally
509 /// visible, does not have its address taken, and has no callers). To make
510 /// this more accurate, call removeDeadConstantUsers first.
511 bool isDefTriviallyDead() const;
512
513 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
514 /// setjmp or other function that gcc recognizes as "returning twice".
515 bool callsFunctionThatReturnsTwice() const;
516
517 private:
518 // Shadow Value::setValueSubclassData with a private forwarding method so that
519 // subclasses cannot accidentally use it.
520 void setValueSubclassData(unsigned short D) {
521 Value::setValueSubclassData(D);
522 }
523 };
524
525 inline ValueSymbolTable *
526 ilist_traits<BasicBlock>::getSymTab(Function *F) {
527 return F ? &F->getValueSymbolTable() : nullptr;
528 }
529
530 inline ValueSymbolTable *
531 ilist_traits<Argument>::getSymTab(Function *F) {
532 return F ? &F->getValueSymbolTable() : nullptr;
533 }
534
535 } // End llvm namespace
536
537 #endif