[llvm-commits] [dragonegg] r97730 - in /dragonegg/trunk: llvm-abi-default.cpp llvm-backend.cpp llvm-convert.cpp llvm-internal.h llvm-types.cpp

Duncan Sands baldrick at free.fr
Thu Mar 4 06:33:37 PST 2010


Author: baldrick
Date: Thu Mar  4 08:33:37 2010
New Revision: 97730

URL: http://llvm.org/viewvc/llvm-project?rev=97730&view=rev
Log:
Cache the LLVM index corresponding to a GCC FIELD_DECL node "inside"
the node.  This means that the value is automagically deleted if the
node is garbage collected.  This doesn't currently matter, but will
be important when LLVM indices are lazily computed, as will be done
in a later patch.  Also, provide a means to distinguish between "no
index was yet associated with the node" (GetFieldIndex returns -1)
and "the index associated with the node is 'no such index'" (returns
INT_MAX).

Modified:
    dragonegg/trunk/llvm-abi-default.cpp
    dragonegg/trunk/llvm-backend.cpp
    dragonegg/trunk/llvm-convert.cpp
    dragonegg/trunk/llvm-internal.h
    dragonegg/trunk/llvm-types.cpp

Modified: dragonegg/trunk/llvm-abi-default.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-abi-default.cpp?rev=97730&r1=97729&r2=97730&view=diff
==============================================================================
--- dragonegg/trunk/llvm-abi-default.cpp (original)
+++ dragonegg/trunk/llvm-abi-default.cpp Thu Mar  4 08:33:37 2010
@@ -136,8 +136,8 @@
       if (TREE_CODE(Field) == FIELD_DECL) {
         const tree Ftype = getDeclaredType(Field);
         const Type *FTy = ConvertType(Ftype);
-        unsigned FNo = GetFieldIndex(Field);
-        assert(FNo != ~0U && "Case not handled yet!");
+        int FNo = GetFieldIndex(Field);
+        assert(FNo >= 0 && FNo != INT_MAX && "Case not handled yet!");
 
         // Currently, a bvyal type inside a non-byval struct is a zero-length
         // object inside a bigger object on x86-64.  This type should be

Modified: dragonegg/trunk/llvm-backend.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-backend.cpp?rev=97730&r1=97729&r2=97730&view=diff
==============================================================================
--- dragonegg/trunk/llvm-backend.cpp (original)
+++ dragonegg/trunk/llvm-backend.cpp Thu Mar  4 08:33:37 2010
@@ -156,6 +156,32 @@
   return (Value *)llvm_get_cached(t);
 }
 
+/// SetFieldIndex - Set the index of the LLVM field that corresponds to the
+/// given FIELD_DECL.  By convention, a value of INT_MAX indicates that there
+/// is no such LLVM field.
+void SetFieldIndex(tree t, int i) {
+  assert(TREE_CODE(t) == FIELD_DECL && "Expected a FIELD_DECL!");
+  assert(!CODE_CONTAINS_STRUCT(FIELD_DECL, TS_DECL_WRTL) &&
+         "FIELD_DECL has RTL!");
+  assert(i >= 0 && "Negative indices not allowed!");
+  // In order to use zero as a special value (see GetFieldIndex), map the range
+  // 0 .. INT_MAX to -1 .. INT_MIN.
+  llvm_set_cached(t, (void *)(intptr_t)(-i - 1));
+}
+
+/// GetFieldIndex - Get the index of the LLVM field that corresponds to the
+/// given FIELD_DECL.  By convention, a value of INT_MAX indicates that there
+/// is no such LLVM field.  Returns a negative number if no index was yet set
+/// for the field.
+int GetFieldIndex(tree t) {
+  assert(TREE_CODE(t) == FIELD_DECL && "Expected a FIELD_DECL!");
+  assert(!CODE_CONTAINS_STRUCT(FIELD_DECL, TS_DECL_WRTL) &&
+         "FIELD_DECL has RTL!");
+  // Map the range -1 .. INT_MIN back to 0 .. INT_MAX (see SetFieldIndex), while
+  // sending 0 (aka void) to -1.
+  return -(1 + (int)(intptr_t)llvm_get_cached(t));
+}
+
 /// changeLLVMConstant - Replace Old with New everywhere, updating all maps
 /// (except for AttributeAnnotateGlobals, which is a different kind of animal).
 /// At this point we know that New is not in any of these maps.

