Commit Graph

41 Commits

Author SHA1 Message Date
David Majnemer 929025d1a6 [MS ABI] Allow a member pointers' converted type to change
Member pointers in the MS ABI are tricky for a variety of reasons.
The size of a member pointer is indeterminate until the program reaches
a point where the representation is required to be known.  However,
*pointers* to member pointers may exist without knowing the pointee
type's representation.  In these cases, we synthesize an opaque LLVM
type for the pointee type.

However, we can be in a situation where the underlying member pointer's
representation became known mid-way through the program.  To account for
this, we attempted to manicure CodeGen's type-cache so that we can
replace the opaque member pointer type with the real deal while leaving
the pointer types unperturbed.  This, unfortunately, is a problematic
approach to take as we will violate CodeGen's invariants.

These violations are mostly harmless but let's do the right thing
instead: invalidate the type-cache if a member pointer's LLVM
representation changes.

This fixes PR26313.

llvm-svn: 258839
2016-01-26 19:30:26 +00:00
David Majnemer 69c3ddc44a [MS ABI] Select an inheritance model in template arguments
We used to only select an inheritance model if the pointer to member was
nullptr.  Instead, select a model regardless of the member pointer's
value.

N.B.  This bug was exposed by making member pointers report true for
isIncompleteType but has been latent since the member pointer scheme's
inception.

llvm-svn: 247464
2015-09-11 20:18:09 +00:00
David Majnemer cca07d7c48 [MS ABI] Select a pointer to member representation more often
Given a reference to a pointer to member whose class's inheritance model
is unspecified, make sure we come up with an inheritance model in
plausible places.  One place we were missing involved LValue to RValue
conversion, another involved unary type traits.

llvm-svn: 247248
2015-09-10 07:20:05 +00:00
David Majnemer 22ee1a7466 [MS ABI] Don't crash on references to pointers to members in args
We know that a reference can always be dereferenced.  However, we don't
always know the number of bytes if the reference's pointee type is
incomplete.  This case was correctly handled but we didn't consider the
case where the type is complete but we cannot calculate its size for ABI
specific reasons.  In this specific case, a member pointer's size is
available only under certain conditions.

This fixes PR24703.

llvm-svn: 247188
2015-09-09 20:57:59 +00:00
Alexey Bataev a93fb5b048 [MSVC] Crash fix: assigning of overloaded member function pointer caused assertion
Original class was not marked with inheritance attribute and it causes a crash on codegen.
Differential Revision: http://reviews.llvm.org/D11828

llvm-svn: 244428
2015-08-10 04:07:49 +00:00
David Majnemer c1709d387e [MS ABI] Rework member pointer conversion
Member pointers in the MS ABI are made complicated due to the following:
- Virtual methods in the most derived class (MDC) might live in a
  vftable in a virtual base.
- There are four different representations of member pointer: single
  inheritance, multiple inheritance, virtual inheritance and the "most
  general" representation.
- Bases might have a *more* general representation than classes which
  derived from them, a most surprising result.

We believed that we could treat all member pointers as-if they were a
degenerate case of the multiple inheritance model.  This fell apart once
we realized that implementing standard member pointers using this ABI
requires referencing members with a non-zero vbindex.

On a bright note, all but the virtual inheritance model operate rather
similarly.  The virtual inheritance member pointer representation
awkwardly requires a virtual base adjustment in order to refer to
entities in the MDC.

However, the first virtual base might be quite far from the start of the
virtual base.  This means that we must add a negative non-virtual
displacement.

However, things get even more complicated.  The most general
representation interprets vbindex zero differently from the virtual
inheritance model: it doesn't reference the vbtable at all.

It turns out that this complexity can increase for quite some time:
consider a derived to base conversion from the most general model to the
multiple inheritance model...

To manage this complexity we introduce a concept of "normalized" member
pointer which allows us to treat all three models as the most general
model.  Then we try to figure out how to map this generalized member
pointer onto the destination member pointer model.  I've done my best to
furnish the code with comments explaining why each adjustment is
performed.

This fixes PR23878.

llvm-svn: 240384
2015-06-23 07:31:11 +00:00
David Majnemer 569c83daf0 Forgot to update to a test in r240043
llvm-svn: 240049
2015-06-18 20:34:05 +00:00
David Majnemer 7bd29f2613 [MS ABI] Initialize "most general" member pointers which don't point at a vbase
The most general model has fields for the vbptr offset and the vbindex.
Don't initialize the vbptr offset if the vbindex is 0: we aren't
referencing an entity from a vbase.

Getting this wrong can make member pointer equality fail.

