llvm.org GIT mirror llvm / dd5d86d
Remove the very substantial, largely unmaintained legacy PGO infrastructure. This was essentially work toward PGO based on a design that had several flaws, partially dating from a time when LLVM had a different architecture, and with an effort to modernize it abandoned without being completed. Since then, it has bitrotted for several years further. The result is nearly unusable, and isn't helping any of the modern PGO efforts. Instead, it is getting in the way, adding confusion about PGO in LLVM and distracting everyone with maintenance on essentially dead code. Removing it paves the way for modern efforts around PGO. Among other effects, this removes the last of the runtime libraries from LLVM. Those are being developed in the separate 'compiler-rt' project now, with somewhat different licensing specifically more approriate for runtimes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191835 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 5 years ago
61 changed file(s) with 12 addition(s) and 8640 deletion(s). Raw diff Collapse all Expand all
245245 endif()
246246
247247 # Define options to control the inclusion and default build behavior for
248 # components which may not strictly be necessary (tools, runtime, examples, and
249 # tests).
248 # components which may not strictly be necessary (tools, examples, and tests).
250249 #
251250 # This is primarily to support building smaller or faster project files.
252251 option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
253252 option(LLVM_BUILD_TOOLS
254253 "Build the LLVM tools. If OFF, just generate build targets." ON)
255
256 option(LLVM_INCLUDE_RUNTIME "Generate build targets for the LLVM runtimes" ON)
257 option(LLVM_BUILD_RUNTIME
258 "Build the LLVM runtime libraries. If OFF, just generate build targets." ON)
259254
260255 option(LLVM_BUILD_EXAMPLES
261256 "Build the LLVM example programs. If OFF, just generate build targets." OFF)
470465 add_subdirectory(tools)
471466 endif()
472467
473 if( LLVM_INCLUDE_RUNTIME )
474 add_subdirectory(runtime)
475 endif()
476
477468 if( LLVM_INCLUDE_EXAMPLES )
478469 add_subdirectory(examples)
479470 endif()
1515 ;===------------------------------------------------------------------------===;
1616
1717 [common]
18 subdirectories = bindings docs examples lib projects runtime tools utils
18 subdirectories = bindings docs examples lib projects tools utils
1919
2020 [component_0]
2121 type = Group
1414 # 3. Build IR, which builds the Intrinsics.inc file used by libs.
1515 # 4. Build libs, which are needed by llvm-config.
1616 # 5. Build llvm-config, which determines inter-lib dependencies for tools.
17 # 6. Build tools, runtime, docs.
17 # 6. Build tools and docs.
1818 #
1919 # When cross-compiling, there are some things (tablegen) that need to
2020 # be build for the build system first.
3030 OPTIONAL_DIRS := tools/clang/utils/TableGen
3131 else
3232 DIRS := lib/Support lib/TableGen utils lib/IR lib tools/llvm-shlib \
33 tools/llvm-config tools runtime docs unittests
33 tools/llvm-config tools docs unittests
3434 OPTIONAL_DIRS := projects bindings
3535 endif
3636
5151 endif
5252
5353 ifeq ($(MAKECMDGOALS),libs-only)
54 DIRS := $(filter-out tools runtime docs, $(DIRS))
54 DIRS := $(filter-out tools docs, $(DIRS))
5555 OPTIONAL_DIRS :=
5656 endif
5757
5858 ifeq ($(MAKECMDGOALS),install-libs)
59 DIRS := $(filter-out tools runtime docs, $(DIRS))
59 DIRS := $(filter-out tools docs, $(DIRS))
6060 OPTIONAL_DIRS := $(filter bindings, $(OPTIONAL_DIRS))
6161 endif
6262
6363 ifeq ($(MAKECMDGOALS),tools-only)
64 DIRS := $(filter-out runtime docs, $(DIRS))
64 DIRS := $(filter-out docs, $(DIRS))
6565 OPTIONAL_DIRS :=
6666 endif
6767
7171 tools/clang/tools/c-index-test \
7272 tools/clang/include/clang-c \
7373 tools/clang/runtime tools/clang/docs \
74 tools/lto runtime
74 tools/lto
7575 OPTIONAL_DIRS :=
7676 NO_INSTALL = 1
7777 endif
8383 endif
8484
8585 ifeq ($(MAKECMDGOALS),unittests)
86 DIRS := $(filter-out tools runtime docs, $(DIRS)) utils unittests
86 DIRS := $(filter-out tools docs, $(DIRS)) utils unittests
8787 OPTIONAL_DIRS :=
8888 endif
8989
9494
9595 //===--------------------------------------------------------------------===//
9696 //
97 // createProfileLoaderPass - This pass loads information from a profile dump
98 // file.
99 //
100 ModulePass *createProfileLoaderPass();
101 extern char &ProfileLoaderPassID;
102
103 //===--------------------------------------------------------------------===//
104 //
105 // createProfileMetadataLoaderPass - This pass loads information from a
106 // profile dump file and sets branch weight metadata.
107 //
108 ModulePass *createProfileMetadataLoaderPass();
109 extern char &ProfileMetadataLoaderPassID;
110
111 //===--------------------------------------------------------------------===//
112 //
113 // createNoProfileInfoPass - This pass implements the default "no profile".
114 //
115 ImmutablePass *createNoProfileInfoPass();
116
117 //===--------------------------------------------------------------------===//
118 //
119 // createProfileEstimatorPass - This pass estimates profiling information
120 // instead of loading it from a previous run.
121 //
122 FunctionPass *createProfileEstimatorPass();
123 extern char &ProfileEstimatorPassID;
124
125 //===--------------------------------------------------------------------===//
126 //
127 // createProfileVerifierPass - This pass verifies profiling information.
128 //
129 FunctionPass *createProfileVerifierPass();
130
131 //===--------------------------------------------------------------------===//
132 //
133 // createPathProfileLoaderPass - This pass loads information from a path
134 // profile dump file.
135 //
136 ModulePass *createPathProfileLoaderPass();
137 extern char &PathProfileLoaderPassID;
138
139 //===--------------------------------------------------------------------===//
140 //
141 // createNoPathProfileInfoPass - This pass implements the default
142 // "no path profile".
143 //
144 ImmutablePass *createNoPathProfileInfoPass();
145
146 //===--------------------------------------------------------------------===//
147 //
148 // createPathProfileVerifierPass - This pass verifies path profiling
149 // information.
150 //
151 ModulePass *createPathProfileVerifierPass();
152
153 //===--------------------------------------------------------------------===//
154 //
15597 // createDSAAPass - This pass implements simple context sensitive alias
15698 // analysis.
15799 //
+0
-304
include/llvm/Analysis/PathNumbering.h less more
None //===- PathNumbering.h ----------------------------------------*- C++ -*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Ball-Larus path numbers uniquely identify paths through a directed acyclic
10 // graph (DAG) [Ball96]. For a CFG backedges are removed and replaced by phony
11 // edges to obtain a DAG, and thus the unique path numbers [Ball96].
12 //
13 // The purpose of this analysis is to enumerate the edges in a CFG in order
14 // to obtain paths from path numbers in a convenient manner. As described in
15 // [Ball96] edges can be enumerated such that given a path number by following
16 // the CFG and updating the path number, the path is obtained.
17 //
18 // [Ball96]
19 // T. Ball and J. R. Larus. "Efficient Path Profiling."
20 // International Symposium on Microarchitecture, pages 46-57, 1996.
21 // http://portal.acm.org/citation.cfm?id=243857
22 //
23 //===----------------------------------------------------------------------===//
24
25 #ifndef LLVM_ANALYSIS_PATHNUMBERING_H
26 #define LLVM_ANALYSIS_PATHNUMBERING_H
27
28 #include "llvm/Analysis/ProfileInfoTypes.h"
29 #include "llvm/IR/BasicBlock.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CFG.h"
33 #include
34 #include
35 #include
36
37 namespace llvm {
38 class BallLarusNode;
39 class BallLarusEdge;
40 class BallLarusDag;
41
42 // typedefs for storage/ interators of various DAG components
43 typedef std::vector BLNodeVector;
44 typedef std::vector::iterator BLNodeIterator;
45 typedef std::vector BLEdgeVector;
46 typedef std::vector::iterator BLEdgeIterator;
47 typedef std::map BLBlockNodeMap;
48 typedef std::stack BLNodeStack;
49
50 // Represents a basic block with information necessary for the BallLarus
51 // algorithms.
52 class BallLarusNode {
53 public:
54 enum NodeColor { WHITE, GRAY, BLACK };
55
56 // Constructor: Initializes a new Node for the given BasicBlock
57 BallLarusNode(BasicBlock* BB) :
58 _basicBlock(BB), _numberPaths(0), _color(WHITE) {
59 static unsigned nextUID = 0;
60 _uid = nextUID++;
61 }
62
63 // Returns the basic block for the BallLarusNode
64 BasicBlock* getBlock();
65
66 // Get/set the number of paths to the exit starting at the node.
67 unsigned getNumberPaths();
68 void setNumberPaths(unsigned numberPaths);
69
70 // Get/set the NodeColor used in graph algorithms.
71 NodeColor getColor();
72 void setColor(NodeColor color);
73
74 // Iterator information for predecessor edges. Includes phony and
75 // backedges.
76 BLEdgeIterator predBegin();
77 BLEdgeIterator predEnd();
78 unsigned getNumberPredEdges();
79
80 // Iterator information for successor edges. Includes phony and
81 // backedges.
82 BLEdgeIterator succBegin();
83 BLEdgeIterator succEnd();
84 unsigned getNumberSuccEdges();
85
86 // Add an edge to the predecessor list.
87 void addPredEdge(BallLarusEdge* edge);
88
89 // Remove an edge from the predecessor list.
90 void removePredEdge(BallLarusEdge* edge);
91
92 // Add an edge to the successor list.
93 void addSuccEdge(BallLarusEdge* edge);
94
95 // Remove an edge from the successor list.
96 void removeSuccEdge(BallLarusEdge* edge);
97
98 // Returns the name of the BasicBlock being represented. If BasicBlock
99 // is null then returns "". If BasicBlock has no name, then
100 // "" is returned. Intended for use with debug output.
101 std::string getName();
102
103 private:
104 // The corresponding underlying BB.
105 BasicBlock* _basicBlock;
106
107 // Holds the predecessor edges of this node.
108 BLEdgeVector _predEdges;
109
110 // Holds the successor edges of this node.
111 BLEdgeVector _succEdges;
112
113 // The number of paths from the node to the exit.
114 unsigned _numberPaths;
115
116 // 'Color' used by graph algorithms to mark the node.
117 NodeColor _color;
118
119 // Unique ID to ensure naming difference with dotgraphs
120 unsigned _uid;
121
122 // Removes an edge from an edgeVector. Used by removePredEdge and
123 // removeSuccEdge.
124 void removeEdge(BLEdgeVector& v, BallLarusEdge* e);
125 };
126
127 // Represents an edge in the Dag. For an edge, v -> w, v is the source, and
128 // w is the target.
129 class BallLarusEdge {
130 public:
131 enum EdgeType { NORMAL, BACKEDGE, SPLITEDGE,
132 BACKEDGE_PHONY, SPLITEDGE_PHONY, CALLEDGE_PHONY };
133
134 // Constructor: Initializes an BallLarusEdge with a source and target.
135 BallLarusEdge(BallLarusNode* source, BallLarusNode* target,
136 unsigned duplicateNumber)
137 : _source(source), _target(target), _weight(0), _edgeType(NORMAL),
138 _realEdge(NULL), _duplicateNumber(duplicateNumber) {}
139
140 // Returns the source/ target node of this edge.
141 BallLarusNode* getSource() const;
142 BallLarusNode* getTarget() const;
143
144 // Sets the type of the edge.
145 EdgeType getType() const;
146
147 // Gets the type of the edge.
148 void setType(EdgeType type);
149
150 // Returns the weight of this edge. Used to decode path numbers to
151 // sequences of basic blocks.
152 unsigned getWeight();
153
154 // Sets the weight of the edge. Used during path numbering.
155 void setWeight(unsigned weight);
156
157 // Gets/sets the phony edge originating at the root.
158 BallLarusEdge* getPhonyRoot();
159 void setPhonyRoot(BallLarusEdge* phonyRoot);
160
161 // Gets/sets the phony edge terminating at the exit.
162 BallLarusEdge* getPhonyExit();
163 void setPhonyExit(BallLarusEdge* phonyExit);
164
165 // Gets/sets the associated real edge if this is a phony edge.
166 BallLarusEdge* getRealEdge();
167 void setRealEdge(BallLarusEdge* realEdge);
168
169 // Returns the duplicate number of the edge.
170 unsigned getDuplicateNumber();
171
172 protected:
173 // Source node for this edge.
174 BallLarusNode* _source;
175
176 // Target node for this edge.
177 BallLarusNode* _target;
178
179 private:
180 // Edge weight cooresponding to path number increments before removing
181 // increments along a spanning tree. The sum over the edge weights gives
182 // the path number.
183 unsigned _weight;
184
185 // Type to represent for what this edge is intended
186 EdgeType _edgeType;
187
188 // For backedges and split-edges, the phony edge which is linked to the
189 // root node of the DAG. This contains a path number initialization.
190 BallLarusEdge* _phonyRoot;
191
192 // For backedges and split-edges, the phony edge which is linked to the
193 // exit node of the DAG. This contains a path counter increment, and
194 // potentially a path number increment.
195 BallLarusEdge* _phonyExit;
196
197 // If this is a phony edge, _realEdge is a link to the back or split
198 // edge. Otherwise, this is null.
199 BallLarusEdge* _realEdge;
200
201 // An ID to differentiate between those edges which have the same source
202 // and destination blocks.
203 unsigned _duplicateNumber;
204 };
205
206 // Represents the Ball Larus DAG for a given Function. Can calculate
207 // various properties required for instrumentation or analysis. E.g. the
208 // edge weights that determine the path number.
209 class BallLarusDag {
210 public:
211 // Initializes a BallLarusDag from the CFG of a given function. Must
212 // call init() after creation, since some initialization requires
213 // virtual functions.
214 BallLarusDag(Function &F)
215 : _root(NULL), _exit(NULL), _function(F) {}
216
217 // Initialization that requires virtual functions which are not fully
218 // functional in the constructor.
219 void init();
220
221 // Frees all memory associated with the DAG.
222 virtual ~BallLarusDag();
223
224 // Calculate the path numbers by assigning edge increments as prescribed
225 // in Ball-Larus path profiling.
226 void calculatePathNumbers();
227
228 // Returns the number of paths for the DAG.
229 unsigned getNumberOfPaths();
230
231 // Returns the root (i.e. entry) node for the DAG.
232 BallLarusNode* getRoot();
233
234 // Returns the exit node for the DAG.
235 BallLarusNode* getExit();
236
237 // Returns the function for the DAG.
238 Function& getFunction();
239
240 // Clears the node colors.
241 void clearColors(BallLarusNode::NodeColor color);
242
243 protected:
244 // All nodes in the DAG.
245 BLNodeVector _nodes;
246
247 // All edges in the DAG.
248 BLEdgeVector _edges;
249
250 // All backedges in the DAG.
251 BLEdgeVector _backEdges;
252
253 // Allows subclasses to determine which type of Node is created.
254 // Override this method to produce subclasses of BallLarusNode if
255 // necessary. The destructor of BallLarusDag will call free on each pointer
256 // created.
257 virtual BallLarusNode* createNode(BasicBlock* BB);
258
259 // Allows subclasses to determine which type of Edge is created.
260 // Override this method to produce subclasses of BallLarusEdge if
261 // necessary. Parameters source and target will have been created by
262 // createNode and can be cast to the subclass of BallLarusNode*
263 // returned by createNode. The destructor of BallLarusDag will call free
264 // on each pointer created.
265 virtual BallLarusEdge* createEdge(BallLarusNode* source, BallLarusNode*
266 target, unsigned duplicateNumber);
267
268 // Proxy to node's constructor. Updates the DAG state.
269 BallLarusNode* addNode(BasicBlock* BB);
270
271 // Proxy to edge's constructor. Updates the DAG state.
272 BallLarusEdge* addEdge(BallLarusNode* source, BallLarusNode* target,
273 unsigned duplicateNumber);
274
275 private:
276 // The root (i.e. entry) node for this DAG.
277 BallLarusNode* _root;
278
279 // The exit node for this DAG.
280 BallLarusNode* _exit;
281
282 // The function represented by this DAG.
283 Function& _function;
284
285 // Processes one node and its imediate edges for building the DAG.
286 void buildNode(BLBlockNodeMap& inDag, std::stack& dfsStack);
287
288 // Process an edge in the CFG for DAG building.
289 void buildEdge(BLBlockNodeMap& inDag, std::stack& dfsStack,
290 BallLarusNode* currentNode, BasicBlock* succBB,
291 unsigned duplicateNumber);
292
293 // The weight on each edge is the increment required along any path that
294 // contains that edge.
295 void calculatePathNumbersFrom(BallLarusNode* node);
296
297 // Adds a backedge with its phony edges. Updates the DAG state.
298 void addBackedge(BallLarusNode* source, BallLarusNode* target,
299 unsigned duplicateCount);
300 };
301 } // end namespace llvm
302
303 #endif
+0
-112
include/llvm/Analysis/PathProfileInfo.h less more
None //===- PathProfileInfo.h --------------------------------------*- C++ -*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file outlines the interface used by optimizers to load path profiles.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_PATHPROFILEINFO_H
14 #define LLVM_ANALYSIS_PATHPROFILEINFO_H
15
16 #include "llvm/Analysis/PathNumbering.h"
17 #include "llvm/IR/BasicBlock.h"
18
19 namespace llvm {
20
21 class ProfilePath;
22 class ProfilePathEdge;
23 class PathProfileInfo;
24
25 typedef std::vector ProfilePathEdgeVector;
26 typedef std::vector::iterator ProfilePathEdgeIterator;
27
28 typedef std::vector ProfilePathBlockVector;
29 typedef std::vector::iterator ProfilePathBlockIterator;
30
31 typedef std::map ProfilePathMap;
32 typedef std::map::iterator ProfilePathIterator;
33
34 typedef std::map FunctionPathCountMap;
35 typedef std::map FunctionPathMap;
36 typedef std::map::iterator FunctionPathIterator;
37
38 class ProfilePathEdge {
39 public:
40 ProfilePathEdge(BasicBlock* source, BasicBlock* target,
41 unsigned duplicateNumber);
42
43 inline unsigned getDuplicateNumber() { return _duplicateNumber; }
44 inline BasicBlock* getSource() { return _source; }
45 inline BasicBlock* getTarget() { return _target; }
46
47 protected:
48 BasicBlock* _source;
49 BasicBlock* _target;
50 unsigned _duplicateNumber;
51 };
52
53 class ProfilePath {
54 public:
55 ProfilePath(unsigned int number, unsigned int count,
56 double countStdDev, PathProfileInfo* ppi);
57
58 double getFrequency() const;
59
60 inline unsigned int getNumber() const { return _number; }
61 inline unsigned int getCount() const { return _count; }
62 inline double getCountStdDev() const { return _countStdDev; }
63
64 ProfilePathEdgeVector* getPathEdges() const;
65 ProfilePathBlockVector* getPathBlocks() const;
66
67 BasicBlock* getFirstBlockInPath() const;
68
69 private:
70 unsigned int _number;
71 unsigned int _count;
72 double _countStdDev;
73
74 // double pointer back to the profiling info
75 PathProfileInfo* _ppi;
76 };
77
78 // TODO: overload [] operator for getting path
79 // Add: getFunctionCallCount()
80 class PathProfileInfo {
81 public:
82 PathProfileInfo();
83 ~PathProfileInfo();
84
85 void setCurrentFunction(Function* F);
86 Function* getCurrentFunction() const;
87 BasicBlock* getCurrentFunctionEntry();
88
89 ProfilePath* getPath(unsigned int number);
90 unsigned int getPotentialPathCount();
91
92 ProfilePathIterator pathBegin();
93 ProfilePathIterator pathEnd();
94 unsigned int pathsRun();
95
96 static char ID; // Pass identification
97 std::string argList;
98
99 protected:
100 FunctionPathMap _functionPaths;
101 FunctionPathCountMap _functionPathCounts;
102
103 private:
104 BallLarusDag* _currentDag;
105 Function* _currentFunction;
106
107 friend class ProfilePath;
108 };
109 } // end namespace llvm
110
111 #endif
+0
-140
include/llvm/Analysis/ProfileDataLoader.h less more
None //===- ProfileDataLoader.h - Load & convert profile info ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The ProfileDataLoader class is used to load profiling data from a dump file.
10 // The ProfileDataT class is used to store the mapping of this
11 // data to control flow edges.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H
16 #define LLVM_ANALYSIS_PROFILEDATALOADER_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include
24
25 namespace llvm {
26
27 class ModulePass;
28 class Function;
29 class BasicBlock;
30
31 // Helper for dumping edges to dbgs().
32 raw_ostream& operator<<(raw_ostream &O, std::pair
33 const BasicBlock *> E);
34
35 /// \brief The ProfileDataT class is used to store the mapping of
36 /// profiling data to control flow edges.
37 ///
38 /// An edge is defined by its source and sink basic blocks.
39 template
40 class ProfileDataT {
41 public:
42 // The profiling information defines an Edge by its source and sink basic
43 // blocks.
44 typedef std::pair Edge;
45
46 private:
47 typedef DenseMap EdgeWeights;
48
49 /// \brief Count the number of times a transition between two blocks is
50 /// executed.
51 ///
52 /// As a special case, we also hold an edge from the null BasicBlock to the
53 /// entry block to indicate how many times the function was entered.
54 DenseMap EdgeInformation;
55
56 public:
57 /// getFunction() - Returns the Function for an Edge.
58 static const FType *getFunction(Edge e) {
59 // e.first may be NULL
60 assert(((!e.first) || (e.first->getParent() == e.second->getParent()))
61 && "A ProfileData::Edge can not be between two functions");
62 assert(e.second && "A ProfileData::Edge must have a real sink");
63 return e.second->getParent();
64 }
65
66 /// getEdge() - Creates an Edge between two BasicBlocks.
67 static Edge getEdge(const BType *Src, const BType *Dest) {
68 return Edge(Src, Dest);
69 }
70
71 /// getEdgeWeight - Return the number of times that a given edge was
72 /// executed.
73 unsigned getEdgeWeight(Edge e) const {
74 const FType *f = getFunction(e);
75 assert((EdgeInformation.find(f) != EdgeInformation.end())
76 && "No profiling information for function");
77 EdgeWeights weights = EdgeInformation.find(f)->second;
78
79 assert((weights.find(e) != weights.end())
80 && "No profiling information for edge");
81 return weights.find(e)->second;
82 }
83
84 /// addEdgeWeight - Add 'weight' to the already stored execution count for
85 /// this edge.
86 void addEdgeWeight(Edge e, unsigned weight) {
87 EdgeInformation[getFunction(e)][e] += weight;
88 }
89 };
90
91 typedef ProfileDataT ProfileData;
92 //typedef ProfileDataT MachineProfileData;
93
94 /// The ProfileDataLoader class is used to load raw profiling data from the
95 /// dump file.
96 class ProfileDataLoader {
97 private:
98 /// The name of the file where the raw profiling data is stored.
99 const std::string &Filename;
100
101 /// A vector of the command line arguments used when the target program was
102 /// run to generate profiling data. One entry per program run.
103 SmallVector CommandLines;
104
105 /// The raw values for how many times each edge was traversed, values from
106 /// multiple program runs are accumulated.
107 SmallVector EdgeCounts;
108
109 public:
110 /// ProfileDataLoader ctor - Read the specified profiling data file, exiting
111 /// the program if the file is invalid or broken.
112 ProfileDataLoader(const char *ToolName, const std::string &Filename);
113
114 /// A special value used to represent the weight of an edge which has not
115 /// been counted yet.
116 static const unsigned Uncounted;
117
118 /// getNumExecutions - Return the number of times the target program was run
119 /// to generate this profiling data.
120 unsigned getNumExecutions() const { return CommandLines.size(); }
121
122 /// getExecution - Return the command line parameters used to generate the
123 /// i'th set of profiling data.
124 const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
125
126 const std::string &getFileName() const { return Filename; }
127
128 /// getRawEdgeCounts - Return the raw profiling data, this is just a list of
129 /// numbers with no mappings to edges.
130 ArrayRef getRawEdgeCounts() const { return EdgeCounts; }
131 };
132
133 /// createProfileMetadataLoaderPass - This function returns a Pass that loads
134 /// the profiling information for the module from the specified filename.
135 ModulePass *createProfileMetadataLoaderPass(const std::string &Filename);
136
137 } // End llvm namespace
138
139 #endif
+0
-39
include/llvm/Analysis/ProfileDataTypes.h less more
None /*===-- ProfileDataTypes.h - Profiling info shared constants --------------===*\
1 |*
2 |* The LLVM Compiler Infrastructure
3 |*
4 |* This file is distributed under the University of Illinois Open Source
5 |* License. See LICENSE.TXT for details.
6 |*
7 |*===----------------------------------------------------------------------===*|
8 |*
9 |* This file defines constants shared by the various different profiling
10 |* runtime libraries and the LLVM C++ profile metadata loader. It must be a
11 |* C header because, at present, the profiling runtimes are written in C.
12 |*
13 \*===----------------------------------------------------------------------===*/
14
15 #ifndef LLVM_ANALYSIS_PROFILEDATATYPES_H
16 #define LLVM_ANALYSIS_PROFILEDATATYPES_H
17
18 /* Included by libprofile. */
19 #if defined(__cplusplus)
20 extern "C" {
21 #endif
22
23 /* TODO: Strip out unused entries once ProfileInfo etc has been removed. */
24 enum ProfilingType {
25 ArgumentInfo = 1, /* The command line argument block */
26 FunctionInfo = 2, /* Function profiling information */
27 BlockInfo = 3, /* Block profiling information */
28 EdgeInfo = 4, /* Edge profiling information */
29 PathInfo = 5, /* Path profiling information */
30 BBTraceInfo = 6, /* Basic block trace information */
31 OptEdgeInfo = 7 /* Edge profiling information, optimal version */
32 };
33
34 #if defined(__cplusplus)
35 }
36 #endif
37
38 #endif /* LLVM_ANALYSIS_PROFILEDATATYPES_H */
+0
-247
include/llvm/Analysis/ProfileInfo.h less more
None //===- llvm/Analysis/ProfileInfo.h - Profile Info Interface -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the generic ProfileInfo interface, which is used as the
10 // common interface used by all clients of profiling information, and
11 // implemented either by making static guestimations, or by actually reading in
12 // profiling information gathered by running the program.
13 //
14 // Note that to be useful, all profile-based optimizations should preserve
15 // ProfileInfo, which requires that they notify it when changes to the CFG are
16 // made. (This is not implemented yet.)
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_ANALYSIS_PROFILEINFO_H
21 #define LLVM_ANALYSIS_PROFILEINFO_H
22
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include
28 #include
29 #include
30 #include
31
32 namespace llvm {
33 class Pass;
34 class raw_ostream;
35
36 class BasicBlock;
37 class Function;
38 class MachineBasicBlock;
39 class MachineFunction;
40
41 // Helper for dumping edges to dbgs().
42 raw_ostream& operator<<(raw_ostream &O, std::pair E);
43 raw_ostream& operator<<(raw_ostream &O, std::pair E);
44
45 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
46 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB);
47
48 raw_ostream& operator<<(raw_ostream &O, const Function *F);
49 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF);
50
51 /// ProfileInfo Class - This class holds and maintains profiling
52 /// information for some unit of code.
53 template
54 class ProfileInfoT {
55 public:
56 // Types for handling profiling information.
57 typedef std::pair Edge;
58 typedef std::pair EdgeWeight;
59 typedef std::map EdgeWeights;
60 typedef std::map BlockCounts;
61 typedef std::map Path;
62
63 protected:
64 // EdgeInformation - Count the number of times a transition between two
65 // blocks is executed. As a special case, we also hold an edge from the
66 // null BasicBlock to the entry block to indicate how many times the
67 // function was entered.
68 std::map EdgeInformation;
69
70 // BlockInformation - Count the number of times a block is executed.
71 std::map BlockInformation;
72
73 // FunctionInformation - Count the number of times a function is executed.
74 std::map FunctionInformation;
75
76 ProfileInfoT *MachineProfile;
77 public:
78 static char ID; // Class identification, replacement for typeinfo
79 ProfileInfoT();
80 ~ProfileInfoT(); // We want to be subclassed
81
82 // MissingValue - The value that is returned for execution counts in case
83 // no value is available.
84 static const double MissingValue;
85
86 // getFunction() - Returns the Function for an Edge, checking for validity.
87 static const FType* getFunction(Edge e) {
88 if (e.first)
89 return e.first->getParent();
90 if (e.second)
91 return e.second->getParent();
92 llvm_unreachable("Invalid ProfileInfo::Edge");
93 }
94
95 // getEdge() - Creates an Edge from two BasicBlocks.
96 static Edge getEdge(const BType *Src, const BType *Dest) {
97 return std::make_pair(Src, Dest);
98 }
99
100 //===------------------------------------------------------------------===//
101 /// Profile Information Queries
102 ///
103 double getExecutionCount(const FType *F);
104
105 double getExecutionCount(const BType *BB);
106
107 void setExecutionCount(const BType *BB, double w);
108
109 void addExecutionCount(const BType *BB, double w);
110
111 double getEdgeWeight(Edge e) const {
112 typename std::map::const_iterator J =
113 EdgeInformation.find(getFunction(e));
114 if (J == EdgeInformation.end()) return MissingValue;
115
116 typename EdgeWeights::const_iterator I = J->second.find(e);
117 if (I == J->second.end()) return MissingValue;
118
119 return I->second;
120 }
121
122 void setEdgeWeight(Edge e, double w) {
123 DEBUG_WITH_TYPE("profile-info",
124 dbgs() << "Creating Edge " << e
125 << " (weight: " << format("%.20g",w) << ")\n");
126 EdgeInformation[getFunction(e)][e] = w;
127 }
128
129 void addEdgeWeight(Edge e, double w);
130
131 EdgeWeights &getEdgeWeights (const FType *F) {
132 return EdgeInformation[F];
133 }
134
135 //===------------------------------------------------------------------===//
136 /// Analysis Update Methods
137 ///
138 void removeBlock(const BType *BB);
139
140 void removeEdge(Edge e);
141
142 void replaceEdge(const Edge &, const Edge &);
143
144 enum GetPathMode {
145 GetPathToExit = 1,
146 GetPathToValue = 2,
147 GetPathToDest = 4,
148 GetPathWithNewEdges = 8
149 };
150
151 const BType *GetPath(const BType *Src, const BType *Dest,
152 Path &P, unsigned Mode);
153
154 void divertFlow(const Edge &, const Edge &);
155
156 void splitEdge(const BType *FirstBB, const BType *SecondBB,
157 const BType *NewBB, bool MergeIdenticalEdges = false);
158
159 void splitBlock(const BType *Old, const BType* New);
160
161 void splitBlock(const BType *BB, const BType* NewBB,
162 BType *const *Preds, unsigned NumPreds);
163
164 void replaceAllUses(const BType *RmBB, const BType *DestBB);
165
166 void transfer(const FType *Old, const FType *New);
167
168 void repair(const FType *F);
169
170 void dump(FType *F = 0, bool real = true) {
171 dbgs() << "**** This is ProfileInfo " << this << " speaking:\n";
172 if (!real) {
173 typename std::set Functions;
174
175 dbgs() << "Functions: \n";
176 if (F) {
177 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
178 Functions.insert(F);
179 } else {
180 for (typename std::map::iterator fi = FunctionInformation.begin(),
181 fe = FunctionInformation.end(); fi != fe; ++fi) {
182 dbgs() << fi->first << "@" << format("%p",fi->first) << ": " << format("%.20g",fi->second) << "\n";
183 Functions.insert(fi->first);
184 }
185 }
186
187 for (typename std::set::iterator FI = Functions.begin(), FE = Functions.end();
188 FI != FE; ++FI) {
189 const FType *F = *FI;
190 typename std::map::iterator bwi = BlockInformation.find(F);
191 dbgs() << "BasicBlocks for Function " << F << ":\n";
192 for (typename BlockCounts::const_iterator bi = bwi->second.begin(), be = bwi->second.end(); bi != be; ++bi) {
193 dbgs() << bi->first << "@" << format("%p", bi->first) << ": " << format("%.20g",bi->second) << "\n";
194 }
195 }
196
197 for (typename std::set::iterator FI = Functions.begin(), FE = Functions.end();
198 FI != FE; ++FI) {
199 typename std::map::iterator ei = EdgeInformation.find(*FI);
200 dbgs() << "Edges for Function " << ei->first << ":\n";
201 for (typename EdgeWeights::iterator ewi = ei->second.begin(), ewe = ei->second.end();
202 ewi != ewe; ++ewi) {
203 dbgs() << ewi->first << ": " << format("%.20g",ewi->second) << "\n";
204 }
205 }
206 } else {
207 assert(F && "No function given, this is not supported!");
208 dbgs() << "Functions: \n";
209 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
210
211 dbgs() << "BasicBlocks for Function " << F << ":\n";
212 for (typename FType::const_iterator BI = F->begin(), BE = F->end();
213 BI != BE; ++BI) {
214 const BType *BB = &(*BI);
215 dbgs() << BB << "@" << format("%p", BB) << ": " << format("%.20g",getExecutionCount(BB)) << "\n";
216 }
217 }
218 dbgs() << "**** ProfileInfo " << this << ", over and out.\n";
219 }
220
221 bool CalculateMissingEdge(const BType *BB, Edge &removed, bool assumeEmptyExit = false);
222
223 bool EstimateMissingEdges(const BType *BB);
224
225 ProfileInfoT *MI() {
226 if (MachineProfile == 0)
227 MachineProfile = new ProfileInfoT();
228 return MachineProfile;
229 }
230
231 bool hasMI() const {
232 return (MachineProfile != 0);
233 }
234 };
235
236 typedef ProfileInfoT ProfileInfo;
237 typedef ProfileInfoT MachineProfileInfo;
238
239 /// createProfileLoaderPass - This function returns a Pass that loads the
240 /// profiling information for the module from the specified filename, making
241 /// it available to the optimizers.
242 Pass *createProfileLoaderPass(const std::string &Filename);
243
244 } // End llvm namespace
245
246 #endif
+0
-81
include/llvm/Analysis/ProfileInfoLoader.h less more
None //===- ProfileInfoLoader.h - Load & convert profile information -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The ProfileInfoLoader class is used to load and represent profiling
10 // information read in from the dump file. If conversions between formats are
11 // needed, it can also do this.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_PROFILEINFOLOADER_H
16 #define LLVM_ANALYSIS_PROFILEINFOLOADER_H
17
18 #include
19 #include
20 #include
21
22 namespace llvm {
23
24 class Module;
25 class Function;
26 class BasicBlock;
27
28 class ProfileInfoLoader {
29 const std::string &Filename;
30 std::vector CommandLines;
31 std::vector FunctionCounts;
32 std::vector BlockCounts;
33 std::vector EdgeCounts;
34 std::vector OptimalEdgeCounts;
35 std::vector BBTrace;
36 public:
37 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting
38 // the program if the file is invalid or broken.
39 ProfileInfoLoader(const char *ToolName, const std::string &Filename);
40
41 static const unsigned Uncounted;
42
43 unsigned getNumExecutions() const { return CommandLines.size(); }
44 const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
45
46 const std::string &getFileName() const { return Filename; }
47
48 // getRawFunctionCounts - This method is used by consumers of function
49 // counting information.
50 //
51 const std::vector &getRawFunctionCounts() const {
52 return FunctionCounts;
53 }
54
55 // getRawBlockCounts - This method is used by consumers of block counting
56 // information.
57 //
58 const std::vector &getRawBlockCounts() const {
59 return BlockCounts;
60 }
61
62 // getEdgeCounts - This method is used by consumers of edge counting
63 // information.
64 //
65 const std::vector &getRawEdgeCounts() const {
66 return EdgeCounts;
67 }
68
69 // getEdgeOptimalCounts - This method is used by consumers of optimal edge
70 // counting information.
71 //
72 const std::vector &getRawOptimalEdgeCounts() const {
73 return OptimalEdgeCounts;
74 }
75
76 };
77
78 } // End llvm namespace
79
80 #endif
+0
-52
include/llvm/Analysis/ProfileInfoTypes.h less more
None /*===-- ProfileInfoTypes.h - Profiling info shared constants --------------===*\
1 |*
2 |* The LLVM Compiler Infrastructure
3 |*
4 |* This file is distributed under the University of Illinois Open Source
5 |* License. See LICENSE.TXT for details.
6 |*
7 |*===----------------------------------------------------------------------===*|
8 |*
9 |* This file defines constants shared by the various different profiling
10 |* runtime libraries and the LLVM C++ profile info loader. It must be a
11 |* C header because, at present, the profiling runtimes are written in C.
12 |*
13 \*===----------------------------------------------------------------------===*/
14
15 #ifndef LLVM_ANALYSIS_PROFILEINFOTYPES_H
16 #define LLVM_ANALYSIS_PROFILEINFOTYPES_H
17
18 /* Included by libprofile. */
19 #if defined(__cplusplus)
20 extern "C" {
21 #endif
22
23 /* IDs to distinguish between those path counters stored in hashses vs arrays */
24 enum ProfilingStorageType {
25 ProfilingArray = 1,
26 ProfilingHash = 2
27 };
28
29 #include "llvm/Analysis/ProfileDataTypes.h"
30
31 /*
32 * The header for tables that map path numbers to path counters.
33 */
34 typedef struct {
35 unsigned fnNumber; /* function number for these counters */
36 unsigned numEntries; /* number of entries stored */
37 } PathProfileHeader;
38
39 /*
40 * Describes an entry in a tagged table for path counters.
41 */
42 typedef struct {
43 unsigned pathNumber;
44 unsigned pathCounter;
45 } PathProfileTableEntry;
46
47 #if defined(__cplusplus)
48 }
49 #endif
50
51 #endif /* LLVM_ANALYSIS_PROFILEINFOTYPES_H */
112112 void initializeDominatorTreePass(PassRegistry&);
113113 void initializeEarlyIfConverterPass(PassRegistry&);
114114 void initializeEdgeBundlesPass(PassRegistry&);
115 void initializeEdgeProfilerPass(PassRegistry&);
116115 void initializeExpandPostRAPass(PassRegistry&);
117 void initializePathProfilerPass(PassRegistry&);
118116 void initializeGCOVProfilerPass(PassRegistry&);
119117 void initializeAddressSanitizerPass(PassRegistry&);
120118 void initializeAddressSanitizerModulePass(PassRegistry&);
154152 void initializeLiveStacksPass(PassRegistry&);
155153 void initializeLiveVariablesPass(PassRegistry&);
156154 void initializeLoaderPassPass(PassRegistry&);
157 void initializeProfileMetadataLoaderPassPass(PassRegistry&);
158 void initializePathProfileLoaderPassPass(PassRegistry&);
159155 void initializeLocalStackSlotPassPass(PassRegistry&);
160156 void initializeLoopDeletionPass(PassRegistry&);
161157 void initializeLoopExtractorPass(PassRegistry&);
194190 void initializeMergeFunctionsPass(PassRegistry&);
195191 void initializeModuleDebugInfoPrinterPass(PassRegistry&);
196192 void initializeNoAAPass(PassRegistry&);
197 void initializeNoProfileInfoPass(PassRegistry&);
198 void initializeNoPathProfileInfoPass(PassRegistry&);
199193 void initializeObjCARCAliasAnalysisPass(PassRegistry&);
200194 void initializeObjCARCAPElimPass(PassRegistry&);
201195 void initializeObjCARCExpandPass(PassRegistry&);
202196 void initializeObjCARCContractPass(PassRegistry&);
203197 void initializeObjCARCOptPass(PassRegistry&);
204 void initializeOptimalEdgeProfilerPass(PassRegistry&);
205198 void initializeOptimizePHIsPass(PassRegistry&);
206199 void initializePartiallyInlineLibCallsPass(PassRegistry&);
207200 void initializePEIPass(PassRegistry&);
219212 void initializePrintModulePassPass(PassRegistry&);
220213 void initializePrintBasicBlockPassPass(PassRegistry&);
221214 void initializeProcessImplicitDefsPass(PassRegistry&);
222 void initializeProfileEstimatorPassPass(PassRegistry&);
223 void initializeProfileInfoAnalysisGroup(PassRegistry&);
224 void initializePathProfileInfoAnalysisGroup(PassRegistry&);
225 void initializePathProfileVerifierPass(PassRegistry&);
226 void initializeProfileVerifierPassPass(PassRegistry&);
227215 void initializePromotePassPass(PassRegistry&);
228216 void initializePruneEHPass(PassRegistry&);
229217 void initializeReassociatePass(PassRegistry&);
7373 (void) llvm::createDomPrinterPass();
7474 (void) llvm::createDomOnlyViewerPass();
7575 (void) llvm::createDomViewerPass();
76 (void) llvm::createEdgeProfilerPass();
77 (void) llvm::createOptimalEdgeProfilerPass();
78 (void) llvm::createPathProfilerPass();
7976 (void) llvm::createGCOVProfilerPass();
8077 (void) llvm::createFunctionInliningPass();
8178 (void) llvm::createAlwaysInlinerPass();
10198 (void) llvm::createLowerInvokePass();
10299 (void) llvm::createLowerSwitchPass();
103100 (void) llvm::createNoAAPass();
104 (void) llvm::createNoProfileInfoPass();
105101 (void) llvm::createObjCARCAliasAnalysisPass();
106102 (void) llvm::createObjCARCAPElimPass();
107103 (void) llvm::createObjCARCExpandPass();
108104 (void) llvm::createObjCARCContractPass();
109105 (void) llvm::createObjCARCOptPass();
110 (void) llvm::createProfileEstimatorPass();
111 (void) llvm::createProfileVerifierPass();
112 (void) llvm::createPathProfileVerifierPass();
113 (void) llvm::createProfileLoaderPass();
114 (void) llvm::createProfileMetadataLoaderPass();
115 (void) llvm::createPathProfileLoaderPass();
116106 (void) llvm::createPromoteMemoryToRegisterPass();
117107 (void) llvm::createDemoteRegisterToMemoryPass();
118108 (void) llvm::createPruneEHPass();
3333
3434 class ModulePass;
3535 class FunctionPass;
36
37 // Insert edge profiling instrumentation
38 ModulePass *createEdgeProfilerPass();
39
40 // Insert optimal edge profiling instrumentation
41 ModulePass *createOptimalEdgeProfilerPass();
42
43 // Insert path profiling instrumentation
44 ModulePass *createPathProfilerPass();
4536
4637 // Insert GCOV profiling instrumentation
4738 struct GCOVOptions {
5353 initializeMemoryDependenceAnalysisPass(Registry);
5454 initializeModuleDebugInfoPrinterPass(Registry);
5555 initializePostDominatorTreePass(Registry);
56 initializeProfileEstimatorPassPass(Registry);
57 initializeNoProfileInfoPass(Registry);
58 initializeNoPathProfileInfoPass(Registry);
59 initializeProfileInfoAnalysisGroup(Registry);
60 initializePathProfileInfoAnalysisGroup(Registry);
61 initializeLoaderPassPass(Registry);
62 initializePathProfileLoaderPassPass(Registry);
63 initializeProfileVerifierPassPass(Registry);
64 initializePathProfileVerifierPass(Registry);
65 initializeProfileMetadataLoaderPassPass(Registry);
6656 initializeRegionInfoPass(Registry);
6757 initializeRegionViewerPass(Registry);
6858 initializeRegionPrinterPass(Registry);
3434 ModuleDebugInfoPrinter.cpp
3535 NoAliasAnalysis.cpp
3636 PHITransAddr.cpp
37 PathNumbering.cpp
38 PathProfileInfo.cpp
39 PathProfileVerifier.cpp
4037 PostDominators.cpp
41 ProfileEstimatorPass.cpp
42 ProfileInfo.cpp
43 ProfileInfoLoader.cpp
44 ProfileInfoLoaderPass.cpp
45 ProfileVerifierPass.cpp
46 ProfileDataLoader.cpp
47 ProfileDataLoaderPass.cpp
4838 PtrUseVisitor.cpp
4939 RegionInfo.cpp
5040 RegionPass.cpp
+0
-521
lib/Analysis/PathNumbering.cpp less more
None //===- PathNumbering.cpp --------------------------------------*- C++ -*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Ball-Larus path numbers uniquely identify paths through a directed acyclic
10 // graph (DAG) [Ball96]. For a CFG backedges are removed and replaced by phony
11 // edges to obtain a DAG, and thus the unique path numbers [Ball96].
12 //
13 // The purpose of this analysis is to enumerate the edges in a CFG in order
14 // to obtain paths from path numbers in a convenient manner. As described in
15 // [Ball96] edges can be enumerated such that given a path number by following
16 // the CFG and updating the path number, the path is obtained.
17 //
18 // [Ball96]
19 // T. Ball and J. R. Larus. "Efficient Path Profiling."
20 // International Symposium on Microarchitecture, pages 46-57, 1996.
21 // http://portal.acm.org/citation.cfm?id=243857
22 //
23 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "ball-larus-numbering"
25
26 #include "llvm/Analysis/PathNumbering.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/InstrTypes.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/TypeBuilder.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Support/CFG.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include
40 #include
41 #include
42 #include
43 #include
44
45 using namespace llvm;
46
47 // Are we enabling early termination
48 static cl::opt ProcessEarlyTermination(
49 "path-profile-early-termination", cl::Hidden,
50 cl::desc("In path profiling, insert extra instrumentation to account for "
51 "unexpected function termination."));
52
53 // Returns the basic block for the BallLarusNode
54 BasicBlock* BallLarusNode::getBlock() {
55 return(_basicBlock);
56 }
57
58 // Returns the number of paths to the exit starting at the node.
59 unsigned BallLarusNode::getNumberPaths() {
60 return(_numberPaths);
61 }
62
63 // Sets the number of paths to the exit starting at the node.
64 void BallLarusNode::setNumberPaths(unsigned numberPaths) {
65 _numberPaths = numberPaths;
66 }
67
68 // Gets the NodeColor used in graph algorithms.
69 BallLarusNode::NodeColor BallLarusNode::getColor() {
70 return(_color);
71 }
72
73 // Sets the NodeColor used in graph algorithms.
74 void BallLarusNode::setColor(BallLarusNode::NodeColor color) {
75 _color = color;
76 }
77
78 // Returns an iterator over predecessor edges. Includes phony and
79 // backedges.
80 BLEdgeIterator BallLarusNode::predBegin() {
81 return(_predEdges.begin());
82 }
83
84 // Returns the end sentinel for the predecessor iterator.
85 BLEdgeIterator BallLarusNode::predEnd() {
86 return(_predEdges.end());
87 }
88
89 // Returns the number of predecessor edges. Includes phony and
90 // backedges.
91 unsigned BallLarusNode::getNumberPredEdges() {
92 return(_predEdges.size());
93 }
94
95 // Returns an iterator over successor edges. Includes phony and
96 // backedges.
97 BLEdgeIterator BallLarusNode::succBegin() {
98 return(_succEdges.begin());
99 }
100
101 // Returns the end sentinel for the successor iterator.
102 BLEdgeIterator BallLarusNode::succEnd() {
103 return(_succEdges.end());
104 }
105
106 // Returns the number of successor edges. Includes phony and
107 // backedges.
108 unsigned BallLarusNode::getNumberSuccEdges() {
109 return(_succEdges.size());
110 }
111
112 // Add an edge to the predecessor list.
113 void BallLarusNode::addPredEdge(BallLarusEdge* edge) {
114 _predEdges.push_back(edge);
115 }
116
117 // Remove an edge from the predecessor list.
118 void BallLarusNode::removePredEdge(BallLarusEdge* edge) {
119 removeEdge(_predEdges, edge);
120 }
121
122 // Add an edge to the successor list.
123 void BallLarusNode::addSuccEdge(BallLarusEdge* edge) {
124 _succEdges.push_back(edge);
125 }
126
127 // Remove an edge from the successor list.
128 void BallLarusNode::removeSuccEdge(BallLarusEdge* edge) {
129 removeEdge(_succEdges, edge);
130 }
131
132 // Returns the name of the BasicBlock being represented. If BasicBlock
133 // is null then returns "". If BasicBlock has no name, then
134 // "" is returned. Intended for use with debug output.
135 std::string BallLarusNode::getName() {
136 std::stringstream name;
137
138 if(getBlock() != NULL) {
139 if(getBlock()->hasName()) {
140 std::string tempName(getBlock()->getName());
141 name << tempName.c_str() << " (" << _uid << ")";
142 } else
143 name << " (" << _uid << ")";
144 } else
145 name << " (" << _uid << ")";
146
147 return name.str();
148 }
149
150 // Removes an edge from an edgeVector. Used by removePredEdge and
151 // removeSuccEdge.
152 void BallLarusNode::removeEdge(BLEdgeVector& v, BallLarusEdge* e) {
153 // TODO: Avoid linear scan by using a set instead
154 for(BLEdgeIterator i = v.begin(),
155 end = v.end();
156 i != end;
157 ++i) {
158 if((*i) == e) {
159 v.erase(i);
160 break;
161 }
162 }
163 }
164
165 // Returns the source node of this edge.
166 BallLarusNode* BallLarusEdge::getSource() const {
167 return(_source);
168 }
169
170 // Returns the target node of this edge.
171 BallLarusNode* BallLarusEdge::getTarget() const {
172 return(_target);
173 }
174
175 // Sets the type of the edge.
176 BallLarusEdge::EdgeType BallLarusEdge::getType() const {
177 return _edgeType;
178 }
179
180 // Gets the type of the edge.
181 void BallLarusEdge::setType(EdgeType type) {
182 _edgeType = type;
183 }
184
185 // Returns the weight of this edge. Used to decode path numbers to sequences
186 // of basic blocks.
187 unsigned BallLarusEdge::getWeight() {
188 return(_weight);
189 }
190
191 // Sets the weight of the edge. Used during path numbering.
192 void BallLarusEdge::setWeight(unsigned weight) {
193 _weight = weight;
194 }
195
196 // Gets the phony edge originating at the root.
197 BallLarusEdge* BallLarusEdge::getPhonyRoot() {
198 return _phonyRoot;
199 }
200
201 // Sets the phony edge originating at the root.
202 void BallLarusEdge::setPhonyRoot(BallLarusEdge* phonyRoot) {
203 _phonyRoot = phonyRoot;
204 }
205
206 // Gets the phony edge terminating at the exit.
207 BallLarusEdge* BallLarusEdge::getPhonyExit() {
208 return _phonyExit;
209 }
210
211 // Sets the phony edge terminating at the exit.
212 void BallLarusEdge::setPhonyExit(BallLarusEdge* phonyExit) {
213 _phonyExit = phonyExit;
214 }
215
216 // Gets the associated real edge if this is a phony edge.
217 BallLarusEdge* BallLarusEdge::getRealEdge() {
218 return _realEdge;
219 }
220
221 // Sets the associated real edge if this is a phony edge.
222 void BallLarusEdge::setRealEdge(BallLarusEdge* realEdge) {
223 _realEdge = realEdge;
224 }
225
226 // Returns the duplicate number of the edge.
227 unsigned BallLarusEdge::getDuplicateNumber() {
228 return(_duplicateNumber);
229 }
230
231 // Initialization that requires virtual functions which are not fully
232 // functional in the constructor.
233 void BallLarusDag::init() {
234 BLBlockNodeMap inDag;
235 std::stack dfsStack;
236
237 _root = addNode(&(_function.getEntryBlock()));
238 _exit = addNode(NULL);
239
240 // start search from root
241 dfsStack.push(getRoot());
242
243 // dfs to add each bb into the dag
244 while(dfsStack.size())
245 buildNode(inDag, dfsStack);
246
247 // put in the final edge
248 addEdge(getExit(),getRoot(),0);
249 }
250
251 // Frees all memory associated with the DAG.
252 BallLarusDag::~BallLarusDag() {
253 for(BLEdgeIterator edge = _edges.begin(), end = _edges.end(); edge != end;
254 ++edge)
255 delete (*edge);
256
257 for(BLNodeIterator node = _nodes.begin(), end = _nodes.end(); node != end;
258 ++node)
259 delete (*node);
260 }
261
262 // Calculate the path numbers by assigning edge increments as prescribed
263 // in Ball-Larus path profiling.
264 void BallLarusDag::calculatePathNumbers() {
265 BallLarusNode* node;
266 std::queue bfsQueue;
267 bfsQueue.push(getExit());
268
269 while(bfsQueue.size() > 0) {
270 node = bfsQueue.front();
271
272 DEBUG(dbgs() << "calculatePathNumbers on " << node->getName() << "\n");
273
274 bfsQueue.pop();
275 unsigned prevPathNumber = node->getNumberPaths();
276 calculatePathNumbersFrom(node);
277
278 // Check for DAG splitting
279 if( node->getNumberPaths() > 100000000 && node != getRoot() ) {
280 // Add new phony edge from the split-node to the DAG's exit
281 BallLarusEdge* exitEdge = addEdge(node, getExit(), 0);
282 exitEdge->setType(BallLarusEdge::SPLITEDGE_PHONY);
283
284 // Counters to handle the possibility of a multi-graph
285 BasicBlock* oldTarget = 0;
286 unsigned duplicateNumber = 0;
287
288 // Iterate through each successor edge, adding phony edges
289 for( BLEdgeIterator succ = node->succBegin(), end = node->succEnd();
290 succ != end; oldTarget = (*succ)->getTarget()->getBlock(), succ++ ) {
291
292 if( (*succ)->getType() == BallLarusEdge::NORMAL ) {
293 // is this edge a duplicate?
294 if( oldTarget != (*succ)->getTarget()->getBlock() )
295 duplicateNumber = 0;
296
297 // create the new phony edge: root -> succ
298 BallLarusEdge* rootEdge =
299 addEdge(getRoot(), (*succ)->getTarget(), duplicateNumber++);
300 rootEdge->setType(BallLarusEdge::SPLITEDGE_PHONY);
301 rootEdge->setRealEdge(*succ);
302
303 // split on this edge and reference it's exit/root phony edges
304 (*succ)->setType(BallLarusEdge::SPLITEDGE);
305 (*succ)->setPhonyRoot(rootEdge);
306 (*succ)->setPhonyExit(exitEdge);
307 (*succ)->setWeight(0);
308 }
309 }
310
311 calculatePathNumbersFrom(node);
312 }
313
314 DEBUG(dbgs() << "prev, new number paths " << prevPathNumber << ", "
315 << node->getNumberPaths() << ".\n");
316
317 if(prevPathNumber == 0 && node->getNumberPaths() != 0) {
318 DEBUG(dbgs() << "node ready : " << node->getName() << "\n");
319 for(BLEdgeIterator pred = node->predBegin(), end = node->predEnd();
320 pred != end; pred++) {
321 if( (*pred)->getType() == BallLarusEdge::BACKEDGE ||
322 (*pred)->getType() == BallLarusEdge::SPLITEDGE )
323 continue;
324
325 BallLarusNode* nextNode = (*pred)->getSource();
326 // not yet visited?
327 if(nextNode->getNumberPaths() == 0)
328 bfsQueue.push(nextNode);
329 }
330 }
331 }
332
333 DEBUG(dbgs() << "\tNumber of paths: " << getRoot()->getNumberPaths() << "\n");
334 }
335
336 // Returns the number of paths for the Dag.
337 unsigned BallLarusDag::getNumberOfPaths() {
338 return(getRoot()->getNumberPaths());
339 }
340
341 // Returns the root (i.e. entry) node for the DAG.
342 BallLarusNode* BallLarusDag::getRoot() {
343 return _root;
344 }
345
346 // Returns the exit node for the DAG.
347 BallLarusNode* BallLarusDag::getExit() {
348 return _exit;
349 }
350
351 // Returns the function for the DAG.
352 Function& BallLarusDag::getFunction() {
353 return(_function);
354 }
355
356 // Clears the node colors.
357 void BallLarusDag::clearColors(BallLarusNode::NodeColor color) {
358 for (BLNodeIterator nodeIt = _nodes.begin(); nodeIt != _nodes.end(); nodeIt++)
359 (*nodeIt)->setColor(color);
360 }
361
362 // Processes one node and its imediate edges for building the DAG.
363 void BallLarusDag::buildNode(BLBlockNodeMap& inDag, BLNodeStack& dfsStack) {
364 BallLarusNode* currentNode = dfsStack.top();
365 BasicBlock* currentBlock = currentNode->getBlock();
366
367 if(currentNode->getColor() != BallLarusNode::WHITE) {
368 // we have already visited this node
369 dfsStack.pop();
370 currentNode->setColor(BallLarusNode::BLACK);
371 } else {
372 // are there any external procedure calls?
373 if( ProcessEarlyTermination ) {
374 for( BasicBlock::iterator bbCurrent = currentNode->getBlock()->begin(),
375 bbEnd = currentNode->getBlock()->end(); bbCurrent != bbEnd;
376 bbCurrent++ ) {
377 Instruction& instr = *bbCurrent;
378 if( instr.getOpcode() == Instruction::Call ) {
379 BallLarusEdge* callEdge = addEdge(currentNode, getExit(), 0);
380 callEdge->setType(BallLarusEdge::CALLEDGE_PHONY);
381 break;
382 }
383 }
384 }
385
386 TerminatorInst* terminator = currentNode->getBlock()->getTerminator();
387 if(isa(terminator) || isa(terminator) ||
388 isa(terminator))
389 addEdge(currentNode, getExit(),0);
390
391 currentNode->setColor(BallLarusNode::GRAY);
392 inDag[currentBlock] = currentNode;
393
394 BasicBlock* oldSuccessor = 0;
395 unsigned duplicateNumber = 0;
396
397 // iterate through this node's successors
398 for(succ_iterator successor = succ_begin(currentBlock),
399 succEnd = succ_end(currentBlock); successor != succEnd;
400 oldSuccessor = *successor, ++successor ) {
401 BasicBlock* succBB = *successor;
402
403 // is this edge a duplicate?
404 if (oldSuccessor == succBB)
405 duplicateNumber++;
406 else
407 duplicateNumber = 0;
408
409 buildEdge(inDag, dfsStack, currentNode, succBB, duplicateNumber);
410 }
411 }
412 }
413
414 // Process an edge in the CFG for DAG building.
415 void BallLarusDag::buildEdge(BLBlockNodeMap& inDag, std::stack&
416 dfsStack, BallLarusNode* currentNode,
417 BasicBlock* succBB, unsigned duplicateCount) {
418 BallLarusNode* succNode = inDag[succBB];
419
420 if(succNode && succNode->getColor() == BallLarusNode::BLACK) {
421 // visited node and forward edge
422 addEdge(currentNode, succNode, duplicateCount);
423 } else if(succNode && succNode->getColor() == BallLarusNode::GRAY) {
424 // visited node and back edge
425 DEBUG(dbgs() << "Backedge detected.\n");
426 addBackedge(currentNode, succNode, duplicateCount);
427 } else {
428 BallLarusNode* childNode;
429 // not visited node and forward edge
430 if(succNode) // an unvisited node that is child of a gray node
431 childNode = succNode;
432 else { // an unvisited node that is a child of a an unvisted node
433 childNode = addNode(succBB);
434 inDag[succBB] = childNode;
435 }
436 addEdge(currentNode, childNode, duplicateCount);
437 dfsStack.push(childNode);
438 }
439 }
440
441 // The weight on each edge is the increment required along any path that
442 // contains that edge.
443 void BallLarusDag::calculatePathNumbersFrom(BallLarusNode* node) {
444 if(node == getExit())
445 // The Exit node must be base case
446 node->setNumberPaths(1);
447 else {
448 unsigned sumPaths = 0;
449 BallLarusNode* succNode;
450
451 for(BLEdgeIterator succ = node->succBegin(), end = node->succEnd();
452 succ != end; succ++) {
453 if( (*succ)->getType() == BallLarusEdge::BACKEDGE ||
454 (*succ)->getType() == BallLarusEdge::SPLITEDGE )
455 continue;
456
457 (*succ)->setWeight(sumPaths);
458 succNode = (*succ)->getTarget();
459
460 if( !succNode->getNumberPaths() )
461 return;
462 sumPaths += succNode->getNumberPaths();
463 }
464
465 node->setNumberPaths(sumPaths);
466 }
467 }
468
469 // Allows subclasses to determine which type of Node is created.
470 // Override this method to produce subclasses of BallLarusNode if
471 // necessary. The destructor of BallLarusDag will call free on each
472 // pointer created.
473 BallLarusNode* BallLarusDag::createNode(BasicBlock* BB) {
474 return( new BallLarusNode(BB) );
475 }
476
477 // Allows subclasses to determine which type of Edge is created.
478 // Override this method to produce subclasses of BallLarusEdge if
479 // necessary. The destructor of BallLarusDag will call free on each
480 // pointer created.
481 BallLarusEdge* BallLarusDag::createEdge(BallLarusNode* source,
482 BallLarusNode* target,
483 unsigned duplicateCount) {
484 return( new BallLarusEdge(source, target, duplicateCount) );
485 }
486
487 // Proxy to node's constructor. Updates the DAG state.
488 BallLarusNode* BallLarusDag::addNode(BasicBlock* BB) {
489 BallLarusNode* newNode = createNode(BB);
490 _nodes.push_back(newNode);
491 return( newNode );
492 }
493
494 // Proxy to edge's constructor. Updates the DAG state.
495 BallLarusEdge* BallLarusDag::addEdge(BallLarusNode* source,
496 BallLarusNode* target,
497 unsigned duplicateCount) {
498 BallLarusEdge* newEdge = createEdge(source, target, duplicateCount);
499 _edges.push_back(newEdge);
500 source->addSuccEdge(newEdge);
501 target->addPredEdge(newEdge);
502 return(newEdge);
503 }
504
505 // Adds a backedge with its phony edges. Updates the DAG state.
506 void BallLarusDag::addBackedge(BallLarusNode* source, BallLarusNode* target,
507 unsigned duplicateCount) {
508 BallLarusEdge* childEdge = addEdge(source, target, duplicateCount);
509 childEdge->setType(BallLarusEdge::BACKEDGE);
510
511 childEdge->setPhonyRoot(addEdge(getRoot(), target,0));
512 childEdge->setPhonyExit(addEdge(source, getExit(),0));
513
514 childEdge->getPhonyRoot()->setRealEdge(childEdge);
515 childEdge->getPhonyRoot()->setType(BallLarusEdge::BACKEDGE_PHONY);
516
517 childEdge->getPhonyExit()->setRealEdge(childEdge);
518 childEdge->getPhonyExit()->setType(BallLarusEdge::BACKEDGE_PHONY);
519 _backEdges.push_back(childEdge);
520 }
+0
-433
lib/Analysis/PathProfileInfo.cpp less more
None //===- PathProfileInfo.cpp ------------------------------------*- C++ -*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interface used by optimizers to load path profiles,
10 // and provides a loader pass which reads a path profile file.
11 //
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "path-profile-info"
14
15 #include "llvm/Analysis/PathProfileInfo.h"
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/Analysis/ProfileInfoTypes.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include
24
25 using namespace llvm;
26
27 // command line option for loading path profiles
28 static cl::opt
29 PathProfileInfoFilename("path-profile-loader-file", cl::init("llvmprof.out"),
30 cl::value_desc("filename"),
31 cl::desc("Path profile file loaded by -path-profile-loader"), cl::Hidden);
32
33 namespace {
34 class PathProfileLoaderPass : public ModulePass, public PathProfileInfo {
35 public:
36 PathProfileLoaderPass() : ModulePass(ID) { }
37 ~PathProfileLoaderPass();
38
39 // this pass doesn't change anything (only loads information)
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesAll();
42 }
43
44 // the full name of the loader pass
45 virtual const char* getPassName() const {
46 return "Path Profiling Information Loader";
47 }
48
49 // required since this pass implements multiple inheritance
50 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
51 if (PI == &PathProfileInfo::ID)
52 return (PathProfileInfo*)this;
53 return this;
54 }
55
56 // entry point to run the pass
57 bool runOnModule(Module &M);
58
59 // pass identification
60 static char ID;
61
62 private:
63 // make a reference table to refer to function by number
64 void buildFunctionRefs(Module &M);
65
66 // process argument info of a program from the input file
67 void handleArgumentInfo();
68
69 // process path number information from the input file
70 void handlePathInfo();
71
72 // array of references to the functions in the module
73 std::vector _functions;
74
75 // path profile file handle
76 FILE* _file;
77
78 // path profile file name
79 std::string _filename;
80 };
81 }
82
83 // register PathLoader
84 char PathProfileLoaderPass::ID = 0;
85
86 INITIALIZE_ANALYSIS_GROUP(PathProfileInfo, "Path Profile Information",
87 NoPathProfileInfo)
88 INITIALIZE_AG_PASS(PathProfileLoaderPass, PathProfileInfo,
89 "path-profile-loader",
90 "Load path profile information from file",
91 false, true, false)
92
93 char &llvm::PathProfileLoaderPassID = PathProfileLoaderPass::ID;
94
95 // link PathLoader as a pass, and make it available as an optimisation
96 ModulePass *llvm::createPathProfileLoaderPass() {
97 return new PathProfileLoaderPass;
98 }
99
100 // ----------------------------------------------------------------------------
101 // PathEdge implementation
102 //
103 ProfilePathEdge::ProfilePathEdge (BasicBlock* source, BasicBlock* target,
104 unsigned duplicateNumber)
105 : _source(source), _target(target), _duplicateNumber(duplicateNumber) {}
106
107 // ----------------------------------------------------------------------------
108 // Path implementation
109 //
110
111 ProfilePath::ProfilePath (unsigned int number, unsigned int count,
112 double countStdDev, PathProfileInfo* ppi)
113 : _number(number) , _count(count), _countStdDev(countStdDev), _ppi(ppi) {}
114
115 double ProfilePath::getFrequency() const {
116 return 100 * double(_count) /
117 double(_ppi->_functionPathCounts[_ppi->_currentFunction]);
118 }
119
120 static BallLarusEdge* getNextEdge (BallLarusNode* node,
121 unsigned int pathNumber) {
122 BallLarusEdge* best = 0;
123
124 for( BLEdgeIterator next = node->succBegin(),
125 end = node->succEnd(); next != end; next++ ) {
126 if( (*next)->getType() != BallLarusEdge::BACKEDGE && // no backedges
127 (*next)->getType() != BallLarusEdge::SPLITEDGE && // no split edges
128 (*next)->getWeight() <= pathNumber && // weight must be <= pathNumber
129 (!best || (best->getWeight() < (*next)->getWeight())) ) // best one?
130 best = *next;
131 }
132
133 return best;
134 }
135
136 ProfilePathEdgeVector* ProfilePath::getPathEdges() const {
137 BallLarusNode* currentNode = _ppi->_currentDag->getRoot ();
138 unsigned int increment = _number;
139 ProfilePathEdgeVector* pev = new ProfilePathEdgeVector;
140
141 while (currentNode != _ppi->_currentDag->getExit()) {
142 BallLarusEdge* next = getNextEdge(currentNode, increment);
143
144 increment -= next->getWeight();
145
146 if( next->getType() != BallLarusEdge::BACKEDGE_PHONY &&
147 next->getType() != BallLarusEdge::SPLITEDGE_PHONY &&
148 next->getTarget() != _ppi->_currentDag->getExit() )
149 pev->push_back(ProfilePathEdge(
150 next->getSource()->getBlock(),
151 next->getTarget()->getBlock(),
152 next->getDuplicateNumber()));
153
154 if( next->getType() == BallLarusEdge::BACKEDGE_PHONY &&
155 next->getTarget() == _ppi->_currentDag->getExit() )
156 pev->push_back(ProfilePathEdge(
157 next->getRealEdge()->getSource()->getBlock(),
158 next->getRealEdge()->getTarget()->getBlock(),
159 next->getDuplicateNumber()));
160
161 if( next->getType() == BallLarusEdge::SPLITEDGE_PHONY &&
162 next->getSource() == _ppi->_currentDag->getRoot() )
163 pev->push_back(ProfilePathEdge(
164 next->getRealEdge()->getSource()->getBlock(),
165 next->getRealEdge()->getTarget()->getBlock(),
166 next->getDuplicateNumber()));
167
168 // set the new node
169 currentNode = next->getTarget();
170 }
171
172 return pev;
173 }
174
175 ProfilePathBlockVector* ProfilePath::getPathBlocks() const {
176 BallLarusNode* currentNode = _ppi->_currentDag->getRoot ();
177 unsigned int increment = _number;
178 ProfilePathBlockVector* pbv = new ProfilePathBlockVector;
179
180 while (currentNode != _ppi->_currentDag->getExit()) {
181 BallLarusEdge* next = getNextEdge(currentNode, increment);
182 increment -= next->getWeight();
183
184 // add block to the block list if it is a real edge
185 if( next->getType() == BallLarusEdge::NORMAL)
186 pbv->push_back (currentNode->getBlock());
187 // make the back edge the last edge since we are at the end
188 else if( next->getTarget() == _ppi->_currentDag->getExit() ) {
189 pbv->push_back (currentNode->getBlock());
190 pbv->push_back (next->getRealEdge()->getTarget()->getBlock());
191 }
192
193 // set the new node
194 currentNode = next->getTarget();
195 }
196
197 return pbv;
198 }
199
200 BasicBlock* ProfilePath::getFirstBlockInPath() const {
201 BallLarusNode* root = _ppi->_currentDag->getRoot();
202 BallLarusEdge* edge = getNextEdge(root, _number);
203
204 if( edge && (edge->getType() == BallLarusEdge::BACKEDGE_PHONY ||
205 edge->getType() == BallLarusEdge::SPLITEDGE_PHONY) )
206 return edge->getTarget()->getBlock();
207
208 return root->getBlock();
209 }
210
211 // ----------------------------------------------------------------------------
212 // PathProfileInfo implementation
213 //
214
215 // Pass identification
216 char llvm::PathProfileInfo::ID = 0;
217
218 PathProfileInfo::PathProfileInfo () : _currentDag(0) , _currentFunction(0) {
219 }
220
221 PathProfileInfo::~PathProfileInfo() {
222 if (_currentDag)
223 delete _currentDag;
224 }
225
226 // set the function for which paths are currently begin processed
227 void PathProfileInfo::setCurrentFunction(Function* F) {
228 // Make sure it exists
229 if (!F) return;
230
231 if (_currentDag)
232 delete _currentDag;
233
234 _currentFunction = F;
235 _currentDag = new BallLarusDag(*F);
236 _currentDag->init();
237 _currentDag->calculatePathNumbers();
238 }
239
240 // get the function for which paths are currently being processed
241 Function* PathProfileInfo::getCurrentFunction() const {
242 return _currentFunction;
243 }
244
245 // get the entry block of the function
246 BasicBlock* PathProfileInfo::getCurrentFunctionEntry() {
247 return _currentDag->getRoot()->getBlock();
248 }
249
250 // return the path based on its number
251 ProfilePath* PathProfileInfo::getPath(unsigned int number) {
252 return _functionPaths[_currentFunction][number];
253 }
254
255 // return the number of paths which a function may potentially execute
256 unsigned int PathProfileInfo::getPotentialPathCount() {
257 return _currentDag ? _currentDag->getNumberOfPaths() : 0;
258 }
259
260 // return an iterator for the beginning of a functions executed paths
261 ProfilePathIterator PathProfileInfo::pathBegin() {
262 return _functionPaths[_currentFunction].begin();
263 }
264
265 // return an iterator for the end of a functions executed paths
266 ProfilePathIterator PathProfileInfo::pathEnd() {
267 return _functionPaths[_currentFunction].end();
268 }
269
270 // returns the total number of paths run in the function
271 unsigned int PathProfileInfo::pathsRun() {
272 return _currentFunction ? _functionPaths[_currentFunction].size() : 0;
273 }
274
275 // ----------------------------------------------------------------------------
276 // PathLoader implementation
277 //
278
279 // remove all generated paths
280 PathProfileLoaderPass::~PathProfileLoaderPass() {
281 for( FunctionPathIterator funcNext = _functionPaths.begin(),
282 funcEnd = _functionPaths.end(); funcNext != funcEnd; funcNext++)
283 for( ProfilePathIterator pathNext = funcNext->second.begin(),
284 pathEnd = funcNext->second.end(); pathNext != pathEnd; pathNext++)
285 delete pathNext->second;
286 }
287
288 // entry point of the pass; this loads and parses a file
289 bool PathProfileLoaderPass::runOnModule(Module &M) {
290 // get the filename and setup the module's function references
291 _filename = PathProfileInfoFilename;
292 buildFunctionRefs (M);
293
294 if (!(_file = fopen(_filename.c_str(), "rb"))) {
295 errs () << "error: input '" << _filename << "' file does not exist.\n";
296 return false;
297 }
298
299 ProfilingType profType;
300
301 while( fread(&profType, sizeof(ProfilingType), 1, _file) ) {
302 switch (profType) {
303 case ArgumentInfo:
304 handleArgumentInfo ();
305 break;
306 case PathInfo:
307 handlePathInfo ();
308 break;
309 default:
310 errs () << "error: bad path profiling file syntax, " << profType << "\n";
311 fclose (_file);
312 return false;
313 }
314 }
315
316 fclose (_file);
317
318 return true;
319 }
320
321 // create a reference table for functions defined in the path profile file
322 void PathProfileLoaderPass::buildFunctionRefs (Module &M) {
323 _functions.push_back(0); // make the 0 index a null pointer
324
325 for (Module::iterator F = M.begin(), E = M.end(); F != E; F++) {
326 if (F->isDeclaration())
327 continue;
328 _functions.push_back(F);
329 }
330 }
331
332 // handle command like argument infor in the output file
333 void PathProfileLoaderPass::handleArgumentInfo() {
334 // get the argument list's length
335 unsigned savedArgsLength;
336 if( fread(&savedArgsLength, sizeof(unsigned), 1, _file) != 1 ) {
337 errs() << "warning: argument info header/data mismatch\n";
338 return;
339 }
340
341 // allocate a buffer, and get the arguments
342 char* args = new char[savedArgsLength+1];
343 if( fread(args, 1, savedArgsLength, _file) != savedArgsLength )
344 errs() << "warning: argument info header/data mismatch\n";
345
346 args[savedArgsLength] = '\0';
347 argList = std::string(args);
348 delete [] args; // cleanup dynamic string
349
350 // byte alignment
351 if (savedArgsLength & 3)
352 fseek(_file, 4-(savedArgsLength&3), SEEK_CUR);
353 }
354
355 // Handle path profile information in the output file
356 void PathProfileLoaderPass::handlePathInfo () {
357 // get the number of functions in this profile
358 unsigned functionCount;
359 if( fread(&functionCount, sizeof(functionCount), 1, _file) != 1 ) {
360 errs() << "warning: path info header/data mismatch\n";
361 return;
362 }
363
364 // gather path information for each function
365 for (unsigned i = 0; i < functionCount; i++) {
366 PathProfileHeader pathHeader;
367 if( fread(&pathHeader, sizeof(pathHeader), 1, _file) != 1 ) {
368 errs() << "warning: bad header for path function info\n";
369 break;
370 }
371
372 Function* f = _functions[pathHeader.fnNumber];
373
374 // dynamically allocate a table to store path numbers
375 PathProfileTableEntry* pathTable =
376 new PathProfileTableEntry[pathHeader.numEntries];
377
378 if( fread(pathTable, sizeof(PathProfileTableEntry),
379 pathHeader.numEntries, _file) != pathHeader.numEntries) {
380 delete [] pathTable;
381 errs() << "warning: path function info header/data mismatch\n";
382 return;
383 }
384
385 // Build a new path for the current function
386 unsigned int totalPaths = 0;
387 for (unsigned int j = 0; j < pathHeader.numEntries; j++) {
388 totalPaths += pathTable[j].pathCounter;
389 _functionPaths[f][pathTable[j].pathNumber]
390 = new ProfilePath(pathTable[j].pathNumber, pathTable[j].pathCounter,
391 0, this);
392 }
393
394 _functionPathCounts[f] = totalPaths;
395
396 delete [] pathTable;
397 }
398 }
399
400 //===----------------------------------------------------------------------===//
401 // NoProfile PathProfileInfo implementation
402 //
403
404 namespace {
405 struct NoPathProfileInfo : public ImmutablePass, public PathProfileInfo {
406 static char ID; // Class identification, replacement for typeinfo
407 NoPathProfileInfo() : ImmutablePass(ID) {
408 initializeNoPathProfileInfoPass(*PassRegistry::getPassRegistry());
409 }
410
411 /// getAdjustedAnalysisPointer - This method is used when a pass implements
412 /// an analysis interface through multiple inheritance. If needed, it
413 /// should override this to adjust the this pointer as needed for the
414 /// specified pass info.
415 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
416 if (PI == &PathProfileInfo::ID)
417 return (PathProfileInfo*)this;
418 return this;
419 }
420
421 virtual const char *getPassName() const {
422 return "NoPathProfileInfo";
423 }
424 };
425 } // End of anonymous namespace
426
427 char NoPathProfileInfo::ID = 0;
428 // Register this pass...
429 INITIALIZE_AG_PASS(NoPathProfileInfo, PathProfileInfo, "no-path-profile",
430 "No Path Profile Information", false, true, true)
431
432 ImmutablePass *llvm::createNoPathProfileInfoPass() { return new NoPathProfileInfo(); }
+0
-205
lib/Analysis/PathProfileVerifier.cpp less more
None //===- PathProfileVerifier.cpp --------------------------------*- C++ -*---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This verifier derives an edge profile file from current path profile
10 // information
11 //
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "path-profile-verifier"
14
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Analysis/PathProfileInfo.h"
17 #include "llvm/Analysis/ProfileInfoTypes.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include
24
25 using namespace llvm;
26
27 namespace {
28 class PathProfileVerifier : public ModulePass {
29 private:
30 bool runOnModule(Module &M);
31
32 public:
33 static char ID; // Pass identification, replacement for typeid
34 PathProfileVerifier() : ModulePass(ID) {
35 initializePathProfileVerifierPass(*PassRegistry::getPassRegistry());
36 }
37
38
39 virtual const char *getPassName() const {
40 return "Path Profiler Verifier";
41 }
42
43 // The verifier requires the path profile and edge profile.
44 virtual void getAnalysisUsage(AnalysisUsage& AU) const;
45 };
46 }
47
48 static cl::opt
49 EdgeProfileFilename("path-profile-verifier-file",
50 cl::init("edgefrompath.llvmprof.out"),
51 cl::value_desc("filename"),
52 cl::desc("Edge profile file generated by -path-profile-verifier"),
53 cl::Hidden);
54
55 char PathProfileVerifier::ID = 0;
56 INITIALIZE_PASS(PathProfileVerifier, "path-profile-verifier",
57 "Compare the path profile derived edge profile against the "
58 "edge profile.", true, true)
59
60 ModulePass *llvm::createPathProfileVerifierPass() {
61 return new PathProfileVerifier();
62 }
63
64 // The verifier requires the path profile and edge profile.
65 void PathProfileVerifier::getAnalysisUsage(AnalysisUsage& AU) const {
66 AU.addRequired();
67 AU.addPreserved();
68 }
69
70 typedef std::map DuplicateToIndexMap;
71 typedef std::map BlockToDuplicateMap;
72 typedef std::map NestedBlockToIndexMap;
73
74 // the verifier iterates through each path to gather the total
75 // number of edge frequencies
76 bool PathProfileVerifier::runOnModule (Module &M) {
77 PathProfileInfo& pathProfileInfo = getAnalysis();
78
79 // setup a data structure to map path edges which index an
80 // array of edge counters
81 NestedBlockToIndexMap arrayMap;
82 unsigned i = 0;
83 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
84 if (F->isDeclaration()) continue;
85
86 arrayMap[(BasicBlock*)0][F->begin()][0] = i++;
87
88 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
89 TerminatorInst *TI = BB->getTerminator();
90
91 unsigned duplicate = 0;
92 BasicBlock* prev = 0;
93 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e;
94 prev = TI->getSuccessor(s), ++s) {
95 if (prev == TI->getSuccessor(s))
96 duplicate++;
97 else duplicate = 0;
98
99 arrayMap[BB][TI->getSuccessor(s)][duplicate] = i++;
100 }
101 }
102 }
103
104 std::vector edgeArray(i);
105
106 // iterate through each path and increment the edge counters as needed
107 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
108 if (F->isDeclaration()) continue;
109
110 pathProfileInfo.setCurrentFunction(F);
111
112 DEBUG(dbgs() << "function '" << F->getName() << "' ran "
113 << pathProfileInfo.pathsRun()
114 << "/" << pathProfileInfo.getPotentialPathCount()
115 << " potential paths\n");
116
117 for( ProfilePathIterator nextPath = pathProfileInfo.pathBegin(),
118 endPath = pathProfileInfo.pathEnd();
119 nextPath != endPath; nextPath++ ) {
120 ProfilePath* currentPath = nextPath->second;
121
122 ProfilePathEdgeVector* pev = currentPath->getPathEdges();
123 DEBUG(dbgs () << "path #" << currentPath->getNumber() << ": "
124 << currentPath->getCount() << "\n");
125 // setup the entry edge (normally path profiling doesn't care about this)
126 if (currentPath->getFirstBlockInPath() == &F->getEntryBlock())
127 edgeArray[arrayMap[(BasicBlock*)0][currentPath->getFirstBlockInPath()][0]]
128 += currentPath->getCount();
129
130 for( ProfilePathEdgeIterator nextEdge = pev->begin(),
131 endEdge = pev->end(); nextEdge != endEdge; nextEdge++ ) {
132 if (nextEdge != pev->begin())
133 DEBUG(dbgs() << " :: ");
134
135 BasicBlock* source = nextEdge->getSource();
136 BasicBlock* target = nextEdge->getTarget();
137 unsigned duplicateNumber = nextEdge->getDuplicateNumber();
138 DEBUG(dbgs() << source->getName() << " --{" << duplicateNumber
139 << "}--> " << target->getName());
140
141 // Ensure all the referenced edges exist
142 // TODO: make this a separate function
143 if( !arrayMap.count(source) ) {
144 errs() << " error [" << F->getName() << "()]: source '"
145 << source->getName()
146 << "' does not exist in the array map.\n";
147 } else if( !arrayMap[source].count(target) ) {
148 errs() << " error [" << F->getName() << "()]: target '"
149 << target->getName()
150 << "' does not exist in the array map.\n";
151 } else if( !arrayMap[source][target].count(duplicateNumber) ) {
152 errs() << " error [" << F->getName() << "()]: edge "
153 << source->getName() << " -> " << target->getName()
154 << " duplicate number " << duplicateNumber
155 << " does not exist in the array map.\n";
156 } else {
157 edgeArray[arrayMap[source][target][duplicateNumber]]
158 += currentPath->getCount();
159 }
160 }
161
162 DEBUG(errs() << "\n");
163
164 delete pev;
165 }
166 }
167
168 std::string filename = EdgeProfileFilename;
169
170 // Open a handle to the file
171 FILE* edgeFile = fopen(filename.c_str(),"wb");
172
173 if (!edgeFile) {
174 errs() << "error: unable to open file '" << filename << "' for output.\n";
175 return false;
176 }
177
178 errs() << "Generating edge profile '" << filename << "' ...\n";
179
180 // write argument info
181 unsigned type = ArgumentInfo;
182 unsigned num = pathProfileInfo.argList.size();
183 int zeros = 0;
184
185 fwrite(&type,sizeof(unsigned),1,edgeFile);
186 fwrite(&num,sizeof(unsigned),1,edgeFile);
187 fwrite(pathProfileInfo.argList.c_str(),1,num,edgeFile);
188 if (num&3)
189 fwrite(&zeros, 1, 4-(num&3), edgeFile);
190
191 type = EdgeInfo;
192 num = edgeArray.size();
193 fwrite(&type,sizeof(unsigned),1,edgeFile);
194 fwrite(&num,sizeof(unsigned),1,edgeFile);
195
196 // write each edge to the file
197 for( std::vector::iterator s = edgeArray.begin(),
198 e = edgeArray.end(); s != e; s++)
199 fwrite(&*s, sizeof (unsigned), 1, edgeFile);
200
201 fclose (edgeFile);
202
203 return true;
204 }
+0
-155
lib/Analysis/ProfileDataLoader.cpp less more
None //===- ProfileDataLoader.cpp - Load profile information from disk ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The ProfileDataLoader class is used to load raw profiling data from the dump
10 // file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/ProfileDataLoader.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/Analysis/ProfileDataTypes.h"
18 #include "llvm/IR/InstrTypes.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/system_error.h"
22 #include
23 #include
24 using namespace llvm;
25
26 raw_ostream &llvm::operator<<(raw_ostream &O, std::pair
27 const BasicBlock *> E) {
28 O << "(";
29
30 if (E.first)
31 O << E.first->getName();
32 else
33 O << "0";
34
35 O << ",";
36
37 if (E.second)
38 O << E.second->getName();
39 else
40 O << "0";
41
42 return O << ")";
43 }
44
45 /// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
46 /// (or both) may not be defined.
47 static unsigned AddCounts(unsigned A, unsigned B) {
48 // If either value is undefined, use the other.
49 // Undefined + undefined = undefined.
50 if (A == ProfileDataLoader::Uncounted) return B;
51 if (B == ProfileDataLoader::Uncounted) return A;
52
53 return A + B;
54 }
55
56 /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
57 template
58 static void ReadProfilingData(const char *ToolName, FILE *F,
59 T *Data, size_t NumEntries) {
60 // Read in the block of data...
61 if (fread(Data, sizeof(T), NumEntries, F) != NumEntries)
62 report_fatal_error(Twine(ToolName) + ": Profiling data truncated");
63 }
64
65 /// ReadProfilingNumEntries - Read how many entries are in this profiling data
66 /// packet.
67 static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
68 bool ShouldByteSwap) {
69 unsigned Entry;
70 ReadProfilingData(ToolName, F, &Entry, 1);
71 return ShouldByteSwap ? ByteSwap_32(Entry) : Entry;
72 }
73
74 /// ReadProfilingBlock - Read the number of entries in the next profiling data
75 /// packet and then accumulate the entries into 'Data'.
76 static void ReadProfilingBlock(const char *ToolName, FILE *F,
77 bool ShouldByteSwap,
78 SmallVectorImpl &Data) {
79 // Read the number of entries...
80 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
81
82 // Read in the data.
83 SmallVector TempSpace(NumEntries);
84 ReadProfilingData(ToolName, F, TempSpace.data(), NumEntries);
85
86 // Make sure we have enough space ...
87 if (Data.size() < NumEntries)
88 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
89
90 // Accumulate the data we just read into the existing data.
91 for (unsigned i = 0; i < NumEntries; ++i) {
92 unsigned Entry = ShouldByteSwap ? ByteSwap_32(TempSpace[i]) : TempSpace[i];
93 Data[i] = AddCounts(Entry, Data[i]);
94 }
95 }
96
97 /// ReadProfilingArgBlock - Read the command line arguments that the progam was
98 /// run with when the current profiling data packet(s) were generated.
99 static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
100 bool ShouldByteSwap,
101 SmallVectorImpl &CommandLines) {
102 // Read the number of bytes ...
103 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
104
105 // Read in the arguments (if there are any to read). Round up the length to
106 // the nearest 4-byte multiple.
107 SmallVector Args(ArgLength+4);
108 if (ArgLength)
109 ReadProfilingData(ToolName, F, Args.data(), (ArgLength+3) & ~3);
110
111 // Store the arguments.
112 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
113 }
114
115 const unsigned ProfileDataLoader::Uncounted = ~0U;
116
117 /// ProfileDataLoader ctor - Read the specified profiling data file, reporting
118 /// a fatal error if the file is invalid or broken.
119 ProfileDataLoader::ProfileDataLoader(const char *ToolName,
120 const std::string &Filename)
121 : Filename(Filename) {
122 FILE *F = fopen(Filename.c_str(), "rb");
123 if (F == 0)
124 report_fatal_error(Twine(ToolName) + ": Error opening '" +
125 Filename + "': ");
126
127 // Keep reading packets until we run out of them.
128 unsigned PacketType;
129 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
130 // If the low eight bits of the packet are zero, we must be dealing with an
131 // endianness mismatch. Byteswap all words read from the profiling
132 // information. This can happen when the compiler host and target have
133 // different endianness.
134 bool ShouldByteSwap = (char)PacketType == 0;
135 PacketType = ShouldByteSwap ? ByteSwap_32(PacketType) : PacketType;
136
137 switch (PacketType) {
138 case ArgumentInfo:
139 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
140 break;
141
142 case EdgeInfo:
143 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
144 break;
145
146 default:
147 report_fatal_error(std::string(ToolName)
148 + ": Unknown profiling packet type");
149 break;
150 }
151 }
152
153 fclose(F);
154 }
+0
-188
lib/Analysis/ProfileDataLoaderPass.cpp less more
None //===- ProfileDataLoaderPass.cpp - Set branch weight metadata from prof ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass loads profiling data from a dump file and sets branch weight
10 // metadata.
11 //
12 // TODO: Replace all "profile-metadata-loader" strings with "profile-loader"
13 // once ProfileInfo etc. has been removed.
14 //
15 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "profile-metadata-loader"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/ProfileDataLoader.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/MDBuilder.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CFG.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34
35 STATISTIC(NumEdgesRead, "The # of edges read.");
36 STATISTIC(NumTermsAnnotated, "The # of terminator instructions annotated.");
37
38 static cl::opt
39 ProfileMetadataFilename("profile-file", cl::init("llvmprof.out"),
40 cl::value_desc("filename"),
41 cl::desc("Profile file loaded by -profile-metadata-loader"));
42
43 namespace {
44 /// This pass loads profiling data from a dump file and sets branch weight
45 /// metadata.
46 class ProfileMetadataLoaderPass : public ModulePass {
47 std::string Filename;
48 public:
49 static char ID; // Class identification, replacement for typeinfo
50 explicit ProfileMetadataLoaderPass(const std::string &filename = "")
51 : ModulePass(ID), Filename(filename) {
52 initializeProfileMetadataLoaderPassPass(*PassRegistry::getPassRegistry());
53 if (filename.empty()) Filename = ProfileMetadataFilename;
54 }
55
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesAll();
58 }
59
60 virtual const char *getPassName() const {
61 return "Profile loader";
62 }
63
64 virtual void readEdge(unsigned, ProfileData&, ProfileData::Edge,
65 ArrayRef);
66 virtual unsigned matchEdges(Module&, ProfileData&, ArrayRef);
67 virtual void setBranchWeightMetadata(Module&, ProfileData&);
68
69 virtual bool runOnModule(Module &M);
70 };
71 } // End of anonymous namespace
72
73 char ProfileMetadataLoaderPass::ID = 0;
74 INITIALIZE_PASS_BEGIN(ProfileMetadataLoaderPass, "profile-metadata-loader",
75 "Load profile information from llvmprof.out", false, true)
76 INITIALIZE_PASS_END(ProfileMetadataLoaderPass, "profile-metadata-loader",
77 "Load profile information from llvmprof.out", false, true)
78
79 char &llvm::ProfileMetadataLoaderPassID = ProfileMetadataLoaderPass::ID;
80
81 /// createProfileMetadataLoaderPass - This function returns a Pass that loads
82 /// the profiling information for the module from the specified filename,
83 /// making it available to the optimizers.
84 ModulePass *llvm::createProfileMetadataLoaderPass() {
85 return new ProfileMetadataLoaderPass();
86 }
87 ModulePass *llvm::createProfileMetadataLoaderPass(const std::string &Filename) {
88 return new ProfileMetadataLoaderPass(Filename);
89 }
90
91 /// readEdge - Take the value from a profile counter and assign it to an edge.
92 void ProfileMetadataLoaderPass::readEdge(unsigned ReadCount,
93 ProfileData &PB, ProfileData::Edge e,
94 ArrayRef Counters) {
95 if (ReadCount >= Counters.size()) return;
96
97 unsigned weight = Counters[ReadCount];
98 assert(weight != ProfileDataLoader::Uncounted);
99 PB.addEdgeWeight(e, weight);
100
101 DEBUG(dbgs() << "-- Read Edge Counter for " << e
102 << " (# "<< (ReadCount) << "): "
103 << PB.getEdgeWeight(e) << "\n");
104 }
105
106 /// matchEdges - Link every profile counter with an edge.
107 unsigned ProfileMetadataLoaderPass::matchEdges(Module &M, ProfileData &PB,
108 ArrayRef Counters) {
109 if (Counters.size() == 0) return 0;
110
111 unsigned ReadCount = 0;
112
113 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
114 if (F->isDeclaration()) continue;
115 DEBUG(dbgs() << "Loading edges in '" << F->getName() << "'\n");
116 readEdge(ReadCount++, PB, PB.getEdge(0, &F->getEntryBlock()), Counters);
117 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
118 TerminatorInst *TI = BB->getTerminator();
119 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
120 readEdge(ReadCount++, PB, PB.getEdge(BB,TI->getSuccessor(s)),
121 Counters);
122 }
123 }
124 }
125
126 return ReadCount;
127 }
128
129 /// setBranchWeightMetadata - Translate the counter values associated with each
130 /// edge into branch weights for each conditional branch (a branch with 2 or
131 /// more desinations).
132 void ProfileMetadataLoaderPass::setBranchWeightMetadata(Module &M,
133 ProfileData &PB) {
134 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
135 if (F->isDeclaration()) continue;
136 DEBUG(dbgs() << "Setting branch metadata in '" << F->getName() << "'\n");
137
138 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
139 TerminatorInst *TI = BB->getTerminator();
140 unsigned NumSuccessors = TI->getNumSuccessors();
141
142 // If there is only one successor then we can not set a branch
143 // probability as the target is certain.
144 if (NumSuccessors < 2) continue;
145
146 // Load the weights of all edges leading from this terminator.
147 DEBUG(dbgs() << "-- Terminator with " << NumSuccessors
148 << " successors:\n");
149 SmallVector Weights(NumSuccessors);
150 for (unsigned s = 0 ; s < NumSuccessors ; ++s) {
151 ProfileData::Edge edge = PB.getEdge(BB, TI->getSuccessor(s));
152 Weights[s] = (uint32_t)PB.getEdgeWeight(edge);
153 DEBUG(dbgs() << "---- Edge '" << edge << "' has weight "
154 << Weights[s] << "\n");
155 }
156
157 // Set branch weight metadata. This will set branch probabilities of
158 // 100%/0% if that is true of the dynamic execution.
159 // BranchProbabilityInfo can account for this when it loads this metadata
160 // (it gives the unexectuted branch a weight of 1 for the purposes of
161 // probability calculations).
162 MDBuilder MDB(TI->getContext());
163 MDNode *Node = MDB.createBranchWeights(Weights);
164 TI->setMetadata(LLVMContext::MD_prof, Node);
165 NumTermsAnnotated++;
166 }
167 }
168 }
169
170 bool ProfileMetadataLoaderPass::runOnModule(Module &M) {
171 ProfileDataLoader PDL("profile-data-loader", Filename);
172 ProfileData PB;
173
174 ArrayRef Counters = PDL.getRawEdgeCounts();
175
176 unsigned ReadCount = matchEdges(M, PB, Counters);
177
178 if (ReadCount != Counters.size()) {
179 errs() << "WARNING: profile information is inconsistent with "
180 << "the current program!\n";
181 }
182 NumEdgesRead = ReadCount;
183
184 setBranchWeightMetadata(M, PB);
185
186 return ReadCount > 0;
187 }
+0
-426
lib/Analysis/ProfileEstimatorPass.cpp less more
None //===- ProfileEstimatorPass.cpp - LLVM Pass to estimate profile info ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a concrete implementation of profiling information that
10 // estimates the profiling information in a very crude and unimaginative way.
11 //
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "profile-estimator"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/ProfileInfo.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
24 static cl::opt
25 LoopWeight(
26 "profile-estimator-loop-weight", cl::init(10),
27 cl::value_desc("loop-weight"),
28 cl::desc("Number of loop executions used for profile-estimator")
29 );
30
31 namespace {
32 class ProfileEstimatorPass : public FunctionPass, public ProfileInfo {
33 double ExecCount;
34 LoopInfo *LI;
35 std::set BBToVisit;
36 std::map LoopExitWeights;
37 std::map MinimalWeight;
38 public:
39 static char ID; // Class identification, replacement for typeinfo
40 explicit ProfileEstimatorPass(const double execcount = 0)
41 : FunctionPass(ID), ExecCount(execcount) {
42 initializeProfileEstimatorPassPass(*PassRegistry::getPassRegistry());
43 if (execcount == 0) ExecCount = LoopWeight;
44 }
45
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 AU.addRequired();
49 }
50
51 virtual const char *getPassName() const {
52 return "Profiling information estimator";
53 }
54
55 /// run - Estimate the profile information from the specified file.
56 virtual bool runOnFunction(Function &F);
57
58 /// getAdjustedAnalysisPointer - This method is used when a pass implements
59 /// an analysis interface through multiple inheritance. If needed, it
60 /// should override this to adjust the this pointer as needed for the
61 /// specified pass info.
62 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
63 if (PI == &ProfileInfo::ID)
64 return (ProfileInfo*)this;
65 return this;
66 }
67
68 virtual void recurseBasicBlock(BasicBlock *BB);
69
70 void inline printEdgeWeight(Edge);
71 };
72 } // End of anonymous namespace
73
74 char ProfileEstimatorPass::ID = 0;
75 INITIALIZE_AG_PASS_BEGIN(ProfileEstimatorPass, ProfileInfo, "profile-estimator",
76 "Estimate profiling information", false, true, false)
77 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
78 INITIALIZE_AG_PASS_END(ProfileEstimatorPass, ProfileInfo, "profile-estimator",
79 "Estimate profiling information", false, true, false)
80
81 namespace llvm {
82 char &ProfileEstimatorPassID = ProfileEstimatorPass::ID;
83
84 FunctionPass *createProfileEstimatorPass() {
85 return new ProfileEstimatorPass();
86 }
87
88 /// createProfileEstimatorPass - This function returns a Pass that estimates
89 /// profiling information using the given loop execution count.
90 Pass *createProfileEstimatorPass(const unsigned execcount) {
91 return new ProfileEstimatorPass(execcount);
92 }
93 }
94
95 static double ignoreMissing(double w) {
96 if (w == ProfileInfo::MissingValue) return 0;
97 return w;
98 }
99
100 static void inline printEdgeError(ProfileInfo::Edge e, const char *M) {
101 DEBUG(dbgs() << "-- Edge " << e << " is not calculated, " << M << "\n");
102 }
103
104 void inline ProfileEstimatorPass::printEdgeWeight(Edge E) {
105 DEBUG(dbgs() << "-- Weight of Edge " << E << ":"
106 << format("%20.20g", getEdgeWeight(E)) << "\n");
107 }
108
109 // recurseBasicBlock() - This calculates the ProfileInfo estimation for a
110 // single block and then recurses into the successors.
111 // The algorithm preserves the flow condition, meaning that the sum of the
112 // weight of the incoming edges must be equal the block weight which must in
113 // turn be equal to the sume of the weights of the outgoing edges.
114 // Since the flow of an block is deterimined from the current state of the
115 // flow, once an edge has a flow assigned this flow is never changed again,
116 // otherwise it would be possible to violate the flow condition in another
117 // block.
118 void ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) {
119
120 // Break the recursion if this BasicBlock was already visited.
121 if (BBToVisit.find(BB) == BBToVisit.end()) return;
122
123 // Read the LoopInfo for this block.
124 bool BBisHeader = LI->isLoopHeader(BB);
125 Loop* BBLoop = LI->getLoopFor(BB);
126
127 // To get the block weight, read all incoming edges.
128 double BBWeight = 0;
129 std::set ProcessedPreds;
130 for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
131 bbi != bbe; ++bbi ) {
132 // If this block was not considered already, add weight.
133 Edge edge = getEdge(*bbi,BB);
134 double w = getEdgeWeight(edge);
135 if (ProcessedPreds.insert(*bbi).second) {
136 BBWeight += ignoreMissing(w);
137 }
138 // If this block is a loop header and the predecessor is contained in this
139 // loop, thus the edge is a backedge, continue and do not check if the
140 // value is valid.
141 if (BBisHeader && BBLoop->contains(*bbi)) {
142 printEdgeError(edge, "but is backedge, continuing");
143 continue;
144 }
145 // If the edges value is missing (and this is no loop header, and this is
146 // no backedge) return, this block is currently non estimatable.
147 if (w == MissingValue) {
148 printEdgeError(edge, "returning");
149 return;
150 }
151 }
152 if (getExecutionCount(BB) != MissingValue) {
153 BBWeight = getExecutionCount(BB);
154 }
155
156 // Fetch all necessary information for current block.
157 SmallVector ExitEdges;
158 SmallVector Edges;
159 if (BBLoop) {
160 BBLoop->getExitEdges(ExitEdges);
161 }
162
163 // If this is a loop header, consider the following:
164 // Exactly the flow that is entering this block, must exit this block too. So
165 // do the following:
166 // *) get all the exit edges, read the flow that is already leaving this
167 // loop, remember the edges that do not have any flow on them right now.
168 // (The edges that have already flow on them are most likely exiting edges of
169 // other loops, do not touch those flows because the previously caclulated
170 // loopheaders would not be exact anymore.)
171 // *) In case there is not a single exiting edge left, create one at the loop
172 // latch to prevent the flow from building up in the loop.
173 // *) Take the flow that is not leaving the loop already and distribute it on
174 // the remaining exiting edges.
175 // (This ensures that all flow that enters the loop also leaves it.)
176 // *) Increase the flow into the loop by increasing the weight of this block.
177 // There is at least one incoming backedge that will bring us this flow later
178 // on. (So that the flow condition in this node is valid again.)
179 if (BBisHeader) {
180 double incoming = BBWeight;
181 // Subtract the flow leaving the loop.
182 std::set ProcessedExits;
183 for (SmallVectorImpl::iterator ei = ExitEdges.begin(),
184 ee = ExitEdges.end(); ei != ee; ++ei) {
185 if (ProcessedExits.insert(*ei).second) {
186 double w = getEdgeWeight(*ei);
187 if (w == MissingValue) {
188 Edges.push_back(*ei);
189 // Check if there is a necessary minimal weight, if yes, subtract it
190 // from weight.
191 if (MinimalWeight.find(*ei) != MinimalWeight.end()) {
192 incoming -= MinimalWeight[*ei];
193 DEBUG(dbgs() << "Reserving " << format("%.20g",MinimalWeight[*ei]) << " at " << (*ei) << "\n");
194 }
195 } else {
196 incoming -= w;
197 }
198 }
199 }
200 // If no exit edges, create one:
201 if (Edges.size() == 0) {
202 BasicBlock *Latch = BBLoop->getLoopLatch();
203 if (Latch) {
204 Edge edge = getEdge(Latch,0);
205 EdgeInformation[BB->getParent()][edge] = BBWeight;
206 printEdgeWeight(edge);
207 edge = getEdge(Latch, BB);
208 EdgeInformation[BB->getParent()][edge] = BBWeight * ExecCount;
209 printEdgeWeight(edge);
210 }
211 }
212
213 // Distribute remaining weight to the exting edges. To prevent fractions
214 // from building up and provoking precision problems the weight which is to
215 // be distributed is split and the rounded, the last edge gets a somewhat
216 // bigger value, but we are close enough for an estimation.
217 double fraction = floor(incoming/Edges.size());
218 for (SmallVectorImpl::iterator ei = Edges.begin(), ee = Edges.end();
219 ei != ee; ++ei) {
220 double w = 0;
221 if (ei != (ee-1)) {
222 w = fraction;
223 incoming -= fraction;
224 } else {
225 w = incoming;
226 }
227 EdgeInformation[BB->getParent()][*ei] += w;
228 // Read necessary minimal weight.
229 if (MinimalWeight.find(*ei) != MinimalWeight.end()) {
230 EdgeInformation[BB->getParent()][*ei] += MinimalWeight[*ei];
231 DEBUG(dbgs() << "Additionally " << format("%.20g",MinimalWeight[*ei]) << " at " << (*ei) << "\n");
232 }
233 printEdgeWeight(*ei);
234
235 // Add minimal weight to paths to all exit edges, this is used to ensure
236 // that enough flow is reaching this edges.
237 Path p;
238 const BasicBlock *Dest = GetPath(BB, (*ei).first, p, GetPathToDest);
239 while (Dest != BB) {
240 const BasicBlock *Parent = p.find(Dest)->second;
241 Edge e = getEdge(Parent, Dest);
242 if (MinimalWeight.find(e) == MinimalWeight.end()) {
243 MinimalWeight[e] = 0;
244 }
245 MinimalWeight[e] += w;
246 DEBUG(dbgs() << "Minimal Weight for " << e << ": " << format("%.20g",MinimalWeight[e]) << "\n");
247 Dest = Parent;
248 }
249 }
250 // Increase flow into the loop.
251 BBWeight *= (ExecCount+1);
252 }
253
254 BlockInformation[BB->getParent()][BB] = BBWeight;
255 // Up until now we considered only the loop exiting edges, now we have a
256 // definite block weight and must distribute this onto the outgoing edges.
257 // Since there may be already flow attached to some of the edges, read this
258 // flow first and remember the edges that have still now flow attached.
259 Edges.clear();
260 std::set ProcessedSuccs;
261
262 succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
263 // Also check for (BB,0) edges that may already contain some flow. (But only
264 // in case there are no successors.)
265 if (bbi == bbe) {
266 Edge edge = getEdge(BB,0);
267 EdgeInformation[BB->getParent()][edge] = BBWeight;
268 printEdgeWeight(edge);
269 }
270 for ( ; bbi != bbe; ++bbi ) {
271 if (ProcessedSuccs.insert(*bbi).second) {
272 Edge edge = getEdge(BB,*bbi);
273 double w = getEdgeWeight(edge);
274 if (w != MissingValue) {
275 BBWeight -= getEdgeWeight(edge);
276 } else {
277 Edges.push_back(edge);
278 // If minimal weight is necessary, reserve weight by subtracting weight
279 // from block weight, this is readded later on.
280 if (MinimalWeight.find(edge) != MinimalWeight.end()) {
281 BBWeight -= MinimalWeight[edge];
282 DEBUG(dbgs() << "Reserving " << format("%.20g",MinimalWeight[edge]) << " at " << edge << "\n");
283 }
284 }
285 }
286 }
287
288 double fraction = Edges.size() ? floor(BBWeight/Edges.size()) : 0.0;
289 // Finally we know what flow is still not leaving the block, distribute this
290 // flow onto the empty edges.
291 for (SmallVectorImpl::iterator ei = Edges.begin(), ee = Edges.end();
292 ei != ee; ++ei) {
293 if (ei != (ee-1)) {
294 EdgeInformation[BB->getParent()][*ei] += fraction;
295 BBWeight -= fraction;
296 } else {
297 EdgeInformation[BB->getParent()][*ei] += BBWeight;
298 }
299 // Readd minial necessary weight.
300 if (MinimalWeight.find(*ei) != MinimalWeight.end()) {
301 EdgeInformation[BB->getParent()][*ei] += MinimalWeight[*ei];
302 DEBUG(dbgs() << "Additionally " << format("%.20g",MinimalWeight[*ei]) << " at " << (*ei) << "\n");
303 }
304 printEdgeWeight(*ei);
305 }
306
307 // This block is visited, mark this before the recursion.
308 BBToVisit.erase(BB);
309
310 // Recurse into successors.
311 for (succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
312 bbi != bbe; ++bbi) {
313 recurseBasicBlock(*bbi);
314 }
315 }
316
317 bool ProfileEstimatorPass::runOnFunction(Function &F) {
318 if (F.isDeclaration()) return false;
319
320 // Fetch LoopInfo and clear ProfileInfo for this function.
321 LI = &getAnalysis();
322 FunctionInformation.erase(&F);
323 BlockInformation[&F].clear();
324 EdgeInformation[&F].clear();
325 BBToVisit.clear();
326
327 // Mark all blocks as to visit.
328 for (Function::iterator bi = F.begin(), be = F.end(); bi != be; ++bi)
329 BBToVisit.insert(bi);
330
331 // Clear Minimal Edges.
332 MinimalWeight.clear();
333
334 DEBUG(dbgs() << "Working on function " << F.getName() << "\n");
335
336 // Since the entry block is the first one and has no predecessors, the edge
337 // (0,entry) is inserted with the starting weight of 1.
338 BasicBlock *entry = &F.getEntryBlock();
339 BlockInformation[&F][entry] = pow(2.0, 32.0);
340 Edge edge = getEdge(0,entry);
341 EdgeInformation[&F][edge] = BlockInformation[&F][entry];
342 printEdgeWeight(edge);
343
344 // Since recurseBasicBlock() maybe returns with a block which was not fully
345 // estimated, use recurseBasicBlock() until everything is calculated.
346 bool cleanup = false;
347 recurseBasicBlock(entry);
348 while (BBToVisit.size() > 0 && !cleanup) {
349 // Remember number of open blocks, this is later used to check if progress
350 // was made.
351 unsigned size = BBToVisit.size();
352
353 // Try to calculate all blocks in turn.
354 for (std::set::iterator bi = BBToVisit.begin(),
355 be = BBToVisit.end(); bi != be; ++bi) {
356 recurseBasicBlock(*bi);
357 // If at least one block was finished, break because iterator may be
358 // invalid.
359 if (BBToVisit.size() < size) break;
360 }
361
362 // If there was not a single block resolved, make some assumptions.
363 if (BBToVisit.size() == size) {
364 bool found = false;
365 for (std::set::iterator BBI = BBToVisit.begin(), BBE = BBToVisit.end();
366 (BBI != BBE) && (!found); ++BBI) {
367 BasicBlock *BB = *BBI;
368 // Try each predecessor if it can be assumend.
369 for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
370 (bbi != bbe) && (!found); ++bbi) {
371 Edge e = getEdge(*bbi,BB);
372 double w = getEdgeWeight(e);
373 // Check that edge from predecessor is still free.
374 if (w == MissingValue) {
375 // Check if there is a circle from this block to predecessor.
376 Path P;
377 const BasicBlock *Dest = GetPath(BB, *bbi, P, GetPathToDest);
378 if (Dest != *bbi) {
379 // If there is no circle, just set edge weight to 0
380 EdgeInformation[&F][e] = 0;
381 DEBUG(dbgs() << "Assuming edge weight: ");
382 printEdgeWeight(e);
383 found = true;
384 }
385 }
386 }
387 }
388 if (!found) {
389 cleanup = true;
390 DEBUG(dbgs() << "No assumption possible in Fuction "<
391 }
392 }
393 }
394 // In case there was no safe way to assume edges, set as a last measure,
395 // set _everything_ to zero.
396 if (cleanup) {
397 FunctionInformation[&F] = 0;
398 BlockInformation[&F].clear();
399 EdgeInformation[&F].clear();
400 for (Function::const_iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
401 const BasicBlock *BB = &(*FI);
402 BlockInformation[&F][BB] = 0;
403 const_pred_iterator predi = pred_begin(BB), prede = pred_end(BB);
404 if (predi == prede) {
405 Edge e = getEdge(0,BB);
406 setEdgeWeight(e,0);
407 }
408 for (;predi != prede; ++predi) {
409 Edge e = getEdge(*predi,BB);
410 setEdgeWeight(e,0);
411 }
412 succ_const_iterator succi = succ_begin(BB), succe = succ_end(BB);
413 if (succi == succe) {
414 Edge e = getEdge(BB,0);
415 setEdgeWeight(e,0);
416 }
417 for (;succi != succe; ++succi) {
418 Edge e = getEdge(*succi,BB);
419 setEdgeWeight(e,0);
420 }
421 }
422 }
423
424 return false;
425 }
+0
-1079
lib/Analysis/ProfileInfo.cpp less more
None //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the abstract ProfileInfo interface, and the default
10 // "no profile" implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "profile-info"
14 #include "llvm/Analysis/ProfileInfo.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CFG.h"
21 #include
22 #include
23 #include
24 using namespace llvm;
25
26 namespace llvm {
27 template<> char ProfileInfoT::ID = 0;
28 }
29
30 // Register the ProfileInfo interface, providing a nice name to refer to.
31 INITIALIZE_ANALYSIS_GROUP(ProfileInfo, "Profile Information", NoProfileInfo)
32
33 namespace llvm {
34
35 template <>
36 ProfileInfoT::ProfileInfoT() {}
37 template <>
38 ProfileInfoT::~ProfileInfoT() {}
39
40 template <>
41 ProfileInfoT::ProfileInfoT() {
42 MachineProfile = 0;
43 }
44 template <>
45 ProfileInfoT::~ProfileInfoT() {
46 if (MachineProfile) delete MachineProfile;
47 }
48
49 template<>
50 char ProfileInfoT::ID = 0;
51
52 template<>
53 const double ProfileInfoT::MissingValue = -1;
54
55 template<> const
56 double ProfileInfoT::MissingValue = -1;
57
58 template<> double
59 ProfileInfoT::getExecutionCount(const BasicBlock *BB) {
60 std::map::iterator J =
61 BlockInformation.find(BB->getParent());
62 if (J != BlockInformation.end()) {
63 BlockCounts::iterator I = J->second.find(BB);
64 if (I != J->second.end())
65 return I->second;
66 }
67
68 double Count = MissingValue;
69
70 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
71
72 // Are there zero predecessors of this block?
73 if (PI == PE) {
74 Edge e = getEdge(0, BB);
75 Count = getEdgeWeight(e);
76 } else {
77 // Otherwise, if there are predecessors, the execution count of this block is
78 // the sum of the edge frequencies from the incoming edges.
79 std::set ProcessedPreds;
80 Count = 0;
81 for (; PI != PE; ++PI) {
82 const BasicBlock *P = *PI;
83 if (ProcessedPreds.insert(P).second) {
84 double w = getEdgeWeight(getEdge(P, BB));
85 if (w == MissingValue) {
86 Count = MissingValue;
87 break;
88 }
89 Count += w;
90 }
91 }
92 }
93
94 // If the predecessors did not suffice to get block weight, try successors.
95 if (Count == MissingValue) {
96
97 succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
98
99 // Are there zero successors of this block?
100 if (SI == SE) {
101 Edge e = getEdge(BB,0);
102 Count = getEdgeWeight(e);
103 } else {
104 std::set ProcessedSuccs;
105 Count = 0;
106 for (; SI != SE; ++SI)
107 if (ProcessedSuccs.insert(*SI).second) {
108 double w = getEdgeWeight(getEdge(BB, *SI));
109 if (w == MissingValue) {
110 Count = MissingValue;
111 break;
112 }
113 Count += w;
114 }
115 }
116 }
117
118 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
119 return Count;
120 }
121
122 template<>
123 double ProfileInfoT::
124 getExecutionCount(const MachineBasicBlock *MBB) {
125 std::map::iterator J =
126 BlockInformation.find(MBB->getParent());
127 if (J != BlockInformation.end()) {
128 BlockCounts::iterator I = J->second.find(MBB);
129 if (I != J->second.end())
130 return I->second;
131 }
132
133 return MissingValue;
134 }
135
136 template<>
137 double ProfileInfoT::getExecutionCount(const Function *F) {
138 std::map::iterator J =
139 FunctionInformation.find(F);
140 if (J != FunctionInformation.end())
141 return J->second;
142
143 // isDeclaration() is checked here and not at start of function to allow
144 // functions without a body still to have a execution count.
145 if (F->isDeclaration()) return MissingValue;
146
147 double Count = getExecutionCount(&F->getEntryBlock());
148 if (Count != MissingValue) FunctionInformation[F] = Count;
149 return Count;
150 }
151
152 template<>
153 double ProfileInfoT::
154 getExecutionCount(const MachineFunction *MF) {
155 std::map::iterator J =
156 FunctionInformation.find(MF);
157 if (J != FunctionInformation.end())
158 return J->second;
159
160 double Count = getExecutionCount(&MF->front());
161 if (Count != MissingValue) FunctionInformation[MF] = Count;
162 return Count;
163 }
164
165 template<>
166 void ProfileInfoT::
167 setExecutionCount(const BasicBlock *BB, double w) {
168 DEBUG(dbgs() << "Creating Block " << BB->getName()
169 << " (weight: " << format("%.20g",w) << ")\n");
170 BlockInformation[BB->getParent()][BB] = w;
171 }
172
173 template<>
174 void ProfileInfoT::
175 setExecutionCount(const MachineBasicBlock *MBB, double w) {
176 DEBUG(dbgs() << "Creating Block " << MBB->getBasicBlock()->getName()
177 << " (weight: " << format("%.20g",w) << ")\n");
178 BlockInformation[MBB->getParent()][MBB] = w;
179 }
180
181 template<>
182 void ProfileInfoT::addEdgeWeight(Edge e, double w) {
183 double oldw = getEdgeWeight(e);
184 assert (oldw != MissingValue && "Adding weight to Edge with no previous weight");
185 DEBUG(dbgs() << "Adding to Edge " << e
186 << " (new weight: " << format("%.20g",oldw + w) << ")\n");
187 EdgeInformation[getFunction(e)][e] = oldw + w;
188 }
189
190 template<>
191 void ProfileInfoT::
192 addExecutionCount(const BasicBlock *BB, double w) {
193 double oldw = getExecutionCount(BB);
194 assert (oldw != MissingValue && "Adding weight to Block with no previous weight");
195 DEBUG(dbgs() << "Adding to Block " << BB->getName()
196 << " (new weight: " << format("%.20g",oldw + w) << ")\n");
197 BlockInformation[BB->getParent()][BB] = oldw + w;
198 }
199
200 template<>
201 void ProfileInfoT::removeBlock(const BasicBlock *BB) {
202 std::map::iterator J =
203 BlockInformation.find(BB->getParent());
204 if (J == BlockInformation.end()) return;
205
206 DEBUG(dbgs() << "Deleting " << BB->getName() << "\n");
207 J->second.erase(BB);
208 }
209
210 template<>
211 void ProfileInfoT::removeEdge(Edge e) {
212 std::map::iterator J =
213 EdgeInformation.find(getFunction(e));
214 if (J == EdgeInformation.end()) return;
215
216 DEBUG(dbgs() << "Deleting" << e << "\n");
217 J->second.erase(e);
218 }
219
220 template<>
221 void ProfileInfoT::
222 replaceEdge(const Edge &oldedge, const Edge &newedge) {
223 double w;
224 if ((w = getEdgeWeight(newedge)) == MissingValue) {
225 w = getEdgeWeight(oldedge);
226 DEBUG(dbgs() << "Replacing " << oldedge << " with " << newedge << "\n");
227 } else {
228 w += getEdgeWeight(oldedge);
229 DEBUG(dbgs() << "Adding " << oldedge << " to " << newedge << "\n");
230 }
231 setEdgeWeight(newedge,w);
232 removeEdge(oldedge);
233 }
234
235 template<>
236 const BasicBlock *ProfileInfoT::
237 GetPath(const BasicBlock *Src, const BasicBlock *Dest,
238 Path &P, unsigned Mode) {
239 const BasicBlock *BB = 0;
240 bool hasFoundPath = false;
241
242 std::queue BFS;
243 BFS.push(Src);
244
245 while(BFS.size() && !hasFoundPath) {
246 BB = BFS.front();
247 BFS.pop();
248
249 succ_const_iterator Succ = succ_begin(BB), End = succ_end(BB);
250 if (Succ == End) {
251 P[(const BasicBlock*)0] = BB;
252 if (Mode & GetPathToExit) {
253 hasFoundPath = true;
254 BB = 0;
255 }
256 }
257 for(;Succ != End; ++Succ) {
258 if (P.find(*Succ) != P.end()) continue;
259 Edge e = getEdge(BB,*Succ);
260 if ((Mode & GetPathWithNewEdges) && (getEdgeWeight(e) != MissingValue)) continue;
261 P[*Succ] = BB;
262 BFS.push(*Succ);
263 if ((Mode & GetPathToDest) && *Succ == Dest) {
264 hasFoundPath = true;
265 BB = *Succ;
266 break;
267 }
268 if ((Mode & GetPathToValue) && (getExecutionCount(*Succ) != MissingValue)) {
269 hasFoundPath = true;
270 BB = *Succ;
271 break;
272 }
273 }
274 }
275
276 return BB;
277 }
278
279 template<>
280 void ProfileInfoT::
281 divertFlow(const Edge &oldedge, const Edge &newedge) {
282 DEBUG(dbgs() << "Diverting " << oldedge << " via " << newedge );
283
284 // First check if the old edge was taken, if not, just delete it...
285 if (getEdgeWeight(oldedge) == 0) {
286 removeEdge(oldedge);
287 return;
288 }
289
290 Path P;
291 P[newedge.first] = 0;
292 P[newedge.second] = newedge.first;
293 const BasicBlock *BB = GetPath(newedge.second,oldedge.second,P,GetPathToExit | GetPathToDest);
294
295 double w = getEdgeWeight (oldedge);
296 DEBUG(dbgs() << ", Weight: " << format("%.20g",w) << "\n");
297 do {
298 const BasicBlock *Parent = P.find(BB)->second;
299 Edge e = getEdge(Parent,BB);
300 double oldw = getEdgeWeight(e);
301 double oldc = getExecutionCount(e.first);
302 setEdgeWeight(e, w+oldw);
303 if (Parent != oldedge.first) {
304 setExecutionCount(e.first, w+oldc);
305 }
306 BB = Parent;
307 } while (BB != newedge.first);
308 removeEdge(oldedge);
309 }
310
311 /// Replaces all occurrences of RmBB in the ProfilingInfo with DestBB.
312 /// This checks all edges of the function the blocks reside in and replaces the
313 /// occurrences of RmBB with DestBB.
314 template<>
315 void ProfileInfoT::
316 replaceAllUses(const BasicBlock *RmBB, const BasicBlock *DestBB) {
317 DEBUG(dbgs() << "Replacing " << RmBB->getName()
318 << " with " << DestBB->getName() << "\n");
319 const Function *F = DestBB->getParent();
320 std::map::iterator J =
321 EdgeInformation.find(F);
322 if (J == EdgeInformation.end()) return;
323
324 Edge e, newedge;
325 bool erasededge = false;
326 EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
327 while(I != E) {
328 e = (I++)->first;
329 bool foundedge = false; bool eraseedge = false;
330 if (e.first == RmBB) {
331 if (e.second == DestBB) {
332 eraseedge = true;
333 } else {
334 newedge = getEdge(DestBB, e.second);
335 foundedge = true;
336 }
337 }
338 if (e.second == RmBB) {
339 if (e.first == DestBB) {
340 eraseedge = true;
341 } else {
342 newedge = getEdge(e.first, DestBB);
343 foundedge = true;
344 }
345 }
346 if (foundedge) {
347 replaceEdge(e, newedge);
348 }
349 if (eraseedge) {
350 if (erasededge) {
351 Edge newedge = getEdge(DestBB, DestBB);
352 replaceEdge(e, newedge);
353 } else {
354 removeEdge(e);
355 erasededge = true;
356 }
357 }
358 }
359 }
360
361 /// Splits an edge in the ProfileInfo and redirects flow over NewBB.
362 /// Since its possible that there is more than one edge in the CFG from FristBB
363 /// to SecondBB its necessary to redirect the flow proporionally.
364 template<>
365 void ProfileInfoT::splitEdge(const BasicBlock *FirstBB,
366 const BasicBlock *SecondBB,
367 const BasicBlock *NewBB,
368 bool MergeIdenticalEdges) {
369 const Function *F = FirstBB->getParent();
370 std::map::iterator J =
371 EdgeInformation.find(F);
372 if (J == EdgeInformation.end()) return;
373
374 // Generate edges and read current weight.
375 Edge e = getEdge(FirstBB, SecondBB);
376 Edge n1 = getEdge(FirstBB, NewBB);
377 Edge n2 = getEdge(NewBB, SecondBB);
378 EdgeWeights &ECs = J->second;
379 double w = ECs[e];
380
381 int succ_count = 0;
382 if (!MergeIdenticalEdges) {
383 // First count the edges from FristBB to SecondBB, if there is more than
384 // one, only slice out a proporional part for NewBB.
385 for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
386 BBI != BBE; ++BBI) {
387 if (*BBI == SecondBB) succ_count++;
388 }
389 // When the NewBB is completely new, increment the count by one so that
390 // the counts are properly distributed.
391 if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
392 } else {
393 // When the edges are merged anyway, then redirect all flow.
394 succ_count = 1;
395 }
396
397 // We know now how many edges there are from FirstBB to SecondBB, reroute a
398 // proportional part of the edge weight over NewBB.
399 double neww = floor(w / succ_count);
400 ECs[n1] += neww;
401 ECs[n2] += neww;
402 BlockInformation[F][NewBB] += neww;
403 if (succ_count == 1) {
404 ECs.erase(e);
405 } else {
406 ECs[e] -= neww;
407 }
408 }
409
410 template<>
411 void ProfileInfoT::splitBlock(const BasicBlock *Old,
412 const BasicBlock* New) {
413 const Function *F = Old->getParent();
414 std::map::iterator J =
415 EdgeInformation.find(F);
416 if (J == EdgeInformation.end()) return;
417
418 DEBUG(dbgs() << "Splitting " << Old->getName() << " to " << New->getName() << "\n");
419
420 std::set Edges;
421 for (EdgeWeights::iterator ewi = J->second.begin(), ewe = J->second.end();
422 ewi != ewe; ++ewi) {
423 Edge old = ewi->first;
424 if (old.first == Old) {
425 Edges.insert(old);
426 }
427 }
428 for (std::set::iterator EI = Edges.begin(), EE = Edges.end();
429 EI != EE; ++EI) {
430 Edge newedge = getEdge(New, EI->second);
431 replaceEdge(*EI, newedge);
432 }
433
434 double w = getExecutionCount(Old);
435 setEdgeWeight(getEdge(Old, New), w);
436 setExecutionCount(New, w);
437 }
438
439 template<>
440 void ProfileInfoT::splitBlock(const BasicBlock *BB,
441 const BasicBlock* NewBB,
442 BasicBlock *const *Preds,
443 unsigned NumPreds) {
444 const Function *F = BB->getParent();
445 std::map::iterator J =
446 EdgeInformation.find(F);
447 if (J == EdgeInformation.end()) return;
448
449 DEBUG(dbgs() << "Splitting " << NumPreds << " Edges from " << BB->getName()
450 << " to " << NewBB->getName() << "\n");
451
452 // Collect weight that was redirected over NewBB.
453 double newweight = 0;
454
455 std::set ProcessedPreds;
456 // For all requestes Predecessors.
457 for (unsigned pred = 0; pred < NumPreds; ++pred) {
458 const BasicBlock * Pred = Preds[pred];
459 if (ProcessedPreds.insert(Pred).second) {
460 // Create edges and read old weight.
461 Edge oldedge = getEdge(Pred, BB);
462 Edge newedge = getEdge(Pred, NewBB);
463
464 // Remember how much weight was redirected.
465 newweight += getEdgeWeight(oldedge);
466
467 replaceEdge(oldedge,newedge);
468 }
469 }
470
471 Edge newedge = getEdge(NewBB,BB);
472 setEdgeWeight(newedge, newweight);
473 setExecutionCount(NewBB, newweight);
474 }