1 //===----- CGCall.h - Encapsulate calling convention details ----*- 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 // These classes wrap the information about a call or function 10 // definition used to handle ABI compliancy. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H 15 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H 16 17 #include "CGValue.h" 18 #include "EHScopeStack.h" 19 #include "clang/AST/ASTFwd.h" 20 #include "clang/AST/CanonicalType.h" 21 #include "clang/AST/GlobalDecl.h" 22 #include "clang/AST/Type.h" 23 #include "llvm/IR/Value.h" 24 25 namespace llvm { 26 class Type; 27 class Value; 28 } // namespace llvm 29 30 namespace clang { 31 class Decl; 32 class FunctionDecl; 33 class VarDecl; 34 35 namespace CodeGen { 36 37 /// Abstract information about a function or function prototype. 38 class CGCalleeInfo { 39 /// The function prototype of the callee. 40 const FunctionProtoType *CalleeProtoTy; 41 /// The function declaration of the callee. 42 GlobalDecl CalleeDecl; 43 44 public: 45 explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {} 46 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl) 47 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {} 48 CGCalleeInfo(const FunctionProtoType *calleeProtoTy) 49 : CalleeProtoTy(calleeProtoTy) {} 50 CGCalleeInfo(GlobalDecl calleeDecl) 51 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {} 52 53 const FunctionProtoType *getCalleeFunctionProtoType() const { 54 return CalleeProtoTy; 55 } 56 const GlobalDecl getCalleeDecl() const { return CalleeDecl; } 57 }; 58 59 /// All available information about a concrete callee. 60 class CGCallee { 61 enum class SpecialKind : uintptr_t { 62 Invalid, 63 Builtin, 64 PseudoDestructor, 65 Virtual, 66 67 Last = Virtual 68 }; 69 70 struct BuiltinInfoStorage { 71 const FunctionDecl *Decl; 72 unsigned ID; 73 }; 74 struct PseudoDestructorInfoStorage { 75 const CXXPseudoDestructorExpr *Expr; 76 }; 77 struct VirtualInfoStorage { 78 const CallExpr *CE; 79 GlobalDecl MD; 80 Address Addr; 81 llvm::FunctionType *FTy; 82 }; 83 84 SpecialKind KindOrFunctionPointer; 85 union { 86 CGCalleeInfo AbstractInfo; 87 BuiltinInfoStorage BuiltinInfo; 88 PseudoDestructorInfoStorage PseudoDestructorInfo; 89 VirtualInfoStorage VirtualInfo; 90 }; 91 92 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {} 93 94 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID) 95 : KindOrFunctionPointer(SpecialKind::Builtin) { 96 BuiltinInfo.Decl = builtinDecl; 97 BuiltinInfo.ID = builtinID; 98 } 99 100 public: 101 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {} 102 103 /// Construct a callee. Call this constructor directly when this 104 /// isn't a direct call. 105 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr) 106 : KindOrFunctionPointer( 107 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) { 108 AbstractInfo = abstractInfo; 109 assert(functionPtr && "configuring callee without function pointer"); 110 assert(functionPtr->getType()->isPointerTy()); 111 assert(functionPtr->getType()->isOpaquePointerTy() || 112 functionPtr->getType()->getNonOpaquePointerElementType() 113 ->isFunctionTy()); 114 } 115 116 static CGCallee forBuiltin(unsigned builtinID, 117 const FunctionDecl *builtinDecl) { 118 CGCallee result(SpecialKind::Builtin); 119 result.BuiltinInfo.Decl = builtinDecl; 120 result.BuiltinInfo.ID = builtinID; 121 return result; 122 } 123 124 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) { 125 CGCallee result(SpecialKind::PseudoDestructor); 126 result.PseudoDestructorInfo.Expr = E; 127 return result; 128 } 129 130 static CGCallee forDirect(llvm::Constant *functionPtr, 131 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { 132 return CGCallee(abstractInfo, functionPtr); 133 } 134 135 static CGCallee forDirect(llvm::FunctionCallee functionPtr, 136 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { 137 return CGCallee(abstractInfo, functionPtr.getCallee()); 138 } 139 140 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, 141 llvm::FunctionType *FTy) { 142 CGCallee result(SpecialKind::Virtual); 143 result.VirtualInfo.CE = CE; 144 result.VirtualInfo.MD = MD; 145 result.VirtualInfo.Addr = Addr; 146 result.VirtualInfo.FTy = FTy; 147 return result; 148 } 149 150 bool isBuiltin() const { 151 return KindOrFunctionPointer == SpecialKind::Builtin; 152 } 153 const FunctionDecl *getBuiltinDecl() const { 154 assert(isBuiltin()); 155 return BuiltinInfo.Decl; 156 } 157 unsigned getBuiltinID() const { 158 assert(isBuiltin()); 159 return BuiltinInfo.ID; 160 } 161 162 bool isPseudoDestructor() const { 163 return KindOrFunctionPointer == SpecialKind::PseudoDestructor; 164 } 165 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { 166 assert(isPseudoDestructor()); 167 return PseudoDestructorInfo.Expr; 168 } 169 170 bool isOrdinary() const { 171 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last); 172 } 173 CGCalleeInfo getAbstractInfo() const { 174 if (isVirtual()) 175 return VirtualInfo.MD; 176 assert(isOrdinary()); 177 return AbstractInfo; 178 } 179 llvm::Value *getFunctionPointer() const { 180 assert(isOrdinary()); 181 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer)); 182 } 183 void setFunctionPointer(llvm::Value *functionPtr) { 184 assert(isOrdinary()); 185 KindOrFunctionPointer = 186 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr)); 187 } 188 189 bool isVirtual() const { 190 return KindOrFunctionPointer == SpecialKind::Virtual; 191 } 192 const CallExpr *getVirtualCallExpr() const { 193 assert(isVirtual()); 194 return VirtualInfo.CE; 195 } 196 GlobalDecl getVirtualMethodDecl() const { 197 assert(isVirtual()); 198 return VirtualInfo.MD; 199 } 200 Address getThisAddress() const { 201 assert(isVirtual()); 202 return VirtualInfo.Addr; 203 } 204 llvm::FunctionType *getVirtualFunctionType() const { 205 assert(isVirtual()); 206 return VirtualInfo.FTy; 207 } 208 209 /// If this is a delayed callee computation of some sort, prepare 210 /// a concrete callee. 211 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const; 212 }; 213 214 struct CallArg { 215 private: 216 union { 217 RValue RV; 218 LValue LV; /// The argument is semantically a load from this l-value. 219 }; 220 bool HasLV; 221 222 /// A data-flow flag to make sure getRValue and/or copyInto are not 223 /// called twice for duplicated IR emission. 224 mutable bool IsUsed; 225 226 public: 227 QualType Ty; 228 CallArg(RValue rv, QualType ty) 229 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {} 230 CallArg(LValue lv, QualType ty) 231 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {} 232 bool hasLValue() const { return HasLV; } 233 QualType getType() const { return Ty; } 234 235 /// \returns an independent RValue. If the CallArg contains an LValue, 236 /// a temporary copy is returned. 237 RValue getRValue(CodeGenFunction &CGF) const; 238 239 LValue getKnownLValue() const { 240 assert(HasLV && !IsUsed); 241 return LV; 242 } 243 RValue getKnownRValue() const { 244 assert(!HasLV && !IsUsed); 245 return RV; 246 } 247 void setRValue(RValue _RV) { 248 assert(!HasLV); 249 RV = _RV; 250 } 251 252 bool isAggregate() const { return HasLV || RV.isAggregate(); } 253 254 void copyInto(CodeGenFunction &CGF, Address A) const; 255 }; 256 257 /// CallArgList - Type for representing both the value and type of 258 /// arguments in a call. 259 class CallArgList : public SmallVector<CallArg, 8> { 260 public: 261 CallArgList() : StackBase(nullptr) {} 262 263 struct Writeback { 264 /// The original argument. Note that the argument l-value 265 /// is potentially null. 266 LValue Source; 267 268 /// The temporary alloca. 269 Address Temporary; 270 271 /// A value to "use" after the writeback, or null. 272 llvm::Value *ToUse; 273 }; 274 275 struct CallArgCleanup { 276 EHScopeStack::stable_iterator Cleanup; 277 278 /// The "is active" insertion point. This instruction is temporary and 279 /// will be removed after insertion. 280 llvm::Instruction *IsActiveIP; 281 }; 282 283 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); } 284 285 void addUncopiedAggregate(LValue LV, QualType type) { 286 push_back(CallArg(LV, type)); 287 } 288 289 /// Add all the arguments from another CallArgList to this one. After doing 290 /// this, the old CallArgList retains its list of arguments, but must not 291 /// be used to emit a call. 292 void addFrom(const CallArgList &other) { 293 insert(end(), other.begin(), other.end()); 294 Writebacks.insert(Writebacks.end(), other.Writebacks.begin(), 295 other.Writebacks.end()); 296 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(), 297 other.CleanupsToDeactivate.begin(), 298 other.CleanupsToDeactivate.end()); 299 assert(!(StackBase && other.StackBase) && "can't merge stackbases"); 300 if (!StackBase) 301 StackBase = other.StackBase; 302 } 303 304 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) { 305 Writeback writeback = {srcLV, temporary, toUse}; 306 Writebacks.push_back(writeback); 307 } 308 309 bool hasWritebacks() const { return !Writebacks.empty(); } 310 311 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator> 312 writeback_const_range; 313 314 writeback_const_range writebacks() const { 315 return writeback_const_range(Writebacks.begin(), Writebacks.end()); 316 } 317 318 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, 319 llvm::Instruction *IsActiveIP) { 320 CallArgCleanup ArgCleanup; 321 ArgCleanup.Cleanup = Cleanup; 322 ArgCleanup.IsActiveIP = IsActiveIP; 323 CleanupsToDeactivate.push_back(ArgCleanup); 324 } 325 326 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { 327 return CleanupsToDeactivate; 328 } 329 330 void allocateArgumentMemory(CodeGenFunction &CGF); 331 llvm::Instruction *getStackBase() const { return StackBase; } 332 void freeArgumentMemory(CodeGenFunction &CGF) const; 333 334 /// Returns if we're using an inalloca struct to pass arguments in 335 /// memory. 336 bool isUsingInAlloca() const { return StackBase; } 337 338 private: 339 SmallVector<Writeback, 1> Writebacks; 340 341 /// Deactivate these cleanups immediately before making the call. This 342 /// is used to cleanup objects that are owned by the callee once the call 343 /// occurs. 344 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate; 345 346 /// The stacksave call. It dominates all of the argument evaluation. 347 llvm::CallInst *StackBase; 348 }; 349 350 /// FunctionArgList - Type for representing both the decl and type 351 /// of parameters to a function. The decl must be either a 352 /// ParmVarDecl or ImplicitParamDecl. 353 class FunctionArgList : public SmallVector<const VarDecl *, 16> {}; 354 355 /// ReturnValueSlot - Contains the address where the return value of a 356 /// function can be stored, and whether the address is volatile or not. 357 class ReturnValueSlot { 358 Address Addr = Address::invalid(); 359 360 // Return value slot flags 361 unsigned IsVolatile : 1; 362 unsigned IsUnused : 1; 363 unsigned IsExternallyDestructed : 1; 364 365 public: 366 ReturnValueSlot() 367 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {} 368 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false, 369 bool IsExternallyDestructed = false) 370 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused), 371 IsExternallyDestructed(IsExternallyDestructed) {} 372 373 bool isNull() const { return !Addr.isValid(); } 374 bool isVolatile() const { return IsVolatile; } 375 Address getValue() const { return Addr; } 376 bool isUnused() const { return IsUnused; } 377 bool isExternallyDestructed() const { return IsExternallyDestructed; } 378 }; 379 380 } // end namespace CodeGen 381 } // end namespace clang 382 383 #endif 384