[llvm-commits] CVS: poolalloc/lib/PoolAllocate/Heuristic.cpp Heuristic.h PoolAllocate.cpp PoolAllocate.h

Chris Lattner lattner at cs.uiuc.edu
Wed Nov 10 13:13:58 PST 2004



Changes in directory poolalloc/lib/PoolAllocate:

Heuristic.cpp updated: 1.3 -> 1.4
Heuristic.h updated: 1.1 -> 1.2
PoolAllocate.cpp updated: 1.90 -> 1.91
PoolAllocate.h updated: 1.30 -> 1.31

---
Log message:

Make intelligent alignment decisions.  In particular, align a pool to 8 bytes
(like malloc would) unless we can tell that there is no data in the pool that
requires 8-byte alignment.  In this case, only request 4-byte alignment.


---
Diffs of the changes:  (+63 -13)

Index: poolalloc/lib/PoolAllocate/Heuristic.cpp
diff -u poolalloc/lib/PoolAllocate/Heuristic.cpp:1.3 poolalloc/lib/PoolAllocate/Heuristic.cpp:1.4
--- poolalloc/lib/PoolAllocate/Heuristic.cpp:1.3	Tue Nov  9 14:16:25 2004
+++ poolalloc/lib/PoolAllocate/Heuristic.cpp	Wed Nov 10 15:13:47 2004
@@ -19,6 +19,7 @@
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Target/TargetData.h"
+#include <iostream>
 using namespace llvm;
 using namespace PA;
 
@@ -51,13 +52,53 @@
 unsigned Heuristic::getRecommendedSize(DSNode *N) {
   unsigned PoolSize = 0;
   if (!N->isArray() && N->getType()->isSized()) {
-    DSGraph *G = N->getParentGraph();
-    PoolSize = G->getTargetData().getTypeSize(N->getType());
+    PoolSize = N->getParentGraph()->getTargetData().getTypeSize(N->getType());
   }
   if (PoolSize == 1) PoolSize = 0;
   return PoolSize;
 }
 
+/// Wants8ByteAlignment - FIXME: this is a complete hack for X86 right now.
+static bool Wants8ByteAlignment(const Type *Ty, unsigned Offs,
+                                const TargetData &TD) {
+  if (Ty == Type::DoubleTy && (Offs & 7) == 0)
+    return true;
+  if (Ty->isPrimitiveType() || isa<PointerType>(Ty))
+    return false;
+
+  if (const StructType *STy = dyn_cast<StructType>(Ty)) {
+    const StructLayout *SL = TD.getStructLayout(STy);
+    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+      if (Wants8ByteAlignment(STy->getElementType(i),
+                              Offs+SL->MemberOffsets[i], TD))
+        return true;
+    }
+  } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
+    return Wants8ByteAlignment(STy->getElementType(), Offs, TD);
+  } else {
+    std::cerr << *Ty << "\n";
+    assert(0 && "Unknown type!");
+  }
+  return false;
+}
+
+
+
+/// getRecommendedAlignment - Return the recommended object alignment for this
+/// DSNode.
+///
+unsigned Heuristic::getRecommendedAlignment(DSNode *N) {
+  if (N->getType() == Type::VoidTy)  // Is this void or collapsed?
+    return 0;  // No known alignment, let runtime decide.
+  
+  const TargetData &TD = N->getParentGraph()->getTargetData();
+
+  // If there are no doubles on an 8-byte boundary in this structure, there is
+  // no reason to 8-byte align objects in the pool.
+  return Wants8ByteAlignment(N->getType(), 0, TD) ? 8 : 4;
+}
+ 
+
 //===-- AllNodes Heuristic ------------------------------------------------===//
 //
 // This heuristic pool allocates everything possible into separate pools.
