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