1 | //===-- llvm/Argument.h - Definition of the Argument class ------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file declares the Argument class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_IR_ARGUMENT_H |
14 | #define LLVM_IR_ARGUMENT_H |
15 | |
16 | #include "llvm/ADT/Twine.h" |
17 | #include "llvm/IR/Attributes.h" |
18 | #include "llvm/IR/Value.h" |
19 | #include <optional> |
20 | |
21 | namespace llvm { |
22 | |
23 | class ConstantRange; |
24 | |
25 | /// This class represents an incoming formal argument to a Function. A formal |
26 | /// argument, since it is ``formal'', does not contain an actual value but |
27 | /// instead represents the type, argument number, and attributes of an argument |
28 | /// for a specific function. When used in the body of said function, the |
29 | /// argument of course represents the value of the actual argument that the |
30 | /// function was called with. |
31 | class Argument final : public Value { |
32 | Function *Parent; |
33 | unsigned ArgNo; |
34 | |
35 | friend class Function; |
36 | void setParent(Function *parent); |
37 | |
38 | public: |
39 | /// Argument constructor. |
40 | explicit Argument(Type *Ty, const Twine &Name = "" , Function *F = nullptr, |
41 | unsigned ArgNo = 0); |
42 | |
43 | inline const Function *getParent() const { return Parent; } |
44 | inline Function *getParent() { return Parent; } |
45 | |
46 | /// Return the index of this formal argument in its containing function. |
47 | /// |
48 | /// For example in "void foo(int a, float b)" a is 0 and b is 1. |
49 | unsigned getArgNo() const { |
50 | assert(Parent && "can't get number of unparented arg" ); |
51 | return ArgNo; |
52 | } |
53 | |
54 | /// Return true if this argument has the nonnull attribute. Also returns true |
55 | /// if at least one byte is known to be dereferenceable and the pointer is in |
56 | /// addrspace(0). |
57 | /// If AllowUndefOrPoison is true, respect the semantics of nonnull attribute |
58 | /// and return true even if the argument can be undef or poison. |
59 | bool hasNonNullAttr(bool AllowUndefOrPoison = true) const; |
60 | |
61 | /// If this argument has the dereferenceable attribute, return the number of |
62 | /// bytes known to be dereferenceable. Otherwise, zero is returned. |
63 | uint64_t getDereferenceableBytes() const; |
64 | |
65 | /// If this argument has the dereferenceable_or_null attribute, return the |
66 | /// number of bytes known to be dereferenceable. Otherwise, zero is returned. |
67 | uint64_t getDereferenceableOrNullBytes() const; |
68 | |
69 | /// If this argument has nofpclass attribute, return the mask representing |
70 | /// disallowed floating-point values. Otherwise, fcNone is returned. |
71 | FPClassTest getNoFPClass() const; |
72 | |
73 | /// If this argument has a range attribute, return the value range of the |
74 | /// argument. Otherwise, std::nullopt is returned. |
75 | std::optional<ConstantRange> getRange() const; |
76 | |
77 | /// Return true if this argument has the byval attribute. |
78 | bool hasByValAttr() const; |
79 | |
80 | /// Return true if this argument has the byref attribute. |
81 | bool hasByRefAttr() const; |
82 | |
83 | /// Return true if this argument has the swiftself attribute. |
84 | bool hasSwiftSelfAttr() const; |
85 | |
86 | /// Return true if this argument has the swifterror attribute. |
87 | bool hasSwiftErrorAttr() const; |
88 | |
89 | /// Return true if this argument has the byval, inalloca, or preallocated |
90 | /// attribute. These attributes represent arguments being passed by value, |
91 | /// with an associated copy between the caller and callee |
92 | bool hasPassPointeeByValueCopyAttr() const; |
93 | |
94 | /// If this argument satisfies has hasPassPointeeByValueAttr, return the |
95 | /// in-memory ABI size copied to the stack for the call. Otherwise, return 0. |
96 | uint64_t getPassPointeeByValueCopySize(const DataLayout &DL) const; |
97 | |
98 | /// Return true if this argument has the byval, sret, inalloca, preallocated, |
99 | /// or byref attribute. These attributes represent arguments being passed by |
100 | /// value (which may or may not involve a stack copy) |
101 | bool hasPointeeInMemoryValueAttr() const; |
102 | |
103 | /// If hasPointeeInMemoryValueAttr returns true, the in-memory ABI type is |
104 | /// returned. Otherwise, nullptr. |
105 | Type *getPointeeInMemoryValueType() const; |
106 | |
107 | /// If this is a byval or inalloca argument, return its alignment. |
108 | /// FIXME: Remove this function once transition to Align is over. |
109 | /// Use getParamAlign() instead. |
110 | LLVM_DEPRECATED("Use getParamAlign() instead" , "getParamAlign" ) |
111 | uint64_t getParamAlignment() const; |
112 | |
113 | /// If this is a byval or inalloca argument, return its alignment. |
114 | MaybeAlign getParamAlign() const; |
115 | |
116 | MaybeAlign getParamStackAlign() const; |
117 | |
118 | /// If this is a byval argument, return its type. |
119 | Type *getParamByValType() const; |
120 | |
121 | /// If this is an sret argument, return its type. |
122 | Type *getParamStructRetType() const; |
123 | |
124 | /// If this is a byref argument, return its type. |
125 | Type *getParamByRefType() const; |
126 | |
127 | /// If this is an inalloca argument, return its type. |
128 | Type *getParamInAllocaType() const; |
129 | |
130 | /// Return true if this argument has the nest attribute. |
131 | bool hasNestAttr() const; |
132 | |
133 | /// Return true if this argument has the noalias attribute. |
134 | bool hasNoAliasAttr() const; |
135 | |
136 | /// Return true if this argument has the nocapture attribute. |
137 | bool hasNoCaptureAttr() const; |
138 | |
139 | /// Return true if this argument has the nofree attribute. |
140 | bool hasNoFreeAttr() const; |
141 | |
142 | /// Return true if this argument has the sret attribute. |
143 | bool hasStructRetAttr() const; |
144 | |
145 | /// Return true if this argument has the inreg attribute. |
146 | bool hasInRegAttr() const; |
147 | |
148 | /// Return true if this argument has the returned attribute. |
149 | bool hasReturnedAttr() const; |
150 | |
151 | /// Return true if this argument has the readonly or readnone attribute. |
152 | bool onlyReadsMemory() const; |
153 | |
154 | /// Return true if this argument has the inalloca attribute. |
155 | bool hasInAllocaAttr() const; |
156 | |
157 | /// Return true if this argument has the preallocated attribute. |
158 | bool hasPreallocatedAttr() const; |
159 | |
160 | /// Return true if this argument has the zext attribute. |
161 | bool hasZExtAttr() const; |
162 | |
163 | /// Return true if this argument has the sext attribute. |
164 | bool hasSExtAttr() const; |
165 | |
166 | /// Add attributes to an argument. |
167 | void addAttrs(AttrBuilder &B); |
168 | |
169 | void addAttr(Attribute::AttrKind Kind); |
170 | |
171 | void addAttr(Attribute Attr); |
172 | |
173 | /// Remove attributes from an argument. |
174 | void removeAttr(Attribute::AttrKind Kind); |
175 | |
176 | void removeAttrs(const AttributeMask &AM); |
177 | |
178 | /// Check if an argument has a given attribute. |
179 | bool hasAttribute(Attribute::AttrKind Kind) const; |
180 | |
181 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
182 | |
183 | /// Method for support type inquiry through isa, cast, and dyn_cast. |
184 | static bool classof(const Value *V) { |
185 | return V->getValueID() == ArgumentVal; |
186 | } |
187 | }; |
188 | |
189 | } // End llvm namespace |
190 | |
191 | #endif |
192 | |