[cfe-commits] r102068 - in /cfe/trunk: lib/CodeGen/CGRecordLayoutBuilder.cpp test/CodeGen/bitfield-2.c

Daniel Dunbar daniel at zuster.org
Thu Apr 22 08:22:33 PDT 2010


Author: ddunbar
Date: Thu Apr 22 10:22:33 2010
New Revision: 102068

URL: http://llvm.org/viewvc/llvm-project?rev=102068&view=rev
Log:
IRgen: Fix another case where we generated an invalid access component when we
immediately narrowed the access size. Fix this (and previous case) by just
choosing a better access size up-front.

Modified:
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
    cfe/trunk/test/CodeGen/bitfield-2.c

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=102068&r1=102067&r2=102068&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Thu Apr 22 10:22:33 2010
@@ -191,7 +191,14 @@
 
   // Round down from the field offset to find the first access position that is
   // at an aligned offset of the initial access type.
-  uint64_t AccessStart = FieldOffset - (FieldOffset % TypeSizeInBits);
+  uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
+
+  // Adjust initial access size to fit within record.
+  while (AccessWidth > 8 &&
+         AccessStart + AccessWidth > ContainingTypeSizeInBits) {
+    AccessWidth >>= 1;
+    AccessStart = FieldOffset - (FieldOffset % AccessWidth);
+  }
 
   while (AccessedTargetBits < FieldSize) {
     // Check that we can access using a type of this size, without reading off
@@ -210,20 +217,12 @@
     // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
     // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
     // in the target that we are reading.
+    assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
+    assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
     uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
     uint64_t AccessBitsInFieldSize =
-      std::min(AccessWidth - (AccessBitsInFieldStart - AccessStart),
-               FieldSize - (AccessBitsInFieldStart-FieldOffset));
-
-    // If we haven't accessed any target bits yet and narrowed the access size,
-    // we might not have reached any target bits yet.
-    //
-    // FIXME: This test is unnecessarily once we choose the initial acccess size
-    // more intelligently.
-    if (!AccessedTargetBits && AccessBitsInFieldSize == 0) {
-      AccessStart += AccessWidth;
-      continue;
-    }
+      std::min(AccessWidth + AccessStart,
+               FieldOffset + FieldSize) - AccessBitsInFieldStart;
 
     assert(NumComponents < 3 && "Unexpected number of components!");
     CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];

Modified: cfe/trunk/test/CodeGen/bitfield-2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/bitfield-2.c?rev=102068&r1=102067&r2=102068&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/bitfield-2.c (original)
+++ cfe/trunk/test/CodeGen/bitfield-2.c Thu Apr 22 10:22:33 2010
@@ -345,3 +345,24 @@
   res ^= g8.f0 ^ g8.f2 ^ g8.f3;
   return res;
 }
+
+/***/
+
+// This is another case where we narrow the access width immediately.
+//
+// <rdar://problem/7893760>
+
+struct __attribute__((packed)) s9 {
+  unsigned f0 : 7;
+  unsigned f1 : 7;
+  unsigned f2 : 7;
+  unsigned f3 : 7;
+  unsigned f4 : 7;
+  unsigned f5 : 7;
+  unsigned f6 : 7;
+  unsigned f7 : 7;
+};
+
+int f9_load(struct s9 *a0) {
+  return a0->f7;
+}





More information about the cfe-commits mailing list