LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 603 - [llvm-gcc] CFE does not lay out structures correctly when funny darwin rules apply
Summary: [llvm-gcc] CFE does not lay out structures correctly when funny darwin rules ...
Status: RESOLVED FIXED
Alias: None
Product: tools
Classification: Unclassified
Component: llvm-gcc (show other bugs)
Version: 1.0
Hardware: Macintosh MacOS X
: P normal
Assignee: Chris Lattner
URL:
Keywords: miscompilation
: 449 (view as bug list)
Depends on:
Blocks: 449
  Show dependency tree
 
Reported: 2005-07-21 18:30 PDT by Chris Lattner
Modified: 2010-02-22 12:41 PST (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Lattner 2005-07-21 18:30:43 PDT
llvm-gcc is currently miscompiling the following code:

----
typedef union {
  int A; long long L;
} X;
typedef struct {
  X   x;
  int B;
} Y;
Y* foo(Y *l) { return l+1; }
---

In this case, the X union should have 8-byte alignment due to funny darwin rules (even though long 
long's are normally only 4-byte aligned).  When X is the first element of a struct, that increases its 
alignment requirements to 8-bytes, which increases the size of Y from 12 to 16 bytes.

llvm-gcc currently compiles this to:

---
        %struct.Y = type { %union.X, int }
        %union.X = type { long }

%struct.Y* %_Z3fooP1Y(%struct.Y* %l) {
        %tmp.2 = getelementptr %struct.Y* %l, int 1             ; <%struct.Y*> [#uses=1]
        ret %struct.Y* %tmp.2
}
---

... which is incorrect: Y is only 12 bytes in size.  This causes us to emit the following PPC code:

__Z3fooP1Y:
        addi r3, r3, 12
        blr

... instead of the correct code:

__Z3fooP1Y:
        addi r3, r3, 16
        blr

This problem was reduced from Bug 449.

-Chris
Comment 1 Chris Lattner 2005-07-21 18:37:23 PDT
geeze, here's another simpler case:

typedef struct {
  double X;
  int B;
} Y;
Y* foo(Y *l) { return l+1; }

Darwin only makes "Y" 8-byte aligned if the double is first in the struct.

-Chris
Comment 2 Chris Lattner 2005-07-25 20:19:59 PDT
Fixed.  Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20050725/027198.html

Testcase here:
Regression/C++/ofstream_ctor.cpp

This also fixes PR449.

-Chris
Comment 3 Chris Lattner 2005-07-25 20:22:19 PDT
*** Bug 449 has been marked as a duplicate of this bug. ***