Modified: dragonegg/trunk/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-convert.cpp?rev=97730&r1=97729&r2=97730&view=diff
==============================================================================
--- dragonegg/trunk/llvm-convert.cpp (original)
+++ dragonegg/trunk/llvm-convert.cpp Thu Mar  4 08:33:37 2010
@@ -5333,13 +5333,15 @@
   if (OffsetIsLLVMCompatible(FieldDecl)) {
     BitStart = getFieldOffsetInBits(TREE_OPERAND(exp, 1));
     assert(!TREE_OPERAND(exp, 2) && "Constant not gimple min invariant?");
-    unsigned int MemberIndex = GetFieldIndex(FieldDecl);
+    int MemberIndex = GetFieldIndex(FieldDecl);
+    assert(MemberIndex >= 0 && MemberIndex != INT_MAX &&
+           "Type not laid out for LLVM?");
 
     // If the LLVM struct has zero field, don't try to index into it, just use
     // the current pointer.
     FieldPtr = StructAddrLV.Ptr;
     if (StructTy->getNumContainedTypes() != 0) {
-      assert(MemberIndex < StructTy->getNumContainedTypes() &&
+      assert(MemberIndex < (int)StructTy->getNumContainedTypes() &&
              "Field Idx out of range!");
       FieldPtr = Builder.CreateStructGEP(FieldPtr, MemberIndex);
     }
@@ -8611,7 +8613,9 @@
   // If this is a normal field at a fixed offset from the start, handle it.
   if (OffsetIsLLVMCompatible(FieldDecl)) {
     BitStart = getFieldOffsetInBits(TREE_OPERAND(exp, 1));
-    unsigned int MemberIndex = GetFieldIndex(FieldDecl);
+    int MemberIndex = GetFieldIndex(FieldDecl);
+    assert(MemberIndex >= 0 && MemberIndex != INT_MAX &&
+           "Type not laid out for LLVM?");
 
     Constant *Ops[] = {
       StructAddrLV,

Modified: dragonegg/trunk/llvm-internal.h
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-internal.h?rev=97730&r1=97729&r2=97730&view=diff
==============================================================================
--- dragonegg/trunk/llvm-internal.h (original)
+++ dragonegg/trunk/llvm-internal.h Thu Mar  4 08:33:37 2010
@@ -42,7 +42,6 @@
 // System headers
 #include <vector>
 #include <cassert>
-#include <map>
 #include <string>
 
 namespace llvm {
@@ -122,6 +121,19 @@
 Value *make_definition_llvm(union tree_node *decl);
 #define DEFINITION_LLVM(NODE) make_definition_llvm(NODE)
 
+// Mapping between GCC field declarations and LLVM indices.
+
+/// SetFieldIndex - Set the index of the LLVM field that corresponds to the
+/// given FIELD_DECL.  By convention, a value of INT_MAX indicates that there
+/// is no such LLVM field.
+void SetFieldIndex(union tree_node *, int);
+
+/// GetFieldIndex - Get the index of the LLVM field that corresponds to the
+/// given FIELD_DECL.  By convention, a value of INT_MAX indicates that there
+/// is no such LLVM field.  Returns a negative number if no index was yet set
+/// for the field.
+int GetFieldIndex(union tree_node *);
+
 void changeLLVMConstant(Constant *Old, Constant *New);
 void register_ctor_dtor(Function *, int, bool);
 void readLLVMTypesStringTable();
@@ -152,10 +164,6 @@
   /// we add the POINTER_TYPE to this list.
   ///
   std::vector<tree_node*> PointersToReresolve;
-
-  /// FieldIndexMap - Holds the mapping from a FIELD_DECL to the index of the
-  /// corresponding LLVM field.
-  std::map<tree_node *, unsigned int> FieldIndexMap;
 public:
   TypeConverter() : ConvertingStruct(false) {}
 
@@ -163,10 +171,6 @@
   /// of the given GCC type (GetRegType should be used for values in registers).
   const Type *ConvertType(tree_node *type);
 
-  /// GetFieldIndex - Returns the index of the LLVM field corresponding to
-  /// this FIELD_DECL.
-  unsigned int GetFieldIndex(tree_node *field_decl);
-
   /// GCCTypeOverlapsWithLLVMTypePadding - Return true if the specified GCC type
   /// has any data that overlaps with structure padding in the specified LLVM
   /// type.
@@ -193,7 +197,6 @@
   
 private:
   const Type *ConvertRECORD(tree_node *type);
-  void SetFieldIndex(tree_node *field_decl, unsigned int Index);
   bool DecodeStructFields(tree_node *Field, StructTypeConversionInfo &Info);
   void DecodeStructBitField(tree_node *Field, StructTypeConversionInfo &Info);
   void SelectUnionMember(tree_node *type, StructTypeConversionInfo &Info);
@@ -207,12 +210,6 @@
   return TheTypeConverter->ConvertType(type);
 }
 
-/// GetFieldIndex - Given FIELD_DECL obtain its index.
-///
-inline unsigned int GetFieldIndex(tree_node *field_decl) {
-  return TheTypeConverter->GetFieldIndex(field_decl);
-}
-
 /// getINTEGER_CSTVal - Return the specified INTEGER_CST value as a uint64_t.
 ///
 uint64_t getINTEGER_CSTVal(tree_node *exp);

Modified: dragonegg/trunk/llvm-types.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-types.cpp?rev=97730&r1=97729&r2=97730&view=diff
==============================================================================
--- dragonegg/trunk/llvm-types.cpp (original)
+++ dragonegg/trunk/llvm-types.cpp Thu Mar  4 08:33:37 2010
@@ -607,23 +607,6 @@
   }
 }
 
-/// GetFieldIndex - Returns the index of the LLVM field corresponding to
-/// this FIELD_DECL, or ~0U if the type the field belongs to has not yet
-/// been converted.
-unsigned int TypeConverter::GetFieldIndex(tree field_decl) {
-  assert(TREE_CODE(field_decl) == FIELD_DECL && "Not a FIELD_DECL!");
-  std::map<tree, unsigned int>::iterator I = FieldIndexMap.find(field_decl);
-  assert(I != FieldIndexMap.end() && "Type not laid out for LLVM?");
-  return I->second;
-}
-
-/// SetFieldIndex - Set the index of the LLVM field corresponding to
-/// this FIELD_DECL.
-void TypeConverter::SetFieldIndex(tree_node *field_decl, unsigned int Index) {
-  assert(TREE_CODE(field_decl) == FIELD_DECL && "Not a FIELD_DECL!");
-  FieldIndexMap[field_decl] = Index;
-}
-
 bool TypeConverter::GCCTypeOverlapsWithLLVMTypePadding(tree type, 
                                                        const Type *Ty) {
   
@@ -1496,8 +1479,8 @@
   ///
   /// This returns the first field that contains the specified bit.
   ///
-  unsigned getLLVMFieldFor(uint64_t FieldOffsetInBits, unsigned &CurFieldNo,
-                           bool isZeroSizeField) {
+  int getLLVMFieldFor(uint64_t FieldOffsetInBits, unsigned &CurFieldNo,
+                      bool isZeroSizeField) {
     if (!isZeroSizeField) {
       // Skip over LLVM fields that start and end before the GCC field starts.
       while (CurFieldNo < ElementOffsetInBytes.size() &&
@@ -1508,7 +1491,7 @@
       // Otherwise, we couldn't find the field!
       // FIXME: this works around a latent bug!
       //assert(0 && "Could not find field!");
-      return ~0U;
+      return INT_MAX;
     }
 
     // Handle zero sized fields now.
@@ -1538,7 +1521,7 @@
     
     // Otherwise, we couldn't find the field!
     assert(0 && "Could not find field!");
-    return ~0U;
+    return INT_MAX;
   }
 
   void addNewBitField(uint64_t Size, uint64_t FirstUnallocatedByte);
@@ -2069,11 +2052,11 @@
       bool isZeroSizeField = FieldTy->isSized() &&
         getTargetData().getTypeSizeInBits(FieldTy) == 0;
 
-      unsigned FieldNo =
+      int FieldNo =
         Info->getLLVMFieldFor(FieldOffsetInBits, CurFieldNo, isZeroSizeField);
       SetFieldIndex(Field, FieldNo);
 
-      assert((isBitfield(Field) || FieldNo == ~0U ||
+      assert((isBitfield(Field) || FieldNo == INT_MAX ||
               FieldOffsetInBits == 8*Info->ElementOffsetInBytes[FieldNo]) &&
              "Wrong LLVM field offset!");
     }





More information about the llvm-commits mailing list