llvm-svn: 240043
2015-06-18 20:20:10 +00:00
David Majnemer fc22e4774b [MS ABI] Lock-in the derived memptr rep. for base-to-derived conversions
We would get this right in the case where an explicit cast was formed
but not when we were performing an implicit conversion.

This fixes PR23828.

llvm-svn: 239625
2015-06-12 17:55:44 +00:00
David Majnemer e154456d4a [MS ABI] Fix the preferred alignment of member pointers
Member pointers in the MS ABI have different alignment depending on
whether they were created on the stack or live in a record.

llvm-svn: 235681
2015-04-24 01:25:05 +00:00
David Blaikie d6c88ece21 [opaque pointer types] Explicit non-pointer type for call expressions
(migration for recent LLVM change to textual IR for calls)

llvm-svn: 235147
2015-04-16 23:25:00 +00:00
David Blaikie a953f2825b Update Clang tests to handle explicitly typed load changes in LLVM.
llvm-svn: 230795
2015-02-27 21:19:58 +00:00
David Blaikie 218b783192 Update Clang tests to handle explicitly typed gep changes in LLVM.
llvm-svn: 230783
2015-02-27 19:18:17 +00:00
David Majnemer 8c9cdb6573 MS ABI: Virtual member pointer thunks should be in COMDAT groups
They can be emitted by multiple translation units and thus belong in a
COMDAT group.

llvm-svn: 226630
2015-01-21 01:21:31 +00:00
David Majnemer 6a2fabcc3f MS ABI: Add another test for PR20017
llvm-svn: 223733
2014-12-09 01:36:45 +00:00
Reid Kleckner 0ba8ba4eca MS ABI: Emit more canonical vbptr stores and loads
This eliminates some i8* GEPs and makes the IR that clang emits a bit
more canonical. More work is needed for vftables, but that isn't a clear
win so I plan to send it for review.

llvm-svn: 220398
2014-10-22 17:26:00 +00:00
David Majnemer 9928106536 MS ABI: Don't ICE for pointers to pointers to members of incomplete classes
CodeGen would try to come up with an LLVM IR type for a pointer to
member type on the way to forming an LLVM IR type for a pointer to
pointer to member type.

However, if the pointer to member representation has not been locked in yet,
we would not be able to come up with a pointer to member IR type.

In these cases, make the pointer to member type an incomplete type.
This will make the pointer to pointer to member type a pointer to an
incomplete type.  If the class eventually obtains an inheritance model,
we will make the pointer to member type represent the actual inheritance
model.

Differential Revision: http://reviews.llvm.org/D5373

llvm-svn: 218084
2014-09-18 22:05:54 +00:00
Reid Kleckner c34735148f Make all virtual member pointers use variadic musttail calls
This avoids encoding information about the function prototype into the
thunk at the cost of some function prototype bitcast gymnastics.

Fixes PR20653.

llvm-svn: 216782
2014-08-29 21:43:29 +00:00
David Majnemer d96b99740d MS ABI: Don't force bases to have an inheritance model
Previously, assigning an inheritance model to a derived class would
trigger further assiginments to the various bases of the class.  This
was done to fix a bug where we couldn't handle an implicit
base-to-derived conversion for pointers-to-members when the conversion
was ambiguous at an earlier point.

However, this is not how the MS scheme works.  Instead, assign
inheritance models to *just* the class which owns to declaration we
ended up referencing.

N.B.  This result is surprising in many ways.  It means that it is
possible for a base to have a "larger" inheritance model than it's
derived classes.  It also means that bases in the conversion path do not
get assigned a model.

struct A { void f(); void f(int); };
struct B : A {};
struct C : B {};
void f() { void (C::*x)() = &A::f; }

We can only begin to assign an inheritance model *after* we've seen the
address-of but *before* we've done the implicit conversion the more
derived pointer-to-member type.  After that point, both 'A' and 'C' will
have an inheritance model but 'B' will not.  Surprising, right?

llvm-svn: 215174
2014-08-08 00:10:39 +00:00
David Majnemer 5ef4fe7d8e MS ABI: Fix inheritance model calculation in CRTP
CRTP-like patterns involve a class which inherits from another class
using itself as a template parameter.

However, the base class itself may try to create a pointer-to-member
which involves the derived class.  This is problematic because we
may not have finished parsing the most derived classes' base specifiers
yet.

It turns out that MSVC simply uses the unspecified inheritance model
instead of doing anything fancy.

This fixes PR19987.

