Anders Carlsson | 59486a2 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 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 | // This contains code dealing with code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Fariborz Jahanian | 60d215b | 2010-05-20 21:38:57 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chris Lattner | 26008e0 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 17 | #include "llvm/Intrinsics.h" |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 21 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 22 | llvm::Value *Callee, |
| 23 | ReturnValueSlot ReturnValue, |
| 24 | llvm::Value *This, |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 25 | llvm::Value *VTT, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 26 | CallExpr::const_arg_iterator ArgBeg, |
| 27 | CallExpr::const_arg_iterator ArgEnd) { |
| 28 | assert(MD->isInstance() && |
| 29 | "Trying to emit a member call expr on a static method!"); |
| 30 | |
| 31 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 32 | |
| 33 | CallArgList Args; |
| 34 | |
| 35 | // Push the this ptr. |
| 36 | Args.push_back(std::make_pair(RValue::get(This), |
| 37 | MD->getThisType(getContext()))); |
| 38 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 39 | // If there is a VTT parameter, emit it. |
| 40 | if (VTT) { |
| 41 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 42 | Args.push_back(std::make_pair(RValue::get(VTT), T)); |
| 43 | } |
| 44 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 45 | // And the rest of the call args |
| 46 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 47 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 48 | QualType ResultType = FPT->getResultType(); |
| 49 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 50 | FPT->getExtInfo()), |
| 51 | Callee, ReturnValue, Args, MD); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 55 | /// expr can be devirtualized. |
| 56 | static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { |
| 57 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 58 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 59 | // This is a record decl. We know the type and can devirtualize it. |
| 60 | return VD->getType()->isRecordType(); |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // We can always devirtualize calls on temporary object expressions. |
Eli Friedman | a682427 | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 67 | if (isa<CXXConstructExpr>(Base)) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 68 | return true; |
| 69 | |
| 70 | // And calls on bound temporaries. |
| 71 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 72 | return true; |
| 73 | |
| 74 | // Check if this is a call expr that returns a record type. |
| 75 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 76 | return CE->getCallReturnType()->isRecordType(); |
| 77 | |
| 78 | // We can't devirtualize the call. |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 83 | ReturnValueSlot ReturnValue) { |
| 84 | if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens())) |
| 85 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
| 86 | |
| 87 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens()); |
| 88 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 89 | |
| 90 | if (MD->isStatic()) { |
| 91 | // The method is static, emit it as we would a regular call. |
| 92 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 93 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
| 94 | ReturnValue, CE->arg_begin(), CE->arg_end()); |
| 95 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 96 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 97 | // Compute the object pointer. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 98 | llvm::Value *This; |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 99 | if (ME->isArrow()) |
| 100 | This = EmitScalarExpr(ME->getBase()); |
| 101 | else { |
| 102 | LValue BaseLV = EmitLValue(ME->getBase()); |
Fariborz Jahanian | f93ac89 | 2010-09-10 18:56:35 +0000 | [diff] [blame] | 103 | if (BaseLV.isPropertyRef() || BaseLV.isKVCRef()) { |
| 104 | QualType QT = ME->getBase()->getType(); |
| 105 | RValue RV = |
| 106 | BaseLV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(BaseLV, QT) |
| 107 | : EmitLoadOfKVCRefLValue(BaseLV, QT); |
| 108 | This = RV.isScalar() ? RV.getScalarVal() : RV.getAggregateAddr(); |
| 109 | } |
| 110 | else |
| 111 | This = BaseLV.getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 114 | if (MD->isTrivial()) { |
| 115 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); |
| 116 | |
| 117 | assert(MD->isCopyAssignment() && "unknown trivial member function"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 118 | // We don't like to generate the trivial copy assignment operator when |
| 119 | // it isn't necessary; just produce the proper effect here. |
| 120 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 121 | EmitAggregateCopy(This, RHS, CE->getType()); |
| 122 | return RValue::get(This); |
| 123 | } |
| 124 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 125 | // Compute the function type we're calling. |
| 126 | const CGFunctionInfo &FInfo = |
| 127 | (isa<CXXDestructorDecl>(MD) |
| 128 | ? CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), |
| 129 | Dtor_Complete) |
| 130 | : CGM.getTypes().getFunctionInfo(MD)); |
| 131 | |
| 132 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 133 | const llvm::Type *Ty |
| 134 | = CGM.getTypes().GetFunctionType(FInfo, FPT->isVariadic()); |
| 135 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 136 | // C++ [class.virtual]p12: |
| 137 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 138 | // virtual call mechanism. |
| 139 | // |
| 140 | // We also don't emit a virtual call if the base expression has a record type |
| 141 | // because then we know what the type is. |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 142 | bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier() |
| 143 | && !canDevirtualizeMemberFunctionCalls(ME->getBase()); |
| 144 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 145 | llvm::Value *Callee; |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 146 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
| 147 | if (UseVirtualCall) { |
| 148 | Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 149 | } else { |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 150 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 151 | } |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 152 | } else if (UseVirtualCall) { |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 153 | Callee = BuildVirtualCall(MD, This, Ty); |
| 154 | } else { |
| 155 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
| 156 | } |
| 157 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 158 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 159 | CE->arg_begin(), CE->arg_end()); |
| 160 | } |
| 161 | |
| 162 | RValue |
| 163 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 164 | ReturnValueSlot ReturnValue) { |
| 165 | const BinaryOperator *BO = |
| 166 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 167 | const Expr *BaseExpr = BO->getLHS(); |
| 168 | const Expr *MemFnExpr = BO->getRHS(); |
| 169 | |
| 170 | const MemberPointerType *MPT = |
| 171 | MemFnExpr->getType()->getAs<MemberPointerType>(); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 172 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 173 | const FunctionProtoType *FPT = |
| 174 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 175 | const CXXRecordDecl *RD = |
| 176 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 177 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 178 | // Get the member function pointer. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 179 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 180 | |
| 181 | // Emit the 'this' pointer. |
| 182 | llvm::Value *This; |
| 183 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 184 | if (BO->getOpcode() == BO_PtrMemI) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 185 | This = EmitScalarExpr(BaseExpr); |
| 186 | else |
| 187 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 188 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 189 | // Ask the ABI to load the callee. Note that This is modified. |
| 190 | llvm::Value *Callee = |
| 191 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(CGF, This, MemFnPtr, MPT); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 192 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 193 | CallArgList Args; |
| 194 | |
| 195 | QualType ThisType = |
| 196 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 197 | |
| 198 | // Push the this ptr. |
| 199 | Args.push_back(std::make_pair(RValue::get(This), ThisType)); |
| 200 | |
| 201 | // And the rest of the call args |
| 202 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 203 | const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>(); |
| 204 | return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 205 | ReturnValue, Args); |
| 206 | } |
| 207 | |
| 208 | RValue |
| 209 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 210 | const CXXMethodDecl *MD, |
| 211 | ReturnValueSlot ReturnValue) { |
| 212 | assert(MD->isInstance() && |
| 213 | "Trying to emit a member call expr on a static method!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 214 | if (MD->isCopyAssignment()) { |
| 215 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); |
| 216 | if (ClassDecl->hasTrivialCopyAssignment()) { |
| 217 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 218 | "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); |
Fariborz Jahanian | 43a40f9 | 2010-05-10 22:57:35 +0000 | [diff] [blame] | 219 | LValue LV = EmitLValue(E->getArg(0)); |
| 220 | llvm::Value *This; |
Fariborz Jahanian | 61a3124 | 2010-09-01 19:36:41 +0000 | [diff] [blame] | 221 | if (LV.isPropertyRef() || LV.isKVCRef()) { |
Fariborz Jahanian | 1b8b8bf | 2010-05-16 00:10:46 +0000 | [diff] [blame] | 222 | llvm::Value *AggLoc = CreateMemTemp(E->getArg(1)->getType()); |
Fariborz Jahanian | e1b45a5 | 2010-05-15 23:05:52 +0000 | [diff] [blame] | 223 | EmitAggExpr(E->getArg(1), AggLoc, false /*VolatileDest*/); |
Fariborz Jahanian | 61a3124 | 2010-09-01 19:36:41 +0000 | [diff] [blame] | 224 | if (LV.isPropertyRef()) |
| 225 | EmitObjCPropertySet(LV.getPropertyRefExpr(), |
| 226 | RValue::getAggregate(AggLoc, |
| 227 | false /*VolatileDest*/)); |
| 228 | else |
| 229 | EmitObjCPropertySet(LV.getKVCRefExpr(), |
| 230 | RValue::getAggregate(AggLoc, |
| 231 | false /*VolatileDest*/)); |
Fariborz Jahanian | e1b45a5 | 2010-05-15 23:05:52 +0000 | [diff] [blame] | 232 | return RValue::getAggregate(0, false); |
Fariborz Jahanian | 43a40f9 | 2010-05-10 22:57:35 +0000 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | This = LV.getAddress(); |
| 236 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 237 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 238 | QualType Ty = E->getType(); |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 239 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 240 | return RValue::get(This); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 245 | const llvm::Type *Ty = |
| 246 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 247 | FPT->isVariadic()); |
Fariborz Jahanian | fdf474b | 2010-05-07 18:56:13 +0000 | [diff] [blame] | 248 | LValue LV = EmitLValue(E->getArg(0)); |
| 249 | llvm::Value *This; |
Fariborz Jahanian | 61a3124 | 2010-09-01 19:36:41 +0000 | [diff] [blame] | 250 | if (LV.isPropertyRef() || LV.isKVCRef()) { |
| 251 | QualType QT = E->getArg(0)->getType(); |
| 252 | RValue RV = |
| 253 | LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT) |
| 254 | : EmitLoadOfKVCRefLValue(LV, QT); |
Fariborz Jahanian | 6855ba2 | 2010-05-20 16:46:55 +0000 | [diff] [blame] | 255 | assert (!RV.isScalar() && "EmitCXXOperatorMemberCallExpr"); |
| 256 | This = RV.getAggregateAddr(); |
Fariborz Jahanian | fdf474b | 2010-05-07 18:56:13 +0000 | [diff] [blame] | 257 | } |
| 258 | else |
| 259 | This = LV.getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 260 | |
| 261 | llvm::Value *Callee; |
| 262 | if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0))) |
| 263 | Callee = BuildVirtualCall(MD, This, Ty); |
| 264 | else |
| 265 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
| 266 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 267 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 268 | E->arg_begin() + 1, E->arg_end()); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 273 | const CXXConstructExpr *E) { |
| 274 | assert(Dest && "Must have a destination!"); |
| 275 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 276 | |
| 277 | // If we require zero initialization before (or instead of) calling the |
| 278 | // constructor, as can be the case with a non-user-provided default |
| 279 | // constructor, emit the zero initialization now. |
| 280 | if (E->requiresZeroInitialization()) |
| 281 | EmitNullInitialization(Dest, E->getType()); |
| 282 | |
| 283 | |
| 284 | // If this is a call to a trivial default constructor, do nothing. |
| 285 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 286 | return; |
| 287 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 288 | // Code gen optimization to eliminate copy constructor and return |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 289 | // its first argument instead, if in fact that argument is a temporary |
| 290 | // object. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 291 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 292 | if (const Expr *Arg = E->getArg(0)->getTemporaryObject()) { |
| 293 | EmitAggExpr(Arg, Dest, false); |
| 294 | return; |
| 295 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 296 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 297 | |
| 298 | const ConstantArrayType *Array |
| 299 | = getContext().getAsConstantArrayType(E->getType()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 300 | if (Array) { |
| 301 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 302 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 303 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 304 | llvm::Value *BaseAddrPtr = |
Anders Carlsson | aab3b57 | 2010-05-01 17:02:18 +0000 | [diff] [blame] | 305 | Builder.CreateBitCast(Dest, BasePtr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 306 | |
| 307 | EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr, |
| 308 | E->arg_begin(), E->arg_end()); |
| 309 | } |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 310 | else { |
| 311 | CXXCtorType Type = |
| 312 | (E->getConstructionKind() == CXXConstructExpr::CK_Complete) |
| 313 | ? Ctor_Complete : Ctor_Base; |
| 314 | bool ForVirtualBase = |
| 315 | E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase; |
| 316 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 317 | // Call the constructor. |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 318 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 319 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 320 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 321 | } |
| 322 | |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 323 | /// Check whether the given operator new[] is the global placement |
| 324 | /// operator new[]. |
| 325 | static bool IsPlacementOperatorNewArray(ASTContext &Ctx, |
| 326 | const FunctionDecl *Fn) { |
| 327 | // Must be in global scope. Note that allocation functions can't be |
| 328 | // declared in namespaces. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 329 | if (!Fn->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 330 | return false; |
| 331 | |
| 332 | // Signature must be void *operator new[](size_t, void*). |
| 333 | // The size_t is common to all operator new[]s. |
| 334 | if (Fn->getNumParams() != 2) |
| 335 | return false; |
| 336 | |
| 337 | CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType()); |
| 338 | return (ParamType == Ctx.VoidPtrTy); |
| 339 | } |
| 340 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 341 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 342 | const CXXNewExpr *E) { |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 343 | if (!E->isArray()) |
Ken Dyck | 3eb55cf | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 344 | return CharUnits::Zero(); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 345 | |
Anders Carlsson | 399f499 | 2009-12-13 20:34:34 +0000 | [diff] [blame] | 346 | // No cookie is required if the new operator being used is |
| 347 | // ::operator new[](size_t, void*). |
| 348 | const FunctionDecl *OperatorNew = E->getOperatorNew(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 349 | if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew)) |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 350 | return CharUnits::Zero(); |
| 351 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 352 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E->getAllocatedType()); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 355 | static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context, |
Chris Lattner | cb46bdc | 2010-07-20 18:45:57 +0000 | [diff] [blame] | 356 | CodeGenFunction &CGF, |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 357 | const CXXNewExpr *E, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 358 | llvm::Value *&NumElements, |
| 359 | llvm::Value *&SizeWithoutCookie) { |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 360 | QualType ElemType = E->getAllocatedType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 361 | |
| 362 | const llvm::IntegerType *SizeTy = |
| 363 | cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType())); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 364 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 365 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType); |
| 366 | |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 367 | if (!E->isArray()) { |
| 368 | SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity()); |
| 369 | return SizeWithoutCookie; |
| 370 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 371 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 372 | // Figure out the cookie size. |
| 373 | CharUnits CookieSize = CalculateCookiePadding(CGF, E); |
| 374 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 375 | // Emit the array size expression. |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 376 | // We multiply the size of all dimensions for NumElements. |
| 377 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 378 | NumElements = CGF.EmitScalarExpr(E->getArraySize()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 379 | assert(NumElements->getType() == SizeTy && "element count not a size_t"); |
| 380 | |
| 381 | uint64_t ArraySizeMultiplier = 1; |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 382 | while (const ConstantArrayType *CAT |
| 383 | = CGF.getContext().getAsConstantArrayType(ElemType)) { |
| 384 | ElemType = CAT->getElementType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 385 | ArraySizeMultiplier *= CAT->getSize().getZExtValue(); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 386 | } |
| 387 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 388 | llvm::Value *Size; |
Chris Lattner | f2f3870 | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 390 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 391 | // Don't bloat the -O0 code. |
| 392 | if (llvm::ConstantInt *NumElementsC = |
| 393 | dyn_cast<llvm::ConstantInt>(NumElements)) { |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 394 | llvm::APInt NEC = NumElementsC->getValue(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 395 | unsigned SizeWidth = NEC.getBitWidth(); |
| 396 | |
| 397 | // Determine if there is an overflow here by doing an extended multiply. |
| 398 | NEC.zext(SizeWidth*2); |
| 399 | llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity()); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 400 | SC *= NEC; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 401 | |
| 402 | if (!CookieSize.isZero()) { |
| 403 | // Save the current size without a cookie. We don't care if an |
| 404 | // overflow's already happened because SizeWithoutCookie isn't |
| 405 | // used if the allocator returns null or throws, as it should |
| 406 | // always do on an overflow. |
| 407 | llvm::APInt SWC = SC; |
| 408 | SWC.trunc(SizeWidth); |
| 409 | SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC); |
| 410 | |
| 411 | // Add the cookie size. |
| 412 | SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity()); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 413 | } |
| 414 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 415 | if (SC.countLeadingZeros() >= SizeWidth) { |
| 416 | SC.trunc(SizeWidth); |
| 417 | Size = llvm::ConstantInt::get(SizeTy, SC); |
| 418 | } else { |
| 419 | // On overflow, produce a -1 so operator new throws. |
| 420 | Size = llvm::Constant::getAllOnesValue(SizeTy); |
| 421 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 422 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 423 | // Scale NumElements while we're at it. |
| 424 | uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier; |
| 425 | NumElements = llvm::ConstantInt::get(SizeTy, N); |
| 426 | |
| 427 | // Otherwise, we don't need to do an overflow-checked multiplication if |
| 428 | // we're multiplying by one. |
| 429 | } else if (TypeSize.isOne()) { |
| 430 | assert(ArraySizeMultiplier == 1); |
| 431 | |
| 432 | Size = NumElements; |
| 433 | |
| 434 | // If we need a cookie, add its size in with an overflow check. |
| 435 | // This is maybe a little paranoid. |
| 436 | if (!CookieSize.isZero()) { |
| 437 | SizeWithoutCookie = Size; |
| 438 | |
| 439 | llvm::Value *CookieSizeV |
| 440 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 441 | |
| 442 | const llvm::Type *Types[] = { SizeTy }; |
| 443 | llvm::Value *UAddF |
| 444 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1); |
| 445 | llvm::Value *AddRes |
| 446 | = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV); |
| 447 | |
| 448 | Size = CGF.Builder.CreateExtractValue(AddRes, 0); |
| 449 | llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1); |
| 450 | Size = CGF.Builder.CreateSelect(DidOverflow, |
| 451 | llvm::ConstantInt::get(SizeTy, -1), |
| 452 | Size); |
| 453 | } |
| 454 | |
| 455 | // Otherwise use the int.umul.with.overflow intrinsic. |
| 456 | } else { |
| 457 | llvm::Value *OutermostElementSize |
| 458 | = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity()); |
| 459 | |
| 460 | llvm::Value *NumOutermostElements = NumElements; |
| 461 | |
| 462 | // Scale NumElements by the array size multiplier. This might |
| 463 | // overflow, but only if the multiplication below also overflows, |
| 464 | // in which case this multiplication isn't used. |
| 465 | if (ArraySizeMultiplier != 1) |
| 466 | NumElements = CGF.Builder.CreateMul(NumElements, |
| 467 | llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier)); |
| 468 | |
| 469 | // The requested size of the outermost array is non-constant. |
| 470 | // Multiply that by the static size of the elements of that array; |
| 471 | // on unsigned overflow, set the size to -1 to trigger an |
| 472 | // exception from the allocation routine. This is sufficient to |
| 473 | // prevent buffer overruns from the allocator returning a |
| 474 | // seemingly valid pointer to insufficient space. This idea comes |
| 475 | // originally from MSVC, and GCC has an open bug requesting |
| 476 | // similar behavior: |
| 477 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351 |
| 478 | // |
| 479 | // This will not be sufficient for C++0x, which requires a |
| 480 | // specific exception class (std::bad_array_new_length). |
| 481 | // That will require ABI support that has not yet been specified. |
| 482 | const llvm::Type *Types[] = { SizeTy }; |
| 483 | llvm::Value *UMulF |
| 484 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1); |
| 485 | llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements, |
| 486 | OutermostElementSize); |
| 487 | |
| 488 | // The overflow bit. |
| 489 | llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1); |
| 490 | |
| 491 | // The result of the multiplication. |
| 492 | Size = CGF.Builder.CreateExtractValue(MulRes, 0); |
| 493 | |
| 494 | // If we have a cookie, we need to add that size in, too. |
| 495 | if (!CookieSize.isZero()) { |
| 496 | SizeWithoutCookie = Size; |
| 497 | |
| 498 | llvm::Value *CookieSizeV |
| 499 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 500 | llvm::Value *UAddF |
| 501 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1); |
| 502 | llvm::Value *AddRes |
| 503 | = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV); |
| 504 | |
| 505 | Size = CGF.Builder.CreateExtractValue(AddRes, 0); |
| 506 | |
| 507 | llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1); |
| 508 | DidOverflow = CGF.Builder.CreateAnd(DidOverflow, AddDidOverflow); |
| 509 | } |
| 510 | |
| 511 | Size = CGF.Builder.CreateSelect(DidOverflow, |
| 512 | llvm::ConstantInt::get(SizeTy, -1), |
| 513 | Size); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 514 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 515 | |
| 516 | if (CookieSize.isZero()) |
| 517 | SizeWithoutCookie = Size; |
| 518 | else |
| 519 | assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?"); |
| 520 | |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 521 | return Size; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 524 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 525 | llvm::Value *NewPtr) { |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 526 | |
| 527 | assert(E->getNumConstructorArgs() == 1 && |
| 528 | "Can only have one argument to initializer of POD type."); |
| 529 | |
| 530 | const Expr *Init = E->getConstructorArg(0); |
| 531 | QualType AllocType = E->getAllocatedType(); |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 532 | |
| 533 | unsigned Alignment = |
| 534 | CGF.getContext().getTypeAlignInChars(AllocType).getQuantity(); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 535 | if (!CGF.hasAggregateLLVMType(AllocType)) |
| 536 | CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr, |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 537 | AllocType.isVolatileQualified(), Alignment, |
| 538 | AllocType); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 539 | else if (AllocType->isAnyComplexType()) |
| 540 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 541 | AllocType.isVolatileQualified()); |
| 542 | else |
| 543 | CGF.EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
| 548 | llvm::Value *NewPtr, |
| 549 | llvm::Value *NumElements) { |
Fariborz Jahanian | b66b08e | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 550 | // We have a POD type. |
| 551 | if (E->getNumConstructorArgs() == 0) |
| 552 | return; |
| 553 | |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 554 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 555 | |
| 556 | // Create a temporary for the loop index and initialize it with 0. |
| 557 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 558 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 559 | Builder.CreateStore(Zero, IndexPtr); |
| 560 | |
| 561 | // Start the loop with a block that tests the condition. |
| 562 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 563 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 564 | |
| 565 | EmitBlock(CondBlock); |
| 566 | |
| 567 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 568 | |
| 569 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 570 | // otherwise, go to the block after the for-loop. |
| 571 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 572 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 573 | // If the condition is true, execute the body. |
| 574 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 575 | |
| 576 | EmitBlock(ForBody); |
| 577 | |
| 578 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 579 | // Inside the loop body, emit the constructor call on the array element. |
| 580 | Counter = Builder.CreateLoad(IndexPtr); |
| 581 | llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter, |
| 582 | "arrayidx"); |
| 583 | StoreAnyExprIntoOneUnit(*this, E, Address); |
| 584 | |
| 585 | EmitBlock(ContinueBlock); |
| 586 | |
| 587 | // Emit the increment of the loop counter. |
| 588 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 589 | Counter = Builder.CreateLoad(IndexPtr); |
| 590 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 591 | Builder.CreateStore(NextVal, IndexPtr); |
| 592 | |
| 593 | // Finally, branch back up to the condition for the next iteration. |
| 594 | EmitBranch(CondBlock); |
| 595 | |
| 596 | // Emit the fall-through block. |
| 597 | EmitBlock(AfterFor, true); |
| 598 | } |
| 599 | |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 600 | static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, |
| 601 | llvm::Value *NewPtr, llvm::Value *Size) { |
| 602 | llvm::LLVMContext &VMContext = CGF.CGM.getLLVMContext(); |
| 603 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); |
| 604 | if (NewPtr->getType() != BP) |
| 605 | NewPtr = CGF.Builder.CreateBitCast(NewPtr, BP, "tmp"); |
| 606 | |
| 607 | CGF.Builder.CreateCall5(CGF.CGM.getMemSetFn(BP, CGF.IntPtrTy), NewPtr, |
| 608 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
| 609 | Size, |
| 610 | llvm::ConstantInt::get(CGF.Int32Ty, |
| 611 | CGF.getContext().getTypeAlign(T)/8), |
| 612 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), |
| 613 | 0)); |
| 614 | } |
| 615 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 616 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 617 | llvm::Value *NewPtr, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 618 | llvm::Value *NumElements, |
| 619 | llvm::Value *AllocSizeWithoutCookie) { |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 620 | if (E->isArray()) { |
Anders Carlsson | d040e6b | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 621 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 622 | bool RequiresZeroInitialization = false; |
| 623 | if (Ctor->getParent()->hasTrivialConstructor()) { |
| 624 | // If new expression did not specify value-initialization, then there |
| 625 | // is no initialization. |
| 626 | if (!E->hasInitializer() || Ctor->getParent()->isEmpty()) |
| 627 | return; |
| 628 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 629 | if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 630 | // Optimization: since zero initialization will just set the memory |
| 631 | // to all zeroes, generate a single memset to do it in one shot. |
| 632 | EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr, |
| 633 | AllocSizeWithoutCookie); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | RequiresZeroInitialization = true; |
| 638 | } |
| 639 | |
| 640 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 641 | E->constructor_arg_begin(), |
| 642 | E->constructor_arg_end(), |
| 643 | RequiresZeroInitialization); |
Anders Carlsson | d040e6b | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 644 | return; |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 645 | } else if (E->getNumConstructorArgs() == 1 && |
| 646 | isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) { |
| 647 | // Optimization: since zero initialization will just set the memory |
| 648 | // to all zeroes, generate a single memset to do it in one shot. |
| 649 | EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr, |
| 650 | AllocSizeWithoutCookie); |
| 651 | return; |
| 652 | } else { |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 653 | CGF.EmitNewArrayInitializer(E, NewPtr, NumElements); |
| 654 | return; |
| 655 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 656 | } |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 657 | |
| 658 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 659 | // Per C++ [expr.new]p15, if we have an initializer, then we're performing |
| 660 | // direct initialization. C++ [dcl.init]p5 requires that we |
| 661 | // zero-initialize storage if there are no user-declared constructors. |
| 662 | if (E->hasInitializer() && |
| 663 | !Ctor->getParent()->hasUserDeclaredConstructor() && |
| 664 | !Ctor->getParent()->isEmpty()) |
| 665 | CGF.EmitNullInitialization(NewPtr, E->getAllocatedType()); |
| 666 | |
Douglas Gregor | e182370 | 2010-07-07 23:37:33 +0000 | [diff] [blame] | 667 | CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
| 668 | NewPtr, E->constructor_arg_begin(), |
| 669 | E->constructor_arg_end()); |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 670 | |
| 671 | return; |
| 672 | } |
Fariborz Jahanian | b66b08e | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 673 | // We have a POD type. |
| 674 | if (E->getNumConstructorArgs() == 0) |
| 675 | return; |
| 676 | |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 677 | StoreAnyExprIntoOneUnit(CGF, E, NewPtr); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 678 | } |
| 679 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 680 | namespace { |
| 681 | /// A cleanup to call the given 'operator delete' function upon |
| 682 | /// abnormal exit from a new expression. |
| 683 | class CallDeleteDuringNew : public EHScopeStack::Cleanup { |
| 684 | size_t NumPlacementArgs; |
| 685 | const FunctionDecl *OperatorDelete; |
| 686 | llvm::Value *Ptr; |
| 687 | llvm::Value *AllocSize; |
| 688 | |
| 689 | RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } |
| 690 | |
| 691 | public: |
| 692 | static size_t getExtraSize(size_t NumPlacementArgs) { |
| 693 | return NumPlacementArgs * sizeof(RValue); |
| 694 | } |
| 695 | |
| 696 | CallDeleteDuringNew(size_t NumPlacementArgs, |
| 697 | const FunctionDecl *OperatorDelete, |
| 698 | llvm::Value *Ptr, |
| 699 | llvm::Value *AllocSize) |
| 700 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 701 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 702 | |
| 703 | void setPlacementArg(unsigned I, RValue Arg) { |
| 704 | assert(I < NumPlacementArgs && "index out of range"); |
| 705 | getPlacementArgs()[I] = Arg; |
| 706 | } |
| 707 | |
| 708 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 709 | const FunctionProtoType *FPT |
| 710 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 711 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
John McCall | d441b1e | 2010-09-14 21:45:42 +0000 | [diff] [blame^] | 712 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 713 | |
| 714 | CallArgList DeleteArgs; |
| 715 | |
| 716 | // The first argument is always a void*. |
| 717 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
| 718 | DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++)); |
| 719 | |
| 720 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 721 | if (FPT->getNumArgs() == NumPlacementArgs + 2) |
| 722 | DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++)); |
| 723 | |
| 724 | // Pass the rest of the arguments, which must match exactly. |
| 725 | for (unsigned I = 0; I != NumPlacementArgs; ++I) |
| 726 | DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++)); |
| 727 | |
| 728 | // Call 'operator delete'. |
| 729 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), |
| 730 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 731 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 732 | } |
| 733 | }; |
| 734 | } |
| 735 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 736 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 737 | QualType AllocType = E->getAllocatedType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 738 | if (AllocType->isArrayType()) |
| 739 | while (const ArrayType *AType = getContext().getAsArrayType(AllocType)) |
| 740 | AllocType = AType->getElementType(); |
| 741 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 742 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 743 | const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>(); |
| 744 | |
| 745 | CallArgList NewArgs; |
| 746 | |
| 747 | // The allocation size is the first argument. |
| 748 | QualType SizeTy = getContext().getSizeType(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 749 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 750 | llvm::Value *NumElements = 0; |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 751 | llvm::Value *AllocSizeWithoutCookie = 0; |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 752 | llvm::Value *AllocSize = EmitCXXNewAllocSize(getContext(), |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 753 | *this, E, NumElements, |
| 754 | AllocSizeWithoutCookie); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 755 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 756 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 757 | |
| 758 | // Emit the rest of the arguments. |
| 759 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 760 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 761 | |
| 762 | // First, use the types from the function type. |
| 763 | // We start at 1 here because the first argument (the allocation size) |
| 764 | // has already been emitted. |
| 765 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 766 | QualType ArgType = NewFTy->getArgType(i); |
| 767 | |
| 768 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 769 | getTypePtr() == |
| 770 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 771 | "type mismatch in call argument!"); |
| 772 | |
| 773 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 774 | ArgType)); |
| 775 | |
| 776 | } |
| 777 | |
| 778 | // Either we've emitted all the call args, or we have a call to a |
| 779 | // variadic function. |
| 780 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 781 | "Extra arguments in non-variadic function!"); |
| 782 | |
| 783 | // If we still have any arguments, emit them using the type of the argument. |
| 784 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 785 | NewArg != NewArgEnd; ++NewArg) { |
| 786 | QualType ArgType = NewArg->getType(); |
| 787 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 788 | ArgType)); |
| 789 | } |
| 790 | |
| 791 | // Emit the call to new. |
| 792 | RValue RV = |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 793 | EmitCall(CGM.getTypes().getFunctionInfo(NewArgs, NewFTy), |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 794 | CGM.GetAddrOfFunction(NewFD), ReturnValueSlot(), NewArgs, NewFD); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 795 | |
| 796 | // If an allocation function is declared with an empty exception specification |
| 797 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 798 | // (We don't need to check for null when there's no new initializer and |
| 799 | // we're allocating a POD type). |
| 800 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 801 | !(AllocType->isPODType() && !E->hasInitializer()); |
| 802 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 803 | llvm::BasicBlock *NullCheckSource = 0; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 804 | llvm::BasicBlock *NewNotNull = 0; |
| 805 | llvm::BasicBlock *NewEnd = 0; |
| 806 | |
| 807 | llvm::Value *NewPtr = RV.getScalarVal(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 808 | unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 809 | |
| 810 | if (NullCheckResult) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 811 | NullCheckSource = Builder.GetInsertBlock(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 812 | NewNotNull = createBasicBlock("new.notnull"); |
| 813 | NewEnd = createBasicBlock("new.end"); |
| 814 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 815 | llvm::Value *IsNull = Builder.CreateIsNull(NewPtr, "new.isnull"); |
| 816 | Builder.CreateCondBr(IsNull, NewEnd, NewNotNull); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 817 | EmitBlock(NewNotNull); |
| 818 | } |
Ken Dyck | 3eb55cf | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 819 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 820 | assert((AllocSize == AllocSizeWithoutCookie) == |
| 821 | CalculateCookiePadding(*this, E).isZero()); |
| 822 | if (AllocSize != AllocSizeWithoutCookie) { |
| 823 | assert(E->isArray()); |
| 824 | NewPtr = CGM.getCXXABI().InitializeArrayCookie(CGF, NewPtr, NumElements, |
| 825 | AllocType); |
| 826 | } |
Anders Carlsson | f771681 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 827 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 828 | // If there's an operator delete, enter a cleanup to call it if an |
| 829 | // exception is thrown. |
| 830 | EHScopeStack::stable_iterator CallOperatorDelete; |
| 831 | if (E->getOperatorDelete()) { |
| 832 | CallDeleteDuringNew *Cleanup = CGF.EHStack |
| 833 | .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, |
| 834 | E->getNumPlacementArgs(), |
| 835 | E->getOperatorDelete(), |
| 836 | NewPtr, AllocSize); |
| 837 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
| 838 | Cleanup->setPlacementArg(I, NewArgs[I+1].first); |
| 839 | CallOperatorDelete = EHStack.stable_begin(); |
| 840 | } |
| 841 | |
Douglas Gregor | 040ad50 | 2010-09-02 23:24:14 +0000 | [diff] [blame] | 842 | const llvm::Type *ElementPtrTy |
| 843 | = ConvertTypeForMem(AllocType)->getPointerTo(AS); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 844 | NewPtr = Builder.CreateBitCast(NewPtr, ElementPtrTy); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 845 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 846 | if (E->isArray()) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 847 | EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 848 | |
| 849 | // NewPtr is a pointer to the base element type. If we're |
| 850 | // allocating an array of arrays, we'll need to cast back to the |
| 851 | // array pointer type. |
Douglas Gregor | 040ad50 | 2010-09-02 23:24:14 +0000 | [diff] [blame] | 852 | const llvm::Type *ResultTy = ConvertTypeForMem(E->getType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 853 | if (NewPtr->getType() != ResultTy) |
| 854 | NewPtr = Builder.CreateBitCast(NewPtr, ResultTy); |
| 855 | } else { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 856 | EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 857 | } |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 858 | |
| 859 | // Deactivate the 'operator delete' cleanup if we finished |
| 860 | // initialization. |
| 861 | if (CallOperatorDelete.isValid()) |
| 862 | DeactivateCleanupBlock(CallOperatorDelete); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 863 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 864 | if (NullCheckResult) { |
| 865 | Builder.CreateBr(NewEnd); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 866 | llvm::BasicBlock *NotNullSource = Builder.GetInsertBlock(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 867 | EmitBlock(NewEnd); |
| 868 | |
| 869 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 870 | PHI->reserveOperandSpace(2); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 871 | PHI->addIncoming(NewPtr, NotNullSource); |
| 872 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), |
| 873 | NullCheckSource); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 874 | |
| 875 | NewPtr = PHI; |
| 876 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 877 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 878 | return NewPtr; |
| 879 | } |
| 880 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 881 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 882 | llvm::Value *Ptr, |
| 883 | QualType DeleteTy) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 884 | assert(DeleteFD->getOverloadedOperator() == OO_Delete); |
| 885 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 886 | const FunctionProtoType *DeleteFTy = |
| 887 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 888 | |
| 889 | CallArgList DeleteArgs; |
| 890 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 891 | // Check if we need to pass the size to the delete operator. |
| 892 | llvm::Value *Size = 0; |
| 893 | QualType SizeTy; |
| 894 | if (DeleteFTy->getNumArgs() == 2) { |
| 895 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 7df3cbe | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 896 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 897 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 898 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 901 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 902 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
| 903 | DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); |
| 904 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 905 | if (Size) |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 906 | DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy)); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 907 | |
| 908 | // Emit the call to delete. |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 909 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy), |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 910 | CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 911 | DeleteArgs, DeleteFD); |
| 912 | } |
| 913 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 914 | namespace { |
| 915 | /// Calls the given 'operator delete' on a single object. |
| 916 | struct CallObjectDelete : EHScopeStack::Cleanup { |
| 917 | llvm::Value *Ptr; |
| 918 | const FunctionDecl *OperatorDelete; |
| 919 | QualType ElementType; |
| 920 | |
| 921 | CallObjectDelete(llvm::Value *Ptr, |
| 922 | const FunctionDecl *OperatorDelete, |
| 923 | QualType ElementType) |
| 924 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 925 | |
| 926 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 927 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 928 | } |
| 929 | }; |
| 930 | } |
| 931 | |
| 932 | /// Emit the code for deleting a single object. |
| 933 | static void EmitObjectDelete(CodeGenFunction &CGF, |
| 934 | const FunctionDecl *OperatorDelete, |
| 935 | llvm::Value *Ptr, |
| 936 | QualType ElementType) { |
| 937 | // Find the destructor for the type, if applicable. If the |
| 938 | // destructor is virtual, we'll just emit the vcall and return. |
| 939 | const CXXDestructorDecl *Dtor = 0; |
| 940 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 941 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 942 | if (!RD->hasTrivialDestructor()) { |
| 943 | Dtor = RD->getDestructor(); |
| 944 | |
| 945 | if (Dtor->isVirtual()) { |
| 946 | const llvm::Type *Ty = |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 947 | CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor, |
| 948 | Dtor_Complete), |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 949 | /*isVariadic=*/false); |
| 950 | |
| 951 | llvm::Value *Callee |
| 952 | = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty); |
| 953 | CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0, |
| 954 | 0, 0); |
| 955 | |
| 956 | // The dtor took care of deleting the object. |
| 957 | return; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | // Make sure that we call delete even if the dtor throws. |
| 963 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 964 | Ptr, OperatorDelete, ElementType); |
| 965 | |
| 966 | if (Dtor) |
| 967 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 968 | /*ForVirtualBase=*/false, Ptr); |
| 969 | |
| 970 | CGF.PopCleanupBlock(); |
| 971 | } |
| 972 | |
| 973 | namespace { |
| 974 | /// Calls the given 'operator delete' on an array of objects. |
| 975 | struct CallArrayDelete : EHScopeStack::Cleanup { |
| 976 | llvm::Value *Ptr; |
| 977 | const FunctionDecl *OperatorDelete; |
| 978 | llvm::Value *NumElements; |
| 979 | QualType ElementType; |
| 980 | CharUnits CookieSize; |
| 981 | |
| 982 | CallArrayDelete(llvm::Value *Ptr, |
| 983 | const FunctionDecl *OperatorDelete, |
| 984 | llvm::Value *NumElements, |
| 985 | QualType ElementType, |
| 986 | CharUnits CookieSize) |
| 987 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 988 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 989 | |
| 990 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 991 | const FunctionProtoType *DeleteFTy = |
| 992 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 993 | assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); |
| 994 | |
| 995 | CallArgList Args; |
| 996 | |
| 997 | // Pass the pointer as the first argument. |
| 998 | QualType VoidPtrTy = DeleteFTy->getArgType(0); |
| 999 | llvm::Value *DeletePtr |
| 1000 | = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); |
| 1001 | Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy)); |
| 1002 | |
| 1003 | // Pass the original requested size as the second argument. |
| 1004 | if (DeleteFTy->getNumArgs() == 2) { |
| 1005 | QualType size_t = DeleteFTy->getArgType(1); |
| 1006 | const llvm::IntegerType *SizeTy |
| 1007 | = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); |
| 1008 | |
| 1009 | CharUnits ElementTypeSize = |
| 1010 | CGF.CGM.getContext().getTypeSizeInChars(ElementType); |
| 1011 | |
| 1012 | // The size of an element, multiplied by the number of elements. |
| 1013 | llvm::Value *Size |
| 1014 | = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); |
| 1015 | Size = CGF.Builder.CreateMul(Size, NumElements); |
| 1016 | |
| 1017 | // Plus the size of the cookie if applicable. |
| 1018 | if (!CookieSize.isZero()) { |
| 1019 | llvm::Value *CookieSizeV |
| 1020 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 1021 | Size = CGF.Builder.CreateAdd(Size, CookieSizeV); |
| 1022 | } |
| 1023 | |
| 1024 | Args.push_back(std::make_pair(RValue::get(Size), size_t)); |
| 1025 | } |
| 1026 | |
| 1027 | // Emit the call to delete. |
| 1028 | CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy), |
| 1029 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1030 | ReturnValueSlot(), Args, OperatorDelete); |
| 1031 | } |
| 1032 | }; |
| 1033 | } |
| 1034 | |
| 1035 | /// Emit the code for deleting an array of objects. |
| 1036 | static void EmitArrayDelete(CodeGenFunction &CGF, |
| 1037 | const FunctionDecl *OperatorDelete, |
| 1038 | llvm::Value *Ptr, |
| 1039 | QualType ElementType) { |
| 1040 | llvm::Value *NumElements = 0; |
| 1041 | llvm::Value *AllocatedPtr = 0; |
| 1042 | CharUnits CookieSize; |
| 1043 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, ElementType, |
| 1044 | NumElements, AllocatedPtr, CookieSize); |
| 1045 | |
| 1046 | assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr"); |
| 1047 | |
| 1048 | // Make sure that we call delete even if one of the dtors throws. |
| 1049 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
| 1050 | AllocatedPtr, OperatorDelete, |
| 1051 | NumElements, ElementType, |
| 1052 | CookieSize); |
| 1053 | |
| 1054 | if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) { |
| 1055 | if (!RD->hasTrivialDestructor()) { |
| 1056 | assert(NumElements && "ReadArrayCookie didn't find element count" |
| 1057 | " for a class with destructor"); |
| 1058 | CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | CGF.PopCleanupBlock(); |
| 1063 | } |
| 1064 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1065 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Fariborz Jahanian | 6814eaa | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1067 | // Get at the argument before we performed the implicit conversion |
| 1068 | // to void*. |
| 1069 | const Expr *Arg = E->getArgument(); |
| 1070 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1071 | if (ICE->getCastKind() != CK_UserDefinedConversion && |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1072 | ICE->getType()->isVoidPointerType()) |
| 1073 | Arg = ICE->getSubExpr(); |
Douglas Gregor | e364e7b | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 1074 | else |
| 1075 | break; |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1076 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1078 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Null check the pointer. |
| 1081 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1082 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1083 | |
| 1084 | llvm::Value *IsNull = |
| 1085 | Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()), |
| 1086 | "isnull"); |
| 1087 | |
| 1088 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1089 | EmitBlock(DeleteNotNull); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1090 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1091 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1092 | // first non-array element. |
| 1093 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1094 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1095 | if (DeleteTy->isConstantArrayType()) { |
| 1096 | llvm::Value *Zero = Builder.getInt32(0); |
| 1097 | llvm::SmallVector<llvm::Value*,8> GEP; |
| 1098 | |
| 1099 | GEP.push_back(Zero); // point at the outermost array |
| 1100 | |
| 1101 | // For each layer of array type we're pointing at: |
| 1102 | while (const ConstantArrayType *Arr |
| 1103 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1104 | // 1. Unpeel the array type. |
| 1105 | DeleteTy = Arr->getElementType(); |
| 1106 | |
| 1107 | // 2. GEP to the first element of the array. |
| 1108 | GEP.push_back(Zero); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1109 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1110 | |
| 1111 | Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Douglas Gregor | 04f3621 | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 1114 | assert(ConvertTypeForMem(DeleteTy) == |
| 1115 | cast<llvm::PointerType>(Ptr->getType())->getElementType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (E->isArrayForm()) { |
| 1118 | EmitArrayDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy); |
| 1119 | } else { |
| 1120 | EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy); |
| 1121 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1122 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1123 | EmitBlock(DeleteEnd); |
| 1124 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1125 | |
| 1126 | llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 1127 | QualType Ty = E->getType(); |
| 1128 | const llvm::Type *LTy = ConvertType(Ty)->getPointerTo(); |
Anders Carlsson | fd7dfeb | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 1129 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1130 | if (E->isTypeOperand()) { |
| 1131 | llvm::Constant *TypeInfo = |
| 1132 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); |
| 1133 | return Builder.CreateBitCast(TypeInfo, LTy); |
| 1134 | } |
| 1135 | |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1136 | Expr *subE = E->getExprOperand(); |
Mike Stump | 6fdfea6 | 2009-11-17 22:33:00 +0000 | [diff] [blame] | 1137 | Ty = subE->getType(); |
| 1138 | CanQualType CanTy = CGM.getContext().getCanonicalType(Ty); |
| 1139 | Ty = CanTy.getUnqualifiedType().getNonReferenceType(); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1140 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1141 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1142 | if (RD->isPolymorphic()) { |
| 1143 | // FIXME: if subE is an lvalue do |
| 1144 | LValue Obj = EmitLValue(subE); |
| 1145 | llvm::Value *This = Obj.getAddress(); |
Mike Stump | 1bf924b | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1146 | LTy = LTy->getPointerTo()->getPointerTo(); |
| 1147 | llvm::Value *V = Builder.CreateBitCast(This, LTy); |
| 1148 | // We need to do a zero check for *p, unless it has NonNullAttr. |
| 1149 | // FIXME: PointerType->hasAttr<NonNullAttr>() |
| 1150 | bool CanBeZero = false; |
Mike Stump | c2c0334 | 2009-11-17 00:45:21 +0000 | [diff] [blame] | 1151 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens())) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1152 | if (UO->getOpcode() == UO_Deref) |
Mike Stump | 1bf924b | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1153 | CanBeZero = true; |
| 1154 | if (CanBeZero) { |
| 1155 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 1156 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 1157 | |
| 1158 | llvm::Value *Zero = llvm::Constant::getNullValue(LTy); |
| 1159 | Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero), |
| 1160 | NonZeroBlock, ZeroBlock); |
| 1161 | EmitBlock(ZeroBlock); |
| 1162 | /// Call __cxa_bad_typeid |
| 1163 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
| 1164 | const llvm::FunctionType *FTy; |
| 1165 | FTy = llvm::FunctionType::get(ResultType, false); |
| 1166 | llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1167 | Builder.CreateCall(F)->setDoesNotReturn(); |
Mike Stump | 1bf924b | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1168 | Builder.CreateUnreachable(); |
| 1169 | EmitBlock(NonZeroBlock); |
| 1170 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1171 | V = Builder.CreateLoad(V, "vtable"); |
| 1172 | V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL); |
| 1173 | V = Builder.CreateLoad(V); |
| 1174 | return V; |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1175 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1176 | } |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1177 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1178 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1179 | |
| 1180 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V, |
| 1181 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1182 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 1183 | QualType DestTy = DCE->getTypeAsWritten(); |
| 1184 | QualType InnerType = DestTy->getPointeeType(); |
| 1185 | |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1186 | const llvm::Type *LTy = ConvertType(DCE->getType()); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1187 | |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1188 | bool CanBeZero = false; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1189 | bool ToVoid = false; |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1190 | bool ThrowOnBad = false; |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1191 | if (DestTy->isPointerType()) { |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1192 | // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this |
| 1193 | CanBeZero = true; |
| 1194 | if (InnerType->isVoidType()) |
| 1195 | ToVoid = true; |
| 1196 | } else { |
| 1197 | LTy = LTy->getPointerTo(); |
Douglas Gregor | fa8b495 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 1198 | |
| 1199 | // FIXME: What if exceptions are disabled? |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1200 | ThrowOnBad = true; |
| 1201 | } |
| 1202 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1203 | if (SrcTy->isPointerType() || SrcTy->isReferenceType()) |
| 1204 | SrcTy = SrcTy->getPointeeType(); |
| 1205 | SrcTy = SrcTy.getUnqualifiedType(); |
| 1206 | |
Anders Carlsson | 0087bc8 | 2009-12-18 14:55:04 +0000 | [diff] [blame] | 1207 | if (DestTy->isPointerType() || DestTy->isReferenceType()) |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1208 | DestTy = DestTy->getPointeeType(); |
| 1209 | DestTy = DestTy.getUnqualifiedType(); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1210 | |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1211 | llvm::BasicBlock *ContBlock = createBasicBlock(); |
| 1212 | llvm::BasicBlock *NullBlock = 0; |
| 1213 | llvm::BasicBlock *NonZeroBlock = 0; |
| 1214 | if (CanBeZero) { |
| 1215 | NonZeroBlock = createBasicBlock(); |
| 1216 | NullBlock = createBasicBlock(); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1217 | Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1218 | EmitBlock(NonZeroBlock); |
| 1219 | } |
| 1220 | |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1221 | llvm::BasicBlock *BadCastBlock = 0; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1222 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1223 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1224 | |
| 1225 | // See if this is a dynamic_cast(void*) |
| 1226 | if (ToVoid) { |
| 1227 | llvm::Value *This = V; |
| 1228 | V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo()); |
| 1229 | V = Builder.CreateLoad(V, "vtable"); |
| 1230 | V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL); |
| 1231 | V = Builder.CreateLoad(V, "offset to top"); |
| 1232 | This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext)); |
| 1233 | V = Builder.CreateInBoundsGEP(This, V); |
| 1234 | V = Builder.CreateBitCast(V, LTy); |
| 1235 | } else { |
| 1236 | /// Call __dynamic_cast |
| 1237 | const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext); |
| 1238 | const llvm::FunctionType *FTy; |
| 1239 | std::vector<const llvm::Type*> ArgTys; |
| 1240 | const llvm::Type *PtrToInt8Ty |
| 1241 | = llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1242 | ArgTys.push_back(PtrToInt8Ty); |
| 1243 | ArgTys.push_back(PtrToInt8Ty); |
| 1244 | ArgTys.push_back(PtrToInt8Ty); |
| 1245 | ArgTys.push_back(PtrDiffTy); |
| 1246 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1247 | |
| 1248 | // FIXME: Calculate better hint. |
| 1249 | llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1250 | |
| 1251 | assert(SrcTy->isRecordType() && "Src type must be record type!"); |
| 1252 | assert(DestTy->isRecordType() && "Dest type must be record type!"); |
| 1253 | |
Douglas Gregor | 247894b | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 1254 | llvm::Value *SrcArg |
| 1255 | = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType()); |
| 1256 | llvm::Value *DestArg |
| 1257 | = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType()); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1258 | |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1259 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 1260 | V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"), |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1261 | V, SrcArg, DestArg, hint); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1262 | V = Builder.CreateBitCast(V, LTy); |
| 1263 | |
| 1264 | if (ThrowOnBad) { |
| 1265 | BadCastBlock = createBasicBlock(); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1266 | Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1267 | EmitBlock(BadCastBlock); |
Douglas Gregor | fa8b495 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 1268 | /// Invoke __cxa_bad_cast |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1269 | ResultType = llvm::Type::getVoidTy(VMContext); |
| 1270 | const llvm::FunctionType *FBadTy; |
Mike Stump | 3afea1d | 2009-11-17 03:01:03 +0000 | [diff] [blame] | 1271 | FBadTy = llvm::FunctionType::get(ResultType, false); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1272 | llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast"); |
Douglas Gregor | fa8b495 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 1273 | if (llvm::BasicBlock *InvokeDest = getInvokeDest()) { |
| 1274 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 1275 | Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn(); |
| 1276 | EmitBlock(Cont); |
| 1277 | } else { |
| 1278 | // FIXME: Does this ever make sense? |
| 1279 | Builder.CreateCall(F)->setDoesNotReturn(); |
| 1280 | } |
Mike Stump | e8cdcc9 | 2009-11-17 00:08:50 +0000 | [diff] [blame] | 1281 | Builder.CreateUnreachable(); |
Mike Stump | 6ca0e21 | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1282 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | if (CanBeZero) { |
| 1286 | Builder.CreateBr(ContBlock); |
| 1287 | EmitBlock(NullBlock); |
| 1288 | Builder.CreateBr(ContBlock); |
| 1289 | } |
| 1290 | EmitBlock(ContBlock); |
| 1291 | if (CanBeZero) { |
| 1292 | llvm::PHINode *PHI = Builder.CreatePHI(LTy); |
Mike Stump | 4d0e909 | 2009-11-17 00:10:05 +0000 | [diff] [blame] | 1293 | PHI->reserveOperandSpace(2); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1294 | PHI->addIncoming(V, NonZeroBlock); |
| 1295 | PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1296 | V = PHI; |
| 1297 | } |
| 1298 | |
| 1299 | return V; |
| 1300 | } |