2009-03-04 23:35:22 +08:00
|
|
|
//===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-03-04 23:35:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the internal state used for llvm translation for block literals.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
|
|
|
|
#define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
|
2009-03-04 23:35:22 +08:00
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGBuilder.h"
|
|
|
|
#include "CGCall.h"
|
|
|
|
#include "CGValue.h"
|
|
|
|
#include "CodeGenFunction.h"
|
2009-03-05 02:17:45 +08:00
|
|
|
#include "CodeGenTypes.h"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2009-03-05 02:17:45 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-03-05 02:17:45 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2014-05-09 08:26:20 +08:00
|
|
|
class Constant;
|
|
|
|
class Function;
|
|
|
|
class GlobalValue;
|
|
|
|
class DataLayout;
|
|
|
|
class FunctionType;
|
|
|
|
class PointerType;
|
|
|
|
class Value;
|
|
|
|
class LLVMContext;
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2009-03-05 02:17:45 +08:00
|
|
|
|
2009-03-04 23:35:22 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace CodeGen {
|
2011-02-08 16:22:06 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
class CGBlockInfo;
|
2009-03-04 23:35:22 +08:00
|
|
|
|
2012-10-26 09:13:38 +08:00
|
|
|
// Flags stored in __block variables.
|
|
|
|
enum BlockByrefFlags {
|
|
|
|
BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
|
|
|
|
BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
|
|
|
|
BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
|
|
|
|
BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
|
|
|
|
BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
|
2012-10-27 04:33:59 +08:00
|
|
|
BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
|
|
|
|
BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
|
2012-10-26 09:13:38 +08:00
|
|
|
};
|
|
|
|
|
2012-10-26 06:55:52 +08:00
|
|
|
enum BlockLiteralFlags {
|
2018-07-21 01:10:32 +08:00
|
|
|
BLOCK_IS_NOESCAPE = (1 << 23),
|
2011-02-08 16:22:06 +08:00
|
|
|
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
|
|
|
|
BLOCK_HAS_CXX_OBJ = (1 << 26),
|
|
|
|
BLOCK_IS_GLOBAL = (1 << 28),
|
|
|
|
BLOCK_USE_STRET = (1 << 29),
|
2012-11-11 02:30:40 +08:00
|
|
|
BLOCK_HAS_SIGNATURE = (1 << 30),
|
2018-09-25 01:51:15 +08:00
|
|
|
BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
|
2009-03-04 23:35:22 +08:00
|
|
|
};
|
2011-02-08 16:22:06 +08:00
|
|
|
class BlockFlags {
|
|
|
|
uint32_t flags;
|
2009-03-04 23:35:22 +08:00
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
public:
|
2012-11-15 01:15:51 +08:00
|
|
|
BlockFlags(uint32_t flags) : flags(flags) {}
|
2011-02-08 16:22:06 +08:00
|
|
|
BlockFlags() : flags(0) {}
|
2012-10-26 06:55:52 +08:00
|
|
|
BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
|
2012-11-15 01:15:51 +08:00
|
|
|
BlockFlags(BlockByrefFlags flag) : flags(flag) {}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
uint32_t getBitMask() const { return flags; }
|
|
|
|
bool empty() const { return flags == 0; }
|
2010-02-19 08:24:37 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
|
|
|
|
return BlockFlags(l.flags | r.flags);
|
|
|
|
}
|
|
|
|
friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
|
|
|
|
l.flags |= r.flags;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
friend bool operator&(BlockFlags l, BlockFlags r) {
|
|
|
|
return (l.flags & r.flags);
|
2009-03-05 02:17:45 +08:00
|
|
|
}
|
2012-11-15 01:15:51 +08:00
|
|
|
bool operator==(BlockFlags r) {
|
|
|
|
return (flags == r.flags);
|
|
|
|
}
|
2009-03-04 23:35:22 +08:00
|
|
|
};
|
2012-10-26 06:55:52 +08:00
|
|
|
inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
|
2011-02-08 16:22:06 +08:00
|
|
|
return BlockFlags(l) | BlockFlags(r);
|
|
|
|
}
|
2009-03-04 23:35:22 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
enum BlockFieldFlag_t {
|
|
|
|
BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
|
|
|
|
block, ... */
|
|
|
|
BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
|
|
|
|
variable */
|
|
|
|
BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
|
|
|
|
helpers */
|
2011-06-16 07:02:42 +08:00
|
|
|
BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
|
2011-02-08 16:22:06 +08:00
|
|
|
BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
|
|
|
|
support routines */
|
|
|
|
BLOCK_BYREF_CURRENT_MAX = 256
|
|
|
|
};
|
2010-06-27 15:15:29 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
class BlockFieldFlags {
|
|
|
|
uint32_t flags;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
BlockFieldFlags(uint32_t flags) : flags(flags) {}
|
|
|
|
public:
|
|
|
|
BlockFieldFlags() : flags(0) {}
|
|
|
|
BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
|
2009-03-07 10:35:30 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
uint32_t getBitMask() const { return flags; }
|
|
|
|
bool empty() const { return flags == 0; }
|
2009-03-05 02:57:26 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
/// Answers whether the flags indicate that this field is an object
|
|
|
|
/// or block pointer that requires _Block_object_assign/dispose.
|
|
|
|
bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
|
2009-03-05 02:57:26 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
|
|
|
|
return BlockFieldFlags(l.flags | r.flags);
|
|
|
|
}
|
|
|
|
friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
|
|
|
|
l.flags |= r.flags;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
|
|
|
|
return (l.flags & r.flags);
|
|
|
|
}
|
2018-08-17 23:46:07 +08:00
|
|
|
bool operator==(BlockFieldFlags Other) const {
|
|
|
|
return flags == Other.flags;
|
|
|
|
}
|
2011-02-08 16:22:06 +08:00
|
|
|
};
|
|
|
|
inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
|
|
|
|
return BlockFieldFlags(l) | BlockFieldFlags(r);
|
|
|
|
}
|
2009-03-07 10:35:30 +08:00
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
/// Information about the layout of a __block variable.
|
|
|
|
class BlockByrefInfo {
|
|
|
|
public:
|
|
|
|
llvm::StructType *Type;
|
|
|
|
unsigned FieldIndex;
|
|
|
|
CharUnits ByrefAlignment;
|
|
|
|
CharUnits FieldOffset;
|
|
|
|
};
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
/// CGBlockInfo - Information to generate a block literal.
|
|
|
|
class CGBlockInfo {
|
|
|
|
public:
|
|
|
|
/// Name - The name of the block, kindof.
|
2013-01-13 03:30:44 +08:00
|
|
|
StringRef Name;
|
2011-02-08 16:22:06 +08:00
|
|
|
|
|
|
|
/// The field index of 'this' within the block, if there is one.
|
|
|
|
unsigned CXXThisIndex;
|
|
|
|
|
|
|
|
class Capture {
|
|
|
|
uintptr_t Data;
|
2011-11-10 16:15:53 +08:00
|
|
|
EHScopeStack::stable_iterator Cleanup;
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
CharUnits::QuantityType Offset;
|
2011-02-08 16:22:06 +08:00
|
|
|
|
2016-09-16 08:02:06 +08:00
|
|
|
/// Type of the capture field. Normally, this is identical to the type of
|
|
|
|
/// the capture's VarDecl, but can be different if there is an enclosing
|
|
|
|
/// lambda.
|
|
|
|
QualType FieldType;
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
public:
|
|
|
|
bool isIndex() const { return (Data & 1) != 0; }
|
|
|
|
bool isConstant() const { return !isIndex(); }
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
|
|
|
|
unsigned getIndex() const {
|
|
|
|
assert(isIndex());
|
|
|
|
return Data >> 1;
|
|
|
|
}
|
|
|
|
CharUnits getOffset() const {
|
|
|
|
assert(isIndex());
|
|
|
|
return CharUnits::fromQuantity(Offset);
|
2011-02-08 16:22:06 +08:00
|
|
|
}
|
2011-11-10 16:15:53 +08:00
|
|
|
EHScopeStack::stable_iterator getCleanup() const {
|
|
|
|
assert(isIndex());
|
|
|
|
return Cleanup;
|
|
|
|
}
|
|
|
|
void setCleanup(EHScopeStack::stable_iterator cleanup) {
|
|
|
|
assert(isIndex());
|
|
|
|
Cleanup = cleanup;
|
|
|
|
}
|
2011-02-08 16:22:06 +08:00
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
llvm::Value *getConstant() const {
|
|
|
|
assert(isConstant());
|
|
|
|
return reinterpret_cast<llvm::Value*>(Data);
|
|
|
|
}
|
|
|
|
|
2016-09-16 08:02:06 +08:00
|
|
|
QualType fieldType() const {
|
|
|
|
return FieldType;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Capture makeIndex(unsigned index, CharUnits offset,
|
|
|
|
QualType FieldType) {
|
2011-02-08 16:22:06 +08:00
|
|
|
Capture v;
|
|
|
|
v.Data = (index << 1) | 1;
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
v.Offset = offset.getQuantity();
|
2016-09-16 08:02:06 +08:00
|
|
|
v.FieldType = FieldType;
|
2011-02-08 16:22:06 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Capture makeConstant(llvm::Value *value) {
|
|
|
|
Capture v;
|
|
|
|
v.Data = reinterpret_cast<uintptr_t>(value);
|
|
|
|
return v;
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-02-08 16:22:06 +08:00
|
|
|
};
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
/// CanBeGlobal - True if the block can be global, i.e. it has
|
|
|
|
/// no non-constant captures.
|
|
|
|
bool CanBeGlobal : 1;
|
|
|
|
|
2018-07-21 01:10:32 +08:00
|
|
|
/// True if the block has captures that would necessitate custom copy or
|
|
|
|
/// dispose helper functions if the block were escaping.
|
2011-02-08 16:22:06 +08:00
|
|
|
bool NeedsCopyDispose : 1;
|
|
|
|
|
|
|
|
/// HasCXXObject - True if the block's custom copy/dispose functions
|
|
|
|
/// need to be run even in GC mode.
|
|
|
|
bool HasCXXObject : 1;
|
|
|
|
|
2011-03-09 16:39:33 +08:00
|
|
|
/// UsesStret : True if the block uses an stret return. Mutable
|
|
|
|
/// because it gets set later in the block-creation process.
|
|
|
|
mutable bool UsesStret : 1;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-11-02 02:32:55 +08:00
|
|
|
/// HasCapturedVariableLayout : True if block has captured variables
|
|
|
|
/// and their layout meta-data has been generated.
|
|
|
|
bool HasCapturedVariableLayout : 1;
|
2011-03-09 16:39:33 +08:00
|
|
|
|
2018-08-10 23:09:24 +08:00
|
|
|
/// Indicates whether an object of a non-external C++ class is captured. This
|
|
|
|
/// bit is used to determine the linkage of the block copy/destroy helper
|
|
|
|
/// functions.
|
|
|
|
bool CapturesNonExternalType : 1;
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
/// The mapping of allocated indexes within the block.
|
2018-07-31 03:24:48 +08:00
|
|
|
llvm::DenseMap<const VarDecl*, Capture> Captures;
|
2011-11-10 16:15:53 +08:00
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
Address LocalAddress;
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *StructureType;
|
2011-11-10 16:15:53 +08:00
|
|
|
const BlockDecl *Block;
|
|
|
|
const BlockExpr *BlockExpression;
|
2011-02-08 16:22:06 +08:00
|
|
|
CharUnits BlockSize;
|
|
|
|
CharUnits BlockAlign;
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
CharUnits CXXThisOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-05 01:20:57 +08:00
|
|
|
// Offset of the gap caused by block header having a smaller
|
|
|
|
// alignment than the alignment of the block descriptor. This
|
|
|
|
// is the gap offset before the first capturued field.
|
|
|
|
CharUnits BlockHeaderForcedGapOffset;
|
|
|
|
// Gap size caused by aligning first field after block header.
|
|
|
|
// This could be zero if no forced alignment is required.
|
|
|
|
CharUnits BlockHeaderForcedGapSize;
|
2011-11-10 18:43:54 +08:00
|
|
|
|
|
|
|
/// The next block in the block-info chain. Invalid if this block
|
|
|
|
/// info is not part of the CGF's block-info chain, which is true
|
|
|
|
/// if it corresponds to a global block or a block whose expression
|
|
|
|
/// has been encountered.
|
2011-11-10 16:15:53 +08:00
|
|
|
CGBlockInfo *NextBlockInfo;
|
2011-02-08 16:22:06 +08:00
|
|
|
|
|
|
|
const Capture &getCapture(const VarDecl *var) const {
|
2011-11-10 16:15:53 +08:00
|
|
|
return const_cast<CGBlockInfo*>(this)->getCapture(var);
|
|
|
|
}
|
|
|
|
Capture &getCapture(const VarDecl *var) {
|
|
|
|
llvm::DenseMap<const VarDecl*, Capture>::iterator
|
2011-02-08 16:22:06 +08:00
|
|
|
it = Captures.find(var);
|
|
|
|
assert(it != Captures.end() && "no entry for variable!");
|
|
|
|
return it->second;
|
|
|
|
}
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
const BlockDecl *getBlockDecl() const { return Block; }
|
|
|
|
const BlockExpr *getBlockExpr() const {
|
|
|
|
assert(BlockExpression);
|
|
|
|
assert(BlockExpression->getBlockDecl() == Block);
|
|
|
|
return BlockExpression;
|
|
|
|
}
|
2009-03-05 02:57:26 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
|
2018-07-21 01:10:32 +08:00
|
|
|
|
|
|
|
// Indicates whether the block needs a custom copy or dispose function.
|
|
|
|
bool needsCopyDisposeHelpers() const {
|
|
|
|
return NeedsCopyDispose && !Block->doesNotEscape();
|
|
|
|
}
|
2009-03-04 23:35:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace CodeGen
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|