| 1 | //===----------------------------------------------------------------------===// |
| 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 | // These classes wrap the information about a call or function |
| 10 | // definition used to handle ABI compliancy. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_LIB_CODEGEN_CIRGENCALL_H |
| 15 | #define CLANG_LIB_CODEGEN_CIRGENCALL_H |
| 16 | |
| 17 | #include "CIRGenValue.h" |
| 18 | #include "mlir/IR/Operation.h" |
| 19 | #include "clang/AST/GlobalDecl.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | |
| 22 | namespace clang::CIRGen { |
| 23 | |
| 24 | class CIRGenFunction; |
| 25 | |
| 26 | /// Abstract information about a function or function prototype. |
| 27 | class CIRGenCalleeInfo { |
| 28 | const clang::FunctionProtoType *calleeProtoTy; |
| 29 | clang::GlobalDecl calleeDecl; |
| 30 | |
| 31 | public: |
| 32 | explicit CIRGenCalleeInfo() : calleeProtoTy(nullptr), calleeDecl() {} |
| 33 | CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy, |
| 34 | clang::GlobalDecl calleeDecl) |
| 35 | : calleeProtoTy(calleeProtoTy), calleeDecl(calleeDecl) {} |
| 36 | CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy) |
| 37 | : calleeProtoTy(calleeProtoTy) {} |
| 38 | CIRGenCalleeInfo(clang::GlobalDecl calleeDecl) |
| 39 | : calleeProtoTy(nullptr), calleeDecl(calleeDecl) {} |
| 40 | |
| 41 | const clang::FunctionProtoType *getCalleeFunctionProtoType() const { |
| 42 | return calleeProtoTy; |
| 43 | } |
| 44 | clang::GlobalDecl getCalleeDecl() const { return calleeDecl; } |
| 45 | }; |
| 46 | |
| 47 | class CIRGenCallee { |
| 48 | enum class SpecialKind : uintptr_t { |
| 49 | Invalid, |
| 50 | Builtin, |
| 51 | PseudoDestructor, |
| 52 | Virtual, |
| 53 | |
| 54 | Last = Virtual |
| 55 | }; |
| 56 | |
| 57 | struct BuiltinInfoStorage { |
| 58 | const clang::FunctionDecl *decl; |
| 59 | unsigned id; |
| 60 | }; |
| 61 | struct PseudoDestructorInfoStorage { |
| 62 | const clang::CXXPseudoDestructorExpr *expr; |
| 63 | }; |
| 64 | struct VirtualInfoStorage { |
| 65 | const clang::CallExpr *ce; |
| 66 | clang::GlobalDecl md; |
| 67 | Address addr; |
| 68 | cir::FuncType fTy; |
| 69 | }; |
| 70 | |
| 71 | SpecialKind kindOrFunctionPtr; |
| 72 | |
| 73 | union { |
| 74 | CIRGenCalleeInfo abstractInfo; |
| 75 | BuiltinInfoStorage builtinInfo; |
| 76 | PseudoDestructorInfoStorage pseudoDestructorInfo; |
| 77 | VirtualInfoStorage virtualInfo; |
| 78 | }; |
| 79 | |
| 80 | explicit CIRGenCallee(SpecialKind kind) : kindOrFunctionPtr(kind) {} |
| 81 | |
| 82 | public: |
| 83 | CIRGenCallee() : kindOrFunctionPtr(SpecialKind::Invalid) {} |
| 84 | |
| 85 | CIRGenCallee(const CIRGenCalleeInfo &abstractInfo, mlir::Operation *funcPtr) |
| 86 | : kindOrFunctionPtr(SpecialKind(reinterpret_cast<uintptr_t>(funcPtr))), |
| 87 | abstractInfo(abstractInfo) { |
| 88 | assert(funcPtr && "configuring callee without function pointer" ); |
| 89 | } |
| 90 | |
| 91 | static CIRGenCallee |
| 92 | forDirect(mlir::Operation *funcPtr, |
| 93 | const CIRGenCalleeInfo &abstractInfo = CIRGenCalleeInfo()) { |
| 94 | return CIRGenCallee(abstractInfo, funcPtr); |
| 95 | } |
| 96 | |
| 97 | bool isInvalid() const { return kindOrFunctionPtr == SpecialKind::Invalid; } |
| 98 | |
| 99 | bool isBuiltin() const { return kindOrFunctionPtr == SpecialKind::Builtin; } |
| 100 | |
| 101 | const clang::FunctionDecl *getBuiltinDecl() const { |
| 102 | assert(isBuiltin()); |
| 103 | return builtinInfo.decl; |
| 104 | } |
| 105 | unsigned getBuiltinID() const { |
| 106 | assert(isBuiltin()); |
| 107 | return builtinInfo.id; |
| 108 | } |
| 109 | |
| 110 | static CIRGenCallee forBuiltin(unsigned builtinID, |
| 111 | const clang::FunctionDecl *builtinDecl) { |
| 112 | CIRGenCallee result(SpecialKind::Builtin); |
| 113 | result.builtinInfo.decl = builtinDecl; |
| 114 | result.builtinInfo.id = builtinID; |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | static CIRGenCallee |
| 119 | forPseudoDestructor(const clang::CXXPseudoDestructorExpr *expr) { |
| 120 | CIRGenCallee result(SpecialKind::PseudoDestructor); |
| 121 | result.pseudoDestructorInfo.expr = expr; |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | bool isPseudoDestructor() const { |
| 126 | return kindOrFunctionPtr == SpecialKind::PseudoDestructor; |
| 127 | } |
| 128 | |
| 129 | const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { |
| 130 | assert(isPseudoDestructor()); |
| 131 | return pseudoDestructorInfo.expr; |
| 132 | } |
| 133 | |
| 134 | bool isOrdinary() const { |
| 135 | return uintptr_t(kindOrFunctionPtr) > uintptr_t(SpecialKind::Last); |
| 136 | } |
| 137 | |
| 138 | /// If this is a delayed callee computation of some sort, prepare a concrete |
| 139 | /// callee |
| 140 | CIRGenCallee prepareConcreteCallee(CIRGenFunction &cgf) const; |
| 141 | |
| 142 | CIRGenCalleeInfo getAbstractInfo() const { |
| 143 | if (isVirtual()) |
| 144 | return virtualInfo.md; |
| 145 | assert(isOrdinary()); |
| 146 | return abstractInfo; |
| 147 | } |
| 148 | |
| 149 | mlir::Operation *getFunctionPointer() const { |
| 150 | assert(isOrdinary()); |
| 151 | return reinterpret_cast<mlir::Operation *>(kindOrFunctionPtr); |
| 152 | } |
| 153 | |
| 154 | bool isVirtual() const { return kindOrFunctionPtr == SpecialKind::Virtual; } |
| 155 | |
| 156 | static CIRGenCallee forVirtual(const clang::CallExpr *ce, |
| 157 | clang::GlobalDecl md, Address addr, |
| 158 | cir::FuncType fTy) { |
| 159 | CIRGenCallee result(SpecialKind::Virtual); |
| 160 | result.virtualInfo.ce = ce; |
| 161 | result.virtualInfo.md = md; |
| 162 | result.virtualInfo.addr = addr; |
| 163 | result.virtualInfo.fTy = fTy; |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | const clang::CallExpr *getVirtualCallExpr() const { |
| 168 | assert(isVirtual()); |
| 169 | return virtualInfo.ce; |
| 170 | } |
| 171 | |
| 172 | clang::GlobalDecl getVirtualMethodDecl() const { |
| 173 | assert(isVirtual()); |
| 174 | return virtualInfo.md; |
| 175 | } |
| 176 | |
| 177 | Address getThisAddress() const { |
| 178 | assert(isVirtual()); |
| 179 | return virtualInfo.addr; |
| 180 | } |
| 181 | |
| 182 | cir::FuncType getVirtualFunctionType() const { |
| 183 | assert(isVirtual()); |
| 184 | return virtualInfo.fTy; |
| 185 | } |
| 186 | |
| 187 | void setFunctionPointer(mlir::Operation *functionPtr) { |
| 188 | assert(isOrdinary()); |
| 189 | kindOrFunctionPtr = SpecialKind(reinterpret_cast<uintptr_t>(functionPtr)); |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | /// Type for representing both the decl and type of parameters to a function. |
| 194 | /// The decl must be either a ParmVarDecl or ImplicitParamDecl. |
| 195 | class FunctionArgList : public llvm::SmallVector<const clang::VarDecl *, 16> {}; |
| 196 | |
| 197 | struct CallArg { |
| 198 | private: |
| 199 | union { |
| 200 | RValue rv; |
| 201 | LValue lv; // This argument is semantically a load from this l-value |
| 202 | }; |
| 203 | bool hasLV; |
| 204 | |
| 205 | /// A data-flow flag to make sure getRValue and/or copyInto are not |
| 206 | /// called twice for duplicated IR emission. |
| 207 | mutable bool isUsed; |
| 208 | |
| 209 | public: |
| 210 | clang::QualType ty; |
| 211 | |
| 212 | CallArg(RValue rv, clang::QualType ty) |
| 213 | : rv(rv), hasLV(false), isUsed(false), ty(ty) {} |
| 214 | |
| 215 | CallArg(LValue lv, clang::QualType ty) |
| 216 | : lv(lv), hasLV(true), isUsed(false), ty(ty) {} |
| 217 | |
| 218 | bool hasLValue() const { return hasLV; } |
| 219 | |
| 220 | /// \returns an independent RValue. If the CallArg contains an LValue, |
| 221 | /// a temporary copy is returned. |
| 222 | RValue getRValue(CIRGenFunction &cgf, mlir::Location loc) const; |
| 223 | |
| 224 | LValue getKnownLValue() const { |
| 225 | assert(hasLV && !isUsed); |
| 226 | return lv; |
| 227 | } |
| 228 | |
| 229 | RValue getKnownRValue() const { |
| 230 | assert(!hasLV && !isUsed); |
| 231 | return rv; |
| 232 | } |
| 233 | |
| 234 | bool isAggregate() const { return hasLV || rv.isAggregate(); } |
| 235 | |
| 236 | void copyInto(CIRGenFunction &cgf, Address addr, mlir::Location loc) const; |
| 237 | }; |
| 238 | |
| 239 | class CallArgList : public llvm::SmallVector<CallArg, 8> { |
| 240 | public: |
| 241 | void add(RValue rvalue, clang::QualType type) { emplace_back(Args&: rvalue, Args&: type); } |
| 242 | |
| 243 | void addUncopiedAggregate(LValue lvalue, clang::QualType type) { |
| 244 | emplace_back(Args&: lvalue, Args&: type); |
| 245 | } |
| 246 | |
| 247 | /// Add all the arguments from another CallArgList to this one. After doing |
| 248 | /// this, the old CallArgList retains its list of arguments, but must not |
| 249 | /// be used to emit a call. |
| 250 | void addFrom(const CallArgList &other) { |
| 251 | insert(I: end(), From: other.begin(), To: other.end()); |
| 252 | // Classic codegen has handling for these here. We may not need it here for |
| 253 | // CIR, but if not we should implement equivalent handling in lowering. |
| 254 | assert(!cir::MissingFeatures::writebacks()); |
| 255 | assert(!cir::MissingFeatures::cleanupsToDeactivate()); |
| 256 | assert(!cir::MissingFeatures::stackBase()); |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | /// Contains the address where the return value of a function can be stored, and |
| 261 | /// whether the address is volatile or not. |
| 262 | class ReturnValueSlot { |
| 263 | Address addr = Address::invalid(); |
| 264 | |
| 265 | public: |
| 266 | ReturnValueSlot() = default; |
| 267 | ReturnValueSlot(Address addr) : addr(addr) {} |
| 268 | |
| 269 | bool isNull() const { return !addr.isValid(); } |
| 270 | Address getValue() const { return addr; } |
| 271 | }; |
| 272 | |
| 273 | } // namespace clang::CIRGen |
| 274 | |
| 275 | #endif // CLANG_LIB_CODEGEN_CIRGENCALL_H |
| 276 | |