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 : #include "syzygy/block_graph/transform.h"
16 :
17 : #include "syzygy/block_graph/basic_block_decomposer.h"
18 : #include "syzygy/block_graph/block_builder.h"
19 : #include "syzygy/block_graph/block_util.h"
20 :
21 : namespace block_graph {
22 :
23 : bool ApplyBlockGraphTransform(BlockGraphTransformInterface* transform,
24 : BlockGraph* block_graph,
25 E : BlockGraph::Block* header_block) {
26 E : DCHECK(transform != NULL);
27 E : DCHECK(transform->name() != NULL);
28 E : DCHECK_GT(strlen(transform->name()), 0u);
29 E : DCHECK(block_graph != NULL);
30 E : DCHECK(header_block != NULL);
31 :
32 : // Get the ID of the header block. As a sanity check we want to ensure
33 : // that it still exists after the transform.
34 E : BlockGraph::BlockId header_block_id = header_block->id();
35 :
36 E : if (!transform->TransformBlockGraph(block_graph, header_block)) {
37 E : LOG(ERROR) << "Transform \"" << transform->name() << "\" failed.";
38 E : return false;
39 : }
40 :
41 : // Ensure that the header block still exists. If it was changed, it needs
42 : // to have been changed in place.
43 E : BlockGraph::Block* block = block_graph->GetBlockById(header_block_id);
44 E : if (block == NULL) {
45 E : LOG(ERROR) << "Header block not found after \"" << transform->name()
46 : << "\" transform.";
47 E : return false;
48 : }
49 E : DCHECK_EQ(header_block, block);
50 :
51 E : return true;
52 E : }
53 :
54 : bool ApplyBasicBlockSubGraphTransform(
55 : BasicBlockSubGraphTransformInterface* transform,
56 : BlockGraph* block_graph,
57 : BlockGraph::Block* block,
58 E : BlockVector* new_blocks) {
59 E : DCHECK(transform != NULL);
60 E : DCHECK(block_graph != NULL);
61 E : DCHECK(block != NULL);
62 E : DCHECK_EQ(BlockGraph::CODE_BLOCK, block->type());
63 E : DCHECK(CodeBlockAttributesAreBasicBlockSafe(block));
64 :
65 : // Decompose block to basic blocks.
66 E : BasicBlockSubGraph subgraph;
67 E : BasicBlockDecomposer bb_decomposer(block, &subgraph);
68 E : if (!bb_decomposer.Decompose())
69 i : return false;
70 :
71 : // Call the transform.
72 E : if (!transform->TransformBasicBlockSubGraph(block_graph, &subgraph))
73 E : return false;
74 :
75 : // Update the block-graph post transform.
76 E : BlockBuilder builder(block_graph);
77 E : if (!builder.Merge(&subgraph))
78 i : return false;
79 :
80 E : if (new_blocks != NULL) {
81 : new_blocks->assign(builder.new_blocks().begin(),
82 E : builder.new_blocks().end());
83 : }
84 :
85 E : return true;
86 E : }
87 :
88 : } // namespace block_graph
|