llvm-svn: 210886
2014-06-13 06:43:46 +00:00
Reid Kleckner b6e7d64f30 s/pr2007/20007/ in a test
llvm-svn: 210834
2014-06-12 21:46:38 +00:00
Reid Kleckner a766029b81 MS ABI: Fix forming pointers to members of a base class
Previously we would calculate the inheritance model of a class when
requiring a pointer to member type of that class to be complete.  The
inheritance model is used to figure out how many fields are used by the
member pointer.

However, once we require a pointer to member of a derived class type to
be complete, we can form pointers to members of bases without
calculating the inheritance model for those bases.  This was causing
crashes on this simple test case:

  struct A {
    void f();
    void f(int);
  };
  struct B : public A {};
  void g() { void (B::*a)() = &B::f; }

Now we calculate the inheritance models of all base classes when
completing a member pointer type.

Fixes PR2007.

llvm-svn: 210813
2014-06-12 19:49:17 +00:00
Reid Kleckner 9005f41837 Win64: Pass member pointers larger than 8 bytes by reference
The Win64 ABI docs on MSDN say that arguments bigger than 8 bytes are
passed by reference.  Prior to this change, we were only applying this
logic to RecordType arguments.  This affects both the Itanium and
Microsoft C++ ABIs.

Reviewers: majnemer

Differential Revision: http://reviews.llvm.org/D3587

llvm-svn: 207817
2014-05-02 00:51:20 +00:00
Reid Kleckner 34a38d8efb MS ABI: Fix logic bug in member pointer null test code
This code is trying to test if the pointer is *not* null.  Therefore we
should use 'or' instead of 'and' to combine the results of 'icmp ne'.
This logic is consistent with the general member pointer comparison code
in EmitMemberPointerComparison.

llvm-svn: 207815
2014-05-02 00:05:16 +00:00
Reid Kleckner e4a5220d7c MS ABI: Include the vfptr offset in memptrs to virtual methods
Virtual methods expect 'this' to point to the vfptr containing the
virtual method, and this extends to virtual member pointer thunks.  The
relevant vfptr is always at offset zero on entry to the thunk, and no
this adjustment is needed.

Previously we would not include the vfptr adjustment in the member
pointer, and we'd look at the vfptr offset when loading from the vftable
in the thunk.

Fixes PR18917.

llvm-svn: 201835
2014-02-21 02:27:32 +00:00
David Majnemer 2b0d66df24 Sema: Do not assert when dereferencing member pointer using virtual inheritance with an incomplete class type
The MS ABI requires that we determine the vbptr offset if have a
virtual inheritance model.  Instead, raise an error pointing to the
diagnostic when this happens.

This fixes PR18583.

Differential Revision: http://llvm-reviews.chandlerc.com/D2842

llvm-svn: 201824
2014-02-20 23:22:07 +00:00
Reid Kleckner bd63b33854 [ms-cxxabi] Raise aggregate memptr alignment to 8 for x86_32
With this change, we give different results for __alignof than MSVC, but
our record layout is compatible.

Some data member pointers also now have a size that is not a multiple of
their alignment.

Fixes PR18618.

Reviewers: majnemer

Differential Revision: http://llvm-reviews.chandlerc.com/D2669

llvm-svn: 200585
2014-01-31 22:28:50 +00:00
Hans Wennborg c9bd88e681 Remove the -cxx-abi command-line flag.
This makes the C++ ABI depend entirely on the target: MS ABI for -win32 triples,
Itanium otherwise. It's no longer possible to do weird combinations.

To be able to run a test with a specific ABI without constraining it to a
specific triple, new substitutions are added to lit: %itanium_abi_triple and
%ms_abi_triple can be used to get the current target triple adjusted to the
desired ABI. For example, if the test suite is running with the i686-pc-win32
target, %itanium_abi_triple will expand to i686-pc-mingw32.

Differential Revision: http://llvm-reviews.chandlerc.com/D2545

llvm-svn: 199250
2014-01-14 19:35:09 +00:00
Warren Hunt 6eba907102 [ms-abi] Report VBPtrOffset correctly
Although VBPtrs were being placed correctly by the ms-abi layout engine, 
their offsets were being improperly reported to the ASTRecordLayout 
builder due to a bug.  This patch fixes that and fixes the test cases to 
use the correct values.
y

llvm-svn: 199168
2014-01-14 00:31:30 +00:00
Warren Hunt d640d7d96e [ms-abi] Refactor Microsoft Record Layout
This patch refactors microsoft record layout to be more "natural".  The 
most dominant change is that vbptrs and vfptrs are injected after the 
fact.  This simplifies the implementation and the math for the offest 
for the first base/field after the vbptr.