@@ -337,7 +378,7 @@
                     Function *F, DSGraph &G,
                     std::vector<OnePool> &ResultPools) {
     if (TheGlobalPD == 0)
-      TheGlobalPD = PA->CreateGlobalPool(0);
+      TheGlobalPD = PA->CreateGlobalPool(0, 0);
 
     // All nodes allocate from the same global pool.
     OnePool Pool;


Index: poolalloc/lib/PoolAllocate/Heuristic.h
diff -u poolalloc/lib/PoolAllocate/Heuristic.h:1.1 poolalloc/lib/PoolAllocate/Heuristic.h:1.2
--- poolalloc/lib/PoolAllocate/Heuristic.h:1.1	Mon Nov  8 00:18:07 2004
+++ poolalloc/lib/PoolAllocate/Heuristic.h	Wed Nov 10 15:13:47 2004
@@ -58,13 +58,16 @@
       // PoolSize - If the pool is to be created, indicate the "recommended
       // size" for the pool here.  This gets passed into poolinit.
       unsigned PoolSize;
+      unsigned PoolAlignment;
 
-      OnePool() : PoolDesc(0), PoolSize(0) {}
+      OnePool() : PoolDesc(0), PoolSize(0), PoolAlignment(0) {}
 
-      OnePool(DSNode *N) : PoolDesc(0), PoolSize(getRecommendedSize(N)) {
+      OnePool(DSNode *N) : PoolDesc(0), PoolSize(getRecommendedSize(N)), 
+                           PoolAlignment(getRecommendedAlignment(N)) {
         NodesInPool.push_back(N);
       }
-      OnePool(DSNode *N, Value *PD) : PoolDesc(PD), PoolSize(0) {
+      OnePool(DSNode *N, Value *PD) : PoolDesc(PD), PoolSize(0),
+                                      PoolAlignment(0) {
         NodesInPool.push_back(N);
       }
     };
@@ -83,6 +86,11 @@
     ///
     static unsigned getRecommendedSize(DSNode *N);
 
+    /// getRecommendedAlignment - Return the recommended object alignment for
+    /// this DSNode.
+    ///
+    static unsigned getRecommendedAlignment(DSNode *N);
+    
     /// create - This static ctor creates the heuristic, based on the command
     /// line argument to choose the heuristic.
     static Heuristic *create();


Index: poolalloc/lib/PoolAllocate/PoolAllocate.cpp
diff -u poolalloc/lib/PoolAllocate/PoolAllocate.cpp:1.90 poolalloc/lib/PoolAllocate/PoolAllocate.cpp:1.91
--- poolalloc/lib/PoolAllocate/PoolAllocate.cpp:1.90	Tue Nov  9 14:25:39 2004
+++ poolalloc/lib/PoolAllocate/PoolAllocate.cpp	Wed Nov 10 15:13:47 2004
@@ -407,7 +407,7 @@
     Heuristic::OnePool &Pool = ResultPools[i];
     Value *PoolDesc = Pool.PoolDesc;
     if (PoolDesc == 0) {
-      PoolDesc = CreateGlobalPool(Pool.PoolSize, InsertPt);
+      PoolDesc = CreateGlobalPool(Pool.PoolSize, Pool.PoolAlignment, InsertPt);
 
       if (Pool.NodesInPool.size() == 1 &&
           !Pool.NodesInPool[0]->isNodeCompletelyFolded())
@@ -429,7 +429,7 @@
   return false;
 }
 
-GlobalVariable *PoolAllocate::CreateGlobalPool(unsigned RecSize,
+GlobalVariable *PoolAllocate::CreateGlobalPool(unsigned RecSize, unsigned Align,
                                                Instruction *IPHint) {
   GlobalVariable *GV =
     new GlobalVariable(PoolDescType, false, GlobalValue::InternalLinkage, 
@@ -447,10 +447,9 @@
     while (isa<AllocaInst>(InsertPt)) ++InsertPt;
   }
 
-  unsigned Alignment = 0;
   Value *ElSize = ConstantUInt::get(Type::UIntTy, RecSize);
-  Value *Align = ConstantUInt::get(Type::UIntTy, Alignment);
-  new CallInst(PoolInit, make_vector((Value*)GV, ElSize, Align, 0),
+  Value *AlignV = ConstantUInt::get(Type::UIntTy, Align);
+  new CallInst(PoolInit, make_vector((Value*)GV, ElSize, AlignV, 0),
                "", InsertPt);
   ++NumPools;
   return GV;
@@ -783,7 +782,8 @@
     // Insert the calls to initialize the pool.
     unsigned ElSizeV = Heuristic::getRecommendedSize(Node);
     Value *ElSize = ConstantUInt::get(Type::UIntTy, ElSizeV);
-    Value *Align  = ConstantUInt::get(Type::UIntTy, 0);
+    unsigned AlignV = Heuristic::getRecommendedAlignment(Node);
+    Value *Align  = ConstantUInt::get(Type::UIntTy, AlignV);
 
     for (unsigned i = 0, e = PoolInitPoints.size(); i != e; ++i) {
       new CallInst(PoolInit, make_vector((Value*)PD, ElSize, Align, 0),


Index: poolalloc/lib/PoolAllocate/PoolAllocate.h
diff -u poolalloc/lib/PoolAllocate/PoolAllocate.h:1.30 poolalloc/lib/PoolAllocate/PoolAllocate.h:1.31
--- poolalloc/lib/PoolAllocate/PoolAllocate.h:1.30	Tue Nov  9 14:25:39 2004
+++ poolalloc/lib/PoolAllocate/PoolAllocate.h	Wed Nov 10 15:13:47 2004
@@ -137,7 +137,8 @@
 
   /// CreateGlobalPool - Create a global pool descriptor, initialize it in main,
   /// and return a pointer to the global for it.
-  GlobalVariable *CreateGlobalPool(unsigned RecSize, Instruction *IPHint = 0);
+  GlobalVariable *CreateGlobalPool(unsigned RecSize, unsigned Alignment,
+                                   Instruction *IPHint = 0);
 
  private:
   





More information about the llvm-commits mailing list