1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declaration of BasicBlockSubGraph class.
16 :
17 : #ifndef SYZYGY_BLOCK_GRAPH_BASIC_BLOCK_SUBGRAPH_H_
18 : #define SYZYGY_BLOCK_GRAPH_BASIC_BLOCK_SUBGRAPH_H_
19 :
20 : #include <map>
21 : #include <set>
22 : #include <string>
23 :
24 : #include "base/basictypes.h"
25 : #include "base/string_piece.h"
26 : #include "syzygy/block_graph/basic_block.h"
27 : #include "syzygy/block_graph/block_graph.h"
28 :
29 : namespace block_graph {
30 :
31 : // A basic-block sub-graph describes the make-up and layout of one or
32 : // more blocks as a set of code, data, and/or padding basic-blocks. Optionally,
33 : // it holds a pointer to a block from which the sub-graph was originally
34 : // derived.
35 : //
36 : // In manipulating the basic block sub-graph, note that the sub-graph
37 : // acts as a basic-block factory and retains ownership of all basic-blocks
38 : // that participate in the composition.
39 : class BasicBlockSubGraph {
40 : public:
41 : typedef block_graph::BasicBlock BasicBlock;
42 : typedef block_graph::BasicCodeBlock BasicCodeBlock;
43 : typedef block_graph::BasicDataBlock BasicDataBlock;
44 : typedef BasicBlock::BasicBlockType BasicBlockType;
45 : typedef std::list<BasicBlock*> BasicBlockOrdering;
46 : typedef block_graph::BlockGraph BlockGraph;
47 : typedef BlockGraph::Block Block;
48 : typedef BlockGraph::BlockType BlockType;
49 : typedef BlockGraph::Offset Offset;
50 : typedef BlockGraph::SectionId SectionId;
51 : typedef BlockGraph::Size Size;
52 : typedef BlockGraph::BlockAttributes BlockAttributes;
53 :
54 : // A structure describing a block (its properties, attributes, and
55 : // constituent ordered basic-blocks). A given basic-block may participate
56 : // in at most one BlockDescription at any time.
57 : struct BlockDescription {
58 : std::string name;
59 : std::string compiland_name;
60 : BlockType type;
61 : SectionId section;
62 : Size alignment;
63 : BlockAttributes attributes;
64 : BasicBlockOrdering basic_block_order;
65 : };
66 :
67 : typedef BlockGraph::BlockId BlockId;
68 : typedef std::list<BlockDescription> BlockDescriptionList;
69 : typedef std::set<BasicBlock*, BasicBlockIdLess> BBCollection;
70 : typedef std::map<const BasicBlock*, bool> ReachabilityMap;
71 :
72 : // Initialize a basic block sub-graph.
73 : BasicBlockSubGraph();
74 : // Releases all resources.
75 : ~BasicBlockSubGraph();
76 :
77 : // @name Accessors.
78 : // @{
79 E : void set_original_block(const Block* block) { original_block_ = block; }
80 E : const Block* original_block() const { return original_block_; }
81 :
82 E : const BBCollection& basic_blocks() const { return basic_blocks_; }
83 E : BBCollection& basic_blocks() { return basic_blocks_; }
84 :
85 E : const BlockDescriptionList& block_descriptions() const {
86 E : return block_descriptions_;
87 E : }
88 E : BlockDescriptionList& block_descriptions() { return block_descriptions_; }
89 : // @}
90 :
91 : // Initializes and returns a new block description.
92 : // @param name The name of the block.
93 : // @param compiland The name of the compiland associated with this block.
94 : // @param type The type of the block.
95 : // @param section The ID of the section in which the block should reside.
96 : // @param alignment The alignment of the block.
97 : // (i.e., location % alignment == 0)
98 : // @param attributes The attributes of the block.
99 : // @returns A pointer to the newly created block description.
100 : BlockDescription* AddBlockDescription(const base::StringPiece& name,
101 : const base::StringPiece& compiland,
102 : BlockType type,
103 : SectionId section,
104 : Size alignment,
105 : BlockAttributes attributes);
106 :
107 : // Add a new basic code block to the sub-graph.
108 : // @param name A textual identifier for this basic block.
109 : // @returns A pointer to a newly allocated basic code block.
110 : BasicCodeBlock* AddBasicCodeBlock(const base::StringPiece& name);
111 :
112 : // Add a new basic data block to the sub-graph.
113 : // @param name A textual identifier for this basic block.
114 : // @param size The number of bytes this basic block occupied in the original
115 : // block. Set to 0 if this is a generated basic block.
116 : // @param data The underlying data representing the basic data block.
117 : // @returns A pointer to a newly allocated basic data block representing the
118 : // original source range [@p offset, @p offset + @p size), or NULL on
119 : // ERROR. Ownership of the returned basic-block (if any) is retained
120 : // by the composition.
121 : BasicDataBlock* AddBasicDataBlock(const base::StringPiece& name,
122 : Size size,
123 : const uint8* data);
124 :
125 : // Remove a basic block from the subgraph.
126 : // @param bb The basic block to remove.
127 : // @pre @p bb must be in the graph.
128 : void Remove(BasicBlock* bb);
129 :
130 : // Returns true if the basic-block composition is valid. This tests the
131 : // for following conditions.
132 : // 1. Each basic-block is used in at most one BlockDescription.
133 : // 2. Each code basic-block has valid successors.
134 : // 3. If there is an original block, then each of it's referrers is accounted
135 : // for in the new composition.
136 : bool IsValid() const;
137 :
138 : // Traverses the basic-block subgraph and computes the reachability of all
139 : // basic-blocks starting from the entry-point.
140 : void GetReachabilityMap(ReachabilityMap* rm) const;
141 :
142 : // A helper function for querying a reachability map.
143 : static bool IsReachable(const ReachabilityMap& rm, const BasicBlock* bb);
144 :
145 : // Dump a text representation of this subgraph.
146 : // @param buf receives the text representation.
147 : // @returns true if this subgraph was successfully dumped, false otherwise.
148 : bool ToString(std::string* buf) const;
149 :
150 : protected:
151 : // @name Validation Functions.
152 : // @{
153 : bool MapsBasicBlocksToAtMostOneDescription() const;
154 : bool HasValidSuccessors() const;
155 : bool HasValidReferrers() const;
156 : // @}
157 :
158 : // The original block corresponding from which this sub-graph derives. This
159 : // is optional, and may be NULL.
160 : const Block* original_block_;
161 :
162 : // The set of basic blocks in this sub-graph. This includes any basic-blocks
163 : // created during the initial decomposition process, as well as any additional
164 : // basic-blocks synthesized thereafter.
165 : BBCollection basic_blocks_;
166 :
167 : // A list of block descriptions for the blocks that are to be created from
168 : // this basic block sub-graph.
169 : BlockDescriptionList block_descriptions_;
170 :
171 : // Our block ID allocator.
172 : BlockId next_block_id_;
173 : };
174 :
175 : } // namespace block_graph
176 :
177 : #endif // SYZYGY_BLOCK_GRAPH_BASIC_BLOCK_SUBGRAPH_H_
|