Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | 16d81b8 | 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 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Peter Collingbourne | 6c0aa5f | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 16 | #include "CGCUDARuntime.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Fariborz Jahanian | 842ddd0 | 2010-05-20 21:38:57 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 19 | #include "CGDebugInfo.h" |
Chris Lattner | 6c552c1 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Anders Carlsson | ad3692bb | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
| 22 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 26 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 27 | llvm::Value *Callee, |
| 28 | ReturnValueSlot ReturnValue, |
| 29 | llvm::Value *This, |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 30 | llvm::Value *VTT, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 31 | CallExpr::const_arg_iterator ArgBeg, |
| 32 | CallExpr::const_arg_iterator ArgEnd) { |
| 33 | assert(MD->isInstance() && |
| 34 | "Trying to emit a member call expr on a static method!"); |
| 35 | |
| 36 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 37 | |
| 38 | CallArgList Args; |
| 39 | |
| 40 | // Push the this ptr. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 41 | Args.add(RValue::get(This), MD->getThisType(getContext())); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 43 | // If there is a VTT parameter, emit it. |
| 44 | if (VTT) { |
| 45 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 46 | Args.add(RValue::get(VTT), T); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 49 | // And the rest of the call args |
| 50 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 51 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 52 | QualType ResultType = FPT->getResultType(); |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 53 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 54 | FPT->getExtInfo()), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 55 | Callee, ReturnValue, Args, MD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 58 | static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { |
Anders Carlsson | 268ab8c | 2011-01-29 05:04:11 +0000 | [diff] [blame] | 59 | const Expr *E = Base; |
| 60 | |
| 61 | while (true) { |
| 62 | E = E->IgnoreParens(); |
| 63 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 64 | if (CE->getCastKind() == CK_DerivedToBase || |
| 65 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 66 | CE->getCastKind() == CK_NoOp) { |
| 67 | E = CE->getSubExpr(); |
| 68 | continue; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | QualType DerivedType = E->getType(); |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 76 | if (const PointerType *PTy = DerivedType->getAs<PointerType>()) |
| 77 | DerivedType = PTy->getPointeeType(); |
| 78 | |
| 79 | return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl()); |
| 80 | } |
| 81 | |
Anders Carlsson | cd0b32e | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 82 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 83 | // quite what we want. |
| 84 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 85 | while (true) { |
| 86 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 87 | E = PE->getSubExpr(); |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 92 | if (CE->getCastKind() == CK_NoOp) { |
| 93 | E = CE->getSubExpr(); |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 98 | if (UO->getOpcode() == UO_Extension) { |
| 99 | E = UO->getSubExpr(); |
| 100 | continue; |
| 101 | } |
| 102 | } |
| 103 | return E; |
| 104 | } |
| 105 | } |
| 106 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 107 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 108 | /// expr can be devirtualized. |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 109 | static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, |
| 110 | const Expr *Base, |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 111 | const CXXMethodDecl *MD) { |
| 112 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 113 | // When building with -fapple-kext, all calls must go through the vtable since |
| 114 | // the kernel linker can do runtime patching of vtables. |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 115 | if (Context.getLangOptions().AppleKext) |
| 116 | return false; |
| 117 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 118 | // If the most derived class is marked final, we know that no subclass can |
| 119 | // override this member function and so we can devirtualize it. For example: |
| 120 | // |
| 121 | // struct A { virtual void f(); } |
| 122 | // struct B final : A { }; |
| 123 | // |
| 124 | // void f(B *b) { |
| 125 | // b->f(); |
| 126 | // } |
| 127 | // |
| 128 | const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base); |
| 129 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 130 | return true; |
| 131 | |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 132 | // If the member function is marked 'final', we know that it can't be |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 133 | // overridden and can therefore devirtualize it. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 134 | if (MD->hasAttr<FinalAttr>()) |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 135 | return true; |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 137 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 138 | // and we can therefore devirtualize the member function call. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 139 | if (MD->getParent()->hasAttr<FinalAttr>()) |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 140 | return true; |
| 141 | |
Anders Carlsson | cd0b32e | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 142 | Base = skipNoOpCastsAndParens(Base); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 143 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 144 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 145 | // This is a record decl. We know the type and can devirtualize it. |
| 146 | return VD->getType()->isRecordType(); |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // We can always devirtualize calls on temporary object expressions. |
Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 153 | if (isa<CXXConstructExpr>(Base)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 154 | return true; |
| 155 | |
| 156 | // And calls on bound temporaries. |
| 157 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 158 | return true; |
| 159 | |
| 160 | // Check if this is a call expr that returns a record type. |
| 161 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 162 | return CE->getCallReturnType()->isRecordType(); |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 164 | // We can't devirtualize the call. |
| 165 | return false; |
| 166 | } |
| 167 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 168 | // Note: This function also emit constructor calls to support a MSVC |
| 169 | // extensions allowing explicit constructor function call. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 170 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 171 | ReturnValueSlot ReturnValue) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 172 | const Expr *callee = CE->getCallee()->IgnoreParens(); |
| 173 | |
| 174 | if (isa<BinaryOperator>(callee)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 175 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 176 | |
| 177 | const MemberExpr *ME = cast<MemberExpr>(callee); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 178 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 179 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 180 | CGDebugInfo *DI = getDebugInfo(); |
Devang Patel | 6802027 | 2010-10-22 18:56:27 +0000 | [diff] [blame] | 181 | if (DI && CGM.getCodeGenOpts().LimitDebugInfo |
| 182 | && !isa<CallExpr>(ME->getBase())) { |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 183 | QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType(); |
| 184 | if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) { |
| 185 | DI->getOrCreateRecordType(PTy->getPointeeType(), |
| 186 | MD->getParent()->getLocation()); |
| 187 | } |
| 188 | } |
| 189 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 190 | if (MD->isStatic()) { |
| 191 | // The method is static, emit it as we would a regular call. |
| 192 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 193 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
| 194 | ReturnValue, CE->arg_begin(), CE->arg_end()); |
| 195 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 196 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 197 | // Compute the object pointer. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 198 | llvm::Value *This; |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 199 | if (ME->isArrow()) |
| 200 | This = EmitScalarExpr(ME->getBase()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 201 | else |
| 202 | This = EmitLValue(ME->getBase()).getAddress(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 203 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 204 | if (MD->isTrivial()) { |
| 205 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 206 | if (isa<CXXConstructorDecl>(MD) && |
| 207 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) |
| 208 | return RValue::get(0); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 209 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 210 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { |
| 211 | // We don't like to generate the trivial copy/move assignment operator |
| 212 | // when it isn't necessary; just produce the proper effect here. |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 213 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 214 | EmitAggregateCopy(This, RHS, CE->getType()); |
| 215 | return RValue::get(This); |
| 216 | } |
| 217 | |
| 218 | if (isa<CXXConstructorDecl>(MD) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 219 | cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { |
| 220 | // Trivial move and copy ctor are the same. |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 221 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 222 | EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, |
| 223 | CE->arg_begin(), CE->arg_end()); |
| 224 | return RValue::get(This); |
| 225 | } |
| 226 | llvm_unreachable("unknown trivial member function"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 227 | } |
| 228 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 229 | // Compute the function type we're calling. |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 230 | const CGFunctionInfo *FInfo = 0; |
| 231 | if (isa<CXXDestructorDecl>(MD)) |
| 232 | FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), |
| 233 | Dtor_Complete); |
| 234 | else if (isa<CXXConstructorDecl>(MD)) |
| 235 | FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD), |
| 236 | Ctor_Complete); |
| 237 | else |
| 238 | FInfo = &CGM.getTypes().getFunctionInfo(MD); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 239 | |
| 240 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 241 | llvm::Type *Ty |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 242 | = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic()); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 243 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 244 | // C++ [class.virtual]p12: |
| 245 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 246 | // virtual call mechanism. |
| 247 | // |
| 248 | // We also don't emit a virtual call if the base expression has a record type |
| 249 | // because then we know what the type is. |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 250 | bool UseVirtualCall; |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 251 | UseVirtualCall = MD->isVirtual() && !ME->hasQualifier() |
| 252 | && !canDevirtualizeMemberFunctionCalls(getContext(), |
| 253 | ME->getBase(), MD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 254 | llvm::Value *Callee; |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 255 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
| 256 | if (UseVirtualCall) { |
| 257 | Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 258 | } else { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 259 | if (getContext().getLangOptions().AppleKext && |
| 260 | MD->isVirtual() && |
| 261 | ME->hasQualifier()) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 262 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 263 | else |
| 264 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 265 | } |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 266 | } else if (const CXXConstructorDecl *Ctor = |
| 267 | dyn_cast<CXXConstructorDecl>(MD)) { |
| 268 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 269 | } else if (UseVirtualCall) { |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 270 | Callee = BuildVirtualCall(MD, This, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 271 | } else { |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 272 | if (getContext().getLangOptions().AppleKext && |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 273 | MD->isVirtual() && |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 274 | ME->hasQualifier()) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 275 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 276 | else |
| 277 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 280 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 281 | CE->arg_begin(), CE->arg_end()); |
| 282 | } |
| 283 | |
| 284 | RValue |
| 285 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 286 | ReturnValueSlot ReturnValue) { |
| 287 | const BinaryOperator *BO = |
| 288 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 289 | const Expr *BaseExpr = BO->getLHS(); |
| 290 | const Expr *MemFnExpr = BO->getRHS(); |
| 291 | |
| 292 | const MemberPointerType *MPT = |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 293 | MemFnExpr->getType()->castAs<MemberPointerType>(); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 294 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 295 | const FunctionProtoType *FPT = |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 296 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 297 | const CXXRecordDecl *RD = |
| 298 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 299 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 300 | // Get the member function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 301 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 302 | |
| 303 | // Emit the 'this' pointer. |
| 304 | llvm::Value *This; |
| 305 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 306 | if (BO->getOpcode() == BO_PtrMemI) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 307 | This = EmitScalarExpr(BaseExpr); |
| 308 | else |
| 309 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 310 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 311 | // Ask the ABI to load the callee. Note that This is modified. |
| 312 | llvm::Value *Callee = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 313 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 315 | CallArgList Args; |
| 316 | |
| 317 | QualType ThisType = |
| 318 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 319 | |
| 320 | // Push the this ptr. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 321 | Args.add(RValue::get(This), ThisType); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 322 | |
| 323 | // And the rest of the call args |
| 324 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 325 | return EmitCall(CGM.getTypes().getFunctionInfo(Args, FPT), Callee, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 326 | ReturnValue, Args); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | RValue |
| 330 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 331 | const CXXMethodDecl *MD, |
| 332 | ReturnValueSlot ReturnValue) { |
| 333 | assert(MD->isInstance() && |
| 334 | "Trying to emit a member call expr on a static method!"); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 335 | LValue LV = EmitLValue(E->getArg(0)); |
| 336 | llvm::Value *This = LV.getAddress(); |
| 337 | |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 338 | if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && |
| 339 | MD->isTrivial()) { |
| 340 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 341 | QualType Ty = E->getType(); |
| 342 | EmitAggregateCopy(This, Src, Ty); |
| 343 | return RValue::get(This); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 346 | llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 347 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 348 | E->arg_begin() + 1, E->arg_end()); |
| 349 | } |
| 350 | |
Peter Collingbourne | 6c0aa5f | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 351 | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 352 | ReturnValueSlot ReturnValue) { |
| 353 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
| 354 | } |
| 355 | |
Eli Friedman | 2ed7cb6 | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 356 | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
| 357 | llvm::Value *DestPtr, |
| 358 | const CXXRecordDecl *Base) { |
| 359 | if (Base->isEmpty()) |
| 360 | return; |
| 361 | |
| 362 | DestPtr = CGF.EmitCastToVoidPtr(DestPtr); |
| 363 | |
| 364 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
| 365 | CharUnits Size = Layout.getNonVirtualSize(); |
| 366 | CharUnits Align = Layout.getNonVirtualAlign(); |
| 367 | |
| 368 | llvm::Value *SizeVal = CGF.CGM.getSize(Size); |
| 369 | |
| 370 | // If the type contains a pointer to data member we can't memset it to zero. |
| 371 | // Instead, create a null constant and copy it to the destination. |
| 372 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 373 | // like -1, which happens to be the pattern used by member-pointers. |
| 374 | // TODO: isZeroInitializable can be over-conservative in the case where a |
| 375 | // virtual base contains a member pointer. |
| 376 | if (!CGF.CGM.getTypes().isZeroInitializable(Base)) { |
| 377 | llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base); |
| 378 | |
| 379 | llvm::GlobalVariable *NullVariable = |
| 380 | new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(), |
| 381 | /*isConstant=*/true, |
| 382 | llvm::GlobalVariable::PrivateLinkage, |
| 383 | NullConstant, Twine()); |
| 384 | NullVariable->setAlignment(Align.getQuantity()); |
| 385 | llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable); |
| 386 | |
| 387 | // Get and call the appropriate llvm.memcpy overload. |
| 388 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity()); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | // Otherwise, just memset the whole thing to zero. This is legal |
| 393 | // because in LLVM, all default initializers (other than the ones we just |
| 394 | // handled above) are guaranteed to have a bit pattern of all zeros. |
| 395 | CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal, |
| 396 | Align.getQuantity()); |
| 397 | } |
| 398 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 399 | void |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 400 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 401 | AggValueSlot Dest) { |
| 402 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 403 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 404 | |
| 405 | // If we require zero initialization before (or instead of) calling the |
| 406 | // constructor, as can be the case with a non-user-provided default |
Argyrios Kyrtzidis | 657baf1 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 407 | // constructor, emit the zero initialization now, unless destination is |
| 408 | // already zeroed. |
Eli Friedman | 2ed7cb6 | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 409 | if (E->requiresZeroInitialization() && !Dest.isZeroed()) { |
| 410 | switch (E->getConstructionKind()) { |
| 411 | case CXXConstructExpr::CK_Delegating: |
| 412 | assert(0 && "Delegating constructor should not need zeroing"); |
| 413 | case CXXConstructExpr::CK_Complete: |
| 414 | EmitNullInitialization(Dest.getAddr(), E->getType()); |
| 415 | break; |
| 416 | case CXXConstructExpr::CK_VirtualBase: |
| 417 | case CXXConstructExpr::CK_NonVirtualBase: |
| 418 | EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent()); |
| 419 | break; |
| 420 | } |
| 421 | } |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 422 | |
| 423 | // If this is a call to a trivial default constructor, do nothing. |
| 424 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 425 | return; |
| 426 | |
John McCall | fc1e6c7 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 427 | // Elide the constructor if we're constructing from a temporary. |
| 428 | // The temporary check is required because Sema sets this on NRVO |
| 429 | // returns. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 430 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
John McCall | fc1e6c7 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 431 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 432 | E->getArg(0)->getType())); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 433 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 434 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 435 | return; |
| 436 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 437 | } |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 438 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 439 | if (const ConstantArrayType *arrayType |
| 440 | = getContext().getAsConstantArrayType(E->getType())) { |
| 441 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 442 | E->arg_begin(), E->arg_end()); |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 443 | } else { |
Cameron Esfahani | 6bd2f6a | 2011-05-06 21:28:42 +0000 | [diff] [blame] | 444 | CXXCtorType Type = Ctor_Complete; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 445 | bool ForVirtualBase = false; |
| 446 | |
| 447 | switch (E->getConstructionKind()) { |
| 448 | case CXXConstructExpr::CK_Delegating: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 449 | // We should be emitting a constructor; GlobalDecl will assert this |
| 450 | Type = CurGD.getCtorType(); |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 451 | break; |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 452 | |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 453 | case CXXConstructExpr::CK_Complete: |
| 454 | Type = Ctor_Complete; |
| 455 | break; |
| 456 | |
| 457 | case CXXConstructExpr::CK_VirtualBase: |
| 458 | ForVirtualBase = true; |
| 459 | // fall-through |
| 460 | |
| 461 | case CXXConstructExpr::CK_NonVirtualBase: |
| 462 | Type = Ctor_Base; |
| 463 | } |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 464 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 465 | // Call the constructor. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 466 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(), |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 467 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 468 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 471 | void |
| 472 | CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, |
| 473 | llvm::Value *Src, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 474 | const Expr *Exp) { |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 475 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 476 | Exp = E->getSubExpr(); |
| 477 | assert(isa<CXXConstructExpr>(Exp) && |
| 478 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
| 479 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
| 480 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 481 | RunCleanupsScope Scope(*this); |
| 482 | |
| 483 | // If we require zero initialization before (or instead of) calling the |
| 484 | // constructor, as can be the case with a non-user-provided default |
| 485 | // constructor, emit the zero initialization now. |
| 486 | // FIXME. Do I still need this for a copy ctor synthesis? |
| 487 | if (E->requiresZeroInitialization()) |
| 488 | EmitNullInitialization(Dest, E->getType()); |
| 489 | |
Chandler Carruth | 858a546 | 2010-11-15 13:54:43 +0000 | [diff] [blame] | 490 | assert(!getContext().getAsConstantArrayType(E->getType()) |
| 491 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 492 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, |
| 493 | E->arg_begin(), E->arg_end()); |
| 494 | } |
| 495 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 496 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 497 | const CXXNewExpr *E) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 498 | if (!E->isArray()) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 499 | return CharUnits::Zero(); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 500 | |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 501 | // No cookie is required if the operator new[] being used is the |
| 502 | // reserved placement operator new[]. |
| 503 | if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) |
John McCall | 5172ed9 | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 504 | return CharUnits::Zero(); |
| 505 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 506 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 507 | } |
| 508 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 509 | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
| 510 | const CXXNewExpr *e, |
| 511 | llvm::Value *&numElements, |
| 512 | llvm::Value *&sizeWithoutCookie) { |
| 513 | QualType type = e->getAllocatedType(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 514 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 515 | if (!e->isArray()) { |
| 516 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 517 | sizeWithoutCookie |
| 518 | = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); |
| 519 | return sizeWithoutCookie; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 520 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 521 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 522 | // The width of size_t. |
| 523 | unsigned sizeWidth = CGF.SizeTy->getBitWidth(); |
| 524 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 525 | // Figure out the cookie size. |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 526 | llvm::APInt cookieSize(sizeWidth, |
| 527 | CalculateCookiePadding(CGF, e).getQuantity()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 528 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 529 | // Emit the array size expression. |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 530 | // We multiply the size of all dimensions for NumElements. |
| 531 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 532 | numElements = CGF.EmitScalarExpr(e->getArraySize()); |
| 533 | assert(isa<llvm::IntegerType>(numElements->getType())); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 534 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 535 | // The number of elements can be have an arbitrary integer type; |
| 536 | // essentially, we need to multiply it by a constant factor, add a |
| 537 | // cookie size, and verify that the result is representable as a |
| 538 | // size_t. That's just a gloss, though, and it's wrong in one |
| 539 | // important way: if the count is negative, it's an error even if |
| 540 | // the cookie size would bring the total size >= 0. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 541 | bool isSigned |
| 542 | = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 543 | llvm::IntegerType *numElementsType |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 544 | = cast<llvm::IntegerType>(numElements->getType()); |
| 545 | unsigned numElementsWidth = numElementsType->getBitWidth(); |
| 546 | |
| 547 | // Compute the constant factor. |
| 548 | llvm::APInt arraySizeMultiplier(sizeWidth, 1); |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 549 | while (const ConstantArrayType *CAT |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 550 | = CGF.getContext().getAsConstantArrayType(type)) { |
| 551 | type = CAT->getElementType(); |
| 552 | arraySizeMultiplier *= CAT->getSize(); |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 553 | } |
| 554 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 555 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 556 | llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); |
| 557 | typeSizeMultiplier *= arraySizeMultiplier; |
| 558 | |
| 559 | // This will be a size_t. |
| 560 | llvm::Value *size; |
Chris Lattner | 83252dc | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 562 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 563 | // Don't bloat the -O0 code. |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 564 | if (llvm::ConstantInt *numElementsC = |
| 565 | dyn_cast<llvm::ConstantInt>(numElements)) { |
| 566 | const llvm::APInt &count = numElementsC->getValue(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 567 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 568 | bool hasAnyOverflow = false; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 569 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 570 | // If 'count' was a negative number, it's an overflow. |
| 571 | if (isSigned && count.isNegative()) |
| 572 | hasAnyOverflow = true; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 573 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 574 | // We want to do all this arithmetic in size_t. If numElements is |
| 575 | // wider than that, check whether it's already too big, and if so, |
| 576 | // overflow. |
| 577 | else if (numElementsWidth > sizeWidth && |
| 578 | numElementsWidth - sizeWidth > count.countLeadingZeros()) |
| 579 | hasAnyOverflow = true; |
| 580 | |
| 581 | // Okay, compute a count at the right width. |
| 582 | llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); |
| 583 | |
| 584 | // Scale numElements by that. This might overflow, but we don't |
| 585 | // care because it only overflows if allocationSize does, too, and |
| 586 | // if that overflows then we shouldn't use this. |
| 587 | numElements = llvm::ConstantInt::get(CGF.SizeTy, |
| 588 | adjustedCount * arraySizeMultiplier); |
| 589 | |
| 590 | // Compute the size before cookie, and track whether it overflowed. |
| 591 | bool overflow; |
| 592 | llvm::APInt allocationSize |
| 593 | = adjustedCount.umul_ov(typeSizeMultiplier, overflow); |
| 594 | hasAnyOverflow |= overflow; |
| 595 | |
| 596 | // Add in the cookie, and check whether it's overflowed. |
| 597 | if (cookieSize != 0) { |
| 598 | // Save the current size without a cookie. This shouldn't be |
| 599 | // used if there was overflow. |
| 600 | sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 601 | |
| 602 | allocationSize = allocationSize.uadd_ov(cookieSize, overflow); |
| 603 | hasAnyOverflow |= overflow; |
| 604 | } |
| 605 | |
| 606 | // On overflow, produce a -1 so operator new will fail. |
| 607 | if (hasAnyOverflow) { |
| 608 | size = llvm::Constant::getAllOnesValue(CGF.SizeTy); |
| 609 | } else { |
| 610 | size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 611 | } |
| 612 | |
| 613 | // Otherwise, we might need to use the overflow intrinsics. |
| 614 | } else { |
| 615 | // There are up to four conditions we need to test for: |
| 616 | // 1) if isSigned, we need to check whether numElements is negative; |
| 617 | // 2) if numElementsWidth > sizeWidth, we need to check whether |
| 618 | // numElements is larger than something representable in size_t; |
| 619 | // 3) we need to compute |
| 620 | // sizeWithoutCookie := numElements * typeSizeMultiplier |
| 621 | // and check whether it overflows; and |
| 622 | // 4) if we need a cookie, we need to compute |
| 623 | // size := sizeWithoutCookie + cookieSize |
| 624 | // and check whether it overflows. |
| 625 | |
| 626 | llvm::Value *hasOverflow = 0; |
| 627 | |
| 628 | // If numElementsWidth > sizeWidth, then one way or another, we're |
| 629 | // going to have to do a comparison for (2), and this happens to |
| 630 | // take care of (1), too. |
| 631 | if (numElementsWidth > sizeWidth) { |
| 632 | llvm::APInt threshold(numElementsWidth, 1); |
| 633 | threshold <<= sizeWidth; |
| 634 | |
| 635 | llvm::Value *thresholdV |
| 636 | = llvm::ConstantInt::get(numElementsType, threshold); |
| 637 | |
| 638 | hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); |
| 639 | numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); |
| 640 | |
| 641 | // Otherwise, if we're signed, we want to sext up to size_t. |
| 642 | } else if (isSigned) { |
| 643 | if (numElementsWidth < sizeWidth) |
| 644 | numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); |
| 645 | |
| 646 | // If there's a non-1 type size multiplier, then we can do the |
| 647 | // signedness check at the same time as we do the multiply |
| 648 | // because a negative number times anything will cause an |
| 649 | // unsigned overflow. Otherwise, we have to do it here. |
| 650 | if (typeSizeMultiplier == 1) |
| 651 | hasOverflow = CGF.Builder.CreateICmpSLT(numElements, |
| 652 | llvm::ConstantInt::get(CGF.SizeTy, 0)); |
| 653 | |
| 654 | // Otherwise, zext up to size_t if necessary. |
| 655 | } else if (numElementsWidth < sizeWidth) { |
| 656 | numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); |
| 657 | } |
| 658 | |
| 659 | assert(numElements->getType() == CGF.SizeTy); |
| 660 | |
| 661 | size = numElements; |
| 662 | |
| 663 | // Multiply by the type size if necessary. This multiplier |
| 664 | // includes all the factors for nested arrays. |
| 665 | // |
| 666 | // This step also causes numElements to be scaled up by the |
| 667 | // nested-array factor if necessary. Overflow on this computation |
| 668 | // can be ignored because the result shouldn't be used if |
| 669 | // allocation fails. |
| 670 | if (typeSizeMultiplier != 1) { |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 671 | llvm::Value *umul_with_overflow |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 672 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 673 | |
| 674 | llvm::Value *tsmV = |
| 675 | llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); |
| 676 | llvm::Value *result = |
| 677 | CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV); |
| 678 | |
| 679 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 680 | if (hasOverflow) |
| 681 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 682 | else |
| 683 | hasOverflow = overflowed; |
| 684 | |
| 685 | size = CGF.Builder.CreateExtractValue(result, 0); |
| 686 | |
| 687 | // Also scale up numElements by the array size multiplier. |
| 688 | if (arraySizeMultiplier != 1) { |
| 689 | // If the base element type size is 1, then we can re-use the |
| 690 | // multiply we just did. |
| 691 | if (typeSize.isOne()) { |
| 692 | assert(arraySizeMultiplier == typeSizeMultiplier); |
| 693 | numElements = size; |
| 694 | |
| 695 | // Otherwise we need a separate multiply. |
| 696 | } else { |
| 697 | llvm::Value *asmV = |
| 698 | llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); |
| 699 | numElements = CGF.Builder.CreateMul(numElements, asmV); |
| 700 | } |
| 701 | } |
| 702 | } else { |
| 703 | // numElements doesn't need to be scaled. |
| 704 | assert(arraySizeMultiplier == 1); |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 705 | } |
| 706 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 707 | // Add in the cookie size if necessary. |
| 708 | if (cookieSize != 0) { |
| 709 | sizeWithoutCookie = size; |
| 710 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 711 | llvm::Value *uadd_with_overflow |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 712 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 713 | |
| 714 | llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); |
| 715 | llvm::Value *result = |
| 716 | CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV); |
| 717 | |
| 718 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 719 | if (hasOverflow) |
| 720 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 721 | else |
| 722 | hasOverflow = overflowed; |
| 723 | |
| 724 | size = CGF.Builder.CreateExtractValue(result, 0); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 725 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 726 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 727 | // If we had any possibility of dynamic overflow, make a select to |
| 728 | // overwrite 'size' with an all-ones value, which should cause |
| 729 | // operator new to throw. |
| 730 | if (hasOverflow) |
| 731 | size = CGF.Builder.CreateSelect(hasOverflow, |
| 732 | llvm::Constant::getAllOnesValue(CGF.SizeTy), |
| 733 | size); |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 734 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 735 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 736 | if (cookieSize == 0) |
| 737 | sizeWithoutCookie = size; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 738 | else |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 739 | assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 740 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 741 | return size; |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 744 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 745 | llvm::Value *NewPtr) { |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 746 | |
| 747 | assert(E->getNumConstructorArgs() == 1 && |
| 748 | "Can only have one argument to initializer of POD type."); |
| 749 | |
| 750 | const Expr *Init = E->getConstructorArg(0); |
| 751 | QualType AllocType = E->getAllocatedType(); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 752 | |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame^] | 753 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 754 | if (!CGF.hasAggregateLLVMType(AllocType)) |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame^] | 755 | CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, |
| 756 | Alignment.getQuantity()), |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 757 | false); |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 758 | else if (AllocType->isAnyComplexType()) |
| 759 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 760 | AllocType.isVolatileQualified()); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 761 | else { |
| 762 | AggValueSlot Slot |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 763 | = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 764 | AggValueSlot::IsDestructed, |
John McCall | 4418439 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 765 | AggValueSlot::DoesNotNeedGCBarriers, |
| 766 | AggValueSlot::IsNotAliased); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 767 | CGF.EmitAggExpr(Init, Slot); |
| 768 | } |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void |
| 772 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 773 | QualType elementType, |
| 774 | llvm::Value *beginPtr, |
| 775 | llvm::Value *numElements) { |
Fariborz Jahanian | 5304c95 | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 776 | // We have a POD type. |
| 777 | if (E->getNumConstructorArgs() == 0) |
| 778 | return; |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 779 | |
| 780 | // Check if the number of elements is constant. |
| 781 | bool checkZero = true; |
| 782 | if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) { |
| 783 | // If it's constant zero, skip the whole loop. |
| 784 | if (constNum->isZero()) return; |
| 785 | |
| 786 | checkZero = false; |
| 787 | } |
| 788 | |
| 789 | // Find the end of the array, hoisted out of the loop. |
| 790 | llvm::Value *endPtr = |
| 791 | Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end"); |
| 792 | |
| 793 | // Create the continuation block. |
| 794 | llvm::BasicBlock *contBB = createBasicBlock("new.loop.end"); |
| 795 | |
| 796 | // If we need to check for zero, do so now. |
| 797 | if (checkZero) { |
| 798 | llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty"); |
| 799 | llvm::Value *isEmpty = Builder.CreateICmpEQ(beginPtr, endPtr, |
| 800 | "array.isempty"); |
| 801 | Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB); |
| 802 | EmitBlock(nonEmptyBB); |
| 803 | } |
| 804 | |
| 805 | // Enter the loop. |
| 806 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 807 | llvm::BasicBlock *loopBB = createBasicBlock("new.loop"); |
| 808 | |
| 809 | EmitBlock(loopBB); |
| 810 | |
| 811 | // Set up the current-element phi. |
| 812 | llvm::PHINode *curPtr = |
| 813 | Builder.CreatePHI(beginPtr->getType(), 2, "array.cur"); |
| 814 | curPtr->addIncoming(beginPtr, entryBB); |
| 815 | |
| 816 | // Enter a partial-destruction cleanup if necessary. |
| 817 | QualType::DestructionKind dtorKind = elementType.isDestructedType(); |
| 818 | EHScopeStack::stable_iterator cleanup; |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 819 | llvm::Instruction *cleanupDominator = 0; |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 820 | if (needsEHCleanup(dtorKind)) { |
| 821 | pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType, |
| 822 | getDestroyer(dtorKind)); |
| 823 | cleanup = EHStack.stable_begin(); |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 824 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Emit the initializer into this element. |
| 828 | StoreAnyExprIntoOneUnit(*this, E, curPtr); |
| 829 | |
| 830 | // Leave the cleanup if we entered one. |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 831 | if (cleanup != EHStack.stable_end()) { |
| 832 | DeactivateCleanupBlock(cleanup, cleanupDominator); |
| 833 | cleanupDominator->eraseFromParent(); |
| 834 | } |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 835 | |
| 836 | // Advance to the next element. |
| 837 | llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next"); |
| 838 | |
| 839 | // Check whether we've gotten to the end of the array and, if so, |
| 840 | // exit the loop. |
| 841 | llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend"); |
| 842 | Builder.CreateCondBr(isEnd, contBB, loopBB); |
| 843 | curPtr->addIncoming(nextPtr, Builder.GetInsertBlock()); |
| 844 | |
| 845 | EmitBlock(contBB); |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 848 | static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, |
| 849 | llvm::Value *NewPtr, llvm::Value *Size) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 850 | CGF.EmitCastToVoidPtr(NewPtr); |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 851 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 852 | CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 853 | Alignment.getQuantity(), false); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 856 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 857 | QualType ElementType, |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 858 | llvm::Value *NewPtr, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 859 | llvm::Value *NumElements, |
| 860 | llvm::Value *AllocSizeWithoutCookie) { |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 861 | if (E->isArray()) { |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 862 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 863 | bool RequiresZeroInitialization = false; |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 864 | if (Ctor->getParent()->hasTrivialDefaultConstructor()) { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 865 | // If new expression did not specify value-initialization, then there |
| 866 | // is no initialization. |
| 867 | if (!E->hasInitializer() || Ctor->getParent()->isEmpty()) |
| 868 | return; |
| 869 | |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 870 | if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 871 | // Optimization: since zero initialization will just set the memory |
| 872 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 873 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | |
| 877 | RequiresZeroInitialization = true; |
| 878 | } |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 880 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 881 | E->constructor_arg_begin(), |
| 882 | E->constructor_arg_end(), |
| 883 | RequiresZeroInitialization); |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 884 | return; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 885 | } else if (E->getNumConstructorArgs() == 1 && |
| 886 | isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) { |
| 887 | // Optimization: since zero initialization will just set the memory |
| 888 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 889 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
| 890 | return; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 891 | } else { |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 892 | CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements); |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 895 | } |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 896 | |
| 897 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 898 | // Per C++ [expr.new]p15, if we have an initializer, then we're performing |
| 899 | // direct initialization. C++ [dcl.init]p5 requires that we |
| 900 | // zero-initialize storage if there are no user-declared constructors. |
| 901 | if (E->hasInitializer() && |
| 902 | !Ctor->getParent()->hasUserDeclaredConstructor() && |
| 903 | !Ctor->getParent()->isEmpty()) |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 904 | CGF.EmitNullInitialization(NewPtr, ElementType); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | 8474567 | 2010-07-07 23:37:33 +0000 | [diff] [blame] | 906 | CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
| 907 | NewPtr, E->constructor_arg_begin(), |
| 908 | E->constructor_arg_end()); |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 909 | |
| 910 | return; |
| 911 | } |
Fariborz Jahanian | 5304c95 | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 912 | // We have a POD type. |
| 913 | if (E->getNumConstructorArgs() == 0) |
| 914 | return; |
| 915 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 916 | StoreAnyExprIntoOneUnit(CGF, E, NewPtr); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 917 | } |
| 918 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 919 | namespace { |
| 920 | /// A cleanup to call the given 'operator delete' function upon |
| 921 | /// abnormal exit from a new expression. |
| 922 | class CallDeleteDuringNew : public EHScopeStack::Cleanup { |
| 923 | size_t NumPlacementArgs; |
| 924 | const FunctionDecl *OperatorDelete; |
| 925 | llvm::Value *Ptr; |
| 926 | llvm::Value *AllocSize; |
| 927 | |
| 928 | RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } |
| 929 | |
| 930 | public: |
| 931 | static size_t getExtraSize(size_t NumPlacementArgs) { |
| 932 | return NumPlacementArgs * sizeof(RValue); |
| 933 | } |
| 934 | |
| 935 | CallDeleteDuringNew(size_t NumPlacementArgs, |
| 936 | const FunctionDecl *OperatorDelete, |
| 937 | llvm::Value *Ptr, |
| 938 | llvm::Value *AllocSize) |
| 939 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 940 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 941 | |
| 942 | void setPlacementArg(unsigned I, RValue Arg) { |
| 943 | assert(I < NumPlacementArgs && "index out of range"); |
| 944 | getPlacementArgs()[I] = Arg; |
| 945 | } |
| 946 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 947 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 948 | const FunctionProtoType *FPT |
| 949 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 950 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
John McCall | c384636 | 2010-09-14 21:45:42 +0000 | [diff] [blame] | 951 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 952 | |
| 953 | CallArgList DeleteArgs; |
| 954 | |
| 955 | // The first argument is always a void*. |
| 956 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 957 | DeleteArgs.add(RValue::get(Ptr), *AI++); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 958 | |
| 959 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 960 | if (FPT->getNumArgs() == NumPlacementArgs + 2) |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 961 | DeleteArgs.add(RValue::get(AllocSize), *AI++); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 962 | |
| 963 | // Pass the rest of the arguments, which must match exactly. |
| 964 | for (unsigned I = 0; I != NumPlacementArgs; ++I) |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 965 | DeleteArgs.add(getPlacementArgs()[I], *AI++); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 966 | |
| 967 | // Call 'operator delete'. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 968 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 969 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 970 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 971 | } |
| 972 | }; |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 973 | |
| 974 | /// A cleanup to call the given 'operator delete' function upon |
| 975 | /// abnormal exit from a new expression when the new expression is |
| 976 | /// conditional. |
| 977 | class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { |
| 978 | size_t NumPlacementArgs; |
| 979 | const FunctionDecl *OperatorDelete; |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 980 | DominatingValue<RValue>::saved_type Ptr; |
| 981 | DominatingValue<RValue>::saved_type AllocSize; |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 982 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 983 | DominatingValue<RValue>::saved_type *getPlacementArgs() { |
| 984 | return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | public: |
| 988 | static size_t getExtraSize(size_t NumPlacementArgs) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 989 | return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | CallDeleteDuringConditionalNew(size_t NumPlacementArgs, |
| 993 | const FunctionDecl *OperatorDelete, |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 994 | DominatingValue<RValue>::saved_type Ptr, |
| 995 | DominatingValue<RValue>::saved_type AllocSize) |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 996 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 997 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 998 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 999 | void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1000 | assert(I < NumPlacementArgs && "index out of range"); |
| 1001 | getPlacementArgs()[I] = Arg; |
| 1002 | } |
| 1003 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1004 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1005 | const FunctionProtoType *FPT |
| 1006 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1007 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
| 1008 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
| 1009 | |
| 1010 | CallArgList DeleteArgs; |
| 1011 | |
| 1012 | // The first argument is always a void*. |
| 1013 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1014 | DeleteArgs.add(Ptr.restore(CGF), *AI++); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1015 | |
| 1016 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 1017 | if (FPT->getNumArgs() == NumPlacementArgs + 2) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1018 | RValue RV = AllocSize.restore(CGF); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1019 | DeleteArgs.add(RV, *AI++); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | // Pass the rest of the arguments, which must match exactly. |
| 1023 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1024 | RValue RV = getPlacementArgs()[I].restore(CGF); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1025 | DeleteArgs.add(RV, *AI++); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | // Call 'operator delete'. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1029 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1030 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1031 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 1032 | } |
| 1033 | }; |
| 1034 | } |
| 1035 | |
| 1036 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 1037 | /// new-expression throws. |
| 1038 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 1039 | const CXXNewExpr *E, |
| 1040 | llvm::Value *NewPtr, |
| 1041 | llvm::Value *AllocSize, |
| 1042 | const CallArgList &NewArgs) { |
| 1043 | // If we're not inside a conditional branch, then the cleanup will |
| 1044 | // dominate and we can do the easier (and more efficient) thing. |
| 1045 | if (!CGF.isInConditionalBranch()) { |
| 1046 | CallDeleteDuringNew *Cleanup = CGF.EHStack |
| 1047 | .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, |
| 1048 | E->getNumPlacementArgs(), |
| 1049 | E->getOperatorDelete(), |
| 1050 | NewPtr, AllocSize); |
| 1051 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1052 | Cleanup->setPlacementArg(I, NewArgs[I+1].RV); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1053 | |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | // Otherwise, we need to save all this stuff. |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1058 | DominatingValue<RValue>::saved_type SavedNewPtr = |
| 1059 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); |
| 1060 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 1061 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1062 | |
| 1063 | CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1064 | .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1065 | E->getNumPlacementArgs(), |
| 1066 | E->getOperatorDelete(), |
| 1067 | SavedNewPtr, |
| 1068 | SavedAllocSize); |
| 1069 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1070 | Cleanup->setPlacementArg(I, |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1071 | DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1072 | |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1073 | CGF.initFullExprCleanup(); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1076 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1077 | // The element type being allocated. |
| 1078 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1079 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1080 | // 1. Build a call to the allocation function. |
| 1081 | FunctionDecl *allocator = E->getOperatorNew(); |
| 1082 | const FunctionProtoType *allocatorType = |
| 1083 | allocator->getType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1084 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1085 | CallArgList allocatorArgs; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1086 | |
| 1087 | // The allocation size is the first argument. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1088 | QualType sizeType = getContext().getSizeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1089 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1090 | llvm::Value *numElements = 0; |
| 1091 | llvm::Value *allocSizeWithoutCookie = 0; |
| 1092 | llvm::Value *allocSize = |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1093 | EmitCXXNewAllocSize(*this, E, numElements, allocSizeWithoutCookie); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1094 | |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1095 | allocatorArgs.add(RValue::get(allocSize), sizeType); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Emit the rest of the arguments. |
| 1098 | // FIXME: Ideally, this should just use EmitCallArgs. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1099 | CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1100 | |
| 1101 | // First, use the types from the function type. |
| 1102 | // We start at 1 here because the first argument (the allocation size) |
| 1103 | // has already been emitted. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1104 | for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; |
| 1105 | ++i, ++placementArg) { |
| 1106 | QualType argType = allocatorType->getArgType(i); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1107 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1108 | assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), |
| 1109 | placementArg->getType()) && |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1110 | "type mismatch in call argument!"); |
| 1111 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1112 | EmitCallArg(allocatorArgs, *placementArg, argType); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // Either we've emitted all the call args, or we have a call to a |
| 1116 | // variadic function. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1117 | assert((placementArg == E->placement_arg_end() || |
| 1118 | allocatorType->isVariadic()) && |
| 1119 | "Extra arguments to non-variadic function!"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1120 | |
| 1121 | // If we still have any arguments, emit them using the type of the argument. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1122 | for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); |
| 1123 | placementArg != placementArgsEnd; ++placementArg) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1124 | EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1127 | // Emit the allocation call. If the allocator is a global placement |
| 1128 | // operator, just "inline" it directly. |
| 1129 | RValue RV; |
| 1130 | if (allocator->isReservedGlobalPlacementOperator()) { |
| 1131 | assert(allocatorArgs.size() == 2); |
| 1132 | RV = allocatorArgs[1].RV; |
| 1133 | // TODO: kill any unnecessary computations done for the size |
| 1134 | // argument. |
| 1135 | } else { |
| 1136 | RV = EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType), |
| 1137 | CGM.GetAddrOfFunction(allocator), ReturnValueSlot(), |
| 1138 | allocatorArgs, allocator); |
| 1139 | } |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1140 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1141 | // Emit a null check on the allocation result if the allocation |
| 1142 | // function is allowed to return null (because it has a non-throwing |
| 1143 | // exception spec; for this part, we inline |
| 1144 | // CXXNewExpr::shouldNullCheckAllocation()) and we have an |
| 1145 | // interesting initializer. |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1146 | bool nullCheck = allocatorType->isNothrow(getContext()) && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1147 | !(allocType.isPODType(getContext()) && !E->hasInitializer()); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1148 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1149 | llvm::BasicBlock *nullCheckBB = 0; |
| 1150 | llvm::BasicBlock *contBB = 0; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1151 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1152 | llvm::Value *allocation = RV.getScalarVal(); |
| 1153 | unsigned AS = |
| 1154 | cast<llvm::PointerType>(allocation->getType())->getAddressSpace(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1155 | |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1156 | // The null-check means that the initializer is conditionally |
| 1157 | // evaluated. |
| 1158 | ConditionalEvaluation conditional(*this); |
| 1159 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1160 | if (nullCheck) { |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1161 | conditional.begin(*this); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1162 | |
| 1163 | nullCheckBB = Builder.GetInsertBlock(); |
| 1164 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1165 | contBB = createBasicBlock("new.cont"); |
| 1166 | |
| 1167 | llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); |
| 1168 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1169 | EmitBlock(notNullBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1170 | } |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1171 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1172 | // If there's an operator delete, enter a cleanup to call it if an |
| 1173 | // exception is thrown. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1174 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1175 | llvm::Instruction *cleanupDominator = 0; |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1176 | if (E->getOperatorDelete() && |
| 1177 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1178 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); |
| 1179 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1180 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Eli Friedman | 576cf17 | 2011-09-06 18:53:03 +0000 | [diff] [blame] | 1183 | assert((allocSize == allocSizeWithoutCookie) == |
| 1184 | CalculateCookiePadding(*this, E).isZero()); |
| 1185 | if (allocSize != allocSizeWithoutCookie) { |
| 1186 | assert(E->isArray()); |
| 1187 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1188 | numElements, |
| 1189 | E, allocType); |
| 1190 | } |
| 1191 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1192 | llvm::Type *elementPtrTy |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1193 | = ConvertTypeForMem(allocType)->getPointerTo(AS); |
| 1194 | llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1195 | |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1196 | EmitNewInitializer(*this, E, allocType, result, numElements, |
| 1197 | allocSizeWithoutCookie); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1198 | if (E->isArray()) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1199 | // NewPtr is a pointer to the base element type. If we're |
| 1200 | // allocating an array of arrays, we'll need to cast back to the |
| 1201 | // array pointer type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1202 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1203 | if (result->getType() != resultType) |
| 1204 | result = Builder.CreateBitCast(result, resultType); |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1205 | } |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Deactivate the 'operator delete' cleanup if we finished |
| 1208 | // initialization. |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1209 | if (operatorDeleteCleanup.isValid()) { |
| 1210 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
| 1211 | cleanupDominator->eraseFromParent(); |
| 1212 | } |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1213 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1214 | if (nullCheck) { |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1215 | conditional.end(*this); |
| 1216 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1217 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1218 | EmitBlock(contBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1219 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1220 | llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1221 | PHI->addIncoming(result, notNullBB); |
| 1222 | PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), |
| 1223 | nullCheckBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1224 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1225 | result = PHI; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1226 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1227 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1228 | return result; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1231 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 1232 | llvm::Value *Ptr, |
| 1233 | QualType DeleteTy) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1234 | assert(DeleteFD->getOverloadedOperator() == OO_Delete); |
| 1235 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1236 | const FunctionProtoType *DeleteFTy = |
| 1237 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1238 | |
| 1239 | CallArgList DeleteArgs; |
| 1240 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1241 | // Check if we need to pass the size to the delete operator. |
| 1242 | llvm::Value *Size = 0; |
| 1243 | QualType SizeTy; |
| 1244 | if (DeleteFTy->getNumArgs() == 2) { |
| 1245 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 4f122ef | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 1246 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1247 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 1248 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1251 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 1252 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1253 | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1254 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1255 | if (Size) |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1256 | DeleteArgs.add(RValue::get(Size), SizeTy); |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1257 | |
| 1258 | // Emit the call to delete. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1259 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1260 | CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1261 | DeleteArgs, DeleteFD); |
| 1262 | } |
| 1263 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1264 | namespace { |
| 1265 | /// Calls the given 'operator delete' on a single object. |
| 1266 | struct CallObjectDelete : EHScopeStack::Cleanup { |
| 1267 | llvm::Value *Ptr; |
| 1268 | const FunctionDecl *OperatorDelete; |
| 1269 | QualType ElementType; |
| 1270 | |
| 1271 | CallObjectDelete(llvm::Value *Ptr, |
| 1272 | const FunctionDecl *OperatorDelete, |
| 1273 | QualType ElementType) |
| 1274 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1275 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1276 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1277 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1278 | } |
| 1279 | }; |
| 1280 | } |
| 1281 | |
| 1282 | /// Emit the code for deleting a single object. |
| 1283 | static void EmitObjectDelete(CodeGenFunction &CGF, |
| 1284 | const FunctionDecl *OperatorDelete, |
| 1285 | llvm::Value *Ptr, |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1286 | QualType ElementType, |
| 1287 | bool UseGlobalDelete) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1288 | // Find the destructor for the type, if applicable. If the |
| 1289 | // destructor is virtual, we'll just emit the vcall and return. |
| 1290 | const CXXDestructorDecl *Dtor = 0; |
| 1291 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1292 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | aebab72 | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 1293 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1294 | Dtor = RD->getDestructor(); |
| 1295 | |
| 1296 | if (Dtor->isVirtual()) { |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1297 | if (UseGlobalDelete) { |
| 1298 | // If we're supposed to call the global delete, make sure we do so |
| 1299 | // even if the destructor throws. |
| 1300 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 1301 | Ptr, OperatorDelete, |
| 1302 | ElementType); |
| 1303 | } |
| 1304 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1305 | llvm::Type *Ty = |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 1306 | CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor, |
| 1307 | Dtor_Complete), |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1308 | /*isVariadic=*/false); |
| 1309 | |
| 1310 | llvm::Value *Callee |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1311 | = CGF.BuildVirtualCall(Dtor, |
| 1312 | UseGlobalDelete? Dtor_Complete : Dtor_Deleting, |
| 1313 | Ptr, Ty); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1314 | CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0, |
| 1315 | 0, 0); |
| 1316 | |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1317 | if (UseGlobalDelete) { |
| 1318 | CGF.PopCleanupBlock(); |
| 1319 | } |
| 1320 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1321 | return; |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // Make sure that we call delete even if the dtor throws. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1327 | // This doesn't have to a conditional cleanup because we're going |
| 1328 | // to pop it off in a second. |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1329 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 1330 | Ptr, OperatorDelete, ElementType); |
| 1331 | |
| 1332 | if (Dtor) |
| 1333 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 1334 | /*ForVirtualBase=*/false, Ptr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1335 | else if (CGF.getLangOptions().ObjCAutoRefCount && |
| 1336 | ElementType->isObjCLifetimeType()) { |
| 1337 | switch (ElementType.getObjCLifetime()) { |
| 1338 | case Qualifiers::OCL_None: |
| 1339 | case Qualifiers::OCL_ExplicitNone: |
| 1340 | case Qualifiers::OCL_Autoreleasing: |
| 1341 | break; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1342 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1343 | case Qualifiers::OCL_Strong: { |
| 1344 | // Load the pointer value. |
| 1345 | llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr, |
| 1346 | ElementType.isVolatileQualified()); |
| 1347 | |
| 1348 | CGF.EmitARCRelease(PtrValue, /*precise*/ true); |
| 1349 | break; |
| 1350 | } |
| 1351 | |
| 1352 | case Qualifiers::OCL_Weak: |
| 1353 | CGF.EmitARCDestroyWeak(Ptr); |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1358 | CGF.PopCleanupBlock(); |
| 1359 | } |
| 1360 | |
| 1361 | namespace { |
| 1362 | /// Calls the given 'operator delete' on an array of objects. |
| 1363 | struct CallArrayDelete : EHScopeStack::Cleanup { |
| 1364 | llvm::Value *Ptr; |
| 1365 | const FunctionDecl *OperatorDelete; |
| 1366 | llvm::Value *NumElements; |
| 1367 | QualType ElementType; |
| 1368 | CharUnits CookieSize; |
| 1369 | |
| 1370 | CallArrayDelete(llvm::Value *Ptr, |
| 1371 | const FunctionDecl *OperatorDelete, |
| 1372 | llvm::Value *NumElements, |
| 1373 | QualType ElementType, |
| 1374 | CharUnits CookieSize) |
| 1375 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1376 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1377 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1378 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1379 | const FunctionProtoType *DeleteFTy = |
| 1380 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1381 | assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); |
| 1382 | |
| 1383 | CallArgList Args; |
| 1384 | |
| 1385 | // Pass the pointer as the first argument. |
| 1386 | QualType VoidPtrTy = DeleteFTy->getArgType(0); |
| 1387 | llvm::Value *DeletePtr |
| 1388 | = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1389 | Args.add(RValue::get(DeletePtr), VoidPtrTy); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Pass the original requested size as the second argument. |
| 1392 | if (DeleteFTy->getNumArgs() == 2) { |
| 1393 | QualType size_t = DeleteFTy->getArgType(1); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1394 | llvm::IntegerType *SizeTy |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1395 | = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); |
| 1396 | |
| 1397 | CharUnits ElementTypeSize = |
| 1398 | CGF.CGM.getContext().getTypeSizeInChars(ElementType); |
| 1399 | |
| 1400 | // The size of an element, multiplied by the number of elements. |
| 1401 | llvm::Value *Size |
| 1402 | = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); |
| 1403 | Size = CGF.Builder.CreateMul(Size, NumElements); |
| 1404 | |
| 1405 | // Plus the size of the cookie if applicable. |
| 1406 | if (!CookieSize.isZero()) { |
| 1407 | llvm::Value *CookieSizeV |
| 1408 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 1409 | Size = CGF.Builder.CreateAdd(Size, CookieSizeV); |
| 1410 | } |
| 1411 | |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1412 | Args.add(RValue::get(Size), size_t); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | // Emit the call to delete. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1416 | CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy), |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1417 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1418 | ReturnValueSlot(), Args, OperatorDelete); |
| 1419 | } |
| 1420 | }; |
| 1421 | } |
| 1422 | |
| 1423 | /// Emit the code for deleting an array of objects. |
| 1424 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1425 | const CXXDeleteExpr *E, |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1426 | llvm::Value *deletedPtr, |
| 1427 | QualType elementType) { |
| 1428 | llvm::Value *numElements = 0; |
| 1429 | llvm::Value *allocatedPtr = 0; |
| 1430 | CharUnits cookieSize; |
| 1431 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
| 1432 | numElements, allocatedPtr, cookieSize); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1433 | |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1434 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Make sure that we call delete even if one of the dtors throws. |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1437 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1438 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1439 | allocatedPtr, operatorDelete, |
| 1440 | numElements, elementType, |
| 1441 | cookieSize); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1442 | |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1443 | // Destroy the elements. |
| 1444 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
| 1445 | assert(numElements && "no element count for a type with a destructor!"); |
| 1446 | |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1447 | llvm::Value *arrayEnd = |
| 1448 | CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end"); |
John McCall | fbf780a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Note that it is legal to allocate a zero-length array, and we |
| 1451 | // can never fold the check away because the length should always |
| 1452 | // come from a cookie. |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1453 | CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType, |
| 1454 | CGF.getDestroyer(dtorKind), |
John McCall | fbf780a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1455 | /*checkZeroLength*/ true, |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1456 | CGF.needsEHCleanup(dtorKind)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1459 | // Pop the cleanup block. |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1460 | CGF.PopCleanupBlock(); |
| 1461 | } |
| 1462 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1463 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 1464 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1465 | // Get at the argument before we performed the implicit conversion |
| 1466 | // to void*. |
| 1467 | const Expr *Arg = E->getArgument(); |
| 1468 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1469 | if (ICE->getCastKind() != CK_UserDefinedConversion && |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1470 | ICE->getType()->isVoidPointerType()) |
| 1471 | Arg = ICE->getSubExpr(); |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 1472 | else |
| 1473 | break; |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1474 | } |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1475 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1476 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Null check the pointer. |
| 1479 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1480 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1481 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 1482 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1483 | |
| 1484 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1485 | EmitBlock(DeleteNotNull); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1486 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1487 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1488 | // first non-array element. |
| 1489 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1490 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1491 | if (DeleteTy->isConstantArrayType()) { |
| 1492 | llvm::Value *Zero = Builder.getInt32(0); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1493 | SmallVector<llvm::Value*,8> GEP; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1494 | |
| 1495 | GEP.push_back(Zero); // point at the outermost array |
| 1496 | |
| 1497 | // For each layer of array type we're pointing at: |
| 1498 | while (const ConstantArrayType *Arr |
| 1499 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1500 | // 1. Unpeel the array type. |
| 1501 | DeleteTy = Arr->getElementType(); |
| 1502 | |
| 1503 | // 2. GEP to the first element of the array. |
| 1504 | GEP.push_back(Zero); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1505 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1506 | |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 1507 | Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Douglas Gregor | eede61a | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 1510 | assert(ConvertTypeForMem(DeleteTy) == |
| 1511 | cast<llvm::PointerType>(Ptr->getType())->getElementType()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1512 | |
| 1513 | if (E->isArrayForm()) { |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1514 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1515 | } else { |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1516 | EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy, |
| 1517 | E->isGlobalDelete()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1518 | } |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1519 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1520 | EmitBlock(DeleteEnd); |
| 1521 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1522 | |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1523 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 1524 | // void __cxa_bad_typeid(); |
| 1525 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1526 | llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); |
| 1527 | llvm::FunctionType *FTy = |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1528 | llvm::FunctionType::get(VoidTy, false); |
| 1529 | |
| 1530 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 1531 | } |
| 1532 | |
| 1533 | static void EmitBadTypeidCall(CodeGenFunction &CGF) { |
Anders Carlsson | ad3692bb | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1534 | llvm::Value *Fn = getBadTypeidFn(CGF); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1535 | CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1536 | CGF.Builder.CreateUnreachable(); |
| 1537 | } |
| 1538 | |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1539 | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, |
| 1540 | const Expr *E, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1541 | llvm::Type *StdTypeInfoPtrTy) { |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1542 | // Get the vtable pointer. |
| 1543 | llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); |
| 1544 | |
| 1545 | // C++ [expr.typeid]p2: |
| 1546 | // If the glvalue expression is obtained by applying the unary * operator to |
| 1547 | // a pointer and the pointer is a null pointer value, the typeid expression |
| 1548 | // throws the std::bad_typeid exception. |
| 1549 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { |
| 1550 | if (UO->getOpcode() == UO_Deref) { |
| 1551 | llvm::BasicBlock *BadTypeidBlock = |
| 1552 | CGF.createBasicBlock("typeid.bad_typeid"); |
| 1553 | llvm::BasicBlock *EndBlock = |
| 1554 | CGF.createBasicBlock("typeid.end"); |
| 1555 | |
| 1556 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr); |
| 1557 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
| 1558 | |
| 1559 | CGF.EmitBlock(BadTypeidBlock); |
| 1560 | EmitBadTypeidCall(CGF); |
| 1561 | CGF.EmitBlock(EndBlock); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, |
| 1566 | StdTypeInfoPtrTy->getPointerTo()); |
| 1567 | |
| 1568 | // Load the type info. |
| 1569 | Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); |
| 1570 | return CGF.Builder.CreateLoad(Value); |
| 1571 | } |
| 1572 | |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1573 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1574 | llvm::Type *StdTypeInfoPtrTy = |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1575 | ConvertType(E->getType())->getPointerTo(); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 1576 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1577 | if (E->isTypeOperand()) { |
| 1578 | llvm::Constant *TypeInfo = |
| 1579 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1580 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1581 | } |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1582 | |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1583 | // C++ [expr.typeid]p2: |
| 1584 | // When typeid is applied to a glvalue expression whose type is a |
| 1585 | // polymorphic class type, the result refers to a std::type_info object |
| 1586 | // representing the type of the most derived object (that is, the dynamic |
| 1587 | // type) to which the glvalue refers. |
| 1588 | if (E->getExprOperand()->isGLValue()) { |
| 1589 | if (const RecordType *RT = |
| 1590 | E->getExprOperand()->getType()->getAs<RecordType>()) { |
| 1591 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1592 | if (RD->isPolymorphic()) |
| 1593 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
| 1594 | StdTypeInfoPtrTy); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1595 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1596 | } |
Anders Carlsson | 3f6c5e1 | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1597 | |
| 1598 | QualType OperandTy = E->getExprOperand()->getType(); |
| 1599 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
| 1600 | StdTypeInfoPtrTy); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1601 | } |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1602 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1603 | static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { |
| 1604 | // void *__dynamic_cast(const void *sub, |
| 1605 | // const abi::__class_type_info *src, |
| 1606 | // const abi::__class_type_info *dst, |
| 1607 | // std::ptrdiff_t src2dst_offset); |
| 1608 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1609 | llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 1610 | llvm::Type *PtrDiffTy = |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1611 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1612 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1613 | llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1615 | llvm::FunctionType *FTy = |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1616 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 1617 | |
| 1618 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"); |
| 1619 | } |
| 1620 | |
| 1621 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 1622 | // void __cxa_bad_cast(); |
| 1623 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1624 | llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); |
| 1625 | llvm::FunctionType *FTy = |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1626 | llvm::FunctionType::get(VoidTy, false); |
| 1627 | |
| 1628 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 1629 | } |
| 1630 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1631 | static void EmitBadCastCall(CodeGenFunction &CGF) { |
Anders Carlsson | ad3692bb | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1632 | llvm::Value *Fn = getBadCastFn(CGF); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1633 | CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1634 | CGF.Builder.CreateUnreachable(); |
| 1635 | } |
| 1636 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1637 | static llvm::Value * |
| 1638 | EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 1639 | QualType SrcTy, QualType DestTy, |
| 1640 | llvm::BasicBlock *CastEnd) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1641 | llvm::Type *PtrDiffLTy = |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1642 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1643 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1644 | |
| 1645 | if (const PointerType *PTy = DestTy->getAs<PointerType>()) { |
| 1646 | if (PTy->getPointeeType()->isVoidType()) { |
| 1647 | // C++ [expr.dynamic.cast]p7: |
| 1648 | // If T is "pointer to cv void," then the result is a pointer to the |
| 1649 | // most derived object pointed to by v. |
| 1650 | |
| 1651 | // Get the vtable pointer. |
| 1652 | llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); |
| 1653 | |
| 1654 | // Get the offset-to-top from the vtable. |
| 1655 | llvm::Value *OffsetToTop = |
| 1656 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
| 1657 | OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); |
| 1658 | |
| 1659 | // Finally, add the offset to the pointer. |
| 1660 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1661 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1662 | |
| 1663 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | QualType SrcRecordTy; |
| 1668 | QualType DestRecordTy; |
| 1669 | |
| 1670 | if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { |
| 1671 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 1672 | DestRecordTy = DestPTy->getPointeeType(); |
| 1673 | } else { |
| 1674 | SrcRecordTy = SrcTy; |
| 1675 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 1676 | } |
| 1677 | |
| 1678 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 1679 | assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); |
| 1680 | |
| 1681 | llvm::Value *SrcRTTI = |
| 1682 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1683 | llvm::Value *DestRTTI = |
| 1684 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1685 | |
| 1686 | // FIXME: Actually compute a hint here. |
| 1687 | llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL); |
| 1688 | |
| 1689 | // Emit the call to __dynamic_cast. |
| 1690 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1691 | Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value, |
| 1692 | SrcRTTI, DestRTTI, OffsetHint); |
| 1693 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1694 | |
| 1695 | /// C++ [expr.dynamic.cast]p9: |
| 1696 | /// A failed cast to reference type throws std::bad_cast |
| 1697 | if (DestTy->isReferenceType()) { |
| 1698 | llvm::BasicBlock *BadCastBlock = |
| 1699 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1700 | |
| 1701 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1702 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1703 | |
| 1704 | CGF.EmitBlock(BadCastBlock); |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1705 | EmitBadCastCall(CGF); |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | return Value; |
| 1709 | } |
| 1710 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1711 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 1712 | QualType DestTy) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1713 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1714 | if (DestTy->isPointerType()) |
| 1715 | return llvm::Constant::getNullValue(DestLTy); |
| 1716 | |
| 1717 | /// C++ [expr.dynamic.cast]p9: |
| 1718 | /// A failed cast to reference type throws std::bad_cast |
| 1719 | EmitBadCastCall(CGF); |
| 1720 | |
| 1721 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 1722 | return llvm::UndefValue::get(DestLTy); |
| 1723 | } |
| 1724 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1725 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1726 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1727 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1728 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1729 | if (DCE->isAlwaysNull()) |
| 1730 | return EmitDynamicCastToNull(*this, DestTy); |
| 1731 | |
| 1732 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 1733 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1734 | // C++ [expr.dynamic.cast]p4: |
| 1735 | // If the value of v is a null pointer value in the pointer case, the result |
| 1736 | // is the null pointer value of type T. |
| 1737 | bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1738 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1739 | llvm::BasicBlock *CastNull = 0; |
| 1740 | llvm::BasicBlock *CastNotNull = 0; |
| 1741 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1742 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1743 | if (ShouldNullCheckSrcValue) { |
| 1744 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 1745 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 1746 | |
| 1747 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
| 1748 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 1749 | EmitBlock(CastNotNull); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1752 | Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); |
| 1753 | |
| 1754 | if (ShouldNullCheckSrcValue) { |
| 1755 | EmitBranch(CastEnd); |
| 1756 | |
| 1757 | EmitBlock(CastNull); |
| 1758 | EmitBranch(CastEnd); |
| 1759 | } |
| 1760 | |
| 1761 | EmitBlock(CastEnd); |
| 1762 | |
| 1763 | if (ShouldNullCheckSrcValue) { |
| 1764 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 1765 | PHI->addIncoming(Value, CastNotNull); |
| 1766 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 1767 | |
| 1768 | Value = PHI; |
| 1769 | } |
| 1770 | |
| 1771 | return Value; |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1772 | } |