From a36e22399cc13100ee531adf80e9ed7b1ac8b2d4 Mon Sep 17 00:00:00 2001 From: Blaine Garst Date: Fri, 5 Mar 2010 01:29:59 +0000 Subject: [PATCH] add support for a 1<<29 bit in the block flags field to mark blocks using alternate struct return ABI git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97775 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGBlocks.cpp | 17 +++++- lib/CodeGen/CGBlocks.h | 4 +- test/CodeGen/blockstret.c | 110 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 test/CodeGen/blockstret.c diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp index 7076067e43..c10a401d8a 100644 --- a/lib/CodeGen/CGBlocks.cpp +++ b/lib/CodeGen/CGBlocks.cpp @@ -154,7 +154,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { { // C = BuildBlockStructInitlist(); - unsigned int flags = BLOCK_HAS_OBJC_TYPE; + unsigned int flags = BLOCK_HAS_SIGNATURE; // We run this first so that we set BlockHasCopyDispose from the entire // block literal. @@ -184,6 +184,18 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { Elts[0] = C; // __flags + { + QualType BPT = BE->getType(); + const FunctionType *ftype = BPT->getPointeeType()->getAs(); + QualType ResultType = ftype->getResultType(); + + CallArgList Args; + CodeGenTypes &Types = CGM.getTypes(); + const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args, + CC_Default, false); + if (CGM.ReturnTypeUsesSret(FnInfo)) + flags |= BLOCK_USE_STRET; + } const llvm::IntegerType *IntTy = cast( CGM.getTypes().ConvertType(CGM.getContext().IntTy)); C = llvm::ConstantInt::get(IntTy, flags); @@ -200,6 +212,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { // Optimize to being a global block. Elts[0] = CGM.getNSConcreteGlobalBlock(); + Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); C = llvm::ConstantStruct::get(VMContext, Elts, false); @@ -604,7 +617,7 @@ BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { // Flags LiteralFields[1] = - llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE); + llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE); // Reserved LiteralFields[2] = llvm::Constant::getNullValue(IntTy); diff --git a/lib/CodeGen/CGBlocks.h b/lib/CodeGen/CGBlocks.h index 39f26f8b13..e91319f6dd 100644 --- a/lib/CodeGen/CGBlocks.h +++ b/lib/CodeGen/CGBlocks.h @@ -54,10 +54,12 @@ public: BLOCK_HAS_COPY_DISPOSE = (1 << 25), BLOCK_HAS_CXX_OBJ = (1 << 26), BLOCK_IS_GLOBAL = (1 << 28), - BLOCK_HAS_OBJC_TYPE = (1 << 30) + BLOCK_USE_STRET = (1 << 29), + BLOCK_HAS_SIGNATURE = (1 << 30) }; }; + class BlockModule : public BlockBase { ASTContext &Context; llvm::Module &TheModule; diff --git a/test/CodeGen/blockstret.c b/test/CodeGen/blockstret.c new file mode 100644 index 0000000000..abe689ed2c --- /dev/null +++ b/test/CodeGen/blockstret.c @@ -0,0 +1,110 @@ +// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X64 +// RUN: %clang_cc1 -fblocks -triple i686-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X32 + +// X64: internal constant %2 { i8** @_NSConcreteGlobalBlock, i32 1879048192 +// X64: store i32 1610612736, i32* %want + +// X32: @_NSConcreteGlobalBlock, i32 1879048192, i32 0, +// X32: store i32 1610612736, i32* %want + +// rdar://7677537 + +#include + +typedef struct bigbig { + int array[512]; + char more[32]; +} BigStruct_t; + +BigStruct_t (^global)(void) = ^{ return *(BigStruct_t *)malloc(sizeof(struct bigbig)); }; + +const char * getBlockSignature(void *); + +BigStruct_t foo(int param) { + BigStruct_t x; + BigStruct_t (^f)(int) = ^(int param) { + BigStruct_t *result = malloc(sizeof(BigStruct_t)); + result->array[23] = param; + return *result; + }; + getBlockSignature(f); + return x; +} + +#if 1 +#include +#include +enum { + BLOCK_HAS_COPY_DISPOSE = (1 << 25), + BLOCK_HAS_CXX_OBJ = (1 << 26), + BLOCK_IS_GLOBAL = (1 << 28), + BLOCK_USE_STRET = (1 << 29), + BLOCK_HAS_OBJC_TYPE = (1 << 30) +}; + +struct block_descriptor_big { + unsigned long int reserved; + unsigned long int size; + void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE + void (*dispose)(void *); // conditional on BLOCK_HAS_COPY_DISPOSE + const char *signature; // conditional on BLOCK_HAS_OBJC + const char *layout; // conditional on BLOCK_HAS_OBJC +}; +struct block_descriptor_small { + unsigned long int reserved; + unsigned long int size; + const char *signature; // conditional on BLOCK_HAS_OBJC + const char *layout; // conditional on BLOCK_HAS_OBJC +}; + +struct block_layout_abi { // can't change + void *isa; + int flags; + int reserved; + void (*invoke)(void *, ...); + struct block_descriptor_big *descriptor; +}; + +const char *getBlockSignature(void *block) { + struct block_layout_abi *layout = (struct block_layout_abi *)block; + if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL; + if (layout->flags & BLOCK_HAS_COPY_DISPOSE) + return layout->descriptor->signature; + else + return ((struct block_descriptor_small *)layout->descriptor)->signature; +} + +bool usesStruct(void *block) { + struct block_layout_abi *layout = (struct block_layout_abi *)block; + int want = BLOCK_HAS_OBJC_TYPE | BLOCK_USE_STRET; + return (layout->flags & want) == want; +} + + +int main(int argc, char *argv[]) { + printf("desired global flags: %d\n", BLOCK_USE_STRET | BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE); + printf("desired stack flags: %d\n", BLOCK_USE_STRET | BLOCK_HAS_OBJC_TYPE); + + printf("should be non-zero: %d\n", usesStruct(global)); + BigStruct_t x; + BigStruct_t (^local)(int) = ^(int param) { + BigStruct_t *result = (BigStruct_t *)malloc(sizeof(BigStruct_t)); + result->array[23] = argc; + return *result; + }; + printf("should be non-zero: %d\n", usesStruct(global)); + printf("should be non-zero: %d\n", usesStruct(local)); + printf("should be zero: %d\n", usesStruct(^void(int x){ })); + return 0; +} + +/* +desired global flags: 1879048192 +desired stack flags: 1610612736 +should be non-zero: 0 +should be non-zero: 0 +should be non-zero: 1 +should be zero: 0 + +*/ +#endif -- 2.40.0