2011-01-28 19:13:47 +08:00
|
|
|
//===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
|
|
|
|
//
|
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
|
2011-01-28 19:13:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code dealing with the IR generation for cleanups
|
|
|
|
// and related information.
|
|
|
|
//
|
|
|
|
// A "cleanup" is a piece of code which needs to be executed whenever
|
|
|
|
// control transfers out of a particular scope. This can be
|
|
|
|
// conditionalized to occur only on exceptional control flow, only on
|
|
|
|
// normal control flow, or both.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGCleanup.h"
|
2013-06-10 00:56:53 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2015-10-29 07:06:42 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) {
|
|
|
|
if (rv.isScalar())
|
|
|
|
return DominatingLLVMValue::needsSaving(rv.getScalarVal());
|
|
|
|
if (rv.isAggregate())
|
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
|
|
|
return DominatingLLVMValue::needsSaving(rv.getAggregatePointer());
|
2011-01-28 19:13:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DominatingValue<RValue>::saved_type
|
|
|
|
DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) {
|
|
|
|
if (rv.isScalar()) {
|
|
|
|
llvm::Value *V = rv.getScalarVal();
|
|
|
|
|
|
|
|
// These automatically dominate and don't need to be saved.
|
|
|
|
if (!DominatingLLVMValue::needsSaving(V))
|
|
|
|
return saved_type(V, ScalarLiteral);
|
|
|
|
|
|
|
|
// Everything else needs an alloca.
|
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 addr =
|
|
|
|
CGF.CreateDefaultAlignTempAlloca(V->getType(), "saved-rvalue");
|
2011-01-28 19:13:47 +08:00
|
|
|
CGF.Builder.CreateStore(V, addr);
|
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
|
|
|
return saved_type(addr.getPointer(), ScalarAddress);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv.isComplex()) {
|
|
|
|
CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ComplexTy =
|
2017-05-10 03:31:30 +08:00
|
|
|
llvm::StructType::get(V.first->getType(), V.second->getType());
|
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 addr = CGF.CreateDefaultAlignTempAlloca(ComplexTy, "saved-complex");
|
2019-02-10 06:22:28 +08:00
|
|
|
CGF.Builder.CreateStore(V.first, CGF.Builder.CreateStructGEP(addr, 0));
|
|
|
|
CGF.Builder.CreateStore(V.second, CGF.Builder.CreateStructGEP(addr, 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
|
|
|
return saved_type(addr.getPointer(), ComplexAddress);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(rv.isAggregate());
|
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 V = rv.getAggregateAddress(); // TODO: volatile?
|
|
|
|
if (!DominatingLLVMValue::needsSaving(V.getPointer()))
|
|
|
|
return saved_type(V.getPointer(), AggregateLiteral,
|
|
|
|
V.getAlignment().getQuantity());
|
|
|
|
|
|
|
|
Address addr =
|
|
|
|
CGF.CreateTempAlloca(V.getType(), CGF.getPointerAlign(), "saved-rvalue");
|
|
|
|
CGF.Builder.CreateStore(V.getPointer(), addr);
|
|
|
|
return saved_type(addr.getPointer(), AggregateAddress,
|
|
|
|
V.getAlignment().getQuantity());
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a saved r-value produced by SaveRValue, perform the code
|
|
|
|
/// necessary to restore it to usability at the current insertion
|
|
|
|
/// point.
|
|
|
|
RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
|
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
|
|
|
auto getSavingAddress = [&](llvm::Value *value) {
|
|
|
|
auto alignment = cast<llvm::AllocaInst>(value)->getAlignment();
|
|
|
|
return Address(value, CharUnits::fromQuantity(alignment));
|
|
|
|
};
|
2011-01-28 19:13:47 +08:00
|
|
|
switch (K) {
|
|
|
|
case ScalarLiteral:
|
|
|
|
return RValue::get(Value);
|
|
|
|
case ScalarAddress:
|
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
|
|
|
return RValue::get(CGF.Builder.CreateLoad(getSavingAddress(Value)));
|
2011-01-28 19:13:47 +08:00
|
|
|
case AggregateLiteral:
|
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
|
|
|
return RValue::getAggregate(Address(Value, CharUnits::fromQuantity(Align)));
|
|
|
|
case AggregateAddress: {
|
|
|
|
auto addr = CGF.Builder.CreateLoad(getSavingAddress(Value));
|
|
|
|
return RValue::getAggregate(Address(addr, CharUnits::fromQuantity(Align)));
|
|
|
|
}
|
2013-03-08 05:37:08 +08:00
|
|
|
case ComplexAddress: {
|
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 address = getSavingAddress(Value);
|
2019-02-10 06:22:28 +08:00
|
|
|
llvm::Value *real =
|
|
|
|
CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 0));
|
|
|
|
llvm::Value *imag =
|
|
|
|
CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 1));
|
2013-03-08 05:37:08 +08:00
|
|
|
return RValue::getComplex(real, imag);
|
|
|
|
}
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("bad saved r-value kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push an entry of the given size onto this protected-scope stack.
|
|
|
|
char *EHScopeStack::allocate(size_t Size) {
|
2016-01-15 05:00:27 +08:00
|
|
|
Size = llvm::alignTo(Size, ScopeStackAlignment);
|
2011-01-28 19:13:47 +08:00
|
|
|
if (!StartOfBuffer) {
|
|
|
|
unsigned Capacity = 1024;
|
|
|
|
while (Capacity < Size) Capacity *= 2;
|
|
|
|
StartOfBuffer = new char[Capacity];
|
|
|
|
StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
|
|
|
|
} else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
|
|
|
|
unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
|
|
|
|
unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
|
|
|
|
|
|
|
|
unsigned NewCapacity = CurrentCapacity;
|
|
|
|
do {
|
|
|
|
NewCapacity *= 2;
|
|
|
|
} while (NewCapacity < UsedCapacity + Size);
|
|
|
|
|
|
|
|
char *NewStartOfBuffer = new char[NewCapacity];
|
|
|
|
char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
|
|
|
|
char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
|
|
|
|
memcpy(NewStartOfData, StartOfData, UsedCapacity);
|
|
|
|
delete [] StartOfBuffer;
|
|
|
|
StartOfBuffer = NewStartOfBuffer;
|
|
|
|
EndOfBuffer = NewEndOfBuffer;
|
|
|
|
StartOfData = NewStartOfData;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartOfBuffer + Size <= StartOfData);
|
|
|
|
StartOfData -= Size;
|
|
|
|
return StartOfData;
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:21:37 +08:00
|
|
|
void EHScopeStack::deallocate(size_t Size) {
|
2016-01-15 05:00:27 +08:00
|
|
|
StartOfData += llvm::alignTo(Size, ScopeStackAlignment);
|
2015-07-18 02:21:37 +08:00
|
|
|
}
|
|
|
|
|
2015-04-23 05:38:15 +08:00
|
|
|
bool EHScopeStack::containsOnlyLifetimeMarkers(
|
|
|
|
EHScopeStack::stable_iterator Old) const {
|
|
|
|
for (EHScopeStack::iterator it = begin(); stabilize(it) != Old; it++) {
|
|
|
|
EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*it);
|
|
|
|
if (!cleanup || !cleanup->isLifetimeMarker())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-02 06:58:55 +08:00
|
|
|
bool EHScopeStack::requiresLandingPad() const {
|
|
|
|
for (stable_iterator si = getInnermostEHScope(); si != stable_end(); ) {
|
|
|
|
// Skip lifetime markers.
|
|
|
|
if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si)))
|
|
|
|
if (cleanup->isLifetimeMarker()) {
|
|
|
|
si = cleanup->getEnclosingEHScope();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
EHScopeStack::stable_iterator
|
2011-08-11 10:22:43 +08:00
|
|
|
EHScopeStack::getInnermostActiveNormalCleanup() const {
|
|
|
|
for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
|
|
|
|
si != se; ) {
|
|
|
|
EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
|
|
|
|
if (cleanup.isActive()) return si;
|
|
|
|
si = cleanup.getEnclosingNormalCleanup();
|
|
|
|
}
|
|
|
|
return stable_end();
|
|
|
|
}
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
|
|
|
|
char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
|
|
|
|
bool IsNormalCleanup = Kind & NormalCleanup;
|
|
|
|
bool IsEHCleanup = Kind & EHCleanup;
|
|
|
|
bool IsActive = !(Kind & InactiveCleanup);
|
2016-07-02 05:08:47 +08:00
|
|
|
bool IsLifetimeMarker = Kind & LifetimeMarker;
|
2011-01-28 19:13:47 +08:00
|
|
|
EHCleanupScope *Scope =
|
|
|
|
new (Buffer) EHCleanupScope(IsNormalCleanup,
|
|
|
|
IsEHCleanup,
|
|
|
|
IsActive,
|
|
|
|
Size,
|
|
|
|
BranchFixups.size(),
|
|
|
|
InnermostNormalCleanup,
|
2011-08-11 10:22:43 +08:00
|
|
|
InnermostEHScope);
|
2011-01-28 19:13:47 +08:00
|
|
|
if (IsNormalCleanup)
|
|
|
|
InnermostNormalCleanup = stable_begin();
|
|
|
|
if (IsEHCleanup)
|
2011-08-11 10:22:43 +08:00
|
|
|
InnermostEHScope = stable_begin();
|
2016-07-02 05:08:47 +08:00
|
|
|
if (IsLifetimeMarker)
|
|
|
|
Scope->setLifetimeMarker();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
return Scope->getCleanupBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EHScopeStack::popCleanup() {
|
|
|
|
assert(!empty() && "popping exception stack when not empty");
|
|
|
|
|
|
|
|
assert(isa<EHCleanupScope>(*begin()));
|
|
|
|
EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
|
|
|
|
InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
|
2011-08-11 10:22:43 +08:00
|
|
|
InnermostEHScope = Cleanup.getEnclosingEHScope();
|
2015-07-18 02:21:37 +08:00
|
|
|
deallocate(Cleanup.getAllocatedSize());
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Destroy the cleanup.
|
2014-10-09 02:31:54 +08:00
|
|
|
Cleanup.Destroy();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Check whether we can shrink the branch-fixups stack.
|
|
|
|
if (!BranchFixups.empty()) {
|
|
|
|
// If we no longer have any normal cleanups, all the fixups are
|
|
|
|
// complete.
|
|
|
|
if (!hasNormalCleanups())
|
|
|
|
BranchFixups.clear();
|
|
|
|
|
|
|
|
// Otherwise we can still trim out unnecessary nulls.
|
|
|
|
else
|
|
|
|
popNullFixups();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
|
|
|
|
assert(getInnermostEHScope() == stable_end());
|
|
|
|
char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
|
|
|
|
EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
|
|
|
|
InnermostEHScope = stable_begin();
|
|
|
|
return filter;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EHScopeStack::popFilter() {
|
|
|
|
assert(!empty() && "popping exception stack when not empty");
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHFilterScope &filter = cast<EHFilterScope>(*begin());
|
2015-07-18 02:21:37 +08:00
|
|
|
deallocate(EHFilterScope::getSizeForNumFilters(filter.getNumFilters()));
|
2011-01-28 19:13:47 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
InnermostEHScope = filter.getEnclosingEHScope();
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
|
|
|
|
char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
|
|
|
|
EHCatchScope *scope =
|
|
|
|
new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
|
|
|
|
InnermostEHScope = stable_begin();
|
|
|
|
return scope;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EHScopeStack::pushTerminate() {
|
|
|
|
char *Buffer = allocate(EHTerminateScope::getSize());
|
2011-08-11 10:22:43 +08:00
|
|
|
new (Buffer) EHTerminateScope(InnermostEHScope);
|
|
|
|
InnermostEHScope = stable_begin();
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove any 'null' fixups on the stack. However, we can't pop more
|
|
|
|
/// fixups than the fixup depth on the innermost normal cleanup, or
|
|
|
|
/// else fixups that we try to add to that cleanup will end up in the
|
|
|
|
/// wrong place. We *could* try to shrink fixup depths, but that's
|
|
|
|
/// actually a lot of work for little benefit.
|
|
|
|
void EHScopeStack::popNullFixups() {
|
|
|
|
// We expect this to only be called when there's still an innermost
|
|
|
|
// normal cleanup; otherwise there really shouldn't be any fixups.
|
|
|
|
assert(hasNormalCleanups());
|
|
|
|
|
|
|
|
EHScopeStack::iterator it = find(InnermostNormalCleanup);
|
|
|
|
unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
|
|
|
|
assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
|
|
|
|
|
|
|
|
while (BranchFixups.size() > MinSize &&
|
2014-05-21 13:09:00 +08:00
|
|
|
BranchFixups.back().Destination == nullptr)
|
2011-01-28 19:13:47 +08:00
|
|
|
BranchFixups.pop_back();
|
|
|
|
}
|
|
|
|
|
2018-07-24 06:56:45 +08:00
|
|
|
Address CodeGenFunction::createCleanupActiveFlag() {
|
2011-01-28 19:13:47 +08:00
|
|
|
// Create a variable to decide whether the cleanup needs to be run.
|
2018-06-16 09:20:52 +08:00
|
|
|
Address active = CreateTempAllocaWithoutCast(
|
|
|
|
Builder.getInt1Ty(), CharUnits::One(), "cleanup.cond");
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Initialize it to false at a site that's guaranteed to be run
|
|
|
|
// before each evaluation.
|
2011-11-10 18:43:54 +08:00
|
|
|
setBeforeOutermostConditional(Builder.getFalse(), active);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Initialize it to true at the current location.
|
|
|
|
Builder.CreateStore(Builder.getTrue(), active);
|
|
|
|
|
2018-07-24 06:56:45 +08:00
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::initFullExprCleanupWithFlag(Address ActiveFlag) {
|
2011-01-28 19:13:47 +08:00
|
|
|
// Set that as the active flag in the cleanup.
|
|
|
|
EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
|
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
|
|
|
assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?");
|
2018-07-24 06:56:45 +08:00
|
|
|
cleanup.setActiveFlag(ActiveFlag);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
|
|
|
|
if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
|
|
|
|
}
|
|
|
|
|
2011-07-12 08:15:30 +08:00
|
|
|
void EHScopeStack::Cleanup::anchor() {}
|
2011-01-28 19:13:47 +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
|
|
|
static void createStoreInstBefore(llvm::Value *value, Address addr,
|
|
|
|
llvm::Instruction *beforeInst) {
|
|
|
|
auto store = new llvm::StoreInst(value, addr.getPointer(), beforeInst);
|
|
|
|
store->setAlignment(addr.getAlignment().getQuantity());
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::LoadInst *createLoadInstBefore(Address addr, const Twine &name,
|
|
|
|
llvm::Instruction *beforeInst) {
|
|
|
|
auto load = new llvm::LoadInst(addr.getPointer(), name, beforeInst);
|
|
|
|
load->setAlignment(addr.getAlignment().getQuantity());
|
|
|
|
return load;
|
2018-07-31 03:24:48 +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
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
/// All the branch fixups on the EH stack have propagated out past the
|
|
|
|
/// outermost normal cleanup; resolve them all by adding cases to the
|
|
|
|
/// given switch instruction.
|
|
|
|
static void ResolveAllBranchFixups(CodeGenFunction &CGF,
|
|
|
|
llvm::SwitchInst *Switch,
|
|
|
|
llvm::BasicBlock *CleanupEntry) {
|
|
|
|
llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) {
|
|
|
|
// Skip this fixup if its destination isn't set.
|
|
|
|
BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
|
2014-05-21 13:09:00 +08:00
|
|
|
if (Fixup.Destination == nullptr) continue;
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// If there isn't an OptimisticBranchBlock, then InitialBranch is
|
|
|
|
// still pointing directly to its destination; forward it to the
|
|
|
|
// appropriate cleanup entry. This is required in the specific
|
|
|
|
// case of
|
|
|
|
// { std::string s; goto lbl; }
|
|
|
|
// lbl:
|
|
|
|
// i.e. where there's an unresolved fixup inside a single cleanup
|
|
|
|
// entry which we're currently popping.
|
2014-05-21 13:09:00 +08:00
|
|
|
if (Fixup.OptimisticBranchBlock == nullptr) {
|
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
|
|
|
createStoreInstBefore(CGF.Builder.getInt32(Fixup.DestinationIndex),
|
|
|
|
CGF.getNormalCleanupDestSlot(),
|
|
|
|
Fixup.InitialBranch);
|
2011-01-28 19:13:47 +08:00
|
|
|
Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't add this case to the switch statement twice.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!CasesAdded.insert(Fixup.Destination).second)
|
|
|
|
continue;
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
|
|
|
|
Fixup.Destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.EHStack.clearFixups();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transitions the terminator of the given exit-block of a cleanup to
|
|
|
|
/// be a cleanup switch.
|
|
|
|
static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
|
|
|
|
llvm::BasicBlock *Block) {
|
|
|
|
// If it's a branch, turn it into a switch whose default
|
|
|
|
// destination is its original target.
|
2018-10-15 18:42:50 +08:00
|
|
|
llvm::Instruction *Term = Block->getTerminator();
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(Term && "can't transition block without terminator");
|
|
|
|
|
|
|
|
if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
|
|
|
|
assert(Br->isUnconditional());
|
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
|
|
|
auto Load = createLoadInstBefore(CGF.getNormalCleanupDestSlot(),
|
|
|
|
"cleanup.dest", Term);
|
2011-01-28 19:13:47 +08:00
|
|
|
llvm::SwitchInst *Switch =
|
|
|
|
llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
|
|
|
|
Br->eraseFromParent();
|
|
|
|
return Switch;
|
|
|
|
} else {
|
|
|
|
return cast<llvm::SwitchInst>(Term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
|
|
|
|
assert(Block && "resolving a null target block");
|
|
|
|
if (!EHStack.getNumBranchFixups()) return;
|
|
|
|
|
|
|
|
assert(EHStack.hasNormalCleanups() &&
|
|
|
|
"branch fixups exist with no normal cleanups on stack");
|
|
|
|
|
|
|
|
llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
|
|
|
|
bool ResolvedAny = false;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
|
|
|
|
// Skip this fixup if its destination doesn't match.
|
|
|
|
BranchFixup &Fixup = EHStack.getBranchFixup(I);
|
|
|
|
if (Fixup.Destination != Block) continue;
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
Fixup.Destination = nullptr;
|
2011-01-28 19:13:47 +08:00
|
|
|
ResolvedAny = true;
|
|
|
|
|
|
|
|
// If it doesn't have an optimistic branch block, LatestBranch is
|
|
|
|
// already pointing to the right place.
|
|
|
|
llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
|
|
|
|
if (!BranchBB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Don't process the same optimistic branch block twice.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!ModifiedOptimisticBlocks.insert(BranchBB).second)
|
2011-01-28 19:13:47 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
|
|
|
|
|
|
|
|
// Add a case to the switch.
|
|
|
|
Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ResolvedAny)
|
|
|
|
EHStack.popNullFixups();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pops cleanup blocks until the given savepoint is reached.
|
2017-03-07 06:18:34 +08:00
|
|
|
void CodeGenFunction::PopCleanupBlocks(
|
|
|
|
EHScopeStack::stable_iterator Old,
|
|
|
|
std::initializer_list<llvm::Value **> ValuesToReload) {
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(Old.isValid());
|
|
|
|
|
2017-03-07 06:18:34 +08:00
|
|
|
bool HadBranches = false;
|
2011-01-28 19:13:47 +08:00
|
|
|
while (EHStack.stable_begin() != Old) {
|
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
|
2017-03-07 06:18:34 +08:00
|
|
|
HadBranches |= Scope.hasBranches();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// As long as Old strictly encloses the scope's enclosing normal
|
|
|
|
// cleanup, we're going to emit another normal cleanup which
|
|
|
|
// fallthrough can propagate through.
|
|
|
|
bool FallThroughIsBranchThrough =
|
|
|
|
Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
|
|
|
|
|
2013-05-16 08:41:26 +08:00
|
|
|
PopCleanupBlock(FallThroughIsBranchThrough);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
2017-03-07 06:18:34 +08:00
|
|
|
|
|
|
|
// If we didn't have any branches, the insertion point before cleanups must
|
|
|
|
// dominate the current insertion point and we don't need to reload any
|
|
|
|
// values.
|
|
|
|
if (!HadBranches)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Spill and reload all values that the caller wants to be live at the current
|
|
|
|
// insertion point.
|
|
|
|
for (llvm::Value **ReloadedValue : ValuesToReload) {
|
|
|
|
auto *Inst = dyn_cast_or_null<llvm::Instruction>(*ReloadedValue);
|
|
|
|
if (!Inst)
|
|
|
|
continue;
|
2017-06-01 03:59:41 +08:00
|
|
|
|
|
|
|
// Don't spill static allocas, they dominate all cleanups. These are created
|
|
|
|
// by binding a reference to a local variable or temporary.
|
|
|
|
auto *AI = dyn_cast<llvm::AllocaInst>(Inst);
|
|
|
|
if (AI && AI->isStaticAlloca())
|
|
|
|
continue;
|
|
|
|
|
2017-03-07 06:18:34 +08:00
|
|
|
Address Tmp =
|
|
|
|
CreateDefaultAlignTempAlloca(Inst->getType(), "tmp.exprcleanup");
|
|
|
|
|
|
|
|
// Find an insertion point after Inst and spill it to the temporary.
|
|
|
|
llvm::BasicBlock::iterator InsertBefore;
|
|
|
|
if (auto *Invoke = dyn_cast<llvm::InvokeInst>(Inst))
|
|
|
|
InsertBefore = Invoke->getNormalDest()->getFirstInsertionPt();
|
|
|
|
else
|
|
|
|
InsertBefore = std::next(Inst->getIterator());
|
|
|
|
CGBuilderTy(CGM, &*InsertBefore).CreateStore(Inst, Tmp);
|
|
|
|
|
|
|
|
// Reload the value at the current insertion point.
|
|
|
|
*ReloadedValue = Builder.CreateLoad(Tmp);
|
|
|
|
}
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 12:05:00 +08:00
|
|
|
/// Pops cleanup blocks until the given savepoint is reached, then add the
|
|
|
|
/// cleanups from the given savepoint in the lifetime-extended cleanups stack.
|
2017-03-07 06:18:34 +08:00
|
|
|
void CodeGenFunction::PopCleanupBlocks(
|
|
|
|
EHScopeStack::stable_iterator Old, size_t OldLifetimeExtendedSize,
|
|
|
|
std::initializer_list<llvm::Value **> ValuesToReload) {
|
|
|
|
PopCleanupBlocks(Old, ValuesToReload);
|
2014-10-10 12:05:00 +08:00
|
|
|
|
|
|
|
// Move our deferred cleanups onto the EH stack.
|
2013-06-13 04:42:33 +08:00
|
|
|
for (size_t I = OldLifetimeExtendedSize,
|
|
|
|
E = LifetimeExtendedCleanupStack.size(); I != E; /**/) {
|
|
|
|
// Alignment should be guaranteed by the vptrs in the individual cleanups.
|
2016-10-20 22:27:22 +08:00
|
|
|
assert((I % alignof(LifetimeExtendedCleanupHeader) == 0) &&
|
2013-06-13 04:42:33 +08:00
|
|
|
"misaligned cleanup stack entry");
|
|
|
|
|
|
|
|
LifetimeExtendedCleanupHeader &Header =
|
|
|
|
reinterpret_cast<LifetimeExtendedCleanupHeader&>(
|
|
|
|
LifetimeExtendedCleanupStack[I]);
|
|
|
|
I += sizeof(Header);
|
|
|
|
|
|
|
|
EHStack.pushCopyOfCleanup(Header.getKind(),
|
|
|
|
&LifetimeExtendedCleanupStack[I],
|
|
|
|
Header.getSize());
|
|
|
|
I += Header.getSize();
|
2018-07-24 06:56:45 +08:00
|
|
|
|
|
|
|
if (Header.isConditional()) {
|
|
|
|
Address ActiveFlag =
|
|
|
|
reinterpret_cast<Address &>(LifetimeExtendedCleanupStack[I]);
|
|
|
|
initFullExprCleanupWithFlag(ActiveFlag);
|
|
|
|
I += sizeof(ActiveFlag);
|
|
|
|
}
|
2013-06-13 04:42:33 +08:00
|
|
|
}
|
|
|
|
LifetimeExtendedCleanupStack.resize(OldLifetimeExtendedSize);
|
|
|
|
}
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
|
|
|
|
EHCleanupScope &Scope) {
|
|
|
|
assert(Scope.isNormalCleanup());
|
|
|
|
llvm::BasicBlock *Entry = Scope.getNormalBlock();
|
|
|
|
if (!Entry) {
|
|
|
|
Entry = CGF.createBasicBlock("cleanup");
|
|
|
|
Scope.setNormalBlock(Entry);
|
|
|
|
}
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to reduce a cleanup's entry block to a fallthrough. This
|
|
|
|
/// is basically llvm::MergeBlockIntoPredecessor, except
|
|
|
|
/// simplified/optimized for the tighter constraints on cleanup blocks.
|
|
|
|
///
|
|
|
|
/// Returns the new block, whatever it is.
|
|
|
|
static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
|
|
|
|
llvm::BasicBlock *Entry) {
|
|
|
|
llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
|
|
|
|
if (!Pred) return Entry;
|
|
|
|
|
|
|
|
llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
|
|
|
|
if (!Br || Br->isConditional()) return Entry;
|
|
|
|
assert(Br->getSuccessor(0) == Entry);
|
|
|
|
|
|
|
|
// If we were previously inserting at the end of the cleanup entry
|
|
|
|
// block, we'll need to continue inserting at the end of the
|
|
|
|
// predecessor.
|
|
|
|
bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
|
|
|
|
assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
|
|
|
|
|
|
|
|
// Kill the branch.
|
|
|
|
Br->eraseFromParent();
|
|
|
|
|
|
|
|
// Replace all uses of the entry with the predecessor, in case there
|
|
|
|
// are phis in the cleanup.
|
|
|
|
Entry->replaceAllUsesWith(Pred);
|
|
|
|
|
2011-06-20 22:38:01 +08:00
|
|
|
// Merge the blocks.
|
|
|
|
Pred->getInstList().splice(Pred->end(), Entry->getInstList());
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
// Kill the entry block.
|
|
|
|
Entry->eraseFromParent();
|
|
|
|
|
|
|
|
if (WasInsertBlock)
|
|
|
|
CGF.Builder.SetInsertPoint(Pred);
|
|
|
|
|
|
|
|
return Pred;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitCleanup(CodeGenFunction &CGF,
|
|
|
|
EHScopeStack::Cleanup *Fn,
|
2011-07-13 04:27:29 +08:00
|
|
|
EHScopeStack::Cleanup::Flags flags,
|
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 ActiveFlag) {
|
2011-01-28 19:13:47 +08:00
|
|
|
// If there's an active flag, load it and skip the cleanup if it's
|
|
|
|
// false.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *ContBB = nullptr;
|
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
|
|
|
if (ActiveFlag.isValid()) {
|
2011-01-28 19:13:47 +08:00
|
|
|
ContBB = CGF.createBasicBlock("cleanup.done");
|
|
|
|
llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
|
|
|
|
llvm::Value *IsActive
|
|
|
|
= CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
|
|
|
|
CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
|
|
|
|
CGF.EmitBlock(CleanupBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the cleanup to emit itself.
|
2011-07-13 04:27:29 +08:00
|
|
|
Fn->Emit(CGF, flags);
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
|
|
|
|
|
|
|
|
// Emit the continuation block if there was an active flag.
|
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
|
|
|
if (ActiveFlag.isValid())
|
2011-01-28 19:13:47 +08:00
|
|
|
CGF.EmitBlock(ContBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
|
|
|
|
llvm::BasicBlock *From,
|
|
|
|
llvm::BasicBlock *To) {
|
|
|
|
// Exit is the exit block of a cleanup, so it always terminates in
|
|
|
|
// an unconditional branch or a switch.
|
2018-10-15 18:42:50 +08:00
|
|
|
llvm::Instruction *Term = Exit->getTerminator();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
|
|
|
|
assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
|
|
|
|
Br->setSuccessor(0, To);
|
|
|
|
} else {
|
|
|
|
llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
|
|
|
|
for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
|
|
|
|
if (Switch->getSuccessor(I) == From)
|
|
|
|
Switch->setSuccessor(I, To);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-06 14:53:52 +08:00
|
|
|
/// We don't need a normal entry block for the given cleanup.
|
|
|
|
/// Optimistic fixup branches can cause these blocks to come into
|
|
|
|
/// existence anyway; if so, destroy it.
|
|
|
|
///
|
|
|
|
/// The validity of this transformation is very much specific to the
|
|
|
|
/// exact ways in which we form branches to cleanup entries.
|
|
|
|
static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
|
|
|
|
EHCleanupScope &scope) {
|
|
|
|
llvm::BasicBlock *entry = scope.getNormalBlock();
|
|
|
|
if (!entry) return;
|
|
|
|
|
|
|
|
// Replace all the uses with unreachable.
|
|
|
|
llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
|
|
|
|
for (llvm::BasicBlock::use_iterator
|
|
|
|
i = entry->use_begin(), e = entry->use_end(); i != e; ) {
|
2014-03-09 11:16:50 +08:00
|
|
|
llvm::Use &use = *i;
|
2011-08-06 14:53:52 +08:00
|
|
|
++i;
|
|
|
|
|
|
|
|
use.set(unreachableBB);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-08-06 14:53:52 +08:00
|
|
|
// The only uses should be fixup switches.
|
|
|
|
llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
|
Compatability fix for SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
llvm-svn: 149482
2012-02-01 15:50:21 +08:00
|
|
|
if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
|
2011-08-06 14:53:52 +08:00
|
|
|
// Replace the switch with a branch.
|
2017-04-12 16:12:30 +08:00
|
|
|
llvm::BranchInst::Create(si->case_begin()->getCaseSuccessor(), si);
|
2011-08-06 14:53:52 +08:00
|
|
|
|
|
|
|
// The switch operand is a load from the cleanup-dest alloca.
|
|
|
|
llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
|
|
|
|
|
|
|
|
// Destroy the switch.
|
|
|
|
si->eraseFromParent();
|
|
|
|
|
|
|
|
// Destroy the load.
|
2018-01-13 06:07:01 +08:00
|
|
|
assert(condition->getOperand(0) == CGF.NormalCleanupDest.getPointer());
|
2011-08-06 14:53:52 +08:00
|
|
|
assert(condition->use_empty());
|
|
|
|
condition->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-08-06 14:53:52 +08:00
|
|
|
assert(entry->use_empty());
|
|
|
|
delete entry;
|
|
|
|
}
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
/// Pops a cleanup block. If the block includes a normal cleanup, the
|
|
|
|
/// current insertion point is threaded through the cleanup, as are
|
|
|
|
/// any branch fixups on the cleanup.
|
2013-05-16 08:41:26 +08:00
|
|
|
void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(!EHStack.empty() && "cleanup stack is empty!");
|
|
|
|
assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
|
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
|
|
|
|
assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
|
|
|
|
|
|
|
|
// Remember activation information.
|
|
|
|
bool IsActive = Scope.isActive();
|
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 NormalActiveFlag =
|
|
|
|
Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag()
|
|
|
|
: Address::invalid();
|
2018-07-31 03:24:48 +08:00
|
|
|
Address EHActiveFlag =
|
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
|
|
|
Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag()
|
|
|
|
: Address::invalid();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Check whether we need an EH cleanup. This is only true if we've
|
|
|
|
// generated a lazy EH cleanup block.
|
2011-08-11 10:22:43 +08:00
|
|
|
llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
|
2014-05-21 13:09:00 +08:00
|
|
|
assert(Scope.hasEHBranches() == (EHEntry != nullptr));
|
|
|
|
bool RequiresEHCleanup = (EHEntry != nullptr);
|
2011-08-11 10:22:43 +08:00
|
|
|
EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Check the three conditions which might require a normal cleanup:
|
|
|
|
|
|
|
|
// - whether there are branch fix-ups through this cleanup
|
|
|
|
unsigned FixupDepth = Scope.getFixupDepth();
|
|
|
|
bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
|
|
|
|
|
|
|
|
// - whether there are branch-throughs or branch-afters
|
|
|
|
bool HasExistingBranches = Scope.hasBranches();
|
|
|
|
|
|
|
|
// - whether there's a fallthrough
|
|
|
|
llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
|
2014-05-21 13:09:00 +08:00
|
|
|
bool HasFallthrough = (FallthroughSource != nullptr && IsActive);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Branch-through fall-throughs leave the insertion point set to the
|
|
|
|
// end of the last cleanup, which points to the current scope. The
|
|
|
|
// rest of IR gen doesn't need to worry about this; it only happens
|
|
|
|
// during the execution of PopCleanupBlocks().
|
|
|
|
bool HasPrebranchedFallthrough =
|
|
|
|
(FallthroughSource && FallthroughSource->getTerminator());
|
|
|
|
|
|
|
|
// If this is a normal cleanup, then having a prebranched
|
|
|
|
// fallthrough implies that the fallthrough source unconditionally
|
|
|
|
// jumps here.
|
|
|
|
assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
|
|
|
|
(Scope.getNormalBlock() &&
|
|
|
|
FallthroughSource->getTerminator()->getSuccessor(0)
|
|
|
|
== Scope.getNormalBlock()));
|
|
|
|
|
|
|
|
bool RequiresNormalCleanup = false;
|
|
|
|
if (Scope.isNormalCleanup() &&
|
|
|
|
(HasFixups || HasExistingBranches || HasFallthrough)) {
|
|
|
|
RequiresNormalCleanup = true;
|
|
|
|
}
|
|
|
|
|
2011-08-07 15:05:57 +08:00
|
|
|
// If we have a prebranched fallthrough into an inactive normal
|
|
|
|
// cleanup, rewrite it so that it leads to the appropriate place.
|
|
|
|
if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
|
|
|
|
llvm::BasicBlock *prebranchDest;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-08-07 15:05:57 +08:00
|
|
|
// If the prebranch is semantically branching through the next
|
|
|
|
// cleanup, just forward it to the next block, leaving the
|
|
|
|
// insertion point in the prebranched block.
|
2011-01-28 19:13:47 +08:00
|
|
|
if (FallthroughIsBranchThrough) {
|
2011-08-07 15:05:57 +08:00
|
|
|
EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
|
|
|
|
prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
|
2011-01-28 19:13:47 +08:00
|
|
|
|
2011-08-07 15:05:57 +08:00
|
|
|
// Otherwise, we need to make a new block. If the normal cleanup
|
|
|
|
// isn't being used at all, we could actually reuse the normal
|
|
|
|
// entry block, but this is simpler, and it avoids conflicts with
|
|
|
|
// dead optimistic fixup branches.
|
2011-01-28 19:13:47 +08:00
|
|
|
} else {
|
2011-08-07 15:05:57 +08:00
|
|
|
prebranchDest = createBasicBlock("forwarded-prebranch");
|
|
|
|
EmitBlock(prebranchDest);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
2011-08-07 15:05:57 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
|
|
|
|
assert(normalEntry && !normalEntry->use_empty());
|
|
|
|
|
|
|
|
ForwardPrebranchedFallthrough(FallthroughSource,
|
|
|
|
normalEntry, prebranchDest);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't need the cleanup at all, we're done.
|
|
|
|
if (!RequiresNormalCleanup && !RequiresEHCleanup) {
|
2011-08-06 14:53:52 +08:00
|
|
|
destroyOptimisticNormalEntry(*this, Scope);
|
2011-01-28 19:13:47 +08:00
|
|
|
EHStack.popCleanup(); // safe because there are no fixups
|
|
|
|
assert(EHStack.getNumBranchFixups() == 0 ||
|
|
|
|
EHStack.hasNormalCleanups());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:58:33 +08:00
|
|
|
// Copy the cleanup emission data out. This uses either a stack
|
|
|
|
// array or malloc'd memory, depending on the size, which is
|
|
|
|
// behavior that SmallVector would provide, if we could use it
|
|
|
|
// here. Unfortunately, if you ask for a SmallVector<char>, the
|
|
|
|
// alignment isn't sufficient.
|
2015-08-04 20:34:30 +08:00
|
|
|
auto *CleanupSource = reinterpret_cast<char *>(Scope.getCleanupBuffer());
|
2019-07-30 07:12:48 +08:00
|
|
|
alignas(EHScopeStack::ScopeStackAlignment) char
|
|
|
|
CleanupBufferStack[8 * sizeof(void *)];
|
2015-12-30 11:58:33 +08:00
|
|
|
std::unique_ptr<char[]> CleanupBufferHeap;
|
|
|
|
size_t CleanupSize = Scope.getCleanupSize();
|
|
|
|
EHScopeStack::Cleanup *Fn;
|
|
|
|
|
|
|
|
if (CleanupSize <= sizeof(CleanupBufferStack)) {
|
2019-07-30 07:12:48 +08:00
|
|
|
memcpy(CleanupBufferStack, CleanupSource, CleanupSize);
|
|
|
|
Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferStack);
|
2015-12-30 11:58:33 +08:00
|
|
|
} else {
|
|
|
|
CleanupBufferHeap.reset(new char[CleanupSize]);
|
|
|
|
memcpy(CleanupBufferHeap.get(), CleanupSource, CleanupSize);
|
|
|
|
Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferHeap.get());
|
|
|
|
}
|
2011-01-28 19:13:47 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHScopeStack::Cleanup::Flags cleanupFlags;
|
|
|
|
if (Scope.isNormalCleanup())
|
|
|
|
cleanupFlags.setIsNormalCleanupKind();
|
|
|
|
if (Scope.isEHCleanup())
|
|
|
|
cleanupFlags.setIsEHCleanupKind();
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
if (!RequiresNormalCleanup) {
|
2011-08-06 14:53:52 +08:00
|
|
|
destroyOptimisticNormalEntry(*this, Scope);
|
2011-01-28 19:13:47 +08:00
|
|
|
EHStack.popCleanup();
|
|
|
|
} else {
|
|
|
|
// If we have a fallthrough and no other need for the cleanup,
|
|
|
|
// emit it directly.
|
|
|
|
if (HasFallthrough && !HasPrebranchedFallthrough &&
|
|
|
|
!HasFixups && !HasExistingBranches) {
|
|
|
|
|
2011-08-06 14:53:52 +08:00
|
|
|
destroyOptimisticNormalEntry(*this, Scope);
|
2011-01-28 19:13:47 +08:00
|
|
|
EHStack.popCleanup();
|
|
|
|
|
2011-07-13 04:27:29 +08:00
|
|
|
EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Otherwise, the best approach is to thread everything through
|
|
|
|
// the cleanup block and then try to clean up after ourselves.
|
|
|
|
} else {
|
|
|
|
// Force the entry block to exist.
|
|
|
|
llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
|
|
|
|
|
|
|
|
// I. Set up the fallthrough edge in.
|
|
|
|
|
2011-08-10 12:11:11 +08:00
|
|
|
CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
|
2011-08-07 15:05:57 +08:00
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
// If there's a fallthrough, we need to store the cleanup
|
|
|
|
// destination index. For fall-throughs this is always zero.
|
|
|
|
if (HasFallthrough) {
|
|
|
|
if (!HasPrebranchedFallthrough)
|
|
|
|
Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
|
|
|
|
|
2011-08-07 15:05:57 +08:00
|
|
|
// Otherwise, save and clear the IP if we don't have fallthrough
|
|
|
|
// because the cleanup is inactive.
|
2011-01-28 19:13:47 +08:00
|
|
|
} else if (FallthroughSource) {
|
|
|
|
assert(!IsActive && "source without fallthrough for active cleanup");
|
2011-08-07 15:05:57 +08:00
|
|
|
savedInactiveFallthroughIP = Builder.saveAndClearIP();
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// II. Emit the entry block. This implicitly branches to it if
|
|
|
|
// we have fallthrough. All the fixups and existing branches
|
|
|
|
// should already be branched to it.
|
|
|
|
EmitBlock(NormalEntry);
|
|
|
|
|
|
|
|
// III. Figure out where we're going and build the cleanup
|
|
|
|
// epilogue.
|
|
|
|
|
|
|
|
bool HasEnclosingCleanups =
|
|
|
|
(Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
|
|
|
|
|
|
|
|
// Compute the branch-through dest if we need it:
|
|
|
|
// - if there are branch-throughs threaded through the scope
|
|
|
|
// - if fall-through is a branch-through
|
|
|
|
// - if there are fixups that will be optimistically forwarded
|
|
|
|
// to the enclosing cleanup
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *BranchThroughDest = nullptr;
|
2011-01-28 19:13:47 +08:00
|
|
|
if (Scope.hasBranchThroughs() ||
|
|
|
|
(FallthroughSource && FallthroughIsBranchThrough) ||
|
|
|
|
(HasFixups && HasEnclosingCleanups)) {
|
|
|
|
assert(HasEnclosingCleanups);
|
|
|
|
EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
|
|
|
|
BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *FallthroughDest = nullptr;
|
2015-02-18 00:53:08 +08:00
|
|
|
SmallVector<llvm::Instruction*, 2> InstsToAppend;
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// If there's exactly one branch-after and no other threads,
|
|
|
|
// we can route it without a switch.
|
|
|
|
if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
|
|
|
|
Scope.getNumBranchAfters() == 1) {
|
|
|
|
assert(!BranchThroughDest || !IsActive);
|
|
|
|
|
2015-04-23 05:38:15 +08:00
|
|
|
// Clean up the possibly dead store to the cleanup dest slot.
|
|
|
|
llvm::Instruction *NormalCleanupDestSlot =
|
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
|
|
|
cast<llvm::Instruction>(getNormalCleanupDestSlot().getPointer());
|
2015-04-23 05:38:15 +08:00
|
|
|
if (NormalCleanupDestSlot->hasOneUse()) {
|
|
|
|
NormalCleanupDestSlot->user_back()->eraseFromParent();
|
|
|
|
NormalCleanupDestSlot->eraseFromParent();
|
2018-01-13 06:07:01 +08:00
|
|
|
NormalCleanupDest = Address::invalid();
|
2015-04-23 05:38:15 +08:00
|
|
|
}
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
|
|
|
|
InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
|
|
|
|
|
|
|
|
// Build a switch-out if we need it:
|
|
|
|
// - if there are branch-afters threaded through the scope
|
|
|
|
// - if fall-through is a branch-after
|
|
|
|
// - if there are fixups that have nowhere left to go and
|
|
|
|
// so must be immediately resolved
|
|
|
|
} else if (Scope.getNumBranchAfters() ||
|
|
|
|
(HasFallthrough && !FallthroughIsBranchThrough) ||
|
|
|
|
(HasFixups && !HasEnclosingCleanups)) {
|
|
|
|
|
|
|
|
llvm::BasicBlock *Default =
|
|
|
|
(BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
|
|
|
|
|
|
|
|
// TODO: base this on the number of branch-afters and fixups
|
|
|
|
const unsigned SwitchCapacity = 10;
|
|
|
|
|
|
|
|
llvm::LoadInst *Load =
|
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
|
|
|
createLoadInstBefore(getNormalCleanupDestSlot(), "cleanup.dest",
|
|
|
|
nullptr);
|
2011-01-28 19:13:47 +08:00
|
|
|
llvm::SwitchInst *Switch =
|
|
|
|
llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
|
|
|
|
|
|
|
|
InstsToAppend.push_back(Load);
|
|
|
|
InstsToAppend.push_back(Switch);
|
|
|
|
|
|
|
|
// Branch-after fallthrough.
|
|
|
|
if (FallthroughSource && !FallthroughIsBranchThrough) {
|
|
|
|
FallthroughDest = createBasicBlock("cleanup.cont");
|
|
|
|
if (HasFallthrough)
|
|
|
|
Switch->addCase(Builder.getInt32(0), FallthroughDest);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
|
|
|
|
Switch->addCase(Scope.getBranchAfterIndex(I),
|
|
|
|
Scope.getBranchAfterBlock(I));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there aren't any enclosing cleanups, we can resolve all
|
|
|
|
// the fixups now.
|
|
|
|
if (HasFixups && !HasEnclosingCleanups)
|
|
|
|
ResolveAllBranchFixups(*this, Switch, NormalEntry);
|
|
|
|
} else {
|
|
|
|
// We should always have a branch-through destination in this case.
|
|
|
|
assert(BranchThroughDest);
|
|
|
|
InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
|
|
|
|
}
|
|
|
|
|
|
|
|
// IV. Pop the cleanup and emit it.
|
|
|
|
EHStack.popCleanup();
|
|
|
|
assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
|
|
|
|
|
2011-07-13 04:27:29 +08:00
|
|
|
EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Append the prepared cleanup prologue from above.
|
|
|
|
llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
|
2015-02-18 00:53:08 +08:00
|
|
|
for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
|
|
|
|
NormalExit->getInstList().push_back(InstsToAppend[I]);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Optimistically hope that any fixups will continue falling through.
|
|
|
|
for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
|
|
|
|
I < E; ++I) {
|
2011-02-08 16:22:06 +08:00
|
|
|
BranchFixup &Fixup = EHStack.getBranchFixup(I);
|
2011-01-28 19:13:47 +08:00
|
|
|
if (!Fixup.Destination) continue;
|
|
|
|
if (!Fixup.OptimisticBranchBlock) {
|
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
|
|
|
createStoreInstBefore(Builder.getInt32(Fixup.DestinationIndex),
|
|
|
|
getNormalCleanupDestSlot(),
|
|
|
|
Fixup.InitialBranch);
|
2011-01-28 19:13:47 +08:00
|
|
|
Fixup.InitialBranch->setSuccessor(0, NormalEntry);
|
|
|
|
}
|
|
|
|
Fixup.OptimisticBranchBlock = NormalExit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// V. Set up the fallthrough edge out.
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-08-07 15:05:57 +08:00
|
|
|
// Case 1: a fallthrough source exists but doesn't branch to the
|
|
|
|
// cleanup because the cleanup is inactive.
|
2011-01-28 19:13:47 +08:00
|
|
|
if (!HasFallthrough && FallthroughSource) {
|
2011-08-07 15:05:57 +08:00
|
|
|
// Prebranched fallthrough was forwarded earlier.
|
|
|
|
// Non-prebranched fallthrough doesn't need to be forwarded.
|
|
|
|
// Either way, all we need to do is restore the IP we cleared before.
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(!IsActive);
|
2011-08-07 15:05:57 +08:00
|
|
|
Builder.restoreIP(savedInactiveFallthroughIP);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Case 2: a fallthrough source exists and should branch to the
|
|
|
|
// cleanup, but we're not supposed to branch through to the next
|
|
|
|
// cleanup.
|
|
|
|
} else if (HasFallthrough && FallthroughDest) {
|
|
|
|
assert(!FallthroughIsBranchThrough);
|
|
|
|
EmitBlock(FallthroughDest);
|
|
|
|
|
|
|
|
// Case 3: a fallthrough source exists and should branch to the
|
|
|
|
// cleanup and then through to the next.
|
|
|
|
} else if (HasFallthrough) {
|
|
|
|
// Everything is already set up for this.
|
|
|
|
|
|
|
|
// Case 4: no fallthrough source exists.
|
|
|
|
} else {
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
// VI. Assorted cleaning.
|
|
|
|
|
|
|
|
// Check whether we can merge NormalEntry into a single predecessor.
|
|
|
|
// This might invalidate (non-IR) pointers to NormalEntry.
|
|
|
|
llvm::BasicBlock *NewNormalEntry =
|
|
|
|
SimplifyCleanupEntry(*this, NormalEntry);
|
|
|
|
|
|
|
|
// If it did invalidate those pointers, and NormalEntry was the same
|
|
|
|
// as NormalExit, go back and patch up the fixups.
|
|
|
|
if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
|
|
|
|
for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
|
|
|
|
I < E; ++I)
|
2011-02-08 16:22:06 +08:00
|
|
|
EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
|
|
|
|
|
|
|
|
// Emit the EH cleanup if required.
|
|
|
|
if (RequiresEHCleanup) {
|
|
|
|
CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
|
|
|
|
|
|
|
|
EmitBlock(EHEntry);
|
2015-10-09 05:14:56 +08:00
|
|
|
|
2015-10-29 07:06:42 +08:00
|
|
|
llvm::BasicBlock *NextAction = getEHDispatchBlock(EHParent);
|
|
|
|
|
|
|
|
// Push a terminate scope or cleanupendpad scope around the potentially
|
|
|
|
// throwing cleanups. For funclet EH personalities, the cleanupendpad models
|
|
|
|
// program termination when cleanups throw.
|
2015-10-09 05:14:56 +08:00
|
|
|
bool PushedTerminate = false;
|
2015-12-12 13:39:21 +08:00
|
|
|
SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
|
|
|
|
CurrentFuncletPad);
|
2015-10-29 07:06:42 +08:00
|
|
|
llvm::CleanupPadInst *CPI = nullptr;
|
[WebAssembly] Use Windows EH instructions for Wasm EH
Summary:
Because wasm control flow needs to be structured, using WinEH
instructions to support wasm EH brings several benefits. This patch
makes wasm EH uses Windows EH instructions, with some changes:
1. Because wasm uses a single catch block to catch all C++ exceptions,
this merges all catch clauses into a single catchpad, within which we
test the EH selector as in Itanium EH.
2. Generates a call to `__clang_call_terminate` in case a cleanup
throws. Wasm does not have a runtime to handle this.
3. In case there is no catch-all clause, inserts a call to
`__cxa_rethrow` at the end of a catchpad in order to unwind to an
enclosing EH scope.
Reviewers: majnemer, dschuff
Subscribers: jfb, sbc100, jgravelle-google, sunfish, cfe-commits
Differential Revision: https://reviews.llvm.org/D44931
llvm-svn: 333703
2018-06-01 06:18:13 +08:00
|
|
|
|
|
|
|
const EHPersonality &Personality = EHPersonality::get(*this);
|
|
|
|
if (Personality.usesFuncletPads()) {
|
2015-12-12 13:39:21 +08:00
|
|
|
llvm::Value *ParentPad = CurrentFuncletPad;
|
|
|
|
if (!ParentPad)
|
|
|
|
ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
|
|
|
|
CurrentFuncletPad = CPI = Builder.CreateCleanupPad(ParentPad);
|
2015-10-09 05:14:56 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] Use Windows EH instructions for Wasm EH
Summary:
Because wasm control flow needs to be structured, using WinEH
instructions to support wasm EH brings several benefits. This patch
makes wasm EH uses Windows EH instructions, with some changes:
1. Because wasm uses a single catch block to catch all C++ exceptions,
this merges all catch clauses into a single catchpad, within which we
test the EH selector as in Itanium EH.
2. Generates a call to `__clang_call_terminate` in case a cleanup
throws. Wasm does not have a runtime to handle this.
3. In case there is no catch-all clause, inserts a call to
`__cxa_rethrow` at the end of a catchpad in order to unwind to an
enclosing EH scope.
Reviewers: majnemer, dschuff
Subscribers: jfb, sbc100, jgravelle-google, sunfish, cfe-commits
Differential Revision: https://reviews.llvm.org/D44931
llvm-svn: 333703
2018-06-01 06:18:13 +08:00
|
|
|
// Non-MSVC personalities need to terminate when an EH cleanup throws.
|
|
|
|
if (!Personality.isMSVCPersonality()) {
|
|
|
|
EHStack.pushTerminate();
|
|
|
|
PushedTerminate = true;
|
|
|
|
}
|
|
|
|
|
2012-08-02 08:10:24 +08:00
|
|
|
// We only actually emit the cleanup code if the cleanup is either
|
|
|
|
// active or was used before it was deactivated.
|
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
|
|
|
if (EHActiveFlag.isValid() || IsActive) {
|
2012-08-02 08:10:24 +08:00
|
|
|
cleanupFlags.setIsForEHCleanup();
|
|
|
|
EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
|
|
|
|
}
|
2011-01-28 19:13:47 +08:00
|
|
|
|
2015-08-15 11:21:08 +08:00
|
|
|
if (CPI)
|
2015-08-23 08:26:48 +08:00
|
|
|
Builder.CreateCleanupRet(CPI, NextAction);
|
2015-08-01 01:58:45 +08:00
|
|
|
else
|
|
|
|
Builder.CreateBr(NextAction);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
2015-10-09 05:14:56 +08:00
|
|
|
// Leave the terminate scope.
|
|
|
|
if (PushedTerminate)
|
|
|
|
EHStack.popTerminate();
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
Builder.restoreIP(SavedIP);
|
|
|
|
|
|
|
|
SimplifyCleanupEntry(*this, EHEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 08:35:11 +08:00
|
|
|
/// isObviouslyBranchWithoutCleanups - Return true if a branch to the
|
|
|
|
/// specified destination obviously has no cleanups to run. 'false' is always
|
|
|
|
/// a conservatively correct answer for this method.
|
|
|
|
bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
|
|
|
|
assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
|
|
|
|
&& "stale jump destination");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-01-21 08:35:11 +08:00
|
|
|
// Calculate the innermost active normal cleanup.
|
|
|
|
EHScopeStack::stable_iterator TopCleanup =
|
|
|
|
EHStack.getInnermostActiveNormalCleanup();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-01-21 08:35:11 +08:00
|
|
|
// If we're not in an active normal cleanup scope, or if the
|
|
|
|
// destination scope is within the innermost active normal cleanup
|
|
|
|
// scope, we don't need to worry about fixups.
|
|
|
|
if (TopCleanup == EHStack.stable_end() ||
|
|
|
|
TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, we might need some cleanups.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
/// Terminate the current block by emitting a branch which might leave
|
|
|
|
/// the current cleanup-protected scope. The target scope may not yet
|
|
|
|
/// be known, in which case this will require a fixup.
|
|
|
|
///
|
|
|
|
/// As a side-effect, this method clears the insertion point.
|
|
|
|
void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
|
2011-02-25 12:19:13 +08:00
|
|
|
assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
|
2011-01-28 19:13:47 +08:00
|
|
|
&& "stale jump destination");
|
|
|
|
|
|
|
|
if (!HaveInsertPoint())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create the branch.
|
|
|
|
llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
|
|
|
|
|
|
|
|
// Calculate the innermost active normal cleanup.
|
|
|
|
EHScopeStack::stable_iterator
|
|
|
|
TopCleanup = EHStack.getInnermostActiveNormalCleanup();
|
|
|
|
|
|
|
|
// If we're not in an active normal cleanup scope, or if the
|
|
|
|
// destination scope is within the innermost active normal cleanup
|
|
|
|
// scope, we don't need to worry about fixups.
|
|
|
|
if (TopCleanup == EHStack.stable_end() ||
|
|
|
|
TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't resolve the destination cleanup scope, just add this
|
|
|
|
// to the current cleanup scope as a branch fixup.
|
|
|
|
if (!Dest.getScopeDepth().isValid()) {
|
|
|
|
BranchFixup &Fixup = EHStack.addBranchFixup();
|
|
|
|
Fixup.Destination = Dest.getBlock();
|
|
|
|
Fixup.DestinationIndex = Dest.getDestIndex();
|
|
|
|
Fixup.InitialBranch = BI;
|
2014-05-21 13:09:00 +08:00
|
|
|
Fixup.OptimisticBranchBlock = nullptr;
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, thread through all the normal cleanups in scope.
|
|
|
|
|
|
|
|
// Store the index at the start.
|
|
|
|
llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
|
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
|
|
|
createStoreInstBefore(Index, getNormalCleanupDestSlot(), BI);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Adjust BI to point to the first cleanup block.
|
|
|
|
{
|
|
|
|
EHCleanupScope &Scope =
|
|
|
|
cast<EHCleanupScope>(*EHStack.find(TopCleanup));
|
|
|
|
BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this destination to all the scopes involved.
|
|
|
|
EHScopeStack::stable_iterator I = TopCleanup;
|
|
|
|
EHScopeStack::stable_iterator E = Dest.getScopeDepth();
|
|
|
|
if (E.strictlyEncloses(I)) {
|
|
|
|
while (true) {
|
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
|
|
|
|
assert(Scope.isNormalCleanup());
|
|
|
|
I = Scope.getEnclosingNormalCleanup();
|
|
|
|
|
|
|
|
// If this is the last cleanup we're propagating through, tell it
|
|
|
|
// that there's a resolved jump moving through it.
|
|
|
|
if (!E.strictlyEncloses(I)) {
|
|
|
|
Scope.addBranchAfter(Index, Dest.getBlock());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-26 02:41:41 +08:00
|
|
|
// Otherwise, tell the scope that there's a jump propagating
|
2011-01-28 19:13:47 +08:00
|
|
|
// through it. If this isn't new information, all the rest of
|
|
|
|
// the work has been done before.
|
|
|
|
if (!Scope.addBranchThrough(Dest.getBlock()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-28 19:13:47 +08:00
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
|
|
|
|
EHScopeStack::stable_iterator C) {
|
|
|
|
// If we needed a normal block for any reason, that counts.
|
|
|
|
if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check whether any enclosed cleanups were needed.
|
|
|
|
for (EHScopeStack::stable_iterator
|
|
|
|
I = EHStack.getInnermostNormalCleanup();
|
|
|
|
I != C; ) {
|
|
|
|
assert(C.strictlyEncloses(I));
|
|
|
|
EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
|
|
|
|
if (S.getNormalBlock()) return true;
|
|
|
|
I = S.getEnclosingNormalCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
|
2011-08-11 10:22:43 +08:00
|
|
|
EHScopeStack::stable_iterator cleanup) {
|
2011-01-28 19:13:47 +08:00
|
|
|
// If we needed an EH block for any reason, that counts.
|
2011-08-11 10:22:43 +08:00
|
|
|
if (EHStack.find(cleanup)->hasEHBranches())
|
2011-01-28 19:13:47 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check whether any enclosed cleanups were needed.
|
|
|
|
for (EHScopeStack::stable_iterator
|
2011-08-11 10:22:43 +08:00
|
|
|
i = EHStack.getInnermostEHScope(); i != cleanup; ) {
|
|
|
|
assert(cleanup.strictlyEncloses(i));
|
|
|
|
|
|
|
|
EHScope &scope = *EHStack.find(i);
|
|
|
|
if (scope.hasEHBranches())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
i = scope.getEnclosingEHScope();
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ForActivation_t {
|
|
|
|
ForActivation,
|
|
|
|
ForDeactivation
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The given cleanup block is changing activation state. Configure a
|
|
|
|
/// cleanup variable if necessary.
|
|
|
|
///
|
|
|
|
/// It would be good if we had some way of determining if there were
|
|
|
|
/// extra uses *after* the change-over point.
|
|
|
|
static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
|
|
|
|
EHScopeStack::stable_iterator C,
|
2011-11-10 18:43:54 +08:00
|
|
|
ForActivation_t kind,
|
|
|
|
llvm::Instruction *dominatingIP) {
|
2011-01-28 19:13:47 +08:00
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
|
|
|
|
|
2011-11-10 17:22:44 +08:00
|
|
|
// We always need the flag if we're activating the cleanup in a
|
|
|
|
// conditional context, because we have to assume that the current
|
|
|
|
// location doesn't necessarily dominate the cleanup's code.
|
|
|
|
bool isActivatedInConditional =
|
2011-11-10 18:43:54 +08:00
|
|
|
(kind == ForActivation && CGF.isInConditionalBranch());
|
2011-11-10 17:22:44 +08:00
|
|
|
|
|
|
|
bool needFlag = false;
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Calculate whether the cleanup was used:
|
|
|
|
|
|
|
|
// - as a normal cleanup
|
2011-11-10 17:22:44 +08:00
|
|
|
if (Scope.isNormalCleanup() &&
|
|
|
|
(isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
|
2011-01-28 19:13:47 +08:00
|
|
|
Scope.setTestFlagInNormalCleanup();
|
2011-11-10 17:22:44 +08:00
|
|
|
needFlag = true;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// - as an EH cleanup
|
2011-11-10 17:22:44 +08:00
|
|
|
if (Scope.isEHCleanup() &&
|
|
|
|
(isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
|
2011-01-28 19:13:47 +08:00
|
|
|
Scope.setTestFlagInEHCleanup();
|
2011-11-10 17:22:44 +08:00
|
|
|
needFlag = true;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it hasn't yet been used as either, we're done.
|
2011-11-10 17:22:44 +08:00
|
|
|
if (!needFlag) return;
|
2011-01-28 19:13:47 +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 var = Scope.getActiveFlag();
|
|
|
|
if (!var.isValid()) {
|
|
|
|
var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), CharUnits::One(),
|
|
|
|
"cleanup.isactive");
|
2011-11-10 18:43:54 +08:00
|
|
|
Scope.setActiveFlag(var);
|
|
|
|
|
|
|
|
assert(dominatingIP && "no existing variable and no dominating IP!");
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
// Initialize to true or false depending on whether it was
|
|
|
|
// active up to this point.
|
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::Constant *value = CGF.Builder.getInt1(kind == ForDeactivation);
|
2011-11-10 18:43:54 +08:00
|
|
|
|
|
|
|
// If we're in a conditional block, ignore the dominating IP and
|
|
|
|
// use the outermost conditional branch.
|
|
|
|
if (CGF.isInConditionalBranch()) {
|
|
|
|
CGF.setBeforeOutermostConditional(value, var);
|
|
|
|
} else {
|
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
|
|
|
createStoreInstBefore(value, var, dominatingIP);
|
2011-11-10 18:43:54 +08:00
|
|
|
}
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
2011-11-10 18:43:54 +08:00
|
|
|
CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Activate a cleanup that was created in an inactivated state.
|
2011-11-10 18:43:54 +08:00
|
|
|
void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
|
|
|
|
llvm::Instruction *dominatingIP) {
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(C != EHStack.stable_end() && "activating bottom of stack?");
|
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
|
|
|
|
assert(!Scope.isActive() && "double activation");
|
|
|
|
|
2011-11-10 18:43:54 +08:00
|
|
|
SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
Scope.setActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deactive a cleanup that was created in an active state.
|
2011-11-10 18:43:54 +08:00
|
|
|
void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
|
|
|
|
llvm::Instruction *dominatingIP) {
|
2011-01-28 19:13:47 +08:00
|
|
|
assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
|
|
|
|
EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
|
|
|
|
assert(Scope.isActive() && "double deactivation");
|
|
|
|
|
2018-04-27 14:57:00 +08:00
|
|
|
// If it's the top of the stack, just pop it, but do so only if it belongs
|
|
|
|
// to the current RunCleanupsScope.
|
|
|
|
if (C == EHStack.stable_begin() &&
|
|
|
|
CurrentCleanupScopeDepth.strictlyEncloses(C)) {
|
2011-01-28 19:13:47 +08:00
|
|
|
// If it's a normal cleanup, we need to pretend that the
|
|
|
|
// fallthrough is unreachable.
|
|
|
|
CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
|
|
|
|
PopCleanupBlock();
|
|
|
|
Builder.restoreIP(SavedIP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, follow the general case.
|
2011-11-10 18:43:54 +08:00
|
|
|
SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
|
2011-01-28 19:13:47 +08:00
|
|
|
|
|
|
|
Scope.setActive(false);
|
|
|
|
}
|
|
|
|
|
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 CodeGenFunction::getNormalCleanupDestSlot() {
|
2018-01-13 06:07:01 +08:00
|
|
|
if (!NormalCleanupDest.isValid())
|
2011-01-28 19:13:47 +08:00
|
|
|
NormalCleanupDest =
|
2018-01-13 06:07:01 +08:00
|
|
|
CreateDefaultAlignTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
|
|
|
|
return NormalCleanupDest;
|
2011-01-28 19:13:47 +08:00
|
|
|
}
|
2011-11-28 06:09:22 +08:00
|
|
|
|
|
|
|
/// Emits all the code to cause the given temporary to be cleaned up.
|
|
|
|
void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
|
|
|
|
QualType TempType,
|
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 Ptr) {
|
2012-01-26 11:33:36 +08:00
|
|
|
pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
|
2011-11-28 06:09:22 +08:00
|
|
|
/*useEHCleanup*/ true);
|
|
|
|
}
|