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