1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "slicer/control_flow_graph.h"
18 #include "slicer/chronometer.h"
19 
20 namespace lir {
21 
Finish()22 std::vector<BasicBlock> BasicBlocksVisitor::Finish() {
23   // the .dex format specification has the following constraint:
24   //
25   //  B17	The last reachable instruction of a method must either be a
26   //  backwards goto or branch, a return, or a throw instruction. It must not
27   //  be possible to leave the insns array at the bottom.	4.8.2.20
28   //
29   // NOTE: this is a very aggressive check though since in the LIR we also
30   //  have labels, annotations, directives, etc. For example it's possible to have
31   //  debug annotations (.line, .endlocal, ...) after the last bytecode.
32   //
33   SLICER_WEAK_CHECK(state_ == State::Outside);
34   SLICER_CHECK(state_ != State::BlockBody);
35   current_block_.region = {};
36   state_ = State::Outside;
37   return std::move(basic_blocks_);
38 }
39 
Visit(Bytecode * bytecode)40 bool BasicBlocksVisitor::Visit(Bytecode* bytecode) {
41   switch (state_) {
42     case State::Outside:
43       StartBlock(bytecode);
44       state_ = State::BlockBody;
45       break;
46     case State::BlockHeader:
47       state_ = State::BlockBody;
48       break;
49     case State::BlockBody:
50       // inside basic block body, nothing to do.
51       break;
52   }
53 
54   // terminate the current block?
55   bool terminate_block = false;
56   const auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
57   if (model_exceptions_) {
58     constexpr auto exit_instr_flags =
59         dex::kInstrCanBranch |
60         dex::kInstrCanSwitch |
61         dex::kInstrCanThrow |
62         dex::kInstrCanReturn;
63     terminate_block = (flags & exit_instr_flags) != 0;
64   } else {
65     constexpr auto exit_instr_flags =
66         dex::kInstrCanBranch |
67         dex::kInstrCanSwitch |
68         dex::kInstrCanReturn;
69     terminate_block = bytecode->opcode == dex::OP_THROW || (flags & exit_instr_flags) != 0;
70   }
71   if (terminate_block) {
72       EndBlock(bytecode);
73   }
74 
75   return true;
76 }
77 
Visit(Label * label)78 bool BasicBlocksVisitor::Visit(Label* label) {
79   switch (state_) {
80     case State::Outside:
81       StartBlock(label);
82       break;
83     case State::BlockBody:
84       EndBlock(label->prev);
85       StartBlock(label);
86       break;
87     case State::BlockHeader:
88       break;
89   }
90   return true;
91 }
92 
HandleAnnotation(Instruction * instr)93 bool BasicBlocksVisitor::HandleAnnotation(Instruction* instr) {
94   if (state_ == State::Outside) {
95     StartBlock(instr);
96   }
97   return true;
98 }
99 
SkipInstruction(Instruction * instr)100 bool BasicBlocksVisitor::SkipInstruction(Instruction* instr) {
101   if (state_ != State::Outside) {
102     EndBlock(instr->prev);
103   }
104   return true;
105 }
106 
StartBlock(Instruction * instr)107 void BasicBlocksVisitor::StartBlock(Instruction* instr) {
108   assert(instr != nullptr);
109   assert(state_ == State::Outside);
110   // mark the location of the "first" instruction,
111   // "last" will be set when we end the basic block.
112   current_block_.region.first = instr;
113   current_block_.region.last = nullptr;
114   state_ = State::BlockHeader;
115 }
116 
EndBlock(Instruction * instr)117 void BasicBlocksVisitor::EndBlock(Instruction* instr) {
118   assert(instr != nullptr);
119   if (state_ == State::BlockBody) {
120     ++current_block_.id;
121     assert(current_block_.region.first != nullptr);
122     current_block_.region.last = instr;
123     basic_blocks_.push_back(current_block_);
124   } else {
125     assert(state_ == State::BlockHeader);
126   }
127   current_block_.region = {};
128   state_ = State::Outside;
129 }
130 
CreateBasicBlocks(bool model_exceptions)131 void ControlFlowGraph::CreateBasicBlocks(bool model_exceptions) {
132   BasicBlocksVisitor visitor(model_exceptions);
133   for (auto instr : code_ir->instructions) {
134     instr->Accept(&visitor);
135   }
136   basic_blocks = visitor.Finish();
137 }
138 
139 }  // namespace lir
140