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/Type.h" 22 #include "llvm/IR/Value.h" 23 24 // FIXME: Restructure so we don't have to expose so much stuff. 25 #include "ABIInfo.h" 26 27 namespace llvm { 28 class AttributeSet; 29 class Function; 30 class Type; 31 class Value; 32 } 33 34 namespace clang { 35 class ASTContext; 36 class Decl; 37 class FunctionDecl; 38 class ObjCMethodDecl; 39 class VarDecl; 40 41 namespace CodeGen { 42 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType; 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 73 Last = PseudoDestructor 74 }; 75 76 SpecialKind KindOrFunctionPointer; 77 union { 78 CGCalleeInfo AbstractInfo; 79 struct { 80 const FunctionDecl *Decl; 81 unsigned ID; 82 } BuiltinInfo; 83 struct { 84 const CXXPseudoDestructorExpr *Expr; 85 } PseudoDestructorInfo; 86 }; 87 88 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {} 89 90 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID) 91 : KindOrFunctionPointer(SpecialKind::Builtin) { 92 BuiltinInfo.Decl = builtinDecl; 93 BuiltinInfo.ID = builtinID; 94 } 95 96 public: 97 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {} 98 99 /// Construct a callee. Call this constructor directly when this 100 /// isn't a direct call. 101 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr) 102 : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) { 103 AbstractInfo = abstractInfo; 104 assert(functionPtr && "configuring callee without function pointer"); 105 assert(functionPtr->getType()->isPointerTy()); 106 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy()); 107 } 108 109 static CGCallee forBuiltin(unsigned builtinID, 110 const FunctionDecl *builtinDecl) { 111 CGCallee result(SpecialKind::Builtin); 112 result.BuiltinInfo.Decl = builtinDecl; 113 result.BuiltinInfo.ID = builtinID; 114 return result; 115 } 116 117 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) { 118 CGCallee result(SpecialKind::PseudoDestructor); 119 result.PseudoDestructorInfo.Expr = E; 120 return result; 121 } 122 123 static CGCallee forDirect(llvm::Constant *functionPtr, 124 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { 125 return CGCallee(abstractInfo, functionPtr); 126 } 127 128 bool isBuiltin() const { 129 return KindOrFunctionPointer == SpecialKind::Builtin; 130 } 131 const FunctionDecl *getBuiltinDecl() const { 132 assert(isBuiltin()); 133 return BuiltinInfo.Decl; 134 } 135 unsigned getBuiltinID() const { 136 assert(isBuiltin()); 137 return BuiltinInfo.ID; 138 } 139 140 bool isPseudoDestructor() const { 141 return KindOrFunctionPointer == SpecialKind::PseudoDestructor; 142 } 143 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { 144 assert(isPseudoDestructor()); 145 return PseudoDestructorInfo.Expr; 146 } 147 148 bool isOrdinary() const { 149 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last); 150 } 151 const CGCalleeInfo &getAbstractInfo() const { 152 assert(isOrdinary()); 153 return AbstractInfo; 154 } 155 llvm::Value *getFunctionPointer() const { 156 assert(isOrdinary()); 157 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer)); 158 } 159 llvm::FunctionType *getFunctionType() const { 160 return cast<llvm::FunctionType>( 161 getFunctionPointer()->getType()->getPointerElementType()); 162 } 163 void setFunctionPointer(llvm::Value *functionPtr) { 164 assert(isOrdinary()); 165 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr)); 166 } 167 }; 168 169 struct CallArg { 170 RValue RV; 171 QualType Ty; 172 bool NeedsCopy; 173 CallArg(RValue rv, QualType ty, bool needscopy) 174 : RV(rv), Ty(ty), NeedsCopy(needscopy) 175 { } 176 }; 177 178 /// CallArgList - Type for representing both the value and type of 179 /// arguments in a call. 180 class CallArgList : 181 public SmallVector<CallArg, 16> { 182 public: 183 CallArgList() : StackBase(nullptr) {} 184 185 struct Writeback { 186 /// The original argument. Note that the argument l-value 187 /// is potentially null. 188 LValue Source; 189 190 /// The temporary alloca. 191 Address Temporary; 192 193 /// A value to "use" after the writeback, or null. 194 llvm::Value *ToUse; 195 }; 196 197 struct CallArgCleanup { 198 EHScopeStack::stable_iterator Cleanup; 199 200 /// The "is active" insertion point. This instruction is temporary and 201 /// will be removed after insertion. 202 llvm::Instruction *IsActiveIP; 203 }; 204 205 void add(RValue rvalue, QualType type, bool needscopy = false) { 206 push_back(CallArg(rvalue, type, needscopy)); 207 } 208 209 /// Add all the arguments from another CallArgList to this one. After doing 210 /// this, the old CallArgList retains its list of arguments, but must not 211 /// be used to emit a call. 212 void addFrom(const CallArgList &other) { 213 insert(end(), other.begin(), other.end()); 214 Writebacks.insert(Writebacks.end(), 215 other.Writebacks.begin(), other.Writebacks.end()); 216 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(), 217 other.CleanupsToDeactivate.begin(), 218 other.CleanupsToDeactivate.end()); 219 assert(!(StackBase && other.StackBase) && "can't merge stackbases"); 220 if (!StackBase) 221 StackBase = other.StackBase; 222 } 223 224 void addWriteback(LValue srcLV, Address temporary, 225 llvm::Value *toUse) { 226 Writeback writeback = { srcLV, temporary, toUse }; 227 Writebacks.push_back(writeback); 228 } 229 230 bool hasWritebacks() const { return !Writebacks.empty(); } 231 232 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator> 233 writeback_const_range; 234 235 writeback_const_range writebacks() const { 236 return writeback_const_range(Writebacks.begin(), Writebacks.end()); 237 } 238 239 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, 240 llvm::Instruction *IsActiveIP) { 241 CallArgCleanup ArgCleanup; 242 ArgCleanup.Cleanup = Cleanup; 243 ArgCleanup.IsActiveIP = IsActiveIP; 244 CleanupsToDeactivate.push_back(ArgCleanup); 245 } 246 247 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { 248 return CleanupsToDeactivate; 249 } 250 251 void allocateArgumentMemory(CodeGenFunction &CGF); 252 llvm::Instruction *getStackBase() const { return StackBase; } 253 void freeArgumentMemory(CodeGenFunction &CGF) const; 254 255 /// \brief Returns if we're using an inalloca struct to pass arguments in 256 /// memory. 257 bool isUsingInAlloca() const { return StackBase; } 258 259 private: 260 SmallVector<Writeback, 1> Writebacks; 261 262 /// Deactivate these cleanups immediately before making the call. This 263 /// is used to cleanup objects that are owned by the callee once the call 264 /// occurs. 265 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate; 266 267 /// The stacksave call. It dominates all of the argument evaluation. 268 llvm::CallInst *StackBase; 269 }; 270 271 /// FunctionArgList - Type for representing both the decl and type 272 /// of parameters to a function. The decl must be either a 273 /// ParmVarDecl or ImplicitParamDecl. 274 class FunctionArgList : public SmallVector<const VarDecl*, 16> { 275 }; 276 277 /// ReturnValueSlot - Contains the address where the return value of a 278 /// function can be stored, and whether the address is volatile or not. 279 class ReturnValueSlot { 280 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value; 281 CharUnits Alignment; 282 283 // Return value slot flags 284 enum Flags { 285 IS_VOLATILE = 0x1, 286 IS_UNUSED = 0x2, 287 }; 288 289 public: 290 ReturnValueSlot() {} 291 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false) 292 : Value(Addr.isValid() ? Addr.getPointer() : nullptr, 293 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)), 294 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {} 295 296 bool isNull() const { return !getValue().isValid(); } 297 298 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; } 299 Address getValue() const { return Address(Value.getPointer(), Alignment); } 300 bool isUnused() const { return Value.getInt() & IS_UNUSED; } 301 }; 302 303 } // end namespace CodeGen 304 } // end namespace clang 305 306 #endif 307