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