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 : // Declares the ExplodeBasicBlockSubGraphTransform and
16 : // ExplodeBasicBlocksTransform classes. These transforms separate all
17 : // of the basic-blocks in a subgraph or block-graph respectively, into
18 : // individual code and data blocks. This is primarily a test to exercise
19 : // the basic-block motion machinery.
20 :
21 : #ifndef SYZYGY_PE_TRANSFORMS_EXPLODE_BASIC_BLOCKS_TRANSFORM_H_
22 : #define SYZYGY_PE_TRANSFORMS_EXPLODE_BASIC_BLOCKS_TRANSFORM_H_
23 :
24 : #include "syzygy/block_graph/transforms/iterative_transform.h"
25 : #include "syzygy/block_graph/transforms/named_transform.h"
26 :
27 : namespace pe {
28 : namespace transforms {
29 :
30 : // A BasicBlockSubBlockGraph transform that explodes all basic-blocks in a
31 : // basic_block subgraph into individual code or data blocks.
32 : class ExplodeBasicBlockSubGraphTransform
33 : : public block_graph::transforms::NamedBasicBlockSubGraphTransformImpl<
34 : ExplodeBasicBlockSubGraphTransform> {
35 : public:
36 : typedef block_graph::BlockGraph BlockGraph;
37 : typedef block_graph::BasicBlockSubGraph BasicBlockSubGraph;
38 :
39 : // Initialize a new ExplodeBasicBlockSubGraphTransform instance.
40 : explicit ExplodeBasicBlockSubGraphTransform(bool exclude_padding);
41 :
42 : // @name BasicBlockSubGraphTransformInterface methods.
43 : // @{
44 : virtual bool TransformBasicBlockSubGraph(
45 : BlockGraph* block_graph,
46 : BasicBlockSubGraph* basic_block_subgraph) OVERRIDE;
47 : // @}
48 :
49 : // The transform name.
50 : static const char kTransformName[];
51 :
52 : // @name Accessors.
53 : // @{
54 E : size_t output_code_blocks() const { return output_code_blocks_; }
55 E : size_t output_data_blocks() const { return output_data_blocks_; }
56 : // @}
57 :
58 : protected:
59 : // A flag for whether padding (and dead code) basic-blocks should be excluded
60 : // when reconstituting the exploded blocks.
61 : bool exclude_padding_;
62 : size_t output_code_blocks_;
63 : size_t output_data_blocks_;
64 :
65 : DISALLOW_COPY_AND_ASSIGN(ExplodeBasicBlockSubGraphTransform);
66 : };
67 :
68 : // A BlockGraph transform that, for every code block which is eligible for
69 : // decomposition to basic-blocks, and transforms every basic-blocks in each
70 : // code block into an individual code or data block.
71 : class ExplodeBasicBlocksTransform
72 : : public block_graph::transforms::IterativeTransformImpl<
73 : ExplodeBasicBlocksTransform> {
74 : public:
75 : typedef block_graph::BlockGraph BlockGraph;
76 :
77 : // Initialize a new ExplodeBasicBlocksTransform instance.
78 : ExplodeBasicBlocksTransform();
79 :
80 : // Explodes each basic code block in @p block referenced by into separate
81 : // blocks, then erases @p block from @p block_graph.
82 : // @param block_graph The block graph being modified.
83 : // @param block The block to explode, this must be in @p block_graph.
84 : // @note This method is required by the IterativeTransformImpl parent class.
85 : bool OnBlock(BlockGraph* block_graph, BlockGraph::Block* block);
86 :
87 : // Logs metrics about the performed transform.
88 : bool PostBlockGraphIteration(BlockGraph* block_graph,
89 : BlockGraph::Block* header_block);
90 :
91 : // @name Accessors.
92 : // @{
93 : bool exclude_padding() const { return exclude_padding_; }
94 E : void set_exclude_padding(bool value) { exclude_padding_ = value; }
95 : // @}
96 :
97 : // The transform name.
98 : static const char kTransformName[];
99 :
100 : protected:
101 : // Hooks for unit-testing.
102 : virtual bool SkipThisBlock(const BlockGraph::Block* candidate);
103 :
104 : // A flag for whether padding (and dead code) basic-blocks should be excluded
105 : // when reconstituting the exploded blocks.
106 : bool exclude_padding_;
107 :
108 : // Statistics on blocks encountered and generated.
109 : size_t non_decomposable_code_blocks_;
110 : size_t skipped_code_blocks_;
111 : size_t input_code_blocks_;
112 : size_t output_code_blocks_;
113 : size_t output_data_blocks_;
114 :
115 : DISALLOW_COPY_AND_ASSIGN(ExplodeBasicBlocksTransform);
116 : };
117 :
118 : } // namespace transforms
119 : } // namespace pe
120 :
121 : #endif // SYZYGY_PE_TRANSFORMS_EXPLODE_BASIC_BLOCKS_TRANSFORM_H_
|