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