llvm-svn: 198818
2014-01-09 00:30:56 +00:00
Reid Kleckner ae9451218f [ms-cxxabi] bitcast to i8* to deref a data member pointer
This was causing us to miscompile
llvm::SymbolTableListTraits::getListOwner(), which uses data member
pointers.

llvm-svn: 196545
2013-12-05 22:44:07 +00:00
Reid Kleckner aec4409174 [ms-cxxabi] Fix assert in unspecified inheritance memptr emission
If a class is using the unspecified inheritance model for member
pointers and later we find the class is defined to use single
inheritance, zero out the vbptr offset field of the member pointer when
it is formed.

llvm-svn: 192664
2013-10-15 01:18:02 +00:00
David Majnemer 89594f3733 [ms-cxxabi] Properly mangle member pointers
There were three things missing from the original implementation:

- We would omit the 'E' qualifier for members int 64-bit mode.
- We would not exmaine the qualifiers in 'IsMember' mode.
- We didn't generate the correct backref to the base class.

llvm-svn: 187753
2013-08-05 22:43:06 +00:00
Reid Kleckner ae748148c1 [ms-cxxabi] Fix vbptr offsets in memptrs when the vbptr is in an nvbase
Also addresses a review comment from John from on r180985 by removing
the "== -1" check, since it's now reusing the correct code which has the
comment.

llvm-svn: 183318
2013-06-05 15:58:29 +00:00
Reid Kleckner 452abac4b3 [ms-cxxabi] Implement member pointer conversions
Summary:
This only supports converting along non-virtual inheritance paths by
changing the field offset or the non-virtual base adjustment.

This implements three kinds of conversions:
- codegen for Value conversions
- Constant emission for APValue
- Constant folding for CastExprs

In almost all constant initialization settings
EmitMemberPointer(APValue) is called, except when the expression
contains a reinterpret cast.

reinterpret casts end up being a big corner case because the null value
changes between different kinds of member pointers.

Reviewers: rsmith

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D741

llvm-svn: 181543
2013-05-09 21:01:17 +00:00
Reid Kleckner 7d0efb57d3 [ms-cxxabi] Emit non-virtual member function pointers
Without any conversion, this is pretty straightforward.  Most of the
fields can be zeros.  The order is:

- field offset or pointer
- nonvirtual adjustment (for MI functions)
- vbptr offset (for unspecified)
- virtual adjustment offset (for virtual inheritance)

Differential Revision: http://llvm-reviews.chandlerc.com/D699

llvm-svn: 180985
2013-05-03 01:15:11 +00:00
Reid Kleckner 700c3eea1e [ms-cxxabi] Implement member pointer comparisons
Summary:
Like Itanium, comparisons are basically bitwise comparisons of the two
values, with an exception for null member function pointers.  If two
function pointers are null, only the function pointer field matters for
comparison purposes.  The rest of the bits can be arbitrary.  We take
advantage of this in isZeroInitializable(), and it may matter once we
start emitting conversions.

Reviewers: rjmccall

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D695

llvm-svn: 180800
2013-04-30 20:15:14 +00:00
Arnaud A. de Grandmaison 7c6571bce1 Cleanup: test source file does not need to be executable
llvm-svn: 180002
2013-04-22 08:00:37 +00:00
Reid Kleckner dac9342a52 Widen the checks in the ms abi memptr test to work under NDEBUG
llvm-svn: 179311
2013-04-11 19:01:17 +00:00
Reid Kleckner 2341ae3856 [ms-cxxabi] Implement member pointer emission and dereferencing
Summary:
Handles all inheritance models for both data and function member
pointers.

Also implements isZeroInitializable() and refactors some of the null
member pointer code.

MSVC supports converting member pointers through virtual bases, which
clang does not (yet?) support.  Implementing that extension is covered
by http://llvm.org/15713

Reviewers: rjmccall

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D613

llvm-svn: 179305
2013-04-11 18:13:19 +00:00
Reid Kleckner 407e8b642b [ms-cxxabi] Implement member data pointers for non-dynamic classes
Summary:
For non-dynamic classes (no virtual bases), member data pointers are
simple offsets from the base of the record.  Dynamic classes use an
aggregate for member data pointers and are therefore currently
unsupported.

Unlike Itanium, the ms ABI uses 0 to represent null for polymorphic
classes.  Non-polymorphic classes use -1 like Itanium, since 0 is a
valid field offset.

Reviewers: rjmccall

CC: timurrrr, cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D558

llvm-svn: 177753
2013-03-22 19:02:54 +00:00