Index: ELF/CallGraphSort.cpp
===================================================================
--- ELF/CallGraphSort.cpp
+++ ELF/CallGraphSort.cpp
@@ -66,20 +66,6 @@
   std::vector<int> Sections;
   size_t Size = 0;
   uint64_t Weight = 0;
-};
-
-struct Section {
-  Section(const InputSectionBase *IS) : ISB(IS) { Size = ISB->getSize(); }
-
-  double getDensity() const {
-    if (Size == 0)
-      return 0;
-    return double(Weight) / double(Size);
-  }
-
-  size_t Size = 0;
-  uint64_t Weight = 0;
-  const InputSectionBase *ISB;
   std::vector<int> Preds;
   std::vector<int> Succs;
 };
@@ -119,7 +105,7 @@
   DenseMap<Edge, int, EdgeDenseMapInfo> EdgeMap;
   std::vector<Cluster> Clusters;
   std::vector<Edge> Edges;
-  std::vector<Section> Sections;
+  std::vector<const InputSectionBase *> Sections;
 
   void normalizeEdgeWeights();
   void generateClusters();
@@ -142,12 +128,14 @@
 CallGraphSort::CallGraphSort() {
   llvm::MapVector<std::pair<const Symbol *, const Symbol *>, uint64_t>
       &Profile = Config->CallGraphProfile;
-  DenseMap<const InputSectionBase *, int> SecToSec;
+  DenseMap<const InputSectionBase *, int> SecToCluster;
 
   auto GetOrCreateNode = [&](const InputSectionBase *IS) -> int {
-    auto Res = SecToSec.insert(std::make_pair(IS, Sections.size()));
-    if (Res.second)
-      Sections.emplace_back(IS);
+    auto Res = SecToCluster.insert(std::make_pair(IS, Clusters.size()));
+    if (Res.second) {
+      Sections.push_back(IS);
+      Clusters.emplace_back(Clusters.size(), IS);
+    }
     return Res.first->second;
   };
 
@@ -185,7 +173,7 @@
     int From = GetOrCreateNode(FromSB);
     int To = GetOrCreateNode(ToSB);
 
-    Sections[To].Weight += Weight;
+    Clusters[To].Weight += Weight;
 
     if (From == To)
       continue;
@@ -197,8 +185,8 @@
     int EI = Res.first->second;
     if (Res.second) {
       Edges.push_back(E);
-      Sections[From].Succs.push_back(To);
-      Sections[To].Preds.push_back(From);
+      Clusters[From].Succs.push_back(To);
+      Clusters[To].Preds.push_back(From);
     } else
       Edges[EI].Weight += Weight;
   }
@@ -208,8 +196,8 @@
 // Normalize the edge weights so that we can reject edges which have a low
 // probibility.
 void CallGraphSort::normalizeEdgeWeights() {
-  for (int SI = 0, SE = Sections.size(); SI != SE; ++SI) {
-    Section &S = Sections[SI];
+  for (int SI = 0, SE = Clusters.size(); SI != SE; ++SI) {
+    Cluster &S = Clusters[SI];
     for (int PI : S.Preds) {
       Edge &E = Edges[EdgeMap[{PI, SI, 0, 0}]];
       if (S.Weight == 0)
@@ -240,22 +228,16 @@
 // Group InputSections into clusters using the Call-Chain Clustering heuristic
 // then sort the clusters by density.
 void CallGraphSort::generateClusters() {
-  std::vector<int> SortedSecs;
-  std::vector<Cluster *> SecToCluster(Sections.size());
-
-  Clusters.reserve(Sections.size());
-
-  for (int SI = 0, SE = Sections.size(); SI != SE; ++SI) {
-    Cluster C(SI, Sections[SI].ISB);
-    C.Size = Sections[SI].Size;
-    C.Weight = Sections[SI].Weight;
-    Clusters.push_back(C);
-    SortedSecs.push_back(SI);
-    SecToCluster[SI] = &Clusters.back();
+  std::vector<int> SortedSecs(Clusters.size());
+  std::vector<Cluster *> SecToCluster(Clusters.size());
+
+  for (int SI = 0, SE = Clusters.size(); SI != SE; ++SI) {
+    SortedSecs[SI] = SI;
+    SecToCluster[SI] = &Clusters[SI];
   }
 
   std::stable_sort(SortedSecs.begin(), SortedSecs.end(), [&](int A, int B) {
-    return Sections[B].getDensity() < Sections[A].getDensity();
+    return Clusters[B].getDensity() < Clusters[A].getDensity();
   });
 
   for (int SI : SortedSecs) {
@@ -264,7 +246,7 @@
     int BestPred = -1;
     double BestWeight = 0;
 
-    for (int PI : Sections[SI].Preds) {
+    for (int PI : Clusters[SI].Preds) {
       Edge &E = Edges[EdgeMap[{PI, SI, 0, 0}]];
       if (BestPred == -1 || E.NormalizedWeight > BestWeight) {
         BestPred = PI;
@@ -316,7 +298,7 @@
 
   for (const Cluster &C : Clusters)
     for (int SecIndex : C.Sections)
-      OrderMap[Sections[SecIndex].ISB] = CurOrder++;
+      OrderMap[Sections[SecIndex]] = CurOrder++;
 
   return OrderMap;
 }