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