[llvm] r288904 - [LowerTypeTests] Use the TrailingObjects infrastructure for trailing objects.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 04:31:45 PST 2016


Author: d0k
Date: Wed Dec  7 06:31:45 2016
New Revision: 288904

URL: http://llvm.org/viewvc/llvm-project?rev=288904&view=rev
Log:
[LowerTypeTests] Use the TrailingObjects infrastructure for trailing objects.

Also avoid allocating ~3x as much memory as needed.

Modified:
    llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp

Modified: llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp?rev=288904&r1=288903&r2=288904&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp Wed Dec  7 06:31:45 2016
@@ -30,6 +30,7 @@
 #include "llvm/IR/Operator.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/TrailingObjects.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -211,26 +212,29 @@ struct ByteArrayInfo {
 /// metadata types referenced by a global, which at the IR level is an expensive
 /// operation involving a map lookup; this data structure helps to reduce the
 /// number of times we need to do this lookup.
-class GlobalTypeMember {
+class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
   GlobalObject *GO;
   size_t NTypes;
-  MDNode *Types[1]; // We treat this as a flexible array member.
+
+  friend class TrailingObjects;
+  size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
 
 public:
   static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
                                   ArrayRef<MDNode *> Types) {
-    auto GTM = Alloc.Allocate<GlobalTypeMember>(
-        sizeof(GlobalTypeMember) + (Types.size() - 1) * sizeof(MDNode *));
+    auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
+        totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
     GTM->GO = GO;
     GTM->NTypes = Types.size();
-    std::copy(Types.begin(), Types.end(), GTM->Types);
+    std::uninitialized_copy(Types.begin(), Types.end(),
+                            GTM->getTrailingObjects<MDNode *>());
     return GTM;
   }
   GlobalObject *getGlobal() const {
     return GO;
   }
   ArrayRef<MDNode *> types() const {
-    return {&Types[0], &Types[NTypes]};
+    return makeArrayRef(getTrailingObjects<MDNode *>(), NTypes);
   }
 };
 




More information about the llvm-commits mailing list