1 : // Copyright 2013 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/optimize/transforms/basic_block_reordering_transform.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/block_graph/basic_block_decomposer.h"
20 : #include "syzygy/block_graph/block_builder.h"
21 : #include "syzygy/block_graph/block_graph.h"
22 : #include "syzygy/pe/pe_transform_policy.h"
23 :
24 m : namespace optimize {
25 m : namespace transforms {
26 :
27 m : namespace {
28 :
29 m : using block_graph::BasicBlockDecomposer;
30 m : using block_graph::BlockBuilder;
31 m : using pe::ImageLayout;
32 :
33 m : class BasicBlockReorderingTransformTest : public testing::Test {
34 m : public:
35 m : BasicBlockReorderingTransformTest()
36 m : : image_(&block_graph_),
37 m : profile_(&image_) {
38 m : }
39 :
40 m : void ApplyTransform(BlockGraph::Block** block);
41 :
42 m : protected:
43 m : pe::PETransformPolicy policy_;
44 m : BlockGraph block_graph_;
45 m : ImageLayout image_;
46 m : BasicBlockReorderingTransform tx_;
47 m : ApplicationProfile profile_;
48 m : SubGraphProfile subgraph_profile_;
49 m : };
50 :
51 m : void BasicBlockReorderingTransformTest::ApplyTransform(
52 m : BlockGraph::Block** block) {
53 : // Decompose to subgraph.
54 m : BasicBlockSubGraph subgraph;
55 m : BasicBlockDecomposer decomposer(*block, &subgraph);
56 m : ASSERT_TRUE(decomposer.Decompose());
57 :
58 : // Apply block transform.
59 m : ASSERT_TRUE(
60 m : tx_.TransformBasicBlockSubGraph(&policy_, &block_graph_, &subgraph,
61 m : &profile_, &subgraph_profile_));
62 :
63 : // Rebuild block.
64 m : BlockBuilder builder(&block_graph_);
65 m : ASSERT_TRUE(builder.Merge(&subgraph));
66 m : CHECK_EQ(1u, builder.new_blocks().size());
67 m : *block = *builder.new_blocks().begin();
68 m : }
69 :
70 m : } // namespace
71 :
72 m : } // namespace transforms
73 m : } // namespace optimize
|