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 : BlockType type;
60 : SectionId section;
61 : Size alignment;
62 : BlockAttributes attributes;
63 : BasicBlockOrdering basic_block_order;
64 : };
65 :
66 : typedef std::list<BlockDescription> BlockDescriptionList;
67 : typedef std::set<BasicBlock*> BBCollection;
68 : typedef std::map<const BasicBlock*, bool> ReachabilityMap;
69 :
70 : // Initialize a basic block sub-graph.
71 : BasicBlockSubGraph();
72 : // Releases all resources.
73 : ~BasicBlockSubGraph();
74 :
75 : // @name Accessors.
76 : // @{
77 E : void set_original_block(const Block* block) { original_block_ = block; }
78 E : const Block* original_block() const { return original_block_; }
79 :
80 E : const BBCollection& basic_blocks() const { return basic_blocks_; }
81 E : BBCollection& basic_blocks() { return basic_blocks_; }
82 :
83 E : const BlockDescriptionList& block_descriptions() const {
84 E : return block_descriptions_;
85 E : }
86 E : BlockDescriptionList& block_descriptions() { return block_descriptions_; }
87 : // @}
88 :
89 : // Initializes and returns a new block description.
90 : // @param name The name of the block.
91 : // @param type The type of the block.
92 : // @param section The ID of the section in which the block should reside.
93 : // @param alignment The alignment of the block.
94 : // (i.e., location % alignment == 0)
95 : // @param attributes The attributes of the block.
96 : // @returns A pointer to the newly created block description.
97 : BlockDescription* AddBlockDescription(const base::StringPiece& name,
98 : BlockType type,
99 : SectionId section,
100 : Size alignment,
101 : BlockAttributes attributes);
102 :
103 : // Add a new basic code block to the sub-graph.
104 : // @param name A textual identifier for this basic block.
105 : // @returns A pointer to a newly allocated basic code block.
106 : BasicCodeBlock* AddBasicCodeBlock(const base::StringPiece& name);
107 :
108 : // Add a new basic data block to the sub-graph.
109 : // @param name A textual identifier for this basic block.
110 : // @param size The number of bytes this basic block occupied in the original
111 : // block. Set to 0 if this is a generated basic block.
112 : // @param data The underlying data representing the basic data block.
113 : // @returns A pointer to a newly allocated basic data block representing the
114 : // original source range [@p offset, @p offset + @p size), or NULL on
115 : // ERROR. Ownership of the returned basic-block (if any) is retained
116 : // by the composition.
117 : BasicDataBlock* AddBasicDataBlock(const base::StringPiece& name,
118 : Size size,
119 : const uint8* data);
120 :
121 : // Remove a basic block from the subgraph.
122 : // @param bb The basic block to remove.
123 : // @pre @p bb must be in the graph.
124 : void Remove(BasicBlock* bb);
125 :
126 : // Returns true if the basic-block composition is valid. This tests the
127 : // for following conditions.
128 : // 1. Each basic-block is used in at most one BlockDescription.
129 : // 2. Each code basic-block has valid successors.
130 : // 3. If there is an original block, then each of it's referrers is accounted
131 : // for in the new composition.
132 : bool IsValid() const;
133 :
134 : // Traverses the basic-block subgraph and computes the reachability of all
135 : // basic-blocks starting from the entry-point.
136 : void GetReachabilityMap(ReachabilityMap* rm) const;
137 :
138 : // A helper function for querying a reachability map.
139 : static bool IsReachable(const ReachabilityMap& rm, const BasicBlock* bb);
140 :
141 : // Dump a text representation of this subgraph.
142 : // @param buf receives the text representation.
143 : // @returns true if this subgraph was successfully dumped, false otherwise.
144 : bool ToString(std::string* buf) const;
145 :
146 : protected:
147 : // @name Validation Functions.
148 : // @{
149 : bool MapsBasicBlocksToAtMostOneDescription() const;
150 : bool HasValidSuccessors() const;
151 : bool HasValidReferrers() const;
152 : // @}
153 :
154 : // The original block corresponding from which this sub-graph derives. This
155 : // is optional, and may be NULL.
156 : const Block* original_block_;
157 :
158 : // The set of basic blocks in this sub-graph. This includes any basic-blocks
159 : // created during the initial decomposition process, as well as any additional
160 : // basic-blocks synthesized thereafter.
161 : BBCollection basic_blocks_;
162 :
163 : // A list of block descriptions for the blocks that are to be created from
164 : // this basic block sub-graph.
165 : BlockDescriptionList block_descriptions_;
166 : };
167 :
168 : } // namespace block_graph
169 :
170 : #endif // SYZYGY_BLOCK_GRAPH_BASIC_BLOCK_SUBGRAPH_H_
|