@@ -36,7 +36,7 @@ CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name)
36
36
: Name(name), CXXThisIndex(0 ), CanBeGlobal(false ), NeedsCopyDispose(false ),
37
37
HasCXXObject(false ), UsesStret(false ), HasCapturedVariableLayout(false ),
38
38
CapturesNonExternalType(false ), LocalAddress(Address::invalid()),
39
- StructureType(nullptr ), Block(block), DominatingIP( nullptr ) {
39
+ StructureType(nullptr ), Block(block) {
40
40
41
41
// Skip asm prefix, if any. 'name' is usually taken directly from
42
42
// the mangled name of the enclosing function.
@@ -775,151 +775,23 @@ static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF,
775
775
llvm::StructType::get (CGM.getLLVMContext (), elementTypes, true );
776
776
}
777
777
778
- // / Enter the scope of a block. This should be run at the entrance to
779
- // / a full-expression so that the block's cleanups are pushed at the
780
- // / right place in the stack.
781
- static void enterBlockScope (CodeGenFunction &CGF, BlockDecl *block) {
782
- assert (CGF.HaveInsertPoint ());
783
-
784
- // Allocate the block info and place it at the head of the list.
785
- CGBlockInfo &blockInfo =
786
- *new CGBlockInfo (block, CGF.CurFn ->getName ());
787
- blockInfo.NextBlockInfo = CGF.FirstBlockInfo ;
788
- CGF.FirstBlockInfo = &blockInfo;
789
-
790
- // Compute information about the layout, etc., of this block,
791
- // pushing cleanups as necessary.
792
- computeBlockInfo (CGF.CGM , &CGF, blockInfo);
793
-
794
- // Nothing else to do if it can be global.
795
- if (blockInfo.CanBeGlobal ) return ;
796
-
797
- // Make the allocation for the block.
798
- blockInfo.LocalAddress = CGF.CreateTempAlloca (blockInfo.StructureType ,
799
- blockInfo.BlockAlign , " block" );
800
-
801
- // If there are cleanups to emit, enter them (but inactive).
802
- if (!blockInfo.NeedsCopyDispose ) return ;
803
-
804
- // Walk through the captures (in order) and find the ones not
805
- // captured by constant.
806
- for (const auto &CI : block->captures ()) {
807
- // Ignore __block captures; there's nothing special in the
808
- // on-stack block that we need to do for them.
809
- if (CI.isByRef ()) continue ;
810
-
811
- // Ignore variables that are constant-captured.
812
- const VarDecl *variable = CI.getVariable ();
813
- CGBlockInfo::Capture &capture = blockInfo.getCapture (variable);
814
- if (capture.isConstant ()) continue ;
815
-
816
- // Ignore objects that aren't destructed.
817
- QualType VT = getCaptureFieldType (CGF, CI);
818
- QualType::DestructionKind dtorKind = VT.isDestructedType ();
819
- if (dtorKind == QualType::DK_none) continue ;
820
-
821
- CodeGenFunction::Destroyer *destroyer;
822
-
823
- // Block captures count as local values and have imprecise semantics.
824
- // They also can't be arrays, so need to worry about that.
825
- //
826
- // For const-qualified captures, emit clang.arc.use to ensure the captured
827
- // object doesn't get released while we are still depending on its validity
828
- // within the block.
829
- if (VT.isConstQualified () &&
830
- VT.getObjCLifetime () == Qualifiers::OCL_Strong &&
831
- CGF.CGM .getCodeGenOpts ().OptimizationLevel != 0 ) {
832
- assert (CGF.CGM .getLangOpts ().ObjCAutoRefCount &&
833
- " expected ObjC ARC to be enabled" );
834
- destroyer = CodeGenFunction::emitARCIntrinsicUse;
835
- } else if (dtorKind == QualType::DK_objc_strong_lifetime) {
836
- destroyer = CodeGenFunction::destroyARCStrongImprecise;
837
- } else {
838
- destroyer = CGF.getDestroyer (dtorKind);
839
- }
840
-
841
- // GEP down to the address.
842
- Address addr =
843
- CGF.Builder .CreateStructGEP (blockInfo.LocalAddress , capture.getIndex ());
844
-
845
- // We can use that GEP as the dominating IP.
846
- if (!blockInfo.DominatingIP )
847
- blockInfo.DominatingIP = cast<llvm::Instruction>(addr.getPointer ());
848
-
849
- CleanupKind cleanupKind = InactiveNormalCleanup;
850
- bool useArrayEHCleanup = CGF.needsEHCleanup (dtorKind);
851
- if (useArrayEHCleanup)
852
- cleanupKind = InactiveNormalAndEHCleanup;
853
-
854
- CGF.pushDestroy (cleanupKind, addr, VT,
855
- destroyer, useArrayEHCleanup);
856
-
857
- // Remember where that cleanup was.
858
- capture.setCleanup (CGF.EHStack .stable_begin ());
859
- }
860
- }
861
-
862
- // / Enter a full-expression with a non-trivial number of objects to
863
- // / clean up.
864
- void CodeGenFunction::enterNonTrivialFullExpression (const FullExpr *E) {
865
- if (const auto EWC = dyn_cast<ExprWithCleanups>(E)) {
866
- assert (EWC->getNumObjects () != 0 );
867
- for (const ExprWithCleanups::CleanupObject &C : EWC->getObjects ())
868
- if (auto *BD = C.dyn_cast <BlockDecl *>())
869
- enterBlockScope (*this , BD);
870
- }
871
- }
872
-
873
- // / Find the layout for the given block in a linked list and remove it.
874
- static CGBlockInfo *findAndRemoveBlockInfo (CGBlockInfo **head,
875
- const BlockDecl *block) {
876
- while (true ) {
877
- assert (head && *head);
878
- CGBlockInfo *cur = *head;
879
-
880
- // If this is the block we're looking for, splice it out of the list.
881
- if (cur->getBlockDecl () == block) {
882
- *head = cur->NextBlockInfo ;
883
- return cur;
884
- }
885
-
886
- head = &cur->NextBlockInfo ;
887
- }
888
- }
889
-
890
- // / Destroy a chain of block layouts.
891
- void CodeGenFunction::destroyBlockInfos (CGBlockInfo *head) {
892
- assert (head && " destroying an empty chain" );
893
- do {
894
- CGBlockInfo *cur = head;
895
- head = cur->NextBlockInfo ;
896
- delete cur;
897
- } while (head != nullptr );
898
- }
899
-
900
778
// / Emit a block literal expression in the current function.
901
779
llvm::Value *CodeGenFunction::EmitBlockLiteral (const BlockExpr *blockExpr) {
902
780
// If the block has no captures, we won't have a pre-computed
903
781
// layout for it.
904
- if (!blockExpr->getBlockDecl ()->hasCaptures ()) {
782
+ if (!blockExpr->getBlockDecl ()->hasCaptures ())
905
783
// The block literal is emitted as a global variable, and the block invoke
906
784
// function has to be extracted from its initializer.
907
- if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted (blockExpr)) {
785
+ if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted (blockExpr))
908
786
return Block;
909
- }
910
- CGBlockInfo blockInfo (blockExpr->getBlockDecl (), CurFn->getName ());
911
- computeBlockInfo (CGM, this , blockInfo);
912
- blockInfo.BlockExpression = blockExpr;
913
- return EmitBlockLiteral (blockInfo);
914
- }
915
-
916
- // Find the block info for this block and take ownership of it.
917
- std::unique_ptr<CGBlockInfo> blockInfo;
918
- blockInfo.reset (findAndRemoveBlockInfo (&FirstBlockInfo,
919
- blockExpr->getBlockDecl ()));
920
787
921
- blockInfo->BlockExpression = blockExpr;
922
- return EmitBlockLiteral (*blockInfo);
788
+ CGBlockInfo blockInfo (blockExpr->getBlockDecl (), CurFn->getName ());
789
+ computeBlockInfo (CGM, this , blockInfo);
790
+ blockInfo.BlockExpression = blockExpr;
791
+ if (!blockInfo.CanBeGlobal )
792
+ blockInfo.LocalAddress = CreateTempAlloca (blockInfo.StructureType ,
793
+ blockInfo.BlockAlign , " block" );
794
+ return EmitBlockLiteral (blockInfo);
923
795
}
924
796
925
797
llvm::Value *CodeGenFunction::EmitBlockLiteral (const CGBlockInfo &blockInfo) {
@@ -1161,12 +1033,64 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
1161
1033
/* captured by init*/ false );
1162
1034
}
1163
1035
1164
- // Activate the cleanup if layout pushed one.
1165
- if (!CI.isByRef ()) {
1166
- EHScopeStack::stable_iterator cleanup = capture.getCleanup ();
1167
- if (cleanup.isValid ())
1168
- ActivateCleanupBlock (cleanup, blockInfo.DominatingIP );
1036
+ // Push a cleanup for the capture if necessary.
1037
+ if (!blockInfo.NeedsCopyDispose )
1038
+ continue ;
1039
+
1040
+ // Ignore __block captures; there's nothing special in the on-stack block
1041
+ // that we need to do for them.
1042
+ if (CI.isByRef ())
1043
+ continue ;
1044
+
1045
+ // Ignore objects that aren't destructed.
1046
+ QualType::DestructionKind dtorKind = type.isDestructedType ();
1047
+ if (dtorKind == QualType::DK_none)
1048
+ continue ;
1049
+
1050
+ CodeGenFunction::Destroyer *destroyer;
1051
+
1052
+ // Block captures count as local values and have imprecise semantics.
1053
+ // They also can't be arrays, so need to worry about that.
1054
+ //
1055
+ // For const-qualified captures, emit clang.arc.use to ensure the captured
1056
+ // object doesn't get released while we are still depending on its validity
1057
+ // within the block.
1058
+ if (type.isConstQualified () &&
1059
+ type.getObjCLifetime () == Qualifiers::OCL_Strong &&
1060
+ CGM.getCodeGenOpts ().OptimizationLevel != 0 ) {
1061
+ assert (CGM.getLangOpts ().ObjCAutoRefCount &&
1062
+ " expected ObjC ARC to be enabled" );
1063
+ destroyer = emitARCIntrinsicUse;
1064
+ } else if (dtorKind == QualType::DK_objc_strong_lifetime) {
1065
+ destroyer = destroyARCStrongImprecise;
1066
+ } else {
1067
+ destroyer = getDestroyer (dtorKind);
1169
1068
}
1069
+
1070
+ CleanupKind cleanupKind = NormalCleanup;
1071
+ bool useArrayEHCleanup = needsEHCleanup (dtorKind);
1072
+ if (useArrayEHCleanup)
1073
+ cleanupKind = NormalAndEHCleanup;
1074
+
1075
+ // Extend the lifetime of the capture to the end of the scope enclosing the
1076
+ // block expression except when the block decl is in the list of RetExpr's
1077
+ // cleanup objects, in which case its lifetime ends after the full
1078
+ // expression.
1079
+ auto IsBlockDeclInRetExpr = [&]() {
1080
+ auto *EWC = llvm::dyn_cast_or_null<ExprWithCleanups>(RetExpr);
1081
+ if (EWC)
1082
+ for (auto &C : EWC->getObjects ())
1083
+ if (auto *BD = C.dyn_cast <BlockDecl *>())
1084
+ if (BD == blockDecl)
1085
+ return true ;
1086
+ return false ;
1087
+ };
1088
+
1089
+ if (IsBlockDeclInRetExpr ())
1090
+ pushDestroy (cleanupKind, blockField, type, destroyer, useArrayEHCleanup);
1091
+ else
1092
+ pushLifetimeExtendedDestroy (cleanupKind, blockField, type, destroyer,
1093
+ useArrayEHCleanup);
1170
1094
}
1171
1095
1172
1096
// Cast to the converted block-pointer type, which happens (somewhat
0 commit comments