1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These classes wrap the information about a call or function 11 // definition used to handle ABI compliancy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H 16 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H 17 18 #include "CGValue.h" 19 #include "EHScopeStack.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 } 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 /// \brief The function prototype of the callee. 47 const FunctionProtoType *CalleeProtoTy; 48 /// \brief The function declaration of the callee. 49 const Decl *CalleeDecl; 50 51 public: 52 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {} 53 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl) 54 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {} 55 CGCalleeInfo(const FunctionProtoType *calleeProtoTy) 56 : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {} 57 CGCalleeInfo(const Decl *calleeDecl) 58 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {} 59 60 const FunctionProtoType *getCalleeFunctionProtoType() const { 61 return CalleeProtoTy; 62 } 63 const Decl *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(SpecialKind(uintptr_t(functionPtr))) { 114 AbstractInfo = abstractInfo; 115 assert(functionPtr && "configuring callee without function pointer"); 116 assert(functionPtr->getType()->isPointerTy()); 117 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy()); 118 } 119 120 static CGCallee forBuiltin(unsigned builtinID, 121 const FunctionDecl *builtinDecl) { 122 CGCallee result(SpecialKind::Builtin); 123 result.BuiltinInfo.Decl = builtinDecl; 124 result.BuiltinInfo.ID = builtinID; 125 return result; 126 } 127 128 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) { 129 CGCallee result(SpecialKind::PseudoDestructor); 130 result.PseudoDestructorInfo.Expr = E; 131 return result; 132 } 133 134 static CGCallee forDirect(llvm::Constant *functionPtr, 135 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { 136 return CGCallee(abstractInfo, functionPtr); 137 } 138 139 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, 140 llvm::FunctionType *FTy) { 141 CGCallee result(SpecialKind::Virtual); 142 result.VirtualInfo.CE = CE; 143 result.VirtualInfo.MD = MD; 144 result.VirtualInfo.Addr = Addr; 145 result.VirtualInfo.FTy = FTy; 146 return result; 147 } 148 149 bool isBuiltin() const { 150 return KindOrFunctionPointer == SpecialKind::Builtin; 151 } 152 const FunctionDecl *getBuiltinDecl() const { 153 assert(isBuiltin()); 154 return BuiltinInfo.Decl; 155 } 156 unsigned getBuiltinID() const { 157 assert(isBuiltin()); 158 return BuiltinInfo.ID; 159 } 160 161 bool isPseudoDestructor() const { 162 return KindOrFunctionPointer == SpecialKind::PseudoDestructor; 163 } 164 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { 165 assert(isPseudoDestructor()); 166 return PseudoDestructorInfo.Expr; 167 } 168 169 bool isOrdinary() const { 170 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last); 171 } 172 CGCalleeInfo getAbstractInfo() const { 173 if (isVirtual()) 174 return VirtualInfo.MD.getDecl(); 175 assert(isOrdinary()); 176 return AbstractInfo; 177 } 178 llvm::Value *getFunctionPointer() const { 179 assert(isOrdinary()); 180 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer)); 181 } 182 void setFunctionPointer(llvm::Value *functionPtr) { 183 assert(isOrdinary()); 184 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr)); 185 } 186 187 bool isVirtual() const { 188 return KindOrFunctionPointer == SpecialKind::Virtual; 189 } 190 const CallExpr *getVirtualCallExpr() const { 191 assert(isVirtual()); 192 return VirtualInfo.CE; 193 } 194 GlobalDecl getVirtualMethodDecl() const { 195 assert(isVirtual()); 196 return VirtualInfo.MD; 197 } 198 Address getThisAddress() const { 199 assert(isVirtual()); 200 return VirtualInfo.Addr; 201 } 202 203 llvm::FunctionType *getFunctionType() const { 204 if (isVirtual()) 205 return VirtualInfo.FTy; 206 return cast<llvm::FunctionType>( 207 getFunctionPointer()->getType()->getPointerElementType()); 208 } 209 210 /// If this is a delayed callee computation of some sort, prepare 211 /// a concrete callee. 212 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const; 213 }; 214 215 struct CallArg { 216 RValue RV; 217 QualType Ty; 218 bool NeedsCopy; 219 CallArg(RValue rv, QualType ty, bool needscopy) 220 : RV(rv), Ty(ty), NeedsCopy(needscopy) 221 { } 222 }; 223 224 /// CallArgList - Type for representing both the value and type of 225 /// arguments in a call. 226 class CallArgList : 227 public SmallVector<CallArg, 16> { 228 public: 229 CallArgList() : StackBase(nullptr) {} 230 231 struct Writeback { 232 /// The original argument. Note that the argument l-value 233 /// is potentially null. 234 LValue Source; 235 236 /// The temporary alloca. 237 Address Temporary; 238 239 /// A value to "use" after the writeback, or null. 240 llvm::Value *ToUse; 241 }; 242 243 struct CallArgCleanup { 244 EHScopeStack::stable_iterator Cleanup; 245 246 /// The "is active" insertion point. This instruction is temporary and 247 /// will be removed after insertion. 248 llvm::Instruction *IsActiveIP; 249 }; 250 251 void add(RValue rvalue, QualType type, bool needscopy = false) { 252 push_back(CallArg(rvalue, type, needscopy)); 253 } 254 255 /// Add all the arguments from another CallArgList to this one. After doing 256 /// this, the old CallArgList retains its list of arguments, but must not 257 /// be used to emit a call. 258 void addFrom(const CallArgList &other) { 259 insert(end(), other.begin(), other.end()); 260 Writebacks.insert(Writebacks.end(), 261 other.Writebacks.begin(), other.Writebacks.end()); 262 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(), 263 other.CleanupsToDeactivate.begin(), 264 other.CleanupsToDeactivate.end()); 265 assert(!(StackBase && other.StackBase) && "can't merge stackbases"); 266 if (!StackBase) 267 StackBase = other.StackBase; 268 } 269 270 void addWriteback(LValue srcLV, Address temporary, 271 llvm::Value *toUse) { 272 Writeback writeback = { srcLV, temporary, toUse }; 273 Writebacks.push_back(writeback); 274 } 275 276 bool hasWritebacks() const { return !Writebacks.empty(); } 277 278 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator> 279 writeback_const_range; 280 281 writeback_const_range writebacks() const { 282 return writeback_const_range(Writebacks.begin(), Writebacks.end()); 283 } 284 285 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, 286 llvm::Instruction *IsActiveIP) { 287 CallArgCleanup ArgCleanup; 288 ArgCleanup.Cleanup = Cleanup; 289 ArgCleanup.IsActiveIP = IsActiveIP; 290 CleanupsToDeactivate.push_back(ArgCleanup); 291 } 292 293 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { 294 return CleanupsToDeactivate; 295 } 296 297 void allocateArgumentMemory(CodeGenFunction &CGF); 298 llvm::Instruction *getStackBase() const { return StackBase; } 299 void freeArgumentMemory(CodeGenFunction &CGF) const; 300 301 /// \brief Returns if we're using an inalloca struct to pass arguments in 302 /// memory. 303 bool isUsingInAlloca() const { return StackBase; } 304 305 private: 306 SmallVector<Writeback, 1> Writebacks; 307 308 /// Deactivate these cleanups immediately before making the call. This 309 /// is used to cleanup objects that are owned by the callee once the call 310 /// occurs. 311 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate; 312 313 /// The stacksave call. It dominates all of the argument evaluation. 314 llvm::CallInst *StackBase; 315 }; 316 317 /// FunctionArgList - Type for representing both the decl and type 318 /// of parameters to a function. The decl must be either a 319 /// ParmVarDecl or ImplicitParamDecl. 320 class FunctionArgList : public SmallVector<const VarDecl*, 16> { 321 }; 322 323 /// ReturnValueSlot - Contains the address where the return value of a 324 /// function can be stored, and whether the address is volatile or not. 325 class ReturnValueSlot { 326 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value; 327 CharUnits Alignment; 328 329 // Return value slot flags 330 enum Flags { 331 IS_VOLATILE = 0x1, 332 IS_UNUSED = 0x2, 333 }; 334 335 public: 336 ReturnValueSlot() {} 337 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false) 338 : Value(Addr.isValid() ? Addr.getPointer() : nullptr, 339 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)), 340 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {} 341 342 bool isNull() const { return !getValue().isValid(); } 343 344 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; } 345 Address getValue() const { return Address(Value.getPointer(), Alignment); } 346 bool isUnused() const { return Value.getInt() & IS_UNUSED; } 347 }; 348 349 } // end namespace CodeGen 350 } // end namespace clang 351 352 #endif 353