torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Pruning.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /*
17 #ifdef HAVE_CONFIG_H
18 #include "torc/generic/config.h"
19 #endif //HAVE_CONFIG_H
20 
21 */
22 #include "torc/generic/Pruning.hpp"
23 
24 #include "torc/generic/Cell.hpp"
25 #include "torc/generic/Design.hpp"
27 #include "torc/generic/Library.hpp"
28 #include "torc/generic/View.hpp"
29 #include "torc/generic/Log.hpp"
30 
31 namespace torc {
32 namespace generic {
33 
34 using namespace torc::generic;
35 
36 void collectCells(ViewSharedPtr& inViewPtr, CellUsageMap& inoutCellUsageMap) {
37 
38  // iterate over all of the instances
39  typedef std::vector<InstanceSharedPtr> InstanceSharedPtrVector;
40  InstanceSharedPtrVector instances;
41  inViewPtr->getInstances(instances);
42  InstanceSharedPtrVector::iterator ip = instances.begin();
43  InstanceSharedPtrVector::iterator ie = instances.end();
44  while(ip < ie) {
45  // look up the instance
46  InstanceSharedPtr instancePtr = *ip++;
47  // look up the master view and its parent cell
48  ViewSharedPtr masterViewPtr = instancePtr->getMaster();
49  CellSharedPtr masterCellPtr = masterViewPtr->getParent();
50  // look up the master cell, and increment its usage count
51  inoutCellUsageMap[masterCellPtr]++;
52  // recurse
53  collectCells(masterViewPtr, inoutCellUsageMap);
54  }
55 
56 }
57 
58 void prune(RootSharedPtr& inRootPtr) {
59 
60  // declare a map of all cells actually used by this root
61  CellUsageMap cellUsageMap;
62 
63  // iterate over all of the libraries and collect all cells
64  typedef std::vector<LibrarySharedPtr> LibrarySharedPtrVector;
65  LibrarySharedPtrVector libraries;
66  inRootPtr->getLibraries(libraries);
67  LibrarySharedPtrVector::iterator lp = libraries.begin();
68  LibrarySharedPtrVector::iterator le = libraries.end();
69  while(lp < le) {
70  // look up the library name and specifics
71  LibrarySharedPtr libraryPtr = *lp++;
72  // iterate over all library cells
73  typedef std::vector<CellSharedPtr> CellSharedPtrVector;
74  CellSharedPtrVector cells;
75  libraryPtr->getCells(cells);
76  CellSharedPtrVector::iterator cp = cells.begin();
77  CellSharedPtrVector::iterator ce = cells.end();
78  while(cp < ce) {
79  // look up the cell
80  CellSharedPtr cellPtr = *cp++;
81  cellUsageMap[cellPtr] = 0;
82  }
83  }
84 
85  // iterate over all of the designs and collect cell usage counts
86  typedef std::vector<DesignSharedPtr> DesignSharedPtrVector;
87  DesignSharedPtrVector designs;
88  inRootPtr->getDesigns(designs);
89  DesignSharedPtrVector::iterator dp = designs.begin();
90  DesignSharedPtrVector::iterator de = designs.end();
91  while(dp < de) {
92  // look up the design name and specifics
93  DesignSharedPtr designPtr = *dp++;
94  std::string designName = designPtr->getName();
95  std::string cellName = designPtr->getCellRefName();
96  std::string libraryName = designPtr->getLibraryRefName();
97  // look up the design cell
98  LibrarySharedPtr libraryPtr = inRootPtr->findLibrary(libraryName);
99  CellSharedPtr cellPtr = libraryPtr->findCell(cellName);
100  log("Examining library %s, cell %s, design %s.\n", libraryName.c_str(), cellName.c_str(),
101  designName.c_str());
102  // ensure that this design cell is properly counted
103  cellUsageMap[cellPtr]++;
104  // iterate over all cell views
105  typedef std::vector<ViewSharedPtr> ViewSharedPtrVector;
106  ViewSharedPtrVector views;
107  cellPtr->getViews(views);
108  ViewSharedPtrVector::iterator vp = views.begin();
109  ViewSharedPtrVector::iterator ve = views.end();
110  while(vp < ve) {
111  // look up the view
112  ViewSharedPtr viewPtr = *vp++;
113  // collect all cells
114  collectCells(viewPtr, cellUsageMap);
115  }
116  }
117 
118  // display all cells that are in use
119  CellUsageMap::iterator cp = cellUsageMap.begin();
120  CellUsageMap::iterator ce = cellUsageMap.end();
121  while(cp != ce) {
122  // look up the cell information
123  CellUsageMap::value_type cellUsage = *cp++;
124  CellSharedPtr cellPtr = cellUsage.first;
125  LibrarySharedPtr libraryPtr = cellUsage.first->getParent();
126  std::string cellName = cellPtr->getName();
127  std::string libraryName = libraryPtr->getName();
128  log(" library %s, cell %s: %d\n", libraryName.c_str(), cellName.c_str(),
129  cellUsage.second);
130  if(cellUsage.second == 0) {
131  log(" removing unused cell %s from library %s: %s.\n", cellName.c_str(),
132  libraryName.c_str(), libraryPtr->findCell(cellName) ? "found" : "not found");
133  libraryPtr->removeCell(cellName);
134  }
135  }
136 
137  // iterate over all of the libraries and remove empty ones
138  for(LibrarySharedPtrVector::size_type i = 0; i < libraries.size(); i++) {
139  // look up the library name and specifics
140  LibrarySharedPtr libraryPtr = libraries[i];
141  std::string libraryName = libraryPtr->getName();
142  // if the library has no cells, remove it
143  typedef std::vector<CellSharedPtr> CellSharedPtrVector;
144  CellSharedPtrVector cells;
145  libraryPtr->getCells(cells);
146  if(cells.size() == 0) {
147  log(" removing unused library %s.\n", libraryName.c_str());
148  inRootPtr->removeLibrary(libraryName);
149  }
150  }
151 
152 }
153 
154 } //namespace generic
155 } //namespace torc
void log(const char *fmt,...)
Definition: Log.cpp:89
boost::shared_ptr< Instance > InstanceSharedPtr
std::vector< InstanceSharedPtr > InstanceSharedPtrVector
Vector of Instance shared pointers.
std::vector< DesignSharedPtr > DesignSharedPtrVector
Vector of Design shared pointers.
std::string string
boost::shared_ptr< Design > DesignSharedPtr
boost::shared_ptr< Library > LibrarySharedPtr
void prune(RootSharedPtr &inRootPtr)
Prune all unused cells and libraries from the given root.
Definition: Pruning.cpp:58
void collectCells(ViewSharedPtr &inViewPtr, CellUsageMap &inoutCellUsageMap)
Recursively collect and count all cells used in the given view. The CellUsageMap must already be ini...
Definition: Pruning.cpp:36
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Cell > CellSharedPtr
std::map< CellSharedPtr, int > CellUsageMap
A map from cell shared pointer to usage count.
Definition: Pruning.hpp:28
boost::shared_ptr< Root > RootSharedPtr