Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_COMPILER_OPTIMIZING_NODES_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_NODES_H_ |
| 19 | |
| 20 | #include "utils/allocation.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 21 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "utils/growable_array.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class HBasicBlock; |
| 27 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 28 | class HIntConstant; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 29 | class HGraphVisitor; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 30 | class LocationSummary; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 31 | |
| 32 | static const int kDefaultNumberOfBlocks = 8; |
| 33 | static const int kDefaultNumberOfSuccessors = 2; |
| 34 | static const int kDefaultNumberOfPredecessors = 2; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 35 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 36 | |
| 37 | // Control-flow graph of a method. Contains a list of basic blocks. |
| 38 | class HGraph : public ArenaObject { |
| 39 | public: |
| 40 | explicit HGraph(ArenaAllocator* arena) |
| 41 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 42 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 43 | dominator_order_(arena, kDefaultNumberOfBlocks), |
| 44 | current_instruction_id_(0) { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 45 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 46 | ArenaAllocator* GetArena() const { return arena_; } |
| 47 | const GrowableArray<HBasicBlock*>* GetBlocks() const { return &blocks_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 48 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 50 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 51 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 52 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 53 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 54 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 55 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 56 | void BuildDominatorTree(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 57 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 58 | int GetNextInstructionId() { |
| 59 | return current_instruction_id_++; |
| 60 | } |
| 61 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 62 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 63 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
| 64 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 65 | HBasicBlock* predecessor, |
| 66 | GrowableArray<size_t>* visits); |
| 67 | void FindBackEdges(ArenaBitVector* visited) const; |
| 68 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 69 | ArenaBitVector* visited, |
| 70 | ArenaBitVector* visiting) const; |
| 71 | void RemoveDeadBlocks(const ArenaBitVector& visited) const; |
| 72 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 73 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 74 | |
| 75 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 76 | GrowableArray<HBasicBlock*> blocks_; |
| 77 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 78 | // List of blocks to perform a pre-order dominator tree traversal. |
| 79 | GrowableArray<HBasicBlock*> dominator_order_; |
| 80 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 81 | HBasicBlock* entry_block_; |
| 82 | HBasicBlock* exit_block_; |
| 83 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 84 | // The current id to assign to a newly added instruction. See HInstruction.id_. |
| 85 | int current_instruction_id_; |
| 86 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 88 | }; |
| 89 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 90 | class HLoopInformation : public ArenaObject { |
| 91 | public: |
| 92 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 93 | : header_(header), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 94 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges) { } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 95 | |
| 96 | void AddBackEdge(HBasicBlock* back_edge) { |
| 97 | back_edges_.Add(back_edge); |
| 98 | } |
| 99 | |
| 100 | int NumberOfBackEdges() const { |
| 101 | return back_edges_.Size(); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | HBasicBlock* header_; |
| 106 | GrowableArray<HBasicBlock*> back_edges_; |
| 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 109 | }; |
| 110 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 111 | // A block in a method. Contains the list of instructions represented |
| 112 | // as a double linked list. Each block knows its predecessors and |
| 113 | // successors. |
| 114 | class HBasicBlock : public ArenaObject { |
| 115 | public: |
| 116 | explicit HBasicBlock(HGraph* graph) |
| 117 | : graph_(graph), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 118 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 119 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 120 | first_instruction_(nullptr), |
| 121 | last_instruction_(nullptr), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 122 | loop_information_(nullptr), |
| 123 | dominator_(nullptr), |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 124 | block_id_(-1) { } |
| 125 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 126 | const GrowableArray<HBasicBlock*>* GetPredecessors() const { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 127 | return &predecessors_; |
| 128 | } |
| 129 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 130 | const GrowableArray<HBasicBlock*>* GetSuccessors() const { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 131 | return &successors_; |
| 132 | } |
| 133 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 134 | void AddBackEdge(HBasicBlock* back_edge) { |
| 135 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 136 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 137 | } |
| 138 | loop_information_->AddBackEdge(back_edge); |
| 139 | } |
| 140 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 141 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 142 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 143 | int GetBlockId() const { return block_id_; } |
| 144 | void SetBlockId(int id) { block_id_ = id; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 145 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 146 | HBasicBlock* GetDominator() const { return dominator_; } |
| 147 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 148 | |
| 149 | int NumberOfBackEdges() const { |
| 150 | return loop_information_ == nullptr |
| 151 | ? 0 |
| 152 | : loop_information_->NumberOfBackEdges(); |
| 153 | } |
| 154 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 155 | HInstruction* GetFirstInstruction() const { return first_instruction_; } |
| 156 | HInstruction* GetLastInstruction() const { return last_instruction_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 157 | |
| 158 | void AddSuccessor(HBasicBlock* block) { |
| 159 | successors_.Add(block); |
| 160 | block->predecessors_.Add(this); |
| 161 | } |
| 162 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 163 | void RemovePredecessor(HBasicBlock* block) { |
| 164 | predecessors_.Delete(block); |
| 165 | } |
| 166 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 167 | void AddInstruction(HInstruction* instruction); |
| 168 | |
| 169 | private: |
| 170 | HGraph* const graph_; |
| 171 | GrowableArray<HBasicBlock*> predecessors_; |
| 172 | GrowableArray<HBasicBlock*> successors_; |
| 173 | HInstruction* first_instruction_; |
| 174 | HInstruction* last_instruction_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 175 | HLoopInformation* loop_information_; |
| 176 | HBasicBlock* dominator_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 177 | int block_id_; |
| 178 | |
| 179 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 180 | }; |
| 181 | |
| 182 | #define FOR_EACH_INSTRUCTION(M) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 183 | M(Equal) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 184 | M(Exit) \ |
| 185 | M(Goto) \ |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 186 | M(If) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 187 | M(IntConstant) \ |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame^] | 188 | M(InvokeStatic) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 189 | M(LoadLocal) \ |
| 190 | M(Local) \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 191 | M(Return) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 192 | M(ReturnVoid) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 193 | M(StoreLocal) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 194 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 195 | #define FORWARD_DECLARATION(type) class H##type; |
| 196 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 197 | #undef FORWARD_DECLARATION |
| 198 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 199 | #define DECLARE_INSTRUCTION(type) \ |
| 200 | virtual void Accept(HGraphVisitor* visitor); \ |
| 201 | virtual const char* DebugName() const { return #type; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 202 | virtual H##type* As##type() { return this; } \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 203 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 204 | class HUseListNode : public ArenaObject { |
| 205 | public: |
| 206 | HUseListNode(HInstruction* instruction, HUseListNode* tail) |
| 207 | : instruction_(instruction), tail_(tail) { } |
| 208 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 209 | HUseListNode* GetTail() const { return tail_; } |
| 210 | HInstruction* GetInstruction() const { return instruction_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 211 | |
| 212 | private: |
| 213 | HInstruction* const instruction_; |
| 214 | HUseListNode* const tail_; |
| 215 | |
| 216 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 217 | }; |
| 218 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 219 | class HInstruction : public ArenaObject { |
| 220 | public: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 221 | HInstruction() |
| 222 | : previous_(nullptr), |
| 223 | next_(nullptr), |
| 224 | block_(nullptr), |
| 225 | id_(-1), |
| 226 | uses_(nullptr), |
| 227 | locations_(nullptr) { } |
| 228 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 229 | virtual ~HInstruction() { } |
| 230 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 231 | HInstruction* GetNext() const { return next_; } |
| 232 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 233 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 234 | HBasicBlock* GetBlock() const { return block_; } |
| 235 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 236 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 237 | virtual intptr_t InputCount() const = 0; |
| 238 | virtual HInstruction* InputAt(intptr_t i) const = 0; |
| 239 | |
| 240 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 241 | virtual const char* DebugName() const = 0; |
| 242 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 243 | void AddUse(HInstruction* user) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 244 | uses_ = new (block_->GetGraph()->GetArena()) HUseListNode(user, uses_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 247 | HUseListNode* GetUses() const { return uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 248 | |
| 249 | bool HasUses() const { return uses_ != nullptr; } |
| 250 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 251 | int GetId() const { return id_; } |
| 252 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 253 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 254 | LocationSummary* GetLocations() const { return locations_; } |
| 255 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 256 | |
| 257 | #define INSTRUCTION_TYPE_CHECK(type) \ |
| 258 | virtual H##type* As##type() { return nullptr; } |
| 259 | |
| 260 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 261 | #undef INSTRUCTION_TYPE_CHECK |
| 262 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 263 | private: |
| 264 | HInstruction* previous_; |
| 265 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 266 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 267 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 268 | // An instruction gets an id when it is added to the graph. |
| 269 | // It reflects creation order. A negative id means the instruction |
| 270 | // has not beed added to the graph. |
| 271 | int id_; |
| 272 | |
| 273 | HUseListNode* uses_; |
| 274 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 275 | // Set by the code generator. |
| 276 | LocationSummary* locations_; |
| 277 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 278 | friend class HBasicBlock; |
| 279 | |
| 280 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 281 | }; |
| 282 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 283 | class HUseIterator : public ValueObject { |
| 284 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 285 | explicit HUseIterator(HInstruction* instruction) : current_(instruction->GetUses()) { } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 286 | |
| 287 | bool Done() const { return current_ == nullptr; } |
| 288 | |
| 289 | void Advance() { |
| 290 | DCHECK(!Done()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 291 | current_ = current_->GetTail(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | HInstruction* Current() const { |
| 295 | DCHECK(!Done()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 296 | return current_->GetInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private: |
| 300 | HUseListNode* current_; |
| 301 | |
| 302 | friend class HValue; |
| 303 | }; |
| 304 | |
| 305 | class HInputIterator : public ValueObject { |
| 306 | public: |
| 307 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { } |
| 308 | |
| 309 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 310 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 311 | void Advance() { index_++; } |
| 312 | |
| 313 | private: |
| 314 | HInstruction* instruction_; |
| 315 | int index_; |
| 316 | |
| 317 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 318 | }; |
| 319 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 320 | class HInstructionIterator : public ValueObject { |
| 321 | public: |
| 322 | explicit HInstructionIterator(HBasicBlock* block) |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 323 | : instruction_(block->GetFirstInstruction()) { |
| 324 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 327 | bool Done() const { return instruction_ == nullptr; } |
| 328 | HInstruction* Current() const { return instruction_; } |
| 329 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 330 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 331 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | private: |
| 335 | HInstruction* instruction_; |
| 336 | HInstruction* next_; |
| 337 | }; |
| 338 | |
| 339 | // An embedded container with N elements of type T. Used (with partial |
| 340 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 341 | template<typename T, intptr_t N> |
| 342 | class EmbeddedArray { |
| 343 | public: |
| 344 | EmbeddedArray() : elements_() { } |
| 345 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 346 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 347 | |
| 348 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 349 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 350 | return elements_[i]; |
| 351 | } |
| 352 | |
| 353 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 354 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 355 | return elements_[i]; |
| 356 | } |
| 357 | |
| 358 | const T& At(intptr_t i) const { |
| 359 | return (*this)[i]; |
| 360 | } |
| 361 | |
| 362 | void SetAt(intptr_t i, const T& val) { |
| 363 | (*this)[i] = val; |
| 364 | } |
| 365 | |
| 366 | private: |
| 367 | T elements_[N]; |
| 368 | }; |
| 369 | |
| 370 | template<typename T> |
| 371 | class EmbeddedArray<T, 0> { |
| 372 | public: |
| 373 | intptr_t length() const { return 0; } |
| 374 | const T& operator[](intptr_t i) const { |
| 375 | LOG(FATAL) << "Unreachable"; |
| 376 | static T sentinel = 0; |
| 377 | return sentinel; |
| 378 | } |
| 379 | T& operator[](intptr_t i) { |
| 380 | LOG(FATAL) << "Unreachable"; |
| 381 | static T sentinel = 0; |
| 382 | return sentinel; |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | template<intptr_t N> |
| 387 | class HTemplateInstruction: public HInstruction { |
| 388 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 389 | HTemplateInstruction<N>() : inputs_() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 390 | virtual ~HTemplateInstruction() { } |
| 391 | |
| 392 | virtual intptr_t InputCount() const { return N; } |
| 393 | virtual HInstruction* InputAt(intptr_t i) const { return inputs_[i]; } |
| 394 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 395 | protected: |
| 396 | void SetRawInputAt(intptr_t i, HInstruction* instruction) { |
| 397 | inputs_[i] = instruction; |
| 398 | } |
| 399 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 400 | private: |
| 401 | EmbeddedArray<HInstruction*, N> inputs_; |
| 402 | }; |
| 403 | |
| 404 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 405 | // instruction that branches to the exit block. |
| 406 | class HReturnVoid : public HTemplateInstruction<0> { |
| 407 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 408 | HReturnVoid() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 409 | |
| 410 | DECLARE_INSTRUCTION(ReturnVoid) |
| 411 | |
| 412 | private: |
| 413 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 414 | }; |
| 415 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 416 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 417 | // instruction that branches to the exit block. |
| 418 | class HReturn : public HTemplateInstruction<1> { |
| 419 | public: |
| 420 | explicit HReturn(HInstruction* value) { |
| 421 | SetRawInputAt(0, value); |
| 422 | } |
| 423 | |
| 424 | DECLARE_INSTRUCTION(Return) |
| 425 | |
| 426 | private: |
| 427 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 428 | }; |
| 429 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 430 | // The exit instruction is the only instruction of the exit block. |
| 431 | // Instructions aborting the method (HTrow and HReturn) must branch to the |
| 432 | // exit block. |
| 433 | class HExit : public HTemplateInstruction<0> { |
| 434 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 435 | HExit() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 436 | |
| 437 | DECLARE_INSTRUCTION(Exit) |
| 438 | |
| 439 | private: |
| 440 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 441 | }; |
| 442 | |
| 443 | // Jumps from one block to another. |
| 444 | class HGoto : public HTemplateInstruction<0> { |
| 445 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 446 | HGoto() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 447 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 448 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 449 | return GetBlock()->GetSuccessors()->Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 452 | DECLARE_INSTRUCTION(Goto) |
| 453 | |
| 454 | private: |
| 455 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 456 | }; |
| 457 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 458 | // Conditional branch. A block ending with an HIf instruction must have |
| 459 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 460 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 461 | public: |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 462 | explicit HIf(HInstruction* input) { |
| 463 | SetRawInputAt(0, input); |
| 464 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 465 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 466 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 467 | return GetBlock()->GetSuccessors()->Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 471 | return GetBlock()->GetSuccessors()->Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 474 | DECLARE_INSTRUCTION(If) |
| 475 | |
| 476 | private: |
| 477 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 478 | }; |
| 479 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 480 | // Instruction to check if two inputs are equal to each other. |
| 481 | class HEqual : public HTemplateInstruction<2> { |
| 482 | public: |
| 483 | HEqual(HInstruction* first, HInstruction* second) { |
| 484 | SetRawInputAt(0, first); |
| 485 | SetRawInputAt(1, second); |
| 486 | } |
| 487 | |
| 488 | DECLARE_INSTRUCTION(Equal) |
| 489 | |
| 490 | private: |
| 491 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 492 | }; |
| 493 | |
| 494 | // A local in the graph. Corresponds to a Dex register. |
| 495 | class HLocal : public HTemplateInstruction<0> { |
| 496 | public: |
| 497 | explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { } |
| 498 | |
| 499 | DECLARE_INSTRUCTION(Local) |
| 500 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 501 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 502 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 503 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 504 | // The Dex register number. |
| 505 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 506 | |
| 507 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 508 | }; |
| 509 | |
| 510 | // Load a given local. The local is an input of this instruction. |
| 511 | class HLoadLocal : public HTemplateInstruction<1> { |
| 512 | public: |
| 513 | explicit HLoadLocal(HLocal* local) { |
| 514 | SetRawInputAt(0, local); |
| 515 | } |
| 516 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 517 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 518 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 519 | DECLARE_INSTRUCTION(LoadLocal) |
| 520 | |
| 521 | private: |
| 522 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 523 | }; |
| 524 | |
| 525 | // Store a value in a given local. This instruction has two inputs: the value |
| 526 | // and the local. |
| 527 | class HStoreLocal : public HTemplateInstruction<2> { |
| 528 | public: |
| 529 | HStoreLocal(HLocal* local, HInstruction* value) { |
| 530 | SetRawInputAt(0, local); |
| 531 | SetRawInputAt(1, value); |
| 532 | } |
| 533 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 534 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 535 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 536 | DECLARE_INSTRUCTION(StoreLocal) |
| 537 | |
| 538 | private: |
| 539 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 540 | }; |
| 541 | |
| 542 | // Constants of the type int. Those can be from Dex instructions, or |
| 543 | // synthesized (for example with the if-eqz instruction). |
| 544 | class HIntConstant : public HTemplateInstruction<0> { |
| 545 | public: |
| 546 | explicit HIntConstant(int32_t value) : value_(value) { } |
| 547 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 548 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 549 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 550 | DECLARE_INSTRUCTION(IntConstant) |
| 551 | |
| 552 | private: |
| 553 | const int32_t value_; |
| 554 | |
| 555 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 556 | }; |
| 557 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame^] | 558 | class HInvoke : public HInstruction { |
| 559 | public: |
| 560 | HInvoke(ArenaAllocator* arena, uint32_t number_of_arguments, int32_t dex_pc) |
| 561 | : inputs_(arena, number_of_arguments), |
| 562 | dex_pc_(dex_pc) { |
| 563 | inputs_.SetSize(number_of_arguments); |
| 564 | } |
| 565 | |
| 566 | virtual intptr_t InputCount() const { return inputs_.Size(); } |
| 567 | virtual HInstruction* InputAt(intptr_t i) const { return inputs_.Get(i); } |
| 568 | |
| 569 | int32_t GetDexPc() const { return dex_pc_; } |
| 570 | |
| 571 | protected: |
| 572 | GrowableArray<HInstruction*> inputs_; |
| 573 | const int32_t dex_pc_; |
| 574 | |
| 575 | private: |
| 576 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 577 | }; |
| 578 | |
| 579 | class HInvokeStatic : public HInvoke { |
| 580 | public: |
| 581 | HInvokeStatic(ArenaAllocator* arena, uint32_t number_of_arguments, int32_t dex_pc, int32_t index_in_dex_cache) |
| 582 | : HInvoke(arena, number_of_arguments, dex_pc), index_in_dex_cache_(index_in_dex_cache) { } |
| 583 | |
| 584 | uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; } |
| 585 | |
| 586 | DECLARE_INSTRUCTION(InvokeStatic) |
| 587 | |
| 588 | private: |
| 589 | uint32_t index_in_dex_cache_; |
| 590 | |
| 591 | DISALLOW_COPY_AND_ASSIGN(HInvokeStatic); |
| 592 | }; |
| 593 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 594 | class HGraphVisitor : public ValueObject { |
| 595 | public: |
| 596 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) { } |
| 597 | virtual ~HGraphVisitor() { } |
| 598 | |
| 599 | virtual void VisitInstruction(HInstruction* instruction) { } |
| 600 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 601 | |
| 602 | void VisitInsertionOrder(); |
| 603 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 604 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 605 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 606 | // Visit functions for instruction classes. |
| 607 | #define DECLARE_VISIT_INSTRUCTION(name) \ |
| 608 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 609 | |
| 610 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 611 | |
| 612 | #undef DECLARE_VISIT_INSTRUCTION |
| 613 | |
| 614 | private: |
| 615 | HGraph* graph_; |
| 616 | |
| 617 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 618 | }; |
| 619 | |
| 620 | } // namespace art |
| 621 | |
| 622 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |