1 //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit blocks.
12 //===----------------------------------------------------------------------===//
14 #include "CGDebugInfo.h"
15 #include "CodeGenFunction.h"
16 #include "CGObjCRuntime.h"
17 #include "CodeGenModule.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "llvm/Module.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/Target/TargetData.h"
25 using namespace clang;
26 using namespace CodeGen;
28 CGBlockInfo::CGBlockInfo(const BlockExpr *blockExpr, const char *N)
29 : Name(N), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
30 HasCXXObject(false), UsesStret(false), StructureType(0), Block(blockExpr) {
32 // Skip asm prefix, if any.
33 if (Name && Name[0] == '\01')
37 // Anchor the vtable to this translation unit.
38 CodeGenModule::ByrefHelpers::~ByrefHelpers() {}
40 /// Build the given block as a global block.
41 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
42 const CGBlockInfo &blockInfo,
43 llvm::Constant *blockFn);
45 /// Build the helper function to copy a block.
46 static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
47 const CGBlockInfo &blockInfo) {
48 return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
51 /// Build the helper function to dipose of a block.
52 static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
53 const CGBlockInfo &blockInfo) {
54 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
57 /// Build the block descriptor constant for a block.
58 static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
59 const CGBlockInfo &blockInfo) {
60 ASTContext &C = CGM.getContext();
62 llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
63 llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
65 SmallVector<llvm::Constant*, 6> elements;
68 elements.push_back(llvm::ConstantInt::get(ulong, 0));
71 // FIXME: What is the right way to say this doesn't fit? We should give
72 // a user diagnostic in that case. Better fix would be to change the
74 elements.push_back(llvm::ConstantInt::get(ulong,
75 blockInfo.BlockSize.getQuantity()));
77 // Optional copy/dispose helpers.
78 if (blockInfo.NeedsCopyDispose) {
79 // copy_func_helper_decl
80 elements.push_back(buildCopyHelper(CGM, blockInfo));
83 elements.push_back(buildDisposeHelper(CGM, blockInfo));
86 // Signature. Mandatory ObjC-style method descriptor @encode sequence.
87 std::string typeAtEncoding =
88 CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
89 elements.push_back(llvm::ConstantExpr::getBitCast(
90 CGM.GetAddrOfConstantCString(typeAtEncoding), i8p));
93 if (C.getLangOptions().ObjC1)
94 elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
96 elements.push_back(llvm::Constant::getNullValue(i8p));
98 llvm::Constant *init = llvm::ConstantStruct::getAnon(elements);
100 llvm::GlobalVariable *global =
101 new llvm::GlobalVariable(CGM.getModule(), init->getType(), true,
102 llvm::GlobalValue::InternalLinkage,
103 init, "__block_descriptor_tmp");
105 return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType());
109 Purely notional variadic template describing the layout of a block.
111 template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
112 struct Block_literal {
113 /// Initialized to one of:
114 /// extern void *_NSConcreteStackBlock[];
115 /// extern void *_NSConcreteGlobalBlock[];
117 /// In theory, we could start one off malloc'ed by setting
118 /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
120 /// extern void *_NSConcreteMallocBlock[];
121 struct objc_class *isa;
123 /// These are the flags (with corresponding bit number) that the
124 /// compiler is actually supposed to know about.
125 /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
126 /// descriptor provides copy and dispose helper functions
127 /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
128 /// object with a nontrivial destructor or copy constructor
129 /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated
131 /// 29. BLOCK_USE_STRET - indicates that the block function
132 /// uses stret, which objc_msgSend needs to know about
133 /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an
134 /// @encoded signature string
135 /// And we're not supposed to manipulate these:
136 /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved
137 /// to malloc'ed memory
138 /// 27. BLOCK_IS_GC - indicates that the block has been moved to
139 /// to GC-allocated memory
140 /// Additionally, the bottom 16 bits are a reference count which
141 /// should be zero on the stack.
144 /// Reserved; should be zero-initialized.
147 /// Function pointer generated from block literal.
148 _ResultType (*invoke)(Block_literal *, _ParamTypes...);
150 /// Block description metadata generated from block literal.
151 struct Block_descriptor *block_descriptor;
153 /// Captured values follow.
154 _CapturesTypes captures...;
158 /// The number of fields in a block header.
159 const unsigned BlockHeaderSize = 5;
162 /// A chunk of data that we actually have to capture in the block.
163 struct BlockLayoutChunk {
166 const BlockDecl::Capture *Capture; // null for 'this'
169 BlockLayoutChunk(CharUnits align, CharUnits size,
170 const BlockDecl::Capture *capture,
172 : Alignment(align), Size(size), Capture(capture), Type(type) {}
174 /// Tell the block info that this chunk has the given field index.
175 void setIndex(CGBlockInfo &info, unsigned index) {
177 info.CXXThisIndex = index;
179 info.Captures[Capture->getVariable()]
180 = CGBlockInfo::Capture::makeIndex(index);
184 /// Order by descending alignment.
185 bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
186 return left.Alignment > right.Alignment;
190 /// Determines if the given type is safe for constant capture in C++.
191 static bool isSafeForCXXConstantCapture(QualType type) {
192 const RecordType *recordType =
193 type->getBaseElementTypeUnsafe()->getAs<RecordType>();
195 // Only records can be unsafe.
196 if (!recordType) return true;
198 const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl());
200 // Maintain semantics for classes with non-trivial dtors or copy ctors.
201 if (!record->hasTrivialDestructor()) return false;
202 if (!record->hasTrivialCopyConstructor()) return false;
204 // Otherwise, we just have to make sure there aren't any mutable
205 // fields that might have changed since initialization.
206 return !record->hasMutableFields();
209 /// It is illegal to modify a const object after initialization.
210 /// Therefore, if a const object has a constant initializer, we don't
211 /// actually need to keep storage for it in the block; we'll just
212 /// rematerialize it at the start of the block function. This is
213 /// acceptable because we make no promises about address stability of
214 /// captured variables.
215 static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
216 const VarDecl *var) {
217 QualType type = var->getType();
219 // We can only do this if the variable is const.
220 if (!type.isConstQualified()) return 0;
222 // Furthermore, in C++ we have to worry about mutable fields:
223 // C++ [dcl.type.cv]p4:
224 // Except that any class member declared mutable can be
225 // modified, any attempt to modify a const object during its
226 // lifetime results in undefined behavior.
227 if (CGM.getLangOptions().CPlusPlus && !isSafeForCXXConstantCapture(type))
230 // If the variable doesn't have any initializer (shouldn't this be
231 // invalid?), it's not clear what we should do. Maybe capture as
233 const Expr *init = var->getInit();
236 return CGM.EmitConstantExpr(init, var->getType());
239 /// Get the low bit of a nonzero character count. This is the
240 /// alignment of the nth byte if the 0th byte is universally aligned.
241 static CharUnits getLowBit(CharUnits v) {
242 return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
245 static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
246 SmallVectorImpl<llvm::Type*> &elementTypes) {
247 ASTContext &C = CGM.getContext();
249 // The header is basically a 'struct { void *; int; int; void *; void *; }'.
250 CharUnits ptrSize, ptrAlign, intSize, intAlign;
251 llvm::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy);
252 llvm::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy);
254 // Are there crazy embedded platforms where this isn't true?
255 assert(intSize <= ptrSize && "layout assumptions horribly violated");
257 CharUnits headerSize = ptrSize;
258 if (2 * intSize < ptrAlign) headerSize += ptrSize;
259 else headerSize += 2 * intSize;
260 headerSize += 2 * ptrSize;
262 info.BlockAlign = ptrAlign;
263 info.BlockSize = headerSize;
265 assert(elementTypes.empty());
266 llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
267 llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy);
268 elementTypes.push_back(i8p);
269 elementTypes.push_back(intTy);
270 elementTypes.push_back(intTy);
271 elementTypes.push_back(i8p);
272 elementTypes.push_back(CGM.getBlockDescriptorType());
274 assert(elementTypes.size() == BlockHeaderSize);
277 /// Compute the layout of the given block. Attempts to lay the block
278 /// out with minimal space requirements.
279 static void computeBlockInfo(CodeGenModule &CGM, CGBlockInfo &info) {
280 ASTContext &C = CGM.getContext();
281 const BlockDecl *block = info.getBlockDecl();
283 SmallVector<llvm::Type*, 8> elementTypes;
284 initializeForBlockHeader(CGM, info, elementTypes);
286 if (!block->hasCaptures()) {
288 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
289 info.CanBeGlobal = true;
293 // Collect the layout chunks.
294 SmallVector<BlockLayoutChunk, 16> layout;
295 layout.reserve(block->capturesCXXThis() +
296 (block->capture_end() - block->capture_begin()));
298 CharUnits maxFieldAlign;
301 if (block->capturesCXXThis()) {
302 const DeclContext *DC = block->getDeclContext();
303 for (; isa<BlockDecl>(DC); DC = cast<BlockDecl>(DC)->getDeclContext())
306 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC))
307 thisType = C.getPointerType(C.getRecordType(RD));
309 thisType = cast<CXXMethodDecl>(DC)->getThisType(C);
311 llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
312 std::pair<CharUnits,CharUnits> tinfo
313 = CGM.getContext().getTypeInfoInChars(thisType);
314 maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
316 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 0, llvmType));
319 // Next, all the block captures.
320 for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
321 ce = block->capture_end(); ci != ce; ++ci) {
322 const VarDecl *variable = ci->getVariable();
325 // We have to copy/dispose of the __block reference.
326 info.NeedsCopyDispose = true;
328 // Just use void* instead of a pointer to the byref type.
329 QualType byRefPtrTy = C.VoidPtrTy;
331 llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy);
332 std::pair<CharUnits,CharUnits> tinfo
333 = CGM.getContext().getTypeInfoInChars(byRefPtrTy);
334 maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
336 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
341 // Otherwise, build a layout chunk with the size and alignment of
343 if (llvm::Constant *constant = tryCaptureAsConstant(CGM, variable)) {
344 info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant);
348 // If we have a lifetime qualifier, honor it for capture purposes.
349 // That includes *not* copying it if it's __unsafe_unretained.
350 if (Qualifiers::ObjCLifetime lifetime
351 = variable->getType().getObjCLifetime()) {
353 case Qualifiers::OCL_None: llvm_unreachable("impossible");
354 case Qualifiers::OCL_ExplicitNone:
355 case Qualifiers::OCL_Autoreleasing:
358 case Qualifiers::OCL_Strong:
359 case Qualifiers::OCL_Weak:
360 info.NeedsCopyDispose = true;
363 // Block pointers require copy/dispose. So do Objective-C pointers.
364 } else if (variable->getType()->isObjCRetainableType()) {
365 info.NeedsCopyDispose = true;
367 // So do types that require non-trivial copy construction.
368 } else if (ci->hasCopyExpr()) {
369 info.NeedsCopyDispose = true;
370 info.HasCXXObject = true;
372 // And so do types with destructors.
373 } else if (CGM.getLangOptions().CPlusPlus) {
374 if (const CXXRecordDecl *record =
375 variable->getType()->getAsCXXRecordDecl()) {
376 if (!record->hasTrivialDestructor()) {
377 info.HasCXXObject = true;
378 info.NeedsCopyDispose = true;
383 CharUnits size = C.getTypeSizeInChars(variable->getType());
384 CharUnits align = C.getDeclAlign(variable);
385 maxFieldAlign = std::max(maxFieldAlign, align);
387 llvm::Type *llvmType =
388 CGM.getTypes().ConvertTypeForMem(variable->getType());
390 layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType));
393 // If that was everything, we're done here.
394 if (layout.empty()) {
396 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
397 info.CanBeGlobal = true;
401 // Sort the layout by alignment. We have to use a stable sort here
402 // to get reproducible results. There should probably be an
403 // llvm::array_pod_stable_sort.
404 std::stable_sort(layout.begin(), layout.end());
406 CharUnits &blockSize = info.BlockSize;
407 info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
409 // Assuming that the first byte in the header is maximally aligned,
410 // get the alignment of the first byte following the header.
411 CharUnits endAlign = getLowBit(blockSize);
413 // If the end of the header isn't satisfactorily aligned for the
414 // maximum thing, look for things that are okay with the header-end
415 // alignment, and keep appending them until we get something that's
416 // aligned right. This algorithm is only guaranteed optimal if
417 // that condition is satisfied at some point; otherwise we can get
419 // header // next byte has alignment 4
420 // something_with_size_5; // next byte has alignment 1
421 // something_with_alignment_8;
422 // which has 7 bytes of padding, as opposed to the naive solution
423 // which might have less (?).
424 if (endAlign < maxFieldAlign) {
425 SmallVectorImpl<BlockLayoutChunk>::iterator
426 li = layout.begin() + 1, le = layout.end();
428 // Look for something that the header end is already
429 // satisfactorily aligned for.
430 for (; li != le && endAlign < li->Alignment; ++li)
433 // If we found something that's naturally aligned for the end of
434 // the header, keep adding things...
436 SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
437 for (; li != le; ++li) {
438 assert(endAlign >= li->Alignment);
440 li->setIndex(info, elementTypes.size());
441 elementTypes.push_back(li->Type);
442 blockSize += li->Size;
443 endAlign = getLowBit(blockSize);
445 // ...until we get to the alignment of the maximum field.
446 if (endAlign >= maxFieldAlign)
450 // Don't re-append everything we just appended.
451 layout.erase(first, li);
455 // At this point, we just have to add padding if the end align still
456 // isn't aligned right.
457 if (endAlign < maxFieldAlign) {
458 CharUnits padding = maxFieldAlign - endAlign;
460 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
461 padding.getQuantity()));
462 blockSize += padding;
464 endAlign = getLowBit(blockSize);
465 assert(endAlign >= maxFieldAlign);
468 // Slam everything else on now. This works because they have
469 // strictly decreasing alignment and we expect that size is always a
470 // multiple of alignment.
471 for (SmallVectorImpl<BlockLayoutChunk>::iterator
472 li = layout.begin(), le = layout.end(); li != le; ++li) {
473 assert(endAlign >= li->Alignment);
474 li->setIndex(info, elementTypes.size());
475 elementTypes.push_back(li->Type);
476 blockSize += li->Size;
477 endAlign = getLowBit(blockSize);
481 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
484 /// Emit a block literal expression in the current function.
485 llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
486 std::string Name = CurFn->getName();
487 CGBlockInfo blockInfo(blockExpr, Name.c_str());
489 // Compute information about the layout, etc., of this block.
490 computeBlockInfo(CGM, blockInfo);
492 // Using that metadata, generate the actual block function.
493 llvm::Constant *blockFn
494 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, blockInfo,
495 CurFuncDecl, LocalDeclMap);
496 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
498 // If there is nothing to capture, we can emit this as a global block.
499 if (blockInfo.CanBeGlobal)
500 return buildGlobalBlock(CGM, blockInfo, blockFn);
502 // Otherwise, we have to emit this as a local block.
504 llvm::Constant *isa = CGM.getNSConcreteStackBlock();
505 isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy);
507 // Build the block descriptor.
508 llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo);
510 llvm::Type *intTy = ConvertType(getContext().IntTy);
512 llvm::AllocaInst *blockAddr =
513 CreateTempAlloca(blockInfo.StructureType, "block");
514 blockAddr->setAlignment(blockInfo.BlockAlign.getQuantity());
516 // Compute the initial on-stack block flags.
517 BlockFlags flags = BLOCK_HAS_SIGNATURE;
518 if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
519 if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ;
520 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
522 // Initialize the block literal.
523 Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa"));
524 Builder.CreateStore(llvm::ConstantInt::get(intTy, flags.getBitMask()),
525 Builder.CreateStructGEP(blockAddr, 1, "block.flags"));
526 Builder.CreateStore(llvm::ConstantInt::get(intTy, 0),
527 Builder.CreateStructGEP(blockAddr, 2, "block.reserved"));
528 Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3,
530 Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4,
531 "block.descriptor"));
533 // Finally, capture all the values into the block.
534 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
537 if (blockDecl->capturesCXXThis()) {
538 llvm::Value *addr = Builder.CreateStructGEP(blockAddr,
539 blockInfo.CXXThisIndex,
540 "block.captured-this.addr");
541 Builder.CreateStore(LoadCXXThis(), addr);
544 // Next, captured variables.
545 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
546 ce = blockDecl->capture_end(); ci != ce; ++ci) {
547 const VarDecl *variable = ci->getVariable();
548 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
550 // Ignore constant captures.
551 if (capture.isConstant()) continue;
553 QualType type = variable->getType();
555 // This will be a [[type]]*, except that a byref entry will just be
557 llvm::Value *blockField =
558 Builder.CreateStructGEP(blockAddr, capture.getIndex(),
561 // Compute the address of the thing we're going to move into the
564 if (ci->isNested()) {
565 // We need to use the capture from the enclosing block.
566 const CGBlockInfo::Capture &enclosingCapture =
567 BlockInfo->getCapture(variable);
569 // This is a [[type]]*, except that a byref entry wil just be an i8**.
570 src = Builder.CreateStructGEP(LoadBlockStruct(),
571 enclosingCapture.getIndex(),
572 "block.capture.addr");
574 // This is a [[type]]*.
575 src = LocalDeclMap[variable];
578 // For byrefs, we just write the pointer to the byref struct into
579 // the block field. There's no need to chase the forwarding
580 // pointer at this point, since we're building something that will
581 // live a shorter life than the stack byref anyway.
583 // Get a void* that points to the byref struct.
585 src = Builder.CreateLoad(src, "byref.capture");
587 src = Builder.CreateBitCast(src, VoidPtrTy);
589 // Write that void* into the capture field.
590 Builder.CreateStore(src, blockField);
592 // If we have a copy constructor, evaluate that into the block field.
593 } else if (const Expr *copyExpr = ci->getCopyExpr()) {
594 EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
596 // If it's a reference variable, copy the reference into the block field.
597 } else if (type->isReferenceType()) {
598 Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField);
600 // Otherwise, fake up a POD copy into the block field.
602 // Fake up a new variable so that EmitScalarInit doesn't think
603 // we're referring to the variable in its own initializer.
604 ImplicitParamDecl blockFieldPseudoVar(/*DC*/ 0, SourceLocation(),
607 // We use one of these or the other depending on whether the
608 // reference is nested.
609 DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue,
611 BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type,
612 VK_LValue, SourceLocation(), /*byref*/ false);
615 (ci->isNested() ? static_cast<Expr*>(&nested) : ¬Nested);
617 ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
619 EmitExprAsInit(&l2r, &blockFieldPseudoVar,
620 MakeAddrLValue(blockField, type,
621 getContext().getDeclAlign(variable)
623 /*captured by init*/ false);
626 // Push a destructor if necessary. The semantics for when this
627 // actually gets run are really obscure.
628 if (!ci->isByRef()) {
629 switch (QualType::DestructionKind dtorKind = type.isDestructedType()) {
630 case QualType::DK_none:
633 // Block captures count as local values and have imprecise semantics.
634 // They also can't be arrays, so need to worry about that.
635 case QualType::DK_objc_strong_lifetime: {
636 // This local is a GCC and MSVC compiler workaround.
637 Destroyer *destroyer = &destroyARCStrongImprecise;
638 pushDestroy(getCleanupKind(dtorKind), blockField, type,
639 *destroyer, /*useEHCleanupForArray*/ false);
643 case QualType::DK_objc_weak_lifetime:
644 case QualType::DK_cxx_destructor:
645 pushDestroy(dtorKind, blockField, type);
651 // Cast to the converted block-pointer type, which happens (somewhat
652 // unfortunately) to be a pointer to function type.
653 llvm::Value *result =
654 Builder.CreateBitCast(blockAddr,
655 ConvertType(blockInfo.getBlockExpr()->getType()));
661 llvm::Type *CodeGenModule::getBlockDescriptorType() {
662 if (BlockDescriptorType)
663 return BlockDescriptorType;
665 llvm::Type *UnsignedLongTy =
666 getTypes().ConvertType(getContext().UnsignedLongTy);
668 // struct __block_descriptor {
669 // unsigned long reserved;
670 // unsigned long block_size;
672 // // later, the following will be added
675 // void (*copyHelper)();
676 // void (*copyHelper)();
677 // } helpers; // !!! optional
679 // const char *signature; // the block signature
680 // const char *layout; // reserved
682 BlockDescriptorType =
683 llvm::StructType::create("struct.__block_descriptor",
684 UnsignedLongTy, UnsignedLongTy, NULL);
686 // Now form a pointer to that.
687 BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
688 return BlockDescriptorType;
691 llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
692 if (GenericBlockLiteralType)
693 return GenericBlockLiteralType;
695 llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
697 // struct __block_literal_generic {
701 // void (*__invoke)(void *);
702 // struct __block_descriptor *__descriptor;
704 GenericBlockLiteralType =
705 llvm::StructType::create("struct.__block_literal_generic",
706 VoidPtrTy, IntTy, IntTy, VoidPtrTy,
707 BlockDescPtrTy, NULL);
709 return GenericBlockLiteralType;
713 RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
714 ReturnValueSlot ReturnValue) {
715 const BlockPointerType *BPT =
716 E->getCallee()->getType()->getAs<BlockPointerType>();
718 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
720 // Get a pointer to the generic block literal.
721 llvm::Type *BlockLiteralTy =
722 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
724 // Bitcast the callee to a block literal.
725 llvm::Value *BlockLiteral =
726 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
728 // Get the function pointer from the literal.
729 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3);
731 BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy);
733 // Add the block literal.
735 Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
737 QualType FnType = BPT->getPointeeType();
739 // And the rest of the arguments.
740 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
741 E->arg_begin(), E->arg_end());
743 // Load the function.
744 llvm::Value *Func = Builder.CreateLoad(FuncPtr);
746 const FunctionType *FuncTy = FnType->castAs<FunctionType>();
747 const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(Args, FuncTy);
749 // Cast the function pointer to the right type.
750 llvm::Type *BlockFTy =
751 CGM.getTypes().GetFunctionType(FnInfo, false);
753 llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
754 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
756 // And call the block.
757 return EmitCall(FnInfo, Func, ReturnValue, Args);
760 llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
762 assert(BlockInfo && "evaluating block ref without block information?");
763 const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
765 // Handle constant captures.
766 if (capture.isConstant()) return LocalDeclMap[variable];
769 Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
770 "block.capture.addr");
773 // addr should be a void** right now. Load, then cast the result
776 addr = Builder.CreateLoad(addr);
777 llvm::PointerType *byrefPointerType
778 = llvm::PointerType::get(BuildByRefType(variable), 0);
779 addr = Builder.CreateBitCast(addr, byrefPointerType,
782 // Follow the forwarding pointer.
783 addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
784 addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
786 // Cast back to byref* and GEP over to the actual object.
787 addr = Builder.CreateBitCast(addr, byrefPointerType);
788 addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable),
789 variable->getNameAsString());
792 if (variable->getType()->isReferenceType())
793 addr = Builder.CreateLoad(addr, "ref.tmp");
799 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
801 CGBlockInfo blockInfo(blockExpr, name);
803 // Compute information about the layout, etc., of this block.
804 computeBlockInfo(*this, blockInfo);
806 // Using that metadata, generate the actual block function.
807 llvm::Constant *blockFn;
809 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
810 blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
814 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
816 return buildGlobalBlock(*this, blockInfo, blockFn);
819 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
820 const CGBlockInfo &blockInfo,
821 llvm::Constant *blockFn) {
822 assert(blockInfo.CanBeGlobal);
824 // Generate the constants for the block literal initializer.
825 llvm::Constant *fields[BlockHeaderSize];
828 fields[0] = CGM.getNSConcreteGlobalBlock();
831 BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
832 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
834 fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
837 fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
843 fields[4] = buildBlockDescriptor(CGM, blockInfo);
845 llvm::Constant *init = llvm::ConstantStruct::getAnon(fields);
847 llvm::GlobalVariable *literal =
848 new llvm::GlobalVariable(CGM.getModule(),
851 llvm::GlobalVariable::InternalLinkage,
853 "__block_literal_global");
854 literal->setAlignment(blockInfo.BlockAlign.getQuantity());
856 // Return a constant of the appropriately-casted type.
857 llvm::Type *requiredType =
858 CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
859 return llvm::ConstantExpr::getBitCast(literal, requiredType);
863 CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
864 const CGBlockInfo &blockInfo,
865 const Decl *outerFnDecl,
866 const DeclMapTy &ldm) {
867 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
869 // Check if we should generate debug info for this block function.
870 if (CGM.getModuleDebugInfo())
871 DebugInfo = CGM.getModuleDebugInfo();
873 BlockInfo = &blockInfo;
875 // Arrange for local static and local extern declarations to appear
876 // to be local to this function as well, in case they're directly
877 // referenced in a block.
878 for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
879 const VarDecl *var = dyn_cast<VarDecl>(i->first);
880 if (var && !var->hasLocalStorage())
881 LocalDeclMap[var] = i->second;
884 // Begin building the function declaration.
886 // Build the argument list.
887 FunctionArgList args;
889 // The first argument is the block pointer. Just take it as a void*
890 // and cast it later.
891 QualType selfTy = getContext().VoidPtrTy;
892 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
894 ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl),
895 SourceLocation(), II, selfTy);
896 args.push_back(&selfDecl);
898 // Now add the rest of the parameters.
899 for (BlockDecl::param_const_iterator i = blockDecl->param_begin(),
900 e = blockDecl->param_end(); i != e; ++i)
903 // Create the function declaration.
904 const FunctionProtoType *fnType =
905 cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType());
906 const CGFunctionInfo &fnInfo =
907 CGM.getTypes().getFunctionInfo(fnType->getResultType(), args,
908 fnType->getExtInfo());
909 if (CGM.ReturnTypeUsesSRet(fnInfo))
910 blockInfo.UsesStret = true;
912 llvm::FunctionType *fnLLVMType =
913 CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic());
916 CGM.getBlockMangledName(GD, name, blockDecl);
918 llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage,
919 name.getString(), &CGM.getModule());
920 CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
922 // Begin generating the function.
923 StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args,
924 blockInfo.getBlockExpr()->getBody()->getLocStart());
925 CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl
927 // Okay. Undo some of what StartFunction did.
929 // Pull the 'self' reference out of the local decl map.
930 llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
931 LocalDeclMap.erase(&selfDecl);
932 BlockPointer = Builder.CreateBitCast(blockAddr,
933 blockInfo.StructureType->getPointerTo(),
936 // If we have a C++ 'this' reference, go ahead and force it into
938 if (blockDecl->capturesCXXThis()) {
939 llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
940 blockInfo.CXXThisIndex,
941 "block.captured-this");
942 CXXThisValue = Builder.CreateLoad(addr, "this");
945 // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap;
947 if (const ObjCMethodDecl *method
948 = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) {
949 const VarDecl *self = method->getSelfDecl();
951 // There might not be a capture for 'self', but if there is...
952 if (blockInfo.Captures.count(self)) {
953 const CGBlockInfo::Capture &capture = blockInfo.getCapture(self);
954 llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer,
956 "block.captured-self");
957 LocalDeclMap[self] = selfAddr;
961 // Also force all the constant captures.
962 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
963 ce = blockDecl->capture_end(); ci != ce; ++ci) {
964 const VarDecl *variable = ci->getVariable();
965 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
966 if (!capture.isConstant()) continue;
968 unsigned align = getContext().getDeclAlign(variable).getQuantity();
970 llvm::AllocaInst *alloca =
971 CreateMemTemp(variable->getType(), "block.captured-const");
972 alloca->setAlignment(align);
974 Builder.CreateStore(capture.getConstant(), alloca, align);
976 LocalDeclMap[variable] = alloca;
979 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
980 llvm::BasicBlock *entry = Builder.GetInsertBlock();
981 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
984 EmitStmt(blockDecl->getBody());
986 // Remember where we were...
987 llvm::BasicBlock *resume = Builder.GetInsertBlock();
989 // Go back to the entry.
991 Builder.SetInsertPoint(entry, entry_ptr);
993 // Emit debug information for all the BlockDeclRefDecls.
994 // FIXME: also for 'this'
995 if (CGDebugInfo *DI = getDebugInfo()) {
996 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
997 ce = blockDecl->capture_end(); ci != ce; ++ci) {
998 const VarDecl *variable = ci->getVariable();
999 DI->EmitLocation(Builder, variable->getLocation());
1001 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1002 if (capture.isConstant()) {
1003 DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
1008 DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer,
1009 Builder, blockInfo);
1013 // And resume where we left off.
1015 Builder.ClearInsertionPoint();
1017 Builder.SetInsertPoint(resume);
1019 FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
1025 notes.push_back(HelperInfo());
1026 HelperInfo ¬e = notes.back();
1027 note.index = capture.getIndex();
1028 note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
1029 note.cxxbar_import = ci->getCopyExpr();
1031 if (ci->isByRef()) {
1032 note.flag = BLOCK_FIELD_IS_BYREF;
1033 if (type.isObjCGCWeak())
1034 note.flag |= BLOCK_FIELD_IS_WEAK;
1035 } else if (type->isBlockPointerType()) {
1036 note.flag = BLOCK_FIELD_IS_BLOCK;
1038 note.flag = BLOCK_FIELD_IS_OBJECT;
1045 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
1046 ASTContext &C = getContext();
1048 FunctionArgList args;
1049 ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1050 args.push_back(&dstDecl);
1051 ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1052 args.push_back(&srcDecl);
1054 const CGFunctionInfo &FI =
1055 CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
1057 // FIXME: it would be nice if these were mergeable with things with
1058 // identical semantics.
1059 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
1061 llvm::Function *Fn =
1062 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1063 "__copy_helper_block_", &CGM.getModule());
1066 = &CGM.getContext().Idents.get("__copy_helper_block_");
1068 // Check if we should generate debug info for this block helper function.
1069 if (CGM.getModuleDebugInfo())
1070 DebugInfo = CGM.getModuleDebugInfo();
1072 FunctionDecl *FD = FunctionDecl::Create(C,
1073 C.getTranslationUnitDecl(),
1075 SourceLocation(), II, C.VoidTy, 0,
1080 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1082 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
1084 llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1085 src = Builder.CreateLoad(src);
1086 src = Builder.CreateBitCast(src, structPtrTy, "block.source");
1088 llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
1089 dst = Builder.CreateLoad(dst);
1090 dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
1092 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1094 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1095 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1096 const VarDecl *variable = ci->getVariable();
1097 QualType type = variable->getType();
1099 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1100 if (capture.isConstant()) continue;
1102 const Expr *copyExpr = ci->getCopyExpr();
1103 BlockFieldFlags flags;
1105 bool isARCWeakCapture = false;
1108 assert(!ci->isByRef());
1109 // don't bother computing flags
1111 } else if (ci->isByRef()) {
1112 flags = BLOCK_FIELD_IS_BYREF;
1113 if (type.isObjCGCWeak())
1114 flags |= BLOCK_FIELD_IS_WEAK;
1116 } else if (type->isObjCRetainableType()) {
1117 flags = BLOCK_FIELD_IS_OBJECT;
1118 if (type->isBlockPointerType())
1119 flags = BLOCK_FIELD_IS_BLOCK;
1121 // Special rules for ARC captures:
1122 if (getLangOptions().ObjCAutoRefCount) {
1123 Qualifiers qs = type.getQualifiers();
1125 // Don't generate special copy logic for a captured object
1126 // unless it's __strong or __weak.
1127 if (!qs.hasStrongOrWeakObjCLifetime())
1130 // Support __weak direct captures.
1131 if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
1132 isARCWeakCapture = true;
1138 unsigned index = capture.getIndex();
1139 llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1140 llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
1142 // If there's an explicit copy expression, we do that.
1144 EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
1145 } else if (isARCWeakCapture) {
1146 EmitARCCopyWeak(dstField, srcField);
1148 llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1149 srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
1150 llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
1151 Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue,
1152 llvm::ConstantInt::get(Int32Ty, flags.getBitMask()));
1158 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
1162 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
1163 ASTContext &C = getContext();
1165 FunctionArgList args;
1166 ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1167 args.push_back(&srcDecl);
1169 const CGFunctionInfo &FI =
1170 CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
1172 // FIXME: We'd like to put these into a mergable by content, with
1173 // internal linkage.
1174 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
1176 llvm::Function *Fn =
1177 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1178 "__destroy_helper_block_", &CGM.getModule());
1180 // Check if we should generate debug info for this block destroy function.
1181 if (CGM.getModuleDebugInfo())
1182 DebugInfo = CGM.getModuleDebugInfo();
1185 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1187 FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
1189 SourceLocation(), II, C.VoidTy, 0,
1193 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1195 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
1197 llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1198 src = Builder.CreateLoad(src);
1199 src = Builder.CreateBitCast(src, structPtrTy, "block");
1201 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1203 CodeGenFunction::RunCleanupsScope cleanups(*this);
1205 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1206 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1207 const VarDecl *variable = ci->getVariable();
1208 QualType type = variable->getType();
1210 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1211 if (capture.isConstant()) continue;
1213 BlockFieldFlags flags;
1214 const CXXDestructorDecl *dtor = 0;
1216 bool isARCWeakCapture = false;
1218 if (ci->isByRef()) {
1219 flags = BLOCK_FIELD_IS_BYREF;
1220 if (type.isObjCGCWeak())
1221 flags |= BLOCK_FIELD_IS_WEAK;
1222 } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
1223 if (record->hasTrivialDestructor())
1225 dtor = record->getDestructor();
1226 } else if (type->isObjCRetainableType()) {
1227 flags = BLOCK_FIELD_IS_OBJECT;
1228 if (type->isBlockPointerType())
1229 flags = BLOCK_FIELD_IS_BLOCK;
1231 // Special rules for ARC captures.
1232 if (getLangOptions().ObjCAutoRefCount) {
1233 Qualifiers qs = type.getQualifiers();
1235 // Don't generate special dispose logic for a captured object
1236 // unless it's __strong or __weak.
1237 if (!qs.hasStrongOrWeakObjCLifetime())
1240 // Support __weak direct captures.
1241 if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
1242 isARCWeakCapture = true;
1248 unsigned index = capture.getIndex();
1249 llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1251 // If there's an explicit copy expression, we do that.
1253 PushDestructorCleanup(dtor, srcField);
1255 // If this is a __weak capture, emit the release directly.
1256 } else if (isARCWeakCapture) {
1257 EmitARCDestroyWeak(srcField);
1259 // Otherwise we call _Block_object_dispose. It wouldn't be too
1260 // hard to just emit this as a cleanup if we wanted to make sure
1261 // that things were done in reverse.
1263 llvm::Value *value = Builder.CreateLoad(srcField);
1264 value = Builder.CreateBitCast(value, VoidPtrTy);
1265 BuildBlockRelease(value, flags);
1269 cleanups.ForceCleanup();
1273 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
1278 /// Emits the copy/dispose helper functions for a __block object of id type.
1279 class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
1280 BlockFieldFlags Flags;
1283 ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
1284 : ByrefHelpers(alignment), Flags(flags) {}
1286 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1287 llvm::Value *srcField) {
1288 destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
1290 srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
1291 llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
1293 unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
1295 llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
1296 llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
1297 CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal);
1300 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1301 field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
1302 llvm::Value *value = CGF.Builder.CreateLoad(field);
1304 CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
1307 void profileImpl(llvm::FoldingSetNodeID &id) const {
1308 id.AddInteger(Flags.getBitMask());
1312 /// Emits the copy/dispose helpers for an ARC __block __weak variable.
1313 class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers {
1315 ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
1317 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1318 llvm::Value *srcField) {
1319 CGF.EmitARCMoveWeak(destField, srcField);
1322 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1323 CGF.EmitARCDestroyWeak(field);
1326 void profileImpl(llvm::FoldingSetNodeID &id) const {
1327 // 0 is distinguishable from all pointers and byref flags
1332 /// Emits the copy/dispose helpers for an ARC __block __strong variable
1333 /// that's not of block-pointer type.
1334 class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers {
1336 ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
1338 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1339 llvm::Value *srcField) {
1340 // Do a "move" by copying the value and then zeroing out the old
1343 llvm::Value *value = CGF.Builder.CreateLoad(srcField);
1345 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
1346 CGF.Builder.CreateStore(value, destField);
1347 CGF.Builder.CreateStore(null, srcField);
1350 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1351 llvm::Value *value = CGF.Builder.CreateLoad(field);
1352 CGF.EmitARCRelease(value, /*precise*/ false);
1355 void profileImpl(llvm::FoldingSetNodeID &id) const {
1356 // 1 is distinguishable from all pointers and byref flags
1361 /// Emits the copy/dispose helpers for a __block variable with a
1362 /// nontrivial copy constructor or destructor.
1363 class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
1365 const Expr *CopyExpr;
1368 CXXByrefHelpers(CharUnits alignment, QualType type,
1369 const Expr *copyExpr)
1370 : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
1372 bool needsCopy() const { return CopyExpr != 0; }
1373 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1374 llvm::Value *srcField) {
1375 if (!CopyExpr) return;
1376 CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
1379 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1380 EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
1381 CGF.PushDestructorCleanup(VarType, field);
1382 CGF.PopCleanupBlocks(cleanupDepth);
1385 void profileImpl(llvm::FoldingSetNodeID &id) const {
1386 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
1389 } // end anonymous namespace
1391 static llvm::Constant *
1392 generateByrefCopyHelper(CodeGenFunction &CGF,
1393 llvm::StructType &byrefType,
1394 CodeGenModule::ByrefHelpers &byrefInfo) {
1395 ASTContext &Context = CGF.getContext();
1397 QualType R = Context.VoidTy;
1399 FunctionArgList args;
1400 ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy);
1401 args.push_back(&dst);
1403 ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1404 args.push_back(&src);
1406 const CGFunctionInfo &FI =
1407 CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1409 CodeGenTypes &Types = CGF.CGM.getTypes();
1410 llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1412 // FIXME: We'd like to put these into a mergable by content, with
1413 // internal linkage.
1414 llvm::Function *Fn =
1415 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1416 "__Block_byref_object_copy_", &CGF.CGM.getModule());
1419 = &Context.Idents.get("__Block_byref_object_copy_");
1421 FunctionDecl *FD = FunctionDecl::Create(Context,
1422 Context.getTranslationUnitDecl(),
1424 SourceLocation(), II, R, 0,
1429 CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1431 if (byrefInfo.needsCopy()) {
1432 llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
1435 llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
1436 destField = CGF.Builder.CreateLoad(destField);
1437 destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
1438 destField = CGF.Builder.CreateStructGEP(destField, 6, "x");
1441 llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
1442 srcField = CGF.Builder.CreateLoad(srcField);
1443 srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
1444 srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x");
1446 byrefInfo.emitCopy(CGF, destField, srcField);
1449 CGF.FinishFunction();
1451 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1454 /// Build the copy helper for a __block variable.
1455 static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
1456 llvm::StructType &byrefType,
1457 CodeGenModule::ByrefHelpers &info) {
1458 CodeGenFunction CGF(CGM);
1459 return generateByrefCopyHelper(CGF, byrefType, info);
1462 /// Generate code for a __block variable's dispose helper.
1463 static llvm::Constant *
1464 generateByrefDisposeHelper(CodeGenFunction &CGF,
1465 llvm::StructType &byrefType,
1466 CodeGenModule::ByrefHelpers &byrefInfo) {
1467 ASTContext &Context = CGF.getContext();
1468 QualType R = Context.VoidTy;
1470 FunctionArgList args;
1471 ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1472 args.push_back(&src);
1474 const CGFunctionInfo &FI =
1475 CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1477 CodeGenTypes &Types = CGF.CGM.getTypes();
1478 llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1480 // FIXME: We'd like to put these into a mergable by content, with
1481 // internal linkage.
1482 llvm::Function *Fn =
1483 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1484 "__Block_byref_object_dispose_",
1485 &CGF.CGM.getModule());
1488 = &Context.Idents.get("__Block_byref_object_dispose_");
1490 FunctionDecl *FD = FunctionDecl::Create(Context,
1491 Context.getTranslationUnitDecl(),
1493 SourceLocation(), II, R, 0,
1497 CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1499 if (byrefInfo.needsDispose()) {
1500 llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
1501 V = CGF.Builder.CreateLoad(V);
1502 V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
1503 V = CGF.Builder.CreateStructGEP(V, 6, "x");
1505 byrefInfo.emitDispose(CGF, V);
1508 CGF.FinishFunction();
1510 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1513 /// Build the dispose helper for a __block variable.
1514 static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
1515 llvm::StructType &byrefType,
1516 CodeGenModule::ByrefHelpers &info) {
1517 CodeGenFunction CGF(CGM);
1518 return generateByrefDisposeHelper(CGF, byrefType, info);
1522 template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
1523 llvm::StructType &byrefTy,
1525 // Increase the field's alignment to be at least pointer alignment,
1526 // since the layout of the byref struct will guarantee at least that.
1527 byrefInfo.Alignment = std::max(byrefInfo.Alignment,
1528 CharUnits::fromQuantity(CGM.PointerAlignInBytes));
1530 llvm::FoldingSetNodeID id;
1531 byrefInfo.Profile(id);
1534 CodeGenModule::ByrefHelpers *node
1535 = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
1536 if (node) return static_cast<T*>(node);
1538 byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo);
1539 byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo);
1541 T *copy = new (CGM.getContext()) T(byrefInfo);
1542 CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
1546 CodeGenModule::ByrefHelpers *
1547 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
1548 const AutoVarEmission &emission) {
1549 const VarDecl &var = *emission.Variable;
1550 QualType type = var.getType();
1552 if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
1553 const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
1554 if (!copyExpr && record->hasTrivialDestructor()) return 0;
1556 CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
1557 return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1560 // Otherwise, if we don't have a retainable type, there's nothing to do.
1561 // that the runtime does extra copies.
1562 if (!type->isObjCRetainableType()) return 0;
1564 Qualifiers qs = type.getQualifiers();
1566 // If we have lifetime, that dominates.
1567 if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
1568 assert(getLangOptions().ObjCAutoRefCount);
1571 case Qualifiers::OCL_None: llvm_unreachable("impossible");
1573 // These are just bits as far as the runtime is concerned.
1574 case Qualifiers::OCL_ExplicitNone:
1575 case Qualifiers::OCL_Autoreleasing:
1578 // Tell the runtime that this is ARC __weak, called by the
1580 case Qualifiers::OCL_Weak: {
1581 ARCWeakByrefHelpers byrefInfo(emission.Alignment);
1582 return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1585 // ARC __strong __block variables need to be retained.
1586 case Qualifiers::OCL_Strong:
1587 // Block-pointers need to be _Block_copy'ed, so we let the
1588 // runtime be in charge. But we can't use the code below
1589 // because we don't want to set BYREF_CALLER, which will
1590 // just make the runtime ignore us.
1591 if (type->isBlockPointerType()) {
1592 BlockFieldFlags flags = BLOCK_FIELD_IS_BLOCK;
1593 ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
1594 return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1596 // Otherwise, we transfer ownership of the retain from the stack
1599 ARCStrongByrefHelpers byrefInfo(emission.Alignment);
1600 return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1603 llvm_unreachable("fell out of lifetime switch!");
1606 BlockFieldFlags flags;
1607 if (type->isBlockPointerType()) {
1608 flags |= BLOCK_FIELD_IS_BLOCK;
1609 } else if (CGM.getContext().isObjCNSObjectType(type) ||
1610 type->isObjCObjectPointerType()) {
1611 flags |= BLOCK_FIELD_IS_OBJECT;
1616 if (type.isObjCGCWeak())
1617 flags |= BLOCK_FIELD_IS_WEAK;
1619 ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
1620 return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1623 unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
1624 assert(ByRefValueInfo.count(VD) && "Did not find value!");
1626 return ByRefValueInfo.find(VD)->second.second;
1629 llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
1631 llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
1632 Loc = Builder.CreateLoad(Loc);
1633 Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
1634 V->getNameAsString());
1638 /// BuildByRefType - This routine changes a __block variable declared as T x
1643 /// void *__forwarding;
1644 /// int32_t __flags;
1646 /// void *__copy_helper; // only if needed
1647 /// void *__destroy_helper; // only if needed
1648 /// char padding[X]; // only if needed
1652 llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
1653 std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
1657 QualType Ty = D->getType();
1659 SmallVector<llvm::Type *, 8> types;
1661 llvm::StructType *ByRefType =
1662 llvm::StructType::create(getLLVMContext(),
1663 "struct.__block_byref_" + D->getNameAsString());
1666 types.push_back(Int8PtrTy);
1668 // void *__forwarding;
1669 types.push_back(llvm::PointerType::getUnqual(ByRefType));
1672 types.push_back(Int32Ty);
1675 types.push_back(Int32Ty);
1677 bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
1678 if (HasCopyAndDispose) {
1679 /// void *__copy_helper;
1680 types.push_back(Int8PtrTy);
1682 /// void *__destroy_helper;
1683 types.push_back(Int8PtrTy);
1686 bool Packed = false;
1687 CharUnits Align = getContext().getDeclAlign(D);
1688 if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
1689 // We have to insert padding.
1691 // The struct above has 2 32-bit integers.
1692 unsigned CurrentOffsetInBytes = 4 * 2;
1694 // And either 2 or 4 pointers.
1695 CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
1696 CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
1698 // Align the offset.
1699 unsigned AlignedOffsetInBytes =
1700 llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
1702 unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
1703 if (NumPaddingBytes > 0) {
1704 llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
1705 // FIXME: We need a sema error for alignment larger than the minimum of
1706 // the maximal stack alignment and the alignment of malloc on the system.
1707 if (NumPaddingBytes > 1)
1708 Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
1710 types.push_back(Ty);
1712 // We want a packed struct.
1718 types.push_back(ConvertTypeForMem(Ty));
1720 ByRefType->setBody(types, Packed);
1722 Info.first = ByRefType;
1724 Info.second = types.size() - 1;
1729 /// Initialize the structural components of a __block variable, i.e.
1730 /// everything but the actual object.
1731 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
1732 // Find the address of the local.
1733 llvm::Value *addr = emission.Address;
1735 // That's an alloca of the byref structure type.
1736 llvm::StructType *byrefType = cast<llvm::StructType>(
1737 cast<llvm::PointerType>(addr->getType())->getElementType());
1739 // Build the byref helpers if necessary. This is null if we don't need any.
1740 CodeGenModule::ByrefHelpers *helpers =
1741 buildByrefHelpers(*byrefType, emission);
1743 const VarDecl &D = *emission.Variable;
1744 QualType type = D.getType();
1748 // Initialize the 'isa', which is just 0 or 1.
1750 if (type.isObjCGCWeak())
1752 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
1753 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
1755 // Store the address of the variable into its own forwarding pointer.
1756 Builder.CreateStore(addr,
1757 Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
1760 // c) the flags field is set to either 0 if no helper functions are
1761 // needed or BLOCK_HAS_COPY_DISPOSE if they are,
1763 if (helpers) flags |= BLOCK_HAS_COPY_DISPOSE;
1764 Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
1765 Builder.CreateStructGEP(addr, 2, "byref.flags"));
1767 CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
1768 V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
1769 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
1772 llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
1773 Builder.CreateStore(helpers->CopyHelper, copy_helper);
1775 llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
1776 Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
1780 void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
1781 llvm::Value *F = CGM.getBlockObjectDispose();
1783 V = Builder.CreateBitCast(V, Int8PtrTy);
1784 N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask());
1785 Builder.CreateCall2(F, V, N);
1789 struct CallBlockRelease : EHScopeStack::Cleanup {
1791 CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
1793 void Emit(CodeGenFunction &CGF, Flags flags) {
1794 // Should we be passing FIELD_IS_WEAK here?
1795 CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
1800 /// Enter a cleanup to destroy a __block variable. Note that this
1801 /// cleanup should be a no-op if the variable hasn't left the stack
1802 /// yet; if a cleanup is required for the variable itself, that needs
1803 /// to be done externally.
1804 void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
1805 // We don't enter this cleanup if we're in pure-GC mode.
1806 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
1809 EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
1812 /// Adjust the declaration of something from the blocks API.
1813 static void configureBlocksRuntimeObject(CodeGenModule &CGM,
1814 llvm::Constant *C) {
1815 if (!CGM.getLangOptions().BlocksRuntimeOptional) return;
1817 llvm::GlobalValue *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
1818 if (GV->isDeclaration() &&
1819 GV->getLinkage() == llvm::GlobalValue::ExternalLinkage)
1820 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
1823 llvm::Constant *CodeGenModule::getBlockObjectDispose() {
1824 if (BlockObjectDispose)
1825 return BlockObjectDispose;
1827 llvm::Type *args[] = { Int8PtrTy, Int32Ty };
1828 llvm::FunctionType *fty
1829 = llvm::FunctionType::get(VoidTy, args, false);
1830 BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
1831 configureBlocksRuntimeObject(*this, BlockObjectDispose);
1832 return BlockObjectDispose;
1835 llvm::Constant *CodeGenModule::getBlockObjectAssign() {
1836 if (BlockObjectAssign)
1837 return BlockObjectAssign;
1839 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
1840 llvm::FunctionType *fty
1841 = llvm::FunctionType::get(VoidTy, args, false);
1842 BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
1843 configureBlocksRuntimeObject(*this, BlockObjectAssign);
1844 return BlockObjectAssign;
1847 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
1848 if (NSConcreteGlobalBlock)
1849 return NSConcreteGlobalBlock;
1851 NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock",
1852 Int8PtrTy->getPointerTo(), 0);
1853 configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
1854 return NSConcreteGlobalBlock;
1857 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
1858 if (NSConcreteStackBlock)
1859 return NSConcreteStackBlock;
1861 NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock",
1862 Int8PtrTy->getPointerTo(), 0);
1863 configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
1864 return NSConcreteStackBlock;