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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 20 | #include "base/arena_object.h" |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 21 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 22 | #include "handle.h" |
| 23 | #include "handle_scope.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 24 | #include "invoke_type.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 25 | #include "locations.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 26 | #include "mirror/class.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 27 | #include "offsets.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 28 | #include "primitive.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 29 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 30 | #include "utils/growable_array.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 34 | class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 35 | class HBasicBlock; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 36 | class HEnvironment; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 37 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 38 | class HIntConstant; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 39 | class HInvoke; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 40 | class HGraphVisitor; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 41 | class HNullConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 42 | class HPhi; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 43 | class HSuspendCheck; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 44 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 45 | class LocationSummary; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 46 | |
| 47 | static const int kDefaultNumberOfBlocks = 8; |
| 48 | static const int kDefaultNumberOfSuccessors = 2; |
| 49 | static const int kDefaultNumberOfPredecessors = 2; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 50 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 51 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 52 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 53 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 54 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 55 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 56 | enum IfCondition { |
| 57 | kCondEQ, |
| 58 | kCondNE, |
| 59 | kCondLT, |
| 60 | kCondLE, |
| 61 | kCondGT, |
| 62 | kCondGE, |
| 63 | }; |
| 64 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 65 | class HInstructionList { |
| 66 | public: |
| 67 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 68 | |
| 69 | void AddInstruction(HInstruction* instruction); |
| 70 | void RemoveInstruction(HInstruction* instruction); |
| 71 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 72 | // Return true if this list contains `instruction`. |
| 73 | bool Contains(HInstruction* instruction) const; |
| 74 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 75 | // Return true if `instruction1` is found before `instruction2` in |
| 76 | // this instruction list and false otherwise. Abort if none |
| 77 | // of these instructions is found. |
| 78 | bool FoundBefore(const HInstruction* instruction1, |
| 79 | const HInstruction* instruction2) const; |
| 80 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 81 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 82 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 83 | |
| 84 | // Update the block of all instructions to be `block`. |
| 85 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 86 | |
| 87 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
| 88 | void Add(const HInstructionList& instruction_list); |
| 89 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 90 | private: |
| 91 | HInstruction* first_instruction_; |
| 92 | HInstruction* last_instruction_; |
| 93 | |
| 94 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 95 | friend class HGraph; |
| 96 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 97 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 98 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 101 | }; |
| 102 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 103 | // Control-flow graph of a method. Contains a list of basic blocks. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 104 | class HGraph : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 105 | public: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 106 | HGraph(ArenaAllocator* arena, int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 107 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 108 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 109 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 110 | entry_block_(nullptr), |
| 111 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 112 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 113 | number_of_vregs_(0), |
| 114 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 115 | temporaries_vreg_slots_(0), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 116 | current_instruction_id_(start_instruction_id) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 117 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 118 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 119 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 120 | HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 121 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 122 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 123 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 124 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 125 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 126 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 127 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 128 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 129 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 130 | // Try building the SSA form of this graph, with dominance computation and loop |
| 131 | // recognition. Returns whether it was successful in doing all these steps. |
| 132 | bool TryBuildingSsa() { |
| 133 | BuildDominatorTree(); |
| 134 | TransformToSsa(); |
| 135 | return AnalyzeNaturalLoops(); |
| 136 | } |
| 137 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 138 | void BuildDominatorTree(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 139 | void TransformToSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 140 | void SimplifyCFG(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 141 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 142 | // Analyze all natural loops in this graph. Returns false if one |
| 143 | // loop is not natural, that is the header does not dominate the |
| 144 | // back edge. |
| 145 | bool AnalyzeNaturalLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 146 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 147 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
| 148 | void InlineInto(HGraph* outer_graph, HInvoke* invoke); |
| 149 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 150 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 151 | void SimplifyLoop(HBasicBlock* header); |
| 152 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 153 | int32_t GetNextInstructionId() { |
| 154 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 155 | return current_instruction_id_++; |
| 156 | } |
| 157 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 158 | int32_t GetCurrentInstructionId() const { |
| 159 | return current_instruction_id_; |
| 160 | } |
| 161 | |
| 162 | void SetCurrentInstructionId(int32_t id) { |
| 163 | current_instruction_id_ = id; |
| 164 | } |
| 165 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 166 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 167 | return maximum_number_of_out_vregs_; |
| 168 | } |
| 169 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 170 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 171 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 174 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 175 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 178 | size_t GetTemporariesVRegSlots() const { |
| 179 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 182 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 183 | number_of_vregs_ = number_of_vregs; |
| 184 | } |
| 185 | |
| 186 | uint16_t GetNumberOfVRegs() const { |
| 187 | return number_of_vregs_; |
| 188 | } |
| 189 | |
| 190 | void SetNumberOfInVRegs(uint16_t value) { |
| 191 | number_of_in_vregs_ = value; |
| 192 | } |
| 193 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 194 | uint16_t GetNumberOfLocalVRegs() const { |
| 195 | return number_of_vregs_ - number_of_in_vregs_; |
| 196 | } |
| 197 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 198 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 199 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 200 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 201 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 202 | HNullConstant* GetNullConstant(); |
| 203 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 204 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 205 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
| 206 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 207 | HBasicBlock* predecessor, |
| 208 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 209 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 210 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 211 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 212 | ArenaBitVector* visiting); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 213 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 214 | void RemoveDeadBlocks(const ArenaBitVector& visited) const; |
| 215 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 216 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 217 | |
| 218 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 219 | GrowableArray<HBasicBlock*> blocks_; |
| 220 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 221 | // List of blocks to perform a reverse post order tree traversal. |
| 222 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 223 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 224 | HBasicBlock* entry_block_; |
| 225 | HBasicBlock* exit_block_; |
| 226 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 227 | // The maximum number of virtual registers arguments passed to a HInvoke in this graph. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 228 | uint16_t maximum_number_of_out_vregs_; |
| 229 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 230 | // The number of virtual registers in this method. Contains the parameters. |
| 231 | uint16_t number_of_vregs_; |
| 232 | |
| 233 | // The number of virtual registers used by parameters of this method. |
| 234 | uint16_t number_of_in_vregs_; |
| 235 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 236 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 237 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 238 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 239 | // The current id to assign to a newly added instruction. See HInstruction.id_. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 240 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 241 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 242 | // Cached null constant that might be created when building SSA form. |
| 243 | HNullConstant* cached_null_constant_; |
| 244 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 245 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 246 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 247 | }; |
| 248 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 249 | class HLoopInformation : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 250 | public: |
| 251 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 252 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 253 | suspend_check_(nullptr), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 254 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 255 | // Make bit vector growable, as the number of blocks may change. |
| 256 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 257 | |
| 258 | HBasicBlock* GetHeader() const { |
| 259 | return header_; |
| 260 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 261 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 262 | void SetHeader(HBasicBlock* block) { |
| 263 | header_ = block; |
| 264 | } |
| 265 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 266 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 267 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 268 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 269 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 270 | void AddBackEdge(HBasicBlock* back_edge) { |
| 271 | back_edges_.Add(back_edge); |
| 272 | } |
| 273 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 274 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 275 | back_edges_.Delete(back_edge); |
| 276 | } |
| 277 | |
| 278 | bool IsBackEdge(HBasicBlock* block) { |
| 279 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 280 | if (back_edges_.Get(i) == block) return true; |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 285 | size_t NumberOfBackEdges() const { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 286 | return back_edges_.Size(); |
| 287 | } |
| 288 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 289 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 290 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 291 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 292 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 295 | void ClearBackEdges() { |
| 296 | back_edges_.Reset(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 299 | // Find blocks that are part of this loop. Returns whether the loop is a natural loop, |
| 300 | // that is the header dominates the back edge. |
| 301 | bool Populate(); |
| 302 | |
| 303 | // Returns whether this loop information contains `block`. |
| 304 | // Note that this loop information *must* be populated before entering this function. |
| 305 | bool Contains(const HBasicBlock& block) const; |
| 306 | |
| 307 | // Returns whether this loop information is an inner loop of `other`. |
| 308 | // Note that `other` *must* be populated before entering this function. |
| 309 | bool IsIn(const HLoopInformation& other) const; |
| 310 | |
| 311 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 312 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 313 | void Add(HBasicBlock* block); |
| 314 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 315 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 316 | // Internal recursive implementation of `Populate`. |
| 317 | void PopulateRecursive(HBasicBlock* block); |
| 318 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 319 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 320 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 321 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 322 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 323 | |
| 324 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 325 | }; |
| 326 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 327 | static constexpr size_t kNoLifetime = -1; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 328 | static constexpr uint32_t kNoDexPc = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 329 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 330 | // A block in a method. Contains the list of instructions represented |
| 331 | // as a double linked list. Each block knows its predecessors and |
| 332 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 333 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 334 | class HBasicBlock : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 335 | public: |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 336 | explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 337 | : graph_(graph), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 338 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 339 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 340 | loop_information_(nullptr), |
| 341 | dominator_(nullptr), |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 342 | dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 343 | block_id_(-1), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 344 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 345 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 346 | lifetime_end_(kNoLifetime), |
| 347 | is_catch_block_(false) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 348 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 349 | const GrowableArray<HBasicBlock*>& GetPredecessors() const { |
| 350 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 353 | const GrowableArray<HBasicBlock*>& GetSuccessors() const { |
| 354 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 357 | const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const { |
| 358 | return dominated_blocks_; |
| 359 | } |
| 360 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 361 | bool IsEntryBlock() const { |
| 362 | return graph_->GetEntryBlock() == this; |
| 363 | } |
| 364 | |
| 365 | bool IsExitBlock() const { |
| 366 | return graph_->GetExitBlock() == this; |
| 367 | } |
| 368 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 369 | void AddBackEdge(HBasicBlock* back_edge) { |
| 370 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 371 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 372 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 373 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 374 | loop_information_->AddBackEdge(back_edge); |
| 375 | } |
| 376 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 377 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 378 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 379 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 380 | int GetBlockId() const { return block_id_; } |
| 381 | void SetBlockId(int id) { block_id_ = id; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 382 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 383 | HBasicBlock* GetDominator() const { return dominator_; } |
| 384 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 385 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 386 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
| 387 | for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) { |
| 388 | if (dominated_blocks_.Get(i) == existing) { |
| 389 | dominated_blocks_.Put(i, new_block); |
| 390 | return; |
| 391 | } |
| 392 | } |
| 393 | LOG(FATAL) << "Unreachable"; |
| 394 | UNREACHABLE(); |
| 395 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 396 | |
| 397 | int NumberOfBackEdges() const { |
| 398 | return loop_information_ == nullptr |
| 399 | ? 0 |
| 400 | : loop_information_->NumberOfBackEdges(); |
| 401 | } |
| 402 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 403 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 404 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 405 | const HInstructionList& GetInstructions() const { return instructions_; } |
| 406 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 407 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 408 | |
| 409 | void AddSuccessor(HBasicBlock* block) { |
| 410 | successors_.Add(block); |
| 411 | block->predecessors_.Add(this); |
| 412 | } |
| 413 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 414 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 415 | size_t successor_index = GetSuccessorIndexOf(existing); |
| 416 | DCHECK_NE(successor_index, static_cast<size_t>(-1)); |
| 417 | existing->RemovePredecessor(this); |
| 418 | new_block->predecessors_.Add(this); |
| 419 | successors_.Put(successor_index, new_block); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 422 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 423 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
| 424 | DCHECK_NE(predecessor_index, static_cast<size_t>(-1)); |
| 425 | existing->RemoveSuccessor(this); |
| 426 | new_block->successors_.Add(this); |
| 427 | predecessors_.Put(predecessor_index, new_block); |
| 428 | } |
| 429 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 430 | void RemovePredecessor(HBasicBlock* block) { |
| 431 | predecessors_.Delete(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 434 | void RemoveSuccessor(HBasicBlock* block) { |
| 435 | successors_.Delete(block); |
| 436 | } |
| 437 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 438 | void ClearAllPredecessors() { |
| 439 | predecessors_.Reset(); |
| 440 | } |
| 441 | |
| 442 | void AddPredecessor(HBasicBlock* block) { |
| 443 | predecessors_.Add(block); |
| 444 | block->successors_.Add(this); |
| 445 | } |
| 446 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 447 | void SwapPredecessors() { |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 448 | DCHECK_EQ(predecessors_.Size(), 2u); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 449 | HBasicBlock* temp = predecessors_.Get(0); |
| 450 | predecessors_.Put(0, predecessors_.Get(1)); |
| 451 | predecessors_.Put(1, temp); |
| 452 | } |
| 453 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 454 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) { |
| 455 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
| 456 | if (predecessors_.Get(i) == predecessor) { |
| 457 | return i; |
| 458 | } |
| 459 | } |
| 460 | return -1; |
| 461 | } |
| 462 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 463 | size_t GetSuccessorIndexOf(HBasicBlock* successor) { |
| 464 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 465 | if (successors_.Get(i) == successor) { |
| 466 | return i; |
| 467 | } |
| 468 | } |
| 469 | return -1; |
| 470 | } |
| 471 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 472 | // Split the block into two blocks just after `cursor`. Returns the newly |
| 473 | // created block. Note that this method just updates raw block information, |
| 474 | // like predecessors, successors, dominators, and instruction list. It does not |
| 475 | // update the graph, reverse post order, loop information, nor make sure the |
| 476 | // blocks are consistent (for example ending with a control flow instruction). |
| 477 | HBasicBlock* SplitAfter(HInstruction* cursor); |
| 478 | |
| 479 | // Merge `other` at the end of `this`. Successors and dominated blocks of |
| 480 | // `other` are changed to be successors and dominated blocks of `this`. Note |
| 481 | // that this method does not update the graph, reverse post order, loop |
| 482 | // information, nor make sure the blocks are consistent (for example ending |
| 483 | // with a control flow instruction). |
| 484 | void MergeWith(HBasicBlock* other); |
| 485 | |
| 486 | // Replace `this` with `other`. Predecessors, successors, and dominated blocks |
| 487 | // of `this` are moved to `other`. |
| 488 | // Note that this method does not update the graph, reverse post order, loop |
| 489 | // information, nor make sure the blocks are consistent (for example ending |
| 490 | void ReplaceWith(HBasicBlock* other); |
| 491 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 492 | void AddInstruction(HInstruction* instruction); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 493 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 494 | // Replace instruction `initial` with `replacement` within this block. |
| 495 | void ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 496 | HInstruction* replacement); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 497 | void AddPhi(HPhi* phi); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 498 | void InsertPhiAfter(HPhi* instruction, HPhi* cursor); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 499 | // RemoveInstruction and RemovePhi delete a given instruction from the respective |
| 500 | // instruction list. With 'ensure_safety' set to true, it verifies that the |
| 501 | // instruction is not in use and removes it from the use lists of its inputs. |
| 502 | void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true); |
| 503 | void RemovePhi(HPhi* phi, bool ensure_safety = true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 504 | |
| 505 | bool IsLoopHeader() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 506 | return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 507 | } |
| 508 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 509 | bool IsLoopPreHeaderFirstPredecessor() const { |
| 510 | DCHECK(IsLoopHeader()); |
| 511 | DCHECK(!GetPredecessors().IsEmpty()); |
| 512 | return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader(); |
| 513 | } |
| 514 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 515 | HLoopInformation* GetLoopInformation() const { |
| 516 | return loop_information_; |
| 517 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 518 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 519 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 520 | // loop_information if it is an outer loop of the passed loop information. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 521 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 522 | void SetInLoop(HLoopInformation* info) { |
| 523 | if (IsLoopHeader()) { |
| 524 | // Nothing to do. This just means `info` is an outer loop. |
| 525 | } else if (loop_information_ == nullptr) { |
| 526 | loop_information_ = info; |
| 527 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 528 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 529 | // Note that a non loop header having a loop information means this loop information |
| 530 | // has already been populated |
| 531 | loop_information_ = info; |
| 532 | } else { |
| 533 | // Block is part of an inner loop. Do not update the loop information. |
| 534 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 535 | // at this point, because this method is being called while populating `info`. |
| 536 | } |
| 537 | } |
| 538 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 539 | // Raw update of the loop information. |
| 540 | void SetLoopInformation(HLoopInformation* info) { |
| 541 | loop_information_ = info; |
| 542 | } |
| 543 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 544 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 545 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 546 | // Returns wheter this block dominates the blocked passed as parameter. |
| 547 | bool Dominates(HBasicBlock* block) const; |
| 548 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 549 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 550 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 551 | |
| 552 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 553 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 554 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 555 | uint32_t GetDexPc() const { return dex_pc_; } |
| 556 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 557 | bool IsCatchBlock() const { return is_catch_block_; } |
| 558 | void SetIsCatchBlock() { is_catch_block_ = true; } |
| 559 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 560 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 561 | HGraph* graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 562 | GrowableArray<HBasicBlock*> predecessors_; |
| 563 | GrowableArray<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 564 | HInstructionList instructions_; |
| 565 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 566 | HLoopInformation* loop_information_; |
| 567 | HBasicBlock* dominator_; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 568 | GrowableArray<HBasicBlock*> dominated_blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 569 | int block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 570 | // The dex program counter of the first instruction of this block. |
| 571 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 572 | size_t lifetime_start_; |
| 573 | size_t lifetime_end_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 574 | bool is_catch_block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 575 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 576 | friend class HGraph; |
| 577 | friend class HInstruction; |
| 578 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 579 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 580 | }; |
| 581 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 582 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 583 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 584 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 585 | M(ArrayGet, Instruction) \ |
| 586 | M(ArrayLength, Instruction) \ |
| 587 | M(ArraySet, Instruction) \ |
| 588 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 589 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 590 | M(CheckCast, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 591 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 592 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 593 | M(Condition, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 594 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 595 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 596 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 597 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 598 | M(Exit, Instruction) \ |
| 599 | M(FloatConstant, Constant) \ |
| 600 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 601 | M(GreaterThan, Condition) \ |
| 602 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 603 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 604 | M(InstanceFieldGet, Instruction) \ |
| 605 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 606 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 607 | M(IntConstant, Constant) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 608 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 609 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 610 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 611 | M(LessThan, Condition) \ |
| 612 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 613 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 614 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 615 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 616 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 617 | M(Local, Instruction) \ |
| 618 | M(LongConstant, Constant) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 619 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 620 | M(Mul, BinaryOperation) \ |
| 621 | M(Neg, UnaryOperation) \ |
| 622 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 623 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 624 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 625 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 626 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 627 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 628 | M(Or, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 629 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 630 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 631 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 632 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 633 | M(Return, Instruction) \ |
| 634 | M(ReturnVoid, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 635 | M(Shl, BinaryOperation) \ |
| 636 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 637 | M(StaticFieldGet, Instruction) \ |
| 638 | M(StaticFieldSet, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 639 | M(StoreLocal, Instruction) \ |
| 640 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 641 | M(SuspendCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 642 | M(Temporary, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 643 | M(Throw, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 644 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 645 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 646 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 647 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 648 | #define FOR_EACH_INSTRUCTION(M) \ |
| 649 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 650 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 651 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 652 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 653 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 654 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 655 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 656 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 657 | #undef FORWARD_DECLARATION |
| 658 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 659 | #define DECLARE_INSTRUCTION(type) \ |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 660 | virtual InstructionKind GetKind() const { return k##type; } \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 661 | virtual const char* DebugName() const { return #type; } \ |
| 662 | virtual const H##type* As##type() const OVERRIDE { return this; } \ |
| 663 | virtual H##type* As##type() OVERRIDE { return this; } \ |
| 664 | virtual bool InstructionTypeEquals(HInstruction* other) const { \ |
| 665 | return other->Is##type(); \ |
| 666 | } \ |
| 667 | virtual void Accept(HGraphVisitor* visitor) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 668 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 669 | template <typename T> class HUseList; |
| 670 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 671 | template <typename T> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 672 | class HUseListNode : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 673 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 674 | HUseListNode* GetPrevious() const { return prev_; } |
| 675 | HUseListNode* GetNext() const { return next_; } |
| 676 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 677 | size_t GetIndex() const { return index_; } |
| 678 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 679 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 680 | HUseListNode(T user, size_t index) |
| 681 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 682 | |
| 683 | T const user_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 684 | const size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 685 | HUseListNode<T>* prev_; |
| 686 | HUseListNode<T>* next_; |
| 687 | |
| 688 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 689 | |
| 690 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 691 | }; |
| 692 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 693 | template <typename T> |
| 694 | class HUseList : public ValueObject { |
| 695 | public: |
| 696 | HUseList() : first_(nullptr) {} |
| 697 | |
| 698 | void Clear() { |
| 699 | first_ = nullptr; |
| 700 | } |
| 701 | |
| 702 | // Adds a new entry at the beginning of the use list and returns |
| 703 | // the newly created node. |
| 704 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 705 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 706 | if (IsEmpty()) { |
| 707 | first_ = new_node; |
| 708 | } else { |
| 709 | first_->prev_ = new_node; |
| 710 | new_node->next_ = first_; |
| 711 | first_ = new_node; |
| 712 | } |
| 713 | return new_node; |
| 714 | } |
| 715 | |
| 716 | HUseListNode<T>* GetFirst() const { |
| 717 | return first_; |
| 718 | } |
| 719 | |
| 720 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 721 | DCHECK(node != nullptr); |
| 722 | DCHECK(Contains(node)); |
| 723 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 724 | if (node->prev_ != nullptr) { |
| 725 | node->prev_->next_ = node->next_; |
| 726 | } |
| 727 | if (node->next_ != nullptr) { |
| 728 | node->next_->prev_ = node->prev_; |
| 729 | } |
| 730 | if (node == first_) { |
| 731 | first_ = node->next_; |
| 732 | } |
| 733 | } |
| 734 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 735 | bool Contains(const HUseListNode<T>* node) const { |
| 736 | if (node == nullptr) { |
| 737 | return false; |
| 738 | } |
| 739 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 740 | if (current == node) { |
| 741 | return true; |
| 742 | } |
| 743 | } |
| 744 | return false; |
| 745 | } |
| 746 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 747 | bool IsEmpty() const { |
| 748 | return first_ == nullptr; |
| 749 | } |
| 750 | |
| 751 | bool HasOnlyOneUse() const { |
| 752 | return first_ != nullptr && first_->next_ == nullptr; |
| 753 | } |
| 754 | |
| 755 | private: |
| 756 | HUseListNode<T>* first_; |
| 757 | }; |
| 758 | |
| 759 | template<typename T> |
| 760 | class HUseIterator : public ValueObject { |
| 761 | public: |
| 762 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 763 | |
| 764 | bool Done() const { return current_ == nullptr; } |
| 765 | |
| 766 | void Advance() { |
| 767 | DCHECK(!Done()); |
| 768 | current_ = current_->GetNext(); |
| 769 | } |
| 770 | |
| 771 | HUseListNode<T>* Current() const { |
| 772 | DCHECK(!Done()); |
| 773 | return current_; |
| 774 | } |
| 775 | |
| 776 | private: |
| 777 | HUseListNode<T>* current_; |
| 778 | |
| 779 | friend class HValue; |
| 780 | }; |
| 781 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 782 | // This class is used by HEnvironment and HInstruction classes to record the |
| 783 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 784 | // by the used instructions. |
| 785 | template <typename T> |
| 786 | class HUserRecord : public ValueObject { |
| 787 | public: |
| 788 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 789 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 790 | |
| 791 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 792 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 793 | DCHECK(instruction_ != nullptr); |
| 794 | DCHECK(use_node_ != nullptr); |
| 795 | DCHECK(old_record.use_node_ == nullptr); |
| 796 | } |
| 797 | |
| 798 | HInstruction* GetInstruction() const { return instruction_; } |
| 799 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 800 | |
| 801 | private: |
| 802 | // Instruction used by the user. |
| 803 | HInstruction* instruction_; |
| 804 | |
| 805 | // Corresponding entry in the use list kept by 'instruction_'. |
| 806 | HUseListNode<T>* use_node_; |
| 807 | }; |
| 808 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 809 | // Represents the side effects an instruction may have. |
| 810 | class SideEffects : public ValueObject { |
| 811 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 812 | SideEffects() : flags_(0) {} |
| 813 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 814 | static SideEffects None() { |
| 815 | return SideEffects(0); |
| 816 | } |
| 817 | |
| 818 | static SideEffects All() { |
| 819 | return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_); |
| 820 | } |
| 821 | |
| 822 | static SideEffects ChangesSomething() { |
| 823 | return SideEffects((1 << kFlagChangesCount) - 1); |
| 824 | } |
| 825 | |
| 826 | static SideEffects DependsOnSomething() { |
| 827 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 828 | return SideEffects(((1 << count) - 1) << kFlagChangesCount); |
| 829 | } |
| 830 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 831 | SideEffects Union(SideEffects other) const { |
| 832 | return SideEffects(flags_ | other.flags_); |
| 833 | } |
| 834 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 835 | bool HasSideEffects() const { |
| 836 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 837 | return (flags_ & all_bits_set) != 0; |
| 838 | } |
| 839 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 840 | bool HasAllSideEffects() const { |
| 841 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 842 | return all_bits_set == (flags_ & all_bits_set); |
| 843 | } |
| 844 | |
| 845 | bool DependsOn(SideEffects other) const { |
| 846 | size_t depends_flags = other.ComputeDependsFlags(); |
| 847 | return (flags_ & depends_flags) != 0; |
| 848 | } |
| 849 | |
| 850 | bool HasDependencies() const { |
| 851 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 852 | size_t all_bits_set = (1 << count) - 1; |
| 853 | return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0; |
| 854 | } |
| 855 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 856 | private: |
| 857 | static constexpr int kFlagChangesSomething = 0; |
| 858 | static constexpr int kFlagChangesCount = kFlagChangesSomething + 1; |
| 859 | |
| 860 | static constexpr int kFlagDependsOnSomething = kFlagChangesCount; |
| 861 | static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1; |
| 862 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 863 | explicit SideEffects(size_t flags) : flags_(flags) {} |
| 864 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 865 | size_t ComputeDependsFlags() const { |
| 866 | return flags_ << kFlagChangesCount; |
| 867 | } |
| 868 | |
| 869 | size_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 870 | }; |
| 871 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 872 | // A HEnvironment object contains the values of virtual registers at a given location. |
| 873 | class HEnvironment : public ArenaObject<kArenaAllocMisc> { |
| 874 | public: |
| 875 | HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) |
| 876 | : vregs_(arena, number_of_vregs) { |
| 877 | vregs_.SetSize(number_of_vregs); |
| 878 | for (size_t i = 0; i < number_of_vregs; i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 879 | vregs_.Put(i, HUserRecord<HEnvironment*>()); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | void CopyFrom(HEnvironment* env); |
| 884 | |
| 885 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 886 | vregs_.Put(index, HUserRecord<HEnvironment*>(instruction)); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | HInstruction* GetInstructionAt(size_t index) const { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 890 | return vregs_.Get(index).GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 891 | } |
| 892 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 893 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 894 | |
| 895 | size_t Size() const { return vregs_.Size(); } |
| 896 | |
| 897 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 898 | // Record instructions' use entries of this environment for constant-time removal. |
| 899 | // It should only be called by HInstruction when a new environment use is added. |
| 900 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 901 | DCHECK(env_use->GetUser() == this); |
| 902 | size_t index = env_use->GetIndex(); |
| 903 | vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use)); |
| 904 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 905 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 906 | GrowableArray<HUserRecord<HEnvironment*> > vregs_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 907 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 908 | friend HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 909 | |
| 910 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 911 | }; |
| 912 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 913 | class ReferenceTypeInfo : ValueObject { |
| 914 | public: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 915 | typedef Handle<mirror::Class> TypeHandle; |
| 916 | |
| 917 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) |
| 918 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 919 | if (type_handle->IsObjectClass()) { |
| 920 | // Override the type handle to be consistent with the case when we get to |
| 921 | // Top but don't have the Object class available. It avoids having to guess |
| 922 | // what value the type_handle has when it's Top. |
| 923 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
| 924 | } else { |
| 925 | return ReferenceTypeInfo(type_handle, is_exact, false); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | static ReferenceTypeInfo CreateTop(bool is_exact) { |
| 930 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | bool IsExact() const { return is_exact_; } |
| 934 | bool IsTop() const { return is_top_; } |
| 935 | |
| 936 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 937 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 938 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 939 | if (IsTop()) { |
| 940 | // Top (equivalent for java.lang.Object) is supertype of anything. |
| 941 | return true; |
| 942 | } |
| 943 | if (rti.IsTop()) { |
| 944 | // If we get here `this` is not Top() so it can't be a supertype. |
| 945 | return false; |
| 946 | } |
| 947 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 948 | } |
| 949 | |
| 950 | // Returns true if the type information provide the same amount of details. |
| 951 | // Note that it does not mean that the instructions have the same actual type |
| 952 | // (e.g. tops are equal but they can be the result of a merge). |
| 953 | bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 954 | if (IsExact() != rti.IsExact()) { |
| 955 | return false; |
| 956 | } |
| 957 | if (IsTop() && rti.IsTop()) { |
| 958 | // `Top` means java.lang.Object, so the types are equivalent. |
| 959 | return true; |
| 960 | } |
| 961 | if (IsTop() || rti.IsTop()) { |
| 962 | // If only one is top or object than they are not equivalent. |
| 963 | // NB: We need this extra check because the type_handle of `Top` is invalid |
| 964 | // and we cannot inspect its reference. |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | // Finally check the types. |
| 969 | return GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
| 970 | } |
| 971 | |
| 972 | private: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 973 | ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {} |
| 974 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top) |
| 975 | : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {} |
| 976 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 977 | // The class of the object. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 978 | TypeHandle type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 979 | // Whether or not the type is exact or a superclass of the actual type. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 980 | // Whether or not we have any information about this type. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 981 | bool is_exact_; |
| 982 | // A true value here means that the object type should be java.lang.Object. |
| 983 | // We don't have access to the corresponding mirror object every time so this |
| 984 | // flag acts as a substitute. When true, the TypeHandle refers to a null |
| 985 | // pointer and should not be used. |
| 986 | bool is_top_; |
| 987 | }; |
| 988 | |
| 989 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 990 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 991 | class HInstruction : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 992 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 993 | explicit HInstruction(SideEffects side_effects) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 994 | : previous_(nullptr), |
| 995 | next_(nullptr), |
| 996 | block_(nullptr), |
| 997 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 998 | ssa_index_(-1), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 999 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1000 | locations_(nullptr), |
| 1001 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1002 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1003 | side_effects_(side_effects), |
| 1004 | reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1005 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1006 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1007 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1008 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1009 | enum InstructionKind { |
| 1010 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1011 | }; |
| 1012 | #undef DECLARE_KIND |
| 1013 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1014 | HInstruction* GetNext() const { return next_; } |
| 1015 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1016 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1017 | HInstruction* GetNextDisregardingMoves() const; |
| 1018 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1019 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1020 | HBasicBlock* GetBlock() const { return block_; } |
| 1021 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1022 | bool IsInBlock() const { return block_ != nullptr; } |
| 1023 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 1024 | bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1025 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1026 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1027 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1028 | |
| 1029 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1030 | virtual const char* DebugName() const = 0; |
| 1031 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1032 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1033 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1034 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1035 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1036 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1037 | virtual bool NeedsEnvironment() const { return false; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1038 | virtual bool IsControlFlow() const { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1039 | virtual bool CanThrow() const { return false; } |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1040 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1041 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1042 | virtual bool ActAsNullConstant() const { return false; } |
| 1043 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1044 | // Does not apply for all instructions, but having this at top level greatly |
| 1045 | // simplifies the null check elimination. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1046 | virtual bool CanBeNull() const { |
| 1047 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1048 | return true; |
| 1049 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1050 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1051 | virtual bool CanDoImplicitNullCheck() const { return false; } |
| 1052 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1053 | void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1054 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1055 | reference_type_info_ = reference_type_info; |
| 1056 | } |
| 1057 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1058 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1059 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1060 | return reference_type_info_; |
| 1061 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1062 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1063 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1064 | DCHECK(user != nullptr); |
| 1065 | HUseListNode<HInstruction*>* use = |
| 1066 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1067 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1070 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1071 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1072 | HUseListNode<HEnvironment*>* env_use = |
| 1073 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1074 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1077 | void RemoveAsUserOfInput(size_t input) { |
| 1078 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1079 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1080 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1081 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1082 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1083 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1084 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1085 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1086 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1087 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1088 | // Does this instruction strictly dominate `other_instruction`? |
| 1089 | // Returns false if this instruction and `other_instruction` are the same. |
| 1090 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1091 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1092 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1093 | int GetId() const { return id_; } |
| 1094 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1095 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1096 | int GetSsaIndex() const { return ssa_index_; } |
| 1097 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1098 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1099 | |
| 1100 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1101 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1102 | void SetEnvironment(HEnvironment* environment) { environment_ = environment; } |
| 1103 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1104 | // Returns the number of entries in the environment. Typically, that is the |
| 1105 | // number of dex registers in a method. It could be more in case of inlining. |
| 1106 | size_t EnvironmentSize() const; |
| 1107 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1108 | LocationSummary* GetLocations() const { return locations_; } |
| 1109 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1110 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1111 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1112 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1113 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1114 | // Move `this` instruction before `cursor`. |
| 1115 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1116 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1117 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1118 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1119 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1120 | virtual H##type* As##type() { return nullptr; } |
| 1121 | |
| 1122 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1123 | #undef INSTRUCTION_TYPE_CHECK |
| 1124 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1125 | // Returns whether the instruction can be moved within the graph. |
| 1126 | virtual bool CanBeMoved() const { return false; } |
| 1127 | |
| 1128 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1129 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1130 | UNUSED(other); |
| 1131 | return false; |
| 1132 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1133 | |
| 1134 | // Returns whether any data encoded in the two instructions is equal. |
| 1135 | // This method does not look at the inputs. Both instructions must be |
| 1136 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1137 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1138 | UNUSED(other); |
| 1139 | return false; |
| 1140 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1141 | |
| 1142 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1143 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1144 | // 2) Their inputs are identical. |
| 1145 | bool Equals(HInstruction* other) const; |
| 1146 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1147 | virtual InstructionKind GetKind() const = 0; |
| 1148 | |
| 1149 | virtual size_t ComputeHashCode() const { |
| 1150 | size_t result = GetKind(); |
| 1151 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1152 | result = (result * 31) + InputAt(i)->GetId(); |
| 1153 | } |
| 1154 | return result; |
| 1155 | } |
| 1156 | |
| 1157 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1158 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1159 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1160 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1161 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1162 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1163 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1164 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1165 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1166 | |
| 1167 | // Returns whether the code generation of the instruction will require to have access |
| 1168 | // to the current method. Such instructions are: |
| 1169 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1170 | // to walk the stack and have the current method stored at a specific stack address. |
| 1171 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1172 | // fields of the current method. |
| 1173 | bool NeedsCurrentMethod() const { |
| 1174 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1175 | } |
| 1176 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1177 | protected: |
| 1178 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1179 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1180 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1181 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1182 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1183 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1184 | HInstruction* previous_; |
| 1185 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1186 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1187 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1188 | // An instruction gets an id when it is added to the graph. |
| 1189 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1190 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1191 | int id_; |
| 1192 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1193 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1194 | int ssa_index_; |
| 1195 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1196 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1197 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1198 | |
| 1199 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1200 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1201 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1202 | // The environment associated with this instruction. Not null if the instruction |
| 1203 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1204 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1205 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1206 | // Set by the code generator. |
| 1207 | LocationSummary* locations_; |
| 1208 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1209 | // Set by the liveness analysis. |
| 1210 | LiveInterval* live_interval_; |
| 1211 | |
| 1212 | // Set by the liveness analysis, this is the position in a linear |
| 1213 | // order of blocks where this instruction's live interval start. |
| 1214 | size_t lifetime_position_; |
| 1215 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1216 | const SideEffects side_effects_; |
| 1217 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1218 | // TODO: for primitive types this should be marked as invalid. |
| 1219 | ReferenceTypeInfo reference_type_info_; |
| 1220 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1221 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1222 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1223 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1224 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1225 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1226 | |
| 1227 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1228 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1229 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1230 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1231 | class HInputIterator : public ValueObject { |
| 1232 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1233 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1234 | |
| 1235 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1236 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1237 | void Advance() { index_++; } |
| 1238 | |
| 1239 | private: |
| 1240 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1241 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1242 | |
| 1243 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 1244 | }; |
| 1245 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1246 | class HInstructionIterator : public ValueObject { |
| 1247 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1248 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 1249 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1250 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1253 | bool Done() const { return instruction_ == nullptr; } |
| 1254 | HInstruction* Current() const { return instruction_; } |
| 1255 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1256 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1257 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | private: |
| 1261 | HInstruction* instruction_; |
| 1262 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1263 | |
| 1264 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1265 | }; |
| 1266 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1267 | class HBackwardInstructionIterator : public ValueObject { |
| 1268 | public: |
| 1269 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 1270 | : instruction_(instructions.last_instruction_) { |
| 1271 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1272 | } |
| 1273 | |
| 1274 | bool Done() const { return instruction_ == nullptr; } |
| 1275 | HInstruction* Current() const { return instruction_; } |
| 1276 | void Advance() { |
| 1277 | instruction_ = next_; |
| 1278 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1279 | } |
| 1280 | |
| 1281 | private: |
| 1282 | HInstruction* instruction_; |
| 1283 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1284 | |
| 1285 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1286 | }; |
| 1287 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1288 | // An embedded container with N elements of type T. Used (with partial |
| 1289 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 1290 | template<typename T, intptr_t N> |
| 1291 | class EmbeddedArray { |
| 1292 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1293 | EmbeddedArray() : elements_() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1294 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1295 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1296 | |
| 1297 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1298 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1299 | return elements_[i]; |
| 1300 | } |
| 1301 | |
| 1302 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1303 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1304 | return elements_[i]; |
| 1305 | } |
| 1306 | |
| 1307 | const T& At(intptr_t i) const { |
| 1308 | return (*this)[i]; |
| 1309 | } |
| 1310 | |
| 1311 | void SetAt(intptr_t i, const T& val) { |
| 1312 | (*this)[i] = val; |
| 1313 | } |
| 1314 | |
| 1315 | private: |
| 1316 | T elements_[N]; |
| 1317 | }; |
| 1318 | |
| 1319 | template<typename T> |
| 1320 | class EmbeddedArray<T, 0> { |
| 1321 | public: |
| 1322 | intptr_t length() const { return 0; } |
| 1323 | const T& operator[](intptr_t i) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1324 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1325 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1326 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1327 | } |
| 1328 | T& operator[](intptr_t i) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1329 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1330 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1331 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1332 | } |
| 1333 | }; |
| 1334 | |
| 1335 | template<intptr_t N> |
| 1336 | class HTemplateInstruction: public HInstruction { |
| 1337 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1338 | HTemplateInstruction<N>(SideEffects side_effects) |
| 1339 | : HInstruction(side_effects), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1340 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1341 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1342 | virtual size_t InputCount() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1343 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1344 | protected: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1345 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; } |
| 1346 | |
| 1347 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 1348 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1351 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1352 | EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1353 | |
| 1354 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1355 | }; |
| 1356 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1357 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1358 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1359 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1360 | HExpression<N>(Primitive::Type type, SideEffects side_effects) |
| 1361 | : HTemplateInstruction<N>(side_effects), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1362 | virtual ~HExpression() {} |
| 1363 | |
| 1364 | virtual Primitive::Type GetType() const { return type_; } |
| 1365 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1366 | protected: |
| 1367 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1368 | }; |
| 1369 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1370 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 1371 | // instruction that branches to the exit block. |
| 1372 | class HReturnVoid : public HTemplateInstruction<0> { |
| 1373 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1374 | HReturnVoid() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1375 | |
| 1376 | virtual bool IsControlFlow() const { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1377 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1378 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1379 | |
| 1380 | private: |
| 1381 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 1382 | }; |
| 1383 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1384 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 1385 | // instruction that branches to the exit block. |
| 1386 | class HReturn : public HTemplateInstruction<1> { |
| 1387 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1388 | explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1389 | SetRawInputAt(0, value); |
| 1390 | } |
| 1391 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1392 | virtual bool IsControlFlow() const { return true; } |
| 1393 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1394 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1395 | |
| 1396 | private: |
| 1397 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 1398 | }; |
| 1399 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1400 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1401 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1402 | // exit block. |
| 1403 | class HExit : public HTemplateInstruction<0> { |
| 1404 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1405 | HExit() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1406 | |
| 1407 | virtual bool IsControlFlow() const { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1408 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1409 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1410 | |
| 1411 | private: |
| 1412 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 1413 | }; |
| 1414 | |
| 1415 | // Jumps from one block to another. |
| 1416 | class HGoto : public HTemplateInstruction<0> { |
| 1417 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1418 | HGoto() : HTemplateInstruction(SideEffects::None()) {} |
| 1419 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1420 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1421 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1422 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1423 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1426 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1427 | |
| 1428 | private: |
| 1429 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 1430 | }; |
| 1431 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1432 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1433 | // Conditional branch. A block ending with an HIf instruction must have |
| 1434 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1435 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1436 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1437 | explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1438 | SetRawInputAt(0, input); |
| 1439 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1440 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1441 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1442 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1443 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1444 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1448 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1451 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1452 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1453 | virtual bool IsIfInstruction() const { return true; } |
| 1454 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1455 | private: |
| 1456 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 1457 | }; |
| 1458 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1459 | class HUnaryOperation : public HExpression<1> { |
| 1460 | public: |
| 1461 | HUnaryOperation(Primitive::Type result_type, HInstruction* input) |
| 1462 | : HExpression(result_type, SideEffects::None()) { |
| 1463 | SetRawInputAt(0, input); |
| 1464 | } |
| 1465 | |
| 1466 | HInstruction* GetInput() const { return InputAt(0); } |
| 1467 | Primitive::Type GetResultType() const { return GetType(); } |
| 1468 | |
| 1469 | virtual bool CanBeMoved() const { return true; } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1470 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1471 | UNUSED(other); |
| 1472 | return true; |
| 1473 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1474 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1475 | // Try to statically evaluate `operation` and return a HConstant |
| 1476 | // containing the result of this evaluation. If `operation` cannot |
| 1477 | // be evaluated as a constant, return nullptr. |
| 1478 | HConstant* TryStaticEvaluation() const; |
| 1479 | |
| 1480 | // Apply this operation to `x`. |
| 1481 | virtual int32_t Evaluate(int32_t x) const = 0; |
| 1482 | virtual int64_t Evaluate(int64_t x) const = 0; |
| 1483 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1484 | DECLARE_INSTRUCTION(UnaryOperation); |
| 1485 | |
| 1486 | private: |
| 1487 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 1488 | }; |
| 1489 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1490 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1491 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1492 | HBinaryOperation(Primitive::Type result_type, |
| 1493 | HInstruction* left, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1494 | HInstruction* right) : HExpression(result_type, SideEffects::None()) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1495 | SetRawInputAt(0, left); |
| 1496 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1499 | HInstruction* GetLeft() const { return InputAt(0); } |
| 1500 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1501 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1502 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1503 | virtual bool IsCommutative() const { return false; } |
| 1504 | |
| 1505 | // Put constant on the right. |
| 1506 | // Returns whether order is changed. |
| 1507 | bool OrderInputsWithConstantOnTheRight() { |
| 1508 | HInstruction* left = InputAt(0); |
| 1509 | HInstruction* right = InputAt(1); |
| 1510 | if (left->IsConstant() && !right->IsConstant()) { |
| 1511 | ReplaceInput(right, 0); |
| 1512 | ReplaceInput(left, 1); |
| 1513 | return true; |
| 1514 | } |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
| 1518 | // Order inputs by instruction id, but favor constant on the right side. |
| 1519 | // This helps GVN for commutative ops. |
| 1520 | void OrderInputs() { |
| 1521 | DCHECK(IsCommutative()); |
| 1522 | HInstruction* left = InputAt(0); |
| 1523 | HInstruction* right = InputAt(1); |
| 1524 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 1525 | return; |
| 1526 | } |
| 1527 | if (OrderInputsWithConstantOnTheRight()) { |
| 1528 | return; |
| 1529 | } |
| 1530 | // Order according to instruction id. |
| 1531 | if (left->GetId() > right->GetId()) { |
| 1532 | ReplaceInput(right, 0); |
| 1533 | ReplaceInput(left, 1); |
| 1534 | } |
| 1535 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1536 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1537 | virtual bool CanBeMoved() const { return true; } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1538 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1539 | UNUSED(other); |
| 1540 | return true; |
| 1541 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1542 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1543 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1544 | // containing the result of this evaluation. If `operation` cannot |
| 1545 | // be evaluated as a constant, return nullptr. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1546 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1547 | |
| 1548 | // Apply this operation to `x` and `y`. |
| 1549 | virtual int32_t Evaluate(int32_t x, int32_t y) const = 0; |
| 1550 | virtual int64_t Evaluate(int64_t x, int64_t y) const = 0; |
| 1551 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1552 | DECLARE_INSTRUCTION(BinaryOperation); |
| 1553 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1554 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1555 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 1556 | }; |
| 1557 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1558 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1559 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1560 | HCondition(HInstruction* first, HInstruction* second) |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1561 | : HBinaryOperation(Primitive::kPrimBoolean, first, second), |
| 1562 | needs_materialization_(true) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1563 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1564 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 1565 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1566 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1567 | // For code generation purposes, returns whether this instruction is just before |
| 1568 | // `if_`, and disregard moves in between. |
| 1569 | bool IsBeforeWhenDisregardMoves(HIf* if_) const; |
| 1570 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1571 | DECLARE_INSTRUCTION(Condition); |
| 1572 | |
| 1573 | virtual IfCondition GetCondition() const = 0; |
| 1574 | |
| 1575 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1576 | // For register allocation purposes, returns whether this instruction needs to be |
| 1577 | // materialized (that is, not just be in the processor flags). |
| 1578 | bool needs_materialization_; |
| 1579 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1580 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 1581 | }; |
| 1582 | |
| 1583 | // Instruction to check if two inputs are equal to each other. |
| 1584 | class HEqual : public HCondition { |
| 1585 | public: |
| 1586 | HEqual(HInstruction* first, HInstruction* second) |
| 1587 | : HCondition(first, second) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1588 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1589 | bool IsCommutative() const OVERRIDE { return true; } |
| 1590 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1591 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1592 | return x == y ? 1 : 0; |
| 1593 | } |
| 1594 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1595 | return x == y ? 1 : 0; |
| 1596 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1597 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1598 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1599 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1600 | virtual IfCondition GetCondition() const { |
| 1601 | return kCondEQ; |
| 1602 | } |
| 1603 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1604 | private: |
| 1605 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 1606 | }; |
| 1607 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1608 | class HNotEqual : public HCondition { |
| 1609 | public: |
| 1610 | HNotEqual(HInstruction* first, HInstruction* second) |
| 1611 | : HCondition(first, second) {} |
| 1612 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1613 | bool IsCommutative() const OVERRIDE { return true; } |
| 1614 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1615 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1616 | return x != y ? 1 : 0; |
| 1617 | } |
| 1618 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1619 | return x != y ? 1 : 0; |
| 1620 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1621 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1622 | DECLARE_INSTRUCTION(NotEqual); |
| 1623 | |
| 1624 | virtual IfCondition GetCondition() const { |
| 1625 | return kCondNE; |
| 1626 | } |
| 1627 | |
| 1628 | private: |
| 1629 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 1630 | }; |
| 1631 | |
| 1632 | class HLessThan : public HCondition { |
| 1633 | public: |
| 1634 | HLessThan(HInstruction* first, HInstruction* second) |
| 1635 | : HCondition(first, second) {} |
| 1636 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1637 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1638 | return x < y ? 1 : 0; |
| 1639 | } |
| 1640 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1641 | return x < y ? 1 : 0; |
| 1642 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1643 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1644 | DECLARE_INSTRUCTION(LessThan); |
| 1645 | |
| 1646 | virtual IfCondition GetCondition() const { |
| 1647 | return kCondLT; |
| 1648 | } |
| 1649 | |
| 1650 | private: |
| 1651 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 1652 | }; |
| 1653 | |
| 1654 | class HLessThanOrEqual : public HCondition { |
| 1655 | public: |
| 1656 | HLessThanOrEqual(HInstruction* first, HInstruction* second) |
| 1657 | : HCondition(first, second) {} |
| 1658 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1659 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1660 | return x <= y ? 1 : 0; |
| 1661 | } |
| 1662 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1663 | return x <= y ? 1 : 0; |
| 1664 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1665 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1666 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 1667 | |
| 1668 | virtual IfCondition GetCondition() const { |
| 1669 | return kCondLE; |
| 1670 | } |
| 1671 | |
| 1672 | private: |
| 1673 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 1674 | }; |
| 1675 | |
| 1676 | class HGreaterThan : public HCondition { |
| 1677 | public: |
| 1678 | HGreaterThan(HInstruction* first, HInstruction* second) |
| 1679 | : HCondition(first, second) {} |
| 1680 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1681 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1682 | return x > y ? 1 : 0; |
| 1683 | } |
| 1684 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1685 | return x > y ? 1 : 0; |
| 1686 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1687 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1688 | DECLARE_INSTRUCTION(GreaterThan); |
| 1689 | |
| 1690 | virtual IfCondition GetCondition() const { |
| 1691 | return kCondGT; |
| 1692 | } |
| 1693 | |
| 1694 | private: |
| 1695 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 1696 | }; |
| 1697 | |
| 1698 | class HGreaterThanOrEqual : public HCondition { |
| 1699 | public: |
| 1700 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second) |
| 1701 | : HCondition(first, second) {} |
| 1702 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1703 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 1704 | return x >= y ? 1 : 0; |
| 1705 | } |
| 1706 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 1707 | return x >= y ? 1 : 0; |
| 1708 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1709 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1710 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 1711 | |
| 1712 | virtual IfCondition GetCondition() const { |
| 1713 | return kCondGE; |
| 1714 | } |
| 1715 | |
| 1716 | private: |
| 1717 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 1718 | }; |
| 1719 | |
| 1720 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1721 | // Instruction to check how two inputs compare to each other. |
| 1722 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 1723 | class HCompare : public HBinaryOperation { |
| 1724 | public: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1725 | // The bias applies for floating point operations and indicates how NaN |
| 1726 | // comparisons are treated: |
| 1727 | enum Bias { |
| 1728 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 1729 | kGtBias, // return 1 for NaN comparisons |
| 1730 | kLtBias, // return -1 for NaN comparisons |
| 1731 | }; |
| 1732 | |
| 1733 | HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias) |
| 1734 | : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1735 | DCHECK_EQ(type, first->GetType()); |
| 1736 | DCHECK_EQ(type, second->GetType()); |
| 1737 | } |
| 1738 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1739 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1740 | return |
| 1741 | x == y ? 0 : |
| 1742 | x > y ? 1 : |
| 1743 | -1; |
| 1744 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1745 | |
| 1746 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1747 | return |
| 1748 | x == y ? 0 : |
| 1749 | x > y ? 1 : |
| 1750 | -1; |
| 1751 | } |
| 1752 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1753 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 1754 | return bias_ == other->AsCompare()->bias_; |
| 1755 | } |
| 1756 | |
| 1757 | bool IsGtBias() { return bias_ == kGtBias; } |
| 1758 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1759 | DECLARE_INSTRUCTION(Compare); |
| 1760 | |
| 1761 | private: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1762 | const Bias bias_; |
| 1763 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1764 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 1765 | }; |
| 1766 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1767 | // A local in the graph. Corresponds to a Dex register. |
| 1768 | class HLocal : public HTemplateInstruction<0> { |
| 1769 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1770 | explicit HLocal(uint16_t reg_number) |
| 1771 | : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1772 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1773 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1774 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1775 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1776 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1777 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1778 | // The Dex register number. |
| 1779 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1780 | |
| 1781 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 1782 | }; |
| 1783 | |
| 1784 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1785 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1786 | public: |
Roland Levillain | 5799fc0 | 2014-09-25 12:15:20 +0100 | [diff] [blame] | 1787 | HLoadLocal(HLocal* local, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1788 | : HExpression(type, SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1789 | SetRawInputAt(0, local); |
| 1790 | } |
| 1791 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1792 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 1793 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1794 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1795 | |
| 1796 | private: |
| 1797 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 1798 | }; |
| 1799 | |
| 1800 | // Store a value in a given local. This instruction has two inputs: the value |
| 1801 | // and the local. |
| 1802 | class HStoreLocal : public HTemplateInstruction<2> { |
| 1803 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1804 | HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1805 | SetRawInputAt(0, local); |
| 1806 | SetRawInputAt(1, value); |
| 1807 | } |
| 1808 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1809 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 1810 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1811 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1812 | |
| 1813 | private: |
| 1814 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 1815 | }; |
| 1816 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1817 | class HConstant : public HExpression<0> { |
| 1818 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1819 | explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {} |
| 1820 | |
| 1821 | virtual bool CanBeMoved() const { return true; } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1822 | |
| 1823 | DECLARE_INSTRUCTION(Constant); |
| 1824 | |
| 1825 | private: |
| 1826 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 1827 | }; |
| 1828 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1829 | class HFloatConstant : public HConstant { |
| 1830 | public: |
| 1831 | explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {} |
| 1832 | |
| 1833 | float GetValue() const { return value_; } |
| 1834 | |
| 1835 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1836 | return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) == |
| 1837 | bit_cast<float, int32_t>(value_); |
| 1838 | } |
| 1839 | |
| 1840 | virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); } |
| 1841 | |
| 1842 | DECLARE_INSTRUCTION(FloatConstant); |
| 1843 | |
| 1844 | private: |
| 1845 | const float value_; |
| 1846 | |
| 1847 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 1848 | }; |
| 1849 | |
| 1850 | class HDoubleConstant : public HConstant { |
| 1851 | public: |
| 1852 | explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {} |
| 1853 | |
| 1854 | double GetValue() const { return value_; } |
| 1855 | |
| 1856 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1857 | return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) == |
| 1858 | bit_cast<double, int64_t>(value_); |
| 1859 | } |
| 1860 | |
| 1861 | virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); } |
| 1862 | |
| 1863 | DECLARE_INSTRUCTION(DoubleConstant); |
| 1864 | |
| 1865 | private: |
| 1866 | const double value_; |
| 1867 | |
| 1868 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 1869 | }; |
| 1870 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1871 | class HNullConstant : public HConstant { |
| 1872 | public: |
| 1873 | HNullConstant() : HConstant(Primitive::kPrimNot) {} |
| 1874 | |
| 1875 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 1876 | return true; |
| 1877 | } |
| 1878 | |
| 1879 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 1880 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1881 | bool ActAsNullConstant() const OVERRIDE { return true; } |
| 1882 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1883 | DECLARE_INSTRUCTION(NullConstant); |
| 1884 | |
| 1885 | private: |
| 1886 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 1887 | }; |
| 1888 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1889 | // Constants of the type int. Those can be from Dex instructions, or |
| 1890 | // synthesized (for example with the if-eqz instruction). |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1891 | class HIntConstant : public HConstant { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1892 | public: |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1893 | explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1894 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1895 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1896 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1897 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1898 | return other->AsIntConstant()->value_ == value_; |
| 1899 | } |
| 1900 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1901 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 1902 | |
| 1903 | // TODO: Null is represented by the `0` constant. In most cases we replace it |
| 1904 | // with a HNullConstant but we don't do it when comparing (a != null). This |
| 1905 | // method is an workaround until we fix the above. |
| 1906 | bool ActAsNullConstant() const OVERRIDE { return value_ == 0; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1907 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1908 | DECLARE_INSTRUCTION(IntConstant); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1909 | |
| 1910 | private: |
| 1911 | const int32_t value_; |
| 1912 | |
| 1913 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 1914 | }; |
| 1915 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1916 | class HLongConstant : public HConstant { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1917 | public: |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1918 | explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1919 | |
| 1920 | int64_t GetValue() const { return value_; } |
| 1921 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1922 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1923 | return other->AsLongConstant()->value_ == value_; |
| 1924 | } |
| 1925 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1926 | virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); } |
| 1927 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1928 | DECLARE_INSTRUCTION(LongConstant); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1929 | |
| 1930 | private: |
| 1931 | const int64_t value_; |
| 1932 | |
| 1933 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 1934 | }; |
| 1935 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1936 | enum class Intrinsics { |
| 1937 | #define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name, |
| 1938 | #include "intrinsics_list.h" |
| 1939 | kNone, |
| 1940 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 1941 | #undef INTRINSICS_LIST |
| 1942 | #undef OPTIMIZING_INTRINSICS |
| 1943 | }; |
| 1944 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 1945 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1946 | class HInvoke : public HInstruction { |
| 1947 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1948 | virtual size_t InputCount() const { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1949 | |
| 1950 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 1951 | // know their environment. |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1952 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1953 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 1954 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1955 | SetRawInputAt(index, argument); |
| 1956 | } |
| 1957 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1958 | virtual Primitive::Type GetType() const { return return_type_; } |
| 1959 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 1960 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1961 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1962 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 1963 | |
| 1964 | Intrinsics GetIntrinsic() { |
| 1965 | return intrinsic_; |
| 1966 | } |
| 1967 | |
| 1968 | void SetIntrinsic(Intrinsics intrinsic) { |
| 1969 | intrinsic_ = intrinsic; |
| 1970 | } |
| 1971 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1972 | DECLARE_INSTRUCTION(Invoke); |
| 1973 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1974 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1975 | HInvoke(ArenaAllocator* arena, |
| 1976 | uint32_t number_of_arguments, |
| 1977 | Primitive::Type return_type, |
| 1978 | uint32_t dex_pc, |
| 1979 | uint32_t dex_method_index) |
| 1980 | : HInstruction(SideEffects::All()), |
| 1981 | inputs_(arena, number_of_arguments), |
| 1982 | return_type_(return_type), |
| 1983 | dex_pc_(dex_pc), |
| 1984 | dex_method_index_(dex_method_index), |
| 1985 | intrinsic_(Intrinsics::kNone) { |
| 1986 | inputs_.SetSize(number_of_arguments); |
| 1987 | } |
| 1988 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1989 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 1990 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 1991 | inputs_.Put(index, input); |
| 1992 | } |
| 1993 | |
| 1994 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1995 | const Primitive::Type return_type_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 1996 | const uint32_t dex_pc_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1997 | const uint32_t dex_method_index_; |
| 1998 | Intrinsics intrinsic_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1999 | |
| 2000 | private: |
| 2001 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 2002 | }; |
| 2003 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2004 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2005 | public: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2006 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 2007 | uint32_t number_of_arguments, |
| 2008 | Primitive::Type return_type, |
| 2009 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2010 | uint32_t dex_method_index, |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2011 | bool is_recursive, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2012 | InvokeType invoke_type) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2013 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2014 | invoke_type_(invoke_type), |
| 2015 | is_recursive_(is_recursive) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2016 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2017 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2018 | // We access the method via the dex cache so we can't do an implicit null check. |
| 2019 | // TODO: for intrinsics we can generate implicit null checks. |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2023 | InvokeType GetInvokeType() const { return invoke_type_; } |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2024 | bool IsRecursive() const { return is_recursive_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2025 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2026 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2027 | |
| 2028 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2029 | const InvokeType invoke_type_; |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2030 | const bool is_recursive_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2031 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2032 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2033 | }; |
| 2034 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2035 | class HInvokeVirtual : public HInvoke { |
| 2036 | public: |
| 2037 | HInvokeVirtual(ArenaAllocator* arena, |
| 2038 | uint32_t number_of_arguments, |
| 2039 | Primitive::Type return_type, |
| 2040 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2041 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2042 | uint32_t vtable_index) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2043 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2044 | vtable_index_(vtable_index) {} |
| 2045 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2046 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2047 | // TODO: Add implicit null checks in intrinsics. |
| 2048 | return !GetLocations()->Intrinsified(); |
| 2049 | } |
| 2050 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2051 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 2052 | |
| 2053 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 2054 | |
| 2055 | private: |
| 2056 | const uint32_t vtable_index_; |
| 2057 | |
| 2058 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 2059 | }; |
| 2060 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2061 | class HInvokeInterface : public HInvoke { |
| 2062 | public: |
| 2063 | HInvokeInterface(ArenaAllocator* arena, |
| 2064 | uint32_t number_of_arguments, |
| 2065 | Primitive::Type return_type, |
| 2066 | uint32_t dex_pc, |
| 2067 | uint32_t dex_method_index, |
| 2068 | uint32_t imt_index) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2069 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2070 | imt_index_(imt_index) {} |
| 2071 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2072 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2073 | // TODO: Add implicit null checks in intrinsics. |
| 2074 | return !GetLocations()->Intrinsified(); |
| 2075 | } |
| 2076 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2077 | uint32_t GetImtIndex() const { return imt_index_; } |
| 2078 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 2079 | |
| 2080 | DECLARE_INSTRUCTION(InvokeInterface); |
| 2081 | |
| 2082 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2083 | const uint32_t imt_index_; |
| 2084 | |
| 2085 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 2086 | }; |
| 2087 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2088 | class HNewInstance : public HExpression<0> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2089 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2090 | HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2091 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2092 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2093 | type_index_(type_index), |
| 2094 | entrypoint_(entrypoint) {} |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2095 | |
| 2096 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2097 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2098 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2099 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 2100 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2101 | // It may throw when called on: |
| 2102 | // - interfaces |
| 2103 | // - abstract/innaccessible/unknown classes |
| 2104 | // TODO: optimize when possible. |
| 2105 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2106 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2107 | bool CanBeNull() const OVERRIDE { return false; } |
| 2108 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2109 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2110 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2111 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2112 | |
| 2113 | private: |
| 2114 | const uint32_t dex_pc_; |
| 2115 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2116 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2117 | |
| 2118 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 2119 | }; |
| 2120 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2121 | class HNeg : public HUnaryOperation { |
| 2122 | public: |
| 2123 | explicit HNeg(Primitive::Type result_type, HInstruction* input) |
| 2124 | : HUnaryOperation(result_type, input) {} |
| 2125 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2126 | virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; } |
| 2127 | virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; } |
| 2128 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2129 | DECLARE_INSTRUCTION(Neg); |
| 2130 | |
| 2131 | private: |
| 2132 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 2133 | }; |
| 2134 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2135 | class HNewArray : public HExpression<1> { |
| 2136 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2137 | HNewArray(HInstruction* length, |
| 2138 | uint32_t dex_pc, |
| 2139 | uint16_t type_index, |
| 2140 | QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2141 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2142 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2143 | type_index_(type_index), |
| 2144 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2145 | SetRawInputAt(0, length); |
| 2146 | } |
| 2147 | |
| 2148 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2149 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2150 | |
| 2151 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2152 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2153 | |
| 2154 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2155 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2156 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2157 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2158 | DECLARE_INSTRUCTION(NewArray); |
| 2159 | |
| 2160 | private: |
| 2161 | const uint32_t dex_pc_; |
| 2162 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2163 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2164 | |
| 2165 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 2166 | }; |
| 2167 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2168 | class HAdd : public HBinaryOperation { |
| 2169 | public: |
| 2170 | HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2171 | : HBinaryOperation(result_type, left, right) {} |
| 2172 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2173 | virtual bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2174 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2175 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2176 | return x + y; |
| 2177 | } |
| 2178 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2179 | return x + y; |
| 2180 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2181 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2182 | DECLARE_INSTRUCTION(Add); |
| 2183 | |
| 2184 | private: |
| 2185 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 2186 | }; |
| 2187 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2188 | class HSub : public HBinaryOperation { |
| 2189 | public: |
| 2190 | HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2191 | : HBinaryOperation(result_type, left, right) {} |
| 2192 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2193 | virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2194 | return x - y; |
| 2195 | } |
| 2196 | virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2197 | return x - y; |
| 2198 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2199 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2200 | DECLARE_INSTRUCTION(Sub); |
| 2201 | |
| 2202 | private: |
| 2203 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 2204 | }; |
| 2205 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2206 | class HMul : public HBinaryOperation { |
| 2207 | public: |
| 2208 | HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2209 | : HBinaryOperation(result_type, left, right) {} |
| 2210 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2211 | virtual bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2212 | |
| 2213 | virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; } |
| 2214 | virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; } |
| 2215 | |
| 2216 | DECLARE_INSTRUCTION(Mul); |
| 2217 | |
| 2218 | private: |
| 2219 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 2220 | }; |
| 2221 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2222 | class HDiv : public HBinaryOperation { |
| 2223 | public: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2224 | HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2225 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2226 | |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2227 | virtual int32_t Evaluate(int32_t x, int32_t y) const { |
| 2228 | // Our graph structure ensures we never have 0 for `y` during constant folding. |
| 2229 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2230 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2231 | return (y == -1) ? -x : x / y; |
| 2232 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2233 | |
| 2234 | virtual int64_t Evaluate(int64_t x, int64_t y) const { |
| 2235 | DCHECK_NE(y, 0); |
| 2236 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2237 | return (y == -1) ? -x : x / y; |
| 2238 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2239 | |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2240 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2241 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2242 | DECLARE_INSTRUCTION(Div); |
| 2243 | |
| 2244 | private: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2245 | const uint32_t dex_pc_; |
| 2246 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2247 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 2248 | }; |
| 2249 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2250 | class HRem : public HBinaryOperation { |
| 2251 | public: |
| 2252 | HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2253 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
| 2254 | |
| 2255 | virtual int32_t Evaluate(int32_t x, int32_t y) const { |
| 2256 | DCHECK_NE(y, 0); |
| 2257 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2258 | return (y == -1) ? 0 : x % y; |
| 2259 | } |
| 2260 | |
| 2261 | virtual int64_t Evaluate(int64_t x, int64_t y) const { |
| 2262 | DCHECK_NE(y, 0); |
| 2263 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2264 | return (y == -1) ? 0 : x % y; |
| 2265 | } |
| 2266 | |
| 2267 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2268 | |
| 2269 | DECLARE_INSTRUCTION(Rem); |
| 2270 | |
| 2271 | private: |
| 2272 | const uint32_t dex_pc_; |
| 2273 | |
| 2274 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 2275 | }; |
| 2276 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2277 | class HDivZeroCheck : public HExpression<1> { |
| 2278 | public: |
| 2279 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
| 2280 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
| 2281 | SetRawInputAt(0, value); |
| 2282 | } |
| 2283 | |
| 2284 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2285 | |
| 2286 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2287 | UNUSED(other); |
| 2288 | return true; |
| 2289 | } |
| 2290 | |
| 2291 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2292 | bool CanThrow() const OVERRIDE { return true; } |
| 2293 | |
| 2294 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2295 | |
| 2296 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 2297 | |
| 2298 | private: |
| 2299 | const uint32_t dex_pc_; |
| 2300 | |
| 2301 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 2302 | }; |
| 2303 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 2304 | class HShl : public HBinaryOperation { |
| 2305 | public: |
| 2306 | HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2307 | : HBinaryOperation(result_type, left, right) {} |
| 2308 | |
| 2309 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); } |
| 2310 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); } |
| 2311 | |
| 2312 | DECLARE_INSTRUCTION(Shl); |
| 2313 | |
| 2314 | private: |
| 2315 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 2316 | }; |
| 2317 | |
| 2318 | class HShr : public HBinaryOperation { |
| 2319 | public: |
| 2320 | HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2321 | : HBinaryOperation(result_type, left, right) {} |
| 2322 | |
| 2323 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); } |
| 2324 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); } |
| 2325 | |
| 2326 | DECLARE_INSTRUCTION(Shr); |
| 2327 | |
| 2328 | private: |
| 2329 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 2330 | }; |
| 2331 | |
| 2332 | class HUShr : public HBinaryOperation { |
| 2333 | public: |
| 2334 | HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2335 | : HBinaryOperation(result_type, left, right) {} |
| 2336 | |
| 2337 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2338 | uint32_t ux = static_cast<uint32_t>(x); |
| 2339 | uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue; |
| 2340 | return static_cast<int32_t>(ux >> uy); |
| 2341 | } |
| 2342 | |
| 2343 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2344 | uint64_t ux = static_cast<uint64_t>(x); |
| 2345 | uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue; |
| 2346 | return static_cast<int64_t>(ux >> uy); |
| 2347 | } |
| 2348 | |
| 2349 | DECLARE_INSTRUCTION(UShr); |
| 2350 | |
| 2351 | private: |
| 2352 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 2353 | }; |
| 2354 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2355 | class HAnd : public HBinaryOperation { |
| 2356 | public: |
| 2357 | HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2358 | : HBinaryOperation(result_type, left, right) {} |
| 2359 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2360 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2361 | |
| 2362 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; } |
| 2363 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; } |
| 2364 | |
| 2365 | DECLARE_INSTRUCTION(And); |
| 2366 | |
| 2367 | private: |
| 2368 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 2369 | }; |
| 2370 | |
| 2371 | class HOr : public HBinaryOperation { |
| 2372 | public: |
| 2373 | HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2374 | : HBinaryOperation(result_type, left, right) {} |
| 2375 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2376 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2377 | |
| 2378 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; } |
| 2379 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; } |
| 2380 | |
| 2381 | DECLARE_INSTRUCTION(Or); |
| 2382 | |
| 2383 | private: |
| 2384 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 2385 | }; |
| 2386 | |
| 2387 | class HXor : public HBinaryOperation { |
| 2388 | public: |
| 2389 | HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2390 | : HBinaryOperation(result_type, left, right) {} |
| 2391 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2392 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2393 | |
| 2394 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; } |
| 2395 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; } |
| 2396 | |
| 2397 | DECLARE_INSTRUCTION(Xor); |
| 2398 | |
| 2399 | private: |
| 2400 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 2401 | }; |
| 2402 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2403 | // The value of a parameter in this method. Its location depends on |
| 2404 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2405 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2406 | public: |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2407 | HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false) |
| 2408 | : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2409 | |
| 2410 | uint8_t GetIndex() const { return index_; } |
| 2411 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2412 | bool CanBeNull() const OVERRIDE { return !is_this_; } |
| 2413 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2414 | DECLARE_INSTRUCTION(ParameterValue); |
| 2415 | |
| 2416 | private: |
| 2417 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2418 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2419 | const uint8_t index_; |
| 2420 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2421 | // Whether or not the parameter value corresponds to 'this' argument. |
| 2422 | const bool is_this_; |
| 2423 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2424 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 2425 | }; |
| 2426 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2427 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2428 | public: |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2429 | explicit HNot(Primitive::Type result_type, HInstruction* input) |
| 2430 | : HUnaryOperation(result_type, input) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2431 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2432 | virtual bool CanBeMoved() const { return true; } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2433 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 2434 | UNUSED(other); |
| 2435 | return true; |
| 2436 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2437 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2438 | virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; } |
| 2439 | virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; } |
| 2440 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2441 | DECLARE_INSTRUCTION(Not); |
| 2442 | |
| 2443 | private: |
| 2444 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 2445 | }; |
| 2446 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2447 | class HTypeConversion : public HExpression<1> { |
| 2448 | public: |
| 2449 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2450 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
| 2451 | : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2452 | SetRawInputAt(0, input); |
| 2453 | DCHECK_NE(input->GetType(), result_type); |
| 2454 | } |
| 2455 | |
| 2456 | HInstruction* GetInput() const { return InputAt(0); } |
| 2457 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 2458 | Primitive::Type GetResultType() const { return GetType(); } |
| 2459 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2460 | // Required by the x86 and ARM code generators when producing calls |
| 2461 | // to the runtime. |
| 2462 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2463 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2464 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 2465 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2466 | |
| 2467 | DECLARE_INSTRUCTION(TypeConversion); |
| 2468 | |
| 2469 | private: |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2470 | const uint32_t dex_pc_; |
| 2471 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2472 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 2473 | }; |
| 2474 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 2475 | static constexpr uint32_t kNoRegNumber = -1; |
| 2476 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2477 | class HPhi : public HInstruction { |
| 2478 | public: |
| 2479 | HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2480 | : HInstruction(SideEffects::None()), |
| 2481 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2482 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2483 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2484 | is_live_(false), |
| 2485 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2486 | inputs_.SetSize(number_of_inputs); |
| 2487 | } |
| 2488 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2489 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2490 | |
| 2491 | void AddInput(HInstruction* input); |
| 2492 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2493 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2494 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2495 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2496 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 2497 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 2498 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2499 | uint32_t GetRegNumber() const { return reg_number_; } |
| 2500 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2501 | void SetDead() { is_live_ = false; } |
| 2502 | void SetLive() { is_live_ = true; } |
| 2503 | bool IsDead() const { return !is_live_; } |
| 2504 | bool IsLive() const { return is_live_; } |
| 2505 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2506 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2507 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2508 | protected: |
| 2509 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 2510 | |
| 2511 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 2512 | inputs_.Put(index, input); |
| 2513 | } |
| 2514 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2515 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2516 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2517 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2518 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2519 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2520 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2521 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2522 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 2523 | }; |
| 2524 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2525 | class HNullCheck : public HExpression<1> { |
| 2526 | public: |
| 2527 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2528 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2529 | SetRawInputAt(0, value); |
| 2530 | } |
| 2531 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2532 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2533 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2534 | UNUSED(other); |
| 2535 | return true; |
| 2536 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2537 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2538 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2539 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2540 | bool CanThrow() const OVERRIDE { return true; } |
| 2541 | |
| 2542 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 2543 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2544 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2545 | |
| 2546 | DECLARE_INSTRUCTION(NullCheck); |
| 2547 | |
| 2548 | private: |
| 2549 | const uint32_t dex_pc_; |
| 2550 | |
| 2551 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 2552 | }; |
| 2553 | |
| 2554 | class FieldInfo : public ValueObject { |
| 2555 | public: |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2556 | FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile) |
| 2557 | : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2558 | |
| 2559 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2560 | Primitive::Type GetFieldType() const { return field_type_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2561 | bool IsVolatile() const { return is_volatile_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2562 | |
| 2563 | private: |
| 2564 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2565 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2566 | const bool is_volatile_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2567 | }; |
| 2568 | |
| 2569 | class HInstanceFieldGet : public HExpression<1> { |
| 2570 | public: |
| 2571 | HInstanceFieldGet(HInstruction* value, |
| 2572 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2573 | MemberOffset field_offset, |
| 2574 | bool is_volatile) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2575 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2576 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2577 | SetRawInputAt(0, value); |
| 2578 | } |
| 2579 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 2580 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2581 | |
| 2582 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2583 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 2584 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2587 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2588 | return GetFieldOffset().Uint32Value() < kPageSize; |
| 2589 | } |
| 2590 | |
| 2591 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2592 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 2593 | } |
| 2594 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2595 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2596 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2597 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2598 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2599 | |
| 2600 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 2601 | |
| 2602 | private: |
| 2603 | const FieldInfo field_info_; |
| 2604 | |
| 2605 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 2606 | }; |
| 2607 | |
| 2608 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 2609 | public: |
| 2610 | HInstanceFieldSet(HInstruction* object, |
| 2611 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2612 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2613 | MemberOffset field_offset, |
| 2614 | bool is_volatile) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2615 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2616 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2617 | SetRawInputAt(0, object); |
| 2618 | SetRawInputAt(1, value); |
| 2619 | } |
| 2620 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2621 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2622 | return GetFieldOffset().Uint32Value() < kPageSize; |
| 2623 | } |
| 2624 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2625 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2626 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2627 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2628 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2629 | HInstruction* GetValue() const { return InputAt(1); } |
| 2630 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2631 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 2632 | |
| 2633 | private: |
| 2634 | const FieldInfo field_info_; |
| 2635 | |
| 2636 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 2637 | }; |
| 2638 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2639 | class HArrayGet : public HExpression<2> { |
| 2640 | public: |
| 2641 | HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2642 | : HExpression(type, SideEffects::DependsOnSomething()) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2643 | SetRawInputAt(0, array); |
| 2644 | SetRawInputAt(1, index); |
| 2645 | } |
| 2646 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2647 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2648 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2649 | UNUSED(other); |
| 2650 | return true; |
| 2651 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2652 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2653 | // TODO: We can be smarter here. |
| 2654 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 2655 | // which generates the implicit null check. There are cases when these can be removed |
| 2656 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 2657 | // implicit check here (as long as the address falls in the first page). |
| 2658 | return false; |
| 2659 | } |
| 2660 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2661 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2662 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2663 | HInstruction* GetArray() const { return InputAt(0); } |
| 2664 | HInstruction* GetIndex() const { return InputAt(1); } |
| 2665 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2666 | DECLARE_INSTRUCTION(ArrayGet); |
| 2667 | |
| 2668 | private: |
| 2669 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 2670 | }; |
| 2671 | |
| 2672 | class HArraySet : public HTemplateInstruction<3> { |
| 2673 | public: |
| 2674 | HArraySet(HInstruction* array, |
| 2675 | HInstruction* index, |
| 2676 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2677 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2678 | uint32_t dex_pc) |
| 2679 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
| 2680 | dex_pc_(dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2681 | expected_component_type_(expected_component_type), |
| 2682 | needs_type_check_(value->GetType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2683 | SetRawInputAt(0, array); |
| 2684 | SetRawInputAt(1, index); |
| 2685 | SetRawInputAt(2, value); |
| 2686 | } |
| 2687 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2688 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2689 | // We currently always call a runtime method to catch array store |
| 2690 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2691 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2694 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2695 | // TODO: Same as for ArrayGet. |
| 2696 | return false; |
| 2697 | } |
| 2698 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2699 | void ClearNeedsTypeCheck() { |
| 2700 | needs_type_check_ = false; |
| 2701 | } |
| 2702 | |
| 2703 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 2704 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2705 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2706 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2707 | HInstruction* GetArray() const { return InputAt(0); } |
| 2708 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2709 | HInstruction* GetValue() const { return InputAt(2); } |
| 2710 | |
| 2711 | Primitive::Type GetComponentType() const { |
| 2712 | // The Dex format does not type floating point index operations. Since the |
| 2713 | // `expected_component_type_` is set during building and can therefore not |
| 2714 | // be correct, we also check what is the value type. If it is a floating |
| 2715 | // point type, we must use that type. |
| 2716 | Primitive::Type value_type = GetValue()->GetType(); |
| 2717 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 2718 | ? value_type |
| 2719 | : expected_component_type_; |
| 2720 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2721 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2722 | DECLARE_INSTRUCTION(ArraySet); |
| 2723 | |
| 2724 | private: |
| 2725 | const uint32_t dex_pc_; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2726 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2727 | bool needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2728 | |
| 2729 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 2730 | }; |
| 2731 | |
| 2732 | class HArrayLength : public HExpression<1> { |
| 2733 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2734 | explicit HArrayLength(HInstruction* array) |
| 2735 | : HExpression(Primitive::kPrimInt, SideEffects::None()) { |
| 2736 | // Note that arrays do not change length, so the instruction does not |
| 2737 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2738 | SetRawInputAt(0, array); |
| 2739 | } |
| 2740 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2741 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2742 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2743 | UNUSED(other); |
| 2744 | return true; |
| 2745 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2746 | bool CanDoImplicitNullCheck() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2747 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2748 | DECLARE_INSTRUCTION(ArrayLength); |
| 2749 | |
| 2750 | private: |
| 2751 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 2752 | }; |
| 2753 | |
| 2754 | class HBoundsCheck : public HExpression<2> { |
| 2755 | public: |
| 2756 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2757 | : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2758 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 2759 | SetRawInputAt(0, index); |
| 2760 | SetRawInputAt(1, length); |
| 2761 | } |
| 2762 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2763 | virtual bool CanBeMoved() const { return true; } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2764 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 2765 | UNUSED(other); |
| 2766 | return true; |
| 2767 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2768 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2769 | virtual bool NeedsEnvironment() const { return true; } |
| 2770 | |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 2771 | virtual bool CanThrow() const { return true; } |
| 2772 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2773 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2774 | |
| 2775 | DECLARE_INSTRUCTION(BoundsCheck); |
| 2776 | |
| 2777 | private: |
| 2778 | const uint32_t dex_pc_; |
| 2779 | |
| 2780 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 2781 | }; |
| 2782 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2783 | /** |
| 2784 | * Some DEX instructions are folded into multiple HInstructions that need |
| 2785 | * to stay live until the last HInstruction. This class |
| 2786 | * is used as a marker for the baseline compiler to ensure its preceding |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 2787 | * HInstruction stays live. `index` represents the stack location index of the |
| 2788 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2789 | */ |
| 2790 | class HTemporary : public HTemplateInstruction<0> { |
| 2791 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2792 | explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2793 | |
| 2794 | size_t GetIndex() const { return index_; } |
| 2795 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 2796 | Primitive::Type GetType() const OVERRIDE { |
| 2797 | // The previous instruction is the one that will be stored in the temporary location. |
| 2798 | DCHECK(GetPrevious() != nullptr); |
| 2799 | return GetPrevious()->GetType(); |
| 2800 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 2801 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2802 | DECLARE_INSTRUCTION(Temporary); |
| 2803 | |
| 2804 | private: |
| 2805 | const size_t index_; |
| 2806 | |
| 2807 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 2808 | }; |
| 2809 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 2810 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 2811 | public: |
| 2812 | explicit HSuspendCheck(uint32_t dex_pc) |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 2813 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 2814 | |
| 2815 | virtual bool NeedsEnvironment() const { |
| 2816 | return true; |
| 2817 | } |
| 2818 | |
| 2819 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2820 | |
| 2821 | DECLARE_INSTRUCTION(SuspendCheck); |
| 2822 | |
| 2823 | private: |
| 2824 | const uint32_t dex_pc_; |
| 2825 | |
| 2826 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 2827 | }; |
| 2828 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2829 | /** |
| 2830 | * Instruction to load a Class object. |
| 2831 | */ |
| 2832 | class HLoadClass : public HExpression<0> { |
| 2833 | public: |
| 2834 | HLoadClass(uint16_t type_index, |
| 2835 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2836 | uint32_t dex_pc) |
| 2837 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2838 | type_index_(type_index), |
| 2839 | is_referrers_class_(is_referrers_class), |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2840 | dex_pc_(dex_pc), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 2841 | generate_clinit_check_(false), |
| 2842 | loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2843 | |
| 2844 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2845 | |
| 2846 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2847 | return other->AsLoadClass()->type_index_ == type_index_; |
| 2848 | } |
| 2849 | |
| 2850 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 2851 | |
| 2852 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2853 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2854 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2855 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2856 | bool NeedsEnvironment() const OVERRIDE { |
| 2857 | // Will call runtime and load the class if the class is not loaded yet. |
| 2858 | // TODO: finer grain decision. |
| 2859 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2860 | } |
| 2861 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2862 | bool MustGenerateClinitCheck() const { |
| 2863 | return generate_clinit_check_; |
| 2864 | } |
| 2865 | |
| 2866 | void SetMustGenerateClinitCheck() { |
| 2867 | generate_clinit_check_ = true; |
| 2868 | } |
| 2869 | |
| 2870 | bool CanCallRuntime() const { |
| 2871 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 2872 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2873 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 2874 | bool CanThrow() const OVERRIDE { |
| 2875 | // May call runtime and and therefore can throw. |
| 2876 | // TODO: finer grain decision. |
| 2877 | return !is_referrers_class_; |
| 2878 | } |
| 2879 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2880 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 2881 | return loaded_class_rti_; |
| 2882 | } |
| 2883 | |
| 2884 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 2885 | // Make sure we only set exact types (the loaded class should never be merged). |
| 2886 | DCHECK(rti.IsExact()); |
| 2887 | loaded_class_rti_ = rti; |
| 2888 | } |
| 2889 | |
| 2890 | bool IsResolved() { |
| 2891 | return loaded_class_rti_.IsExact(); |
| 2892 | } |
| 2893 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2894 | DECLARE_INSTRUCTION(LoadClass); |
| 2895 | |
| 2896 | private: |
| 2897 | const uint16_t type_index_; |
| 2898 | const bool is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2899 | const uint32_t dex_pc_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2900 | // Whether this instruction must generate the initialization check. |
| 2901 | // Used for code generation. |
| 2902 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2903 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2904 | ReferenceTypeInfo loaded_class_rti_; |
| 2905 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2906 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 2907 | }; |
| 2908 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 2909 | class HLoadString : public HExpression<0> { |
| 2910 | public: |
| 2911 | HLoadString(uint32_t string_index, uint32_t dex_pc) |
| 2912 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2913 | string_index_(string_index), |
| 2914 | dex_pc_(dex_pc) {} |
| 2915 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2916 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2917 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 2918 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2919 | return other->AsLoadString()->string_index_ == string_index_; |
| 2920 | } |
| 2921 | |
| 2922 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 2923 | |
| 2924 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2925 | uint32_t GetStringIndex() const { return string_index_; } |
| 2926 | |
| 2927 | // TODO: Can we deopt or debug when we resolve a string? |
| 2928 | bool NeedsEnvironment() const OVERRIDE { return false; } |
| 2929 | |
| 2930 | DECLARE_INSTRUCTION(LoadString); |
| 2931 | |
| 2932 | private: |
| 2933 | const uint32_t string_index_; |
| 2934 | const uint32_t dex_pc_; |
| 2935 | |
| 2936 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 2937 | }; |
| 2938 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2939 | // TODO: Pass this check to HInvokeStaticOrDirect nodes. |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2940 | /** |
| 2941 | * Performs an initialization check on its Class object input. |
| 2942 | */ |
| 2943 | class HClinitCheck : public HExpression<1> { |
| 2944 | public: |
| 2945 | explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
| 2946 | : HExpression(Primitive::kPrimNot, SideEffects::All()), |
| 2947 | dex_pc_(dex_pc) { |
| 2948 | SetRawInputAt(0, constant); |
| 2949 | } |
| 2950 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2951 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2952 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2953 | UNUSED(other); |
| 2954 | return true; |
| 2955 | } |
| 2956 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2957 | bool NeedsEnvironment() const OVERRIDE { |
| 2958 | // May call runtime to initialize the class. |
| 2959 | return true; |
| 2960 | } |
| 2961 | |
| 2962 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2963 | |
| 2964 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 2965 | |
| 2966 | DECLARE_INSTRUCTION(ClinitCheck); |
| 2967 | |
| 2968 | private: |
| 2969 | const uint32_t dex_pc_; |
| 2970 | |
| 2971 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 2972 | }; |
| 2973 | |
| 2974 | class HStaticFieldGet : public HExpression<1> { |
| 2975 | public: |
| 2976 | HStaticFieldGet(HInstruction* cls, |
| 2977 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2978 | MemberOffset field_offset, |
| 2979 | bool is_volatile) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2980 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2981 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2982 | SetRawInputAt(0, cls); |
| 2983 | } |
| 2984 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2985 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 2986 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2987 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2988 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2989 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 2990 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | size_t ComputeHashCode() const OVERRIDE { |
| 2994 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 2995 | } |
| 2996 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2997 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2998 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 2999 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3000 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3001 | |
| 3002 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 3003 | |
| 3004 | private: |
| 3005 | const FieldInfo field_info_; |
| 3006 | |
| 3007 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 3008 | }; |
| 3009 | |
| 3010 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 3011 | public: |
| 3012 | HStaticFieldSet(HInstruction* cls, |
| 3013 | HInstruction* value, |
| 3014 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3015 | MemberOffset field_offset, |
| 3016 | bool is_volatile) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3017 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3018 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3019 | SetRawInputAt(0, cls); |
| 3020 | SetRawInputAt(1, value); |
| 3021 | } |
| 3022 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3023 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3024 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3025 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3026 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3027 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3028 | HInstruction* GetValue() const { return InputAt(1); } |
| 3029 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3030 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 3031 | |
| 3032 | private: |
| 3033 | const FieldInfo field_info_; |
| 3034 | |
| 3035 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 3036 | }; |
| 3037 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3038 | // Implement the move-exception DEX instruction. |
| 3039 | class HLoadException : public HExpression<0> { |
| 3040 | public: |
| 3041 | HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {} |
| 3042 | |
| 3043 | DECLARE_INSTRUCTION(LoadException); |
| 3044 | |
| 3045 | private: |
| 3046 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 3047 | }; |
| 3048 | |
| 3049 | class HThrow : public HTemplateInstruction<1> { |
| 3050 | public: |
| 3051 | HThrow(HInstruction* exception, uint32_t dex_pc) |
| 3052 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) { |
| 3053 | SetRawInputAt(0, exception); |
| 3054 | } |
| 3055 | |
| 3056 | bool IsControlFlow() const OVERRIDE { return true; } |
| 3057 | |
| 3058 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3059 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3060 | bool CanThrow() const OVERRIDE { return true; } |
| 3061 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3062 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3063 | |
| 3064 | DECLARE_INSTRUCTION(Throw); |
| 3065 | |
| 3066 | private: |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3067 | const uint32_t dex_pc_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3068 | |
| 3069 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 3070 | }; |
| 3071 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3072 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3073 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3074 | HInstanceOf(HInstruction* object, |
| 3075 | HLoadClass* constant, |
| 3076 | bool class_is_final, |
| 3077 | uint32_t dex_pc) |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3078 | : HExpression(Primitive::kPrimBoolean, SideEffects::None()), |
| 3079 | class_is_final_(class_is_final), |
| 3080 | dex_pc_(dex_pc) { |
| 3081 | SetRawInputAt(0, object); |
| 3082 | SetRawInputAt(1, constant); |
| 3083 | } |
| 3084 | |
| 3085 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3086 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3087 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3088 | return true; |
| 3089 | } |
| 3090 | |
| 3091 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3092 | return false; |
| 3093 | } |
| 3094 | |
| 3095 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3096 | |
| 3097 | bool IsClassFinal() const { return class_is_final_; } |
| 3098 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3099 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3100 | |
| 3101 | private: |
| 3102 | const bool class_is_final_; |
| 3103 | const uint32_t dex_pc_; |
| 3104 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3105 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 3106 | }; |
| 3107 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3108 | class HBoundType : public HExpression<1> { |
| 3109 | public: |
| 3110 | HBoundType(HInstruction* input, ReferenceTypeInfo bound_type) |
| 3111 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3112 | bound_type_(bound_type) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 3113 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3114 | SetRawInputAt(0, input); |
| 3115 | } |
| 3116 | |
| 3117 | const ReferenceTypeInfo& GetBoundType() const { return bound_type_; } |
| 3118 | |
| 3119 | bool CanBeNull() const OVERRIDE { |
| 3120 | // `null instanceof ClassX` always return false so we can't be null. |
| 3121 | return false; |
| 3122 | } |
| 3123 | |
| 3124 | DECLARE_INSTRUCTION(BoundType); |
| 3125 | |
| 3126 | private: |
| 3127 | // Encodes the most upper class that this instruction can have. In other words |
| 3128 | // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()). |
| 3129 | // It is used to bound the type in cases like `if (x instanceof ClassX) {}` |
| 3130 | const ReferenceTypeInfo bound_type_; |
| 3131 | |
| 3132 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 3133 | }; |
| 3134 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3135 | class HCheckCast : public HTemplateInstruction<2> { |
| 3136 | public: |
| 3137 | HCheckCast(HInstruction* object, |
| 3138 | HLoadClass* constant, |
| 3139 | bool class_is_final, |
| 3140 | uint32_t dex_pc) |
| 3141 | : HTemplateInstruction(SideEffects::None()), |
| 3142 | class_is_final_(class_is_final), |
| 3143 | dex_pc_(dex_pc) { |
| 3144 | SetRawInputAt(0, object); |
| 3145 | SetRawInputAt(1, constant); |
| 3146 | } |
| 3147 | |
| 3148 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3149 | |
| 3150 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3151 | return true; |
| 3152 | } |
| 3153 | |
| 3154 | bool NeedsEnvironment() const OVERRIDE { |
| 3155 | // Instruction may throw a CheckCastError. |
| 3156 | return true; |
| 3157 | } |
| 3158 | |
| 3159 | bool CanThrow() const OVERRIDE { return true; } |
| 3160 | |
| 3161 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3162 | |
| 3163 | bool IsClassFinal() const { return class_is_final_; } |
| 3164 | |
| 3165 | DECLARE_INSTRUCTION(CheckCast); |
| 3166 | |
| 3167 | private: |
| 3168 | const bool class_is_final_; |
| 3169 | const uint32_t dex_pc_; |
| 3170 | |
| 3171 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3172 | }; |
| 3173 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3174 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 3175 | public: |
| 3176 | enum OperationKind { |
| 3177 | kEnter, |
| 3178 | kExit, |
| 3179 | }; |
| 3180 | |
| 3181 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
| 3182 | : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) { |
| 3183 | SetRawInputAt(0, object); |
| 3184 | } |
| 3185 | |
| 3186 | // Instruction may throw a Java exception, so we need an environment. |
| 3187 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3188 | bool CanThrow() const OVERRIDE { return true; } |
| 3189 | |
| 3190 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3191 | |
| 3192 | bool IsEnter() const { return kind_ == kEnter; } |
| 3193 | |
| 3194 | DECLARE_INSTRUCTION(MonitorOperation); |
| 3195 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3196 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3197 | const OperationKind kind_; |
| 3198 | const uint32_t dex_pc_; |
| 3199 | |
| 3200 | private: |
| 3201 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 3202 | }; |
| 3203 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3204 | class MoveOperands : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3205 | public: |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3206 | MoveOperands(Location source, Location destination, HInstruction* instruction) |
| 3207 | : source_(source), destination_(destination), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3208 | |
| 3209 | Location GetSource() const { return source_; } |
| 3210 | Location GetDestination() const { return destination_; } |
| 3211 | |
| 3212 | void SetSource(Location value) { source_ = value; } |
| 3213 | void SetDestination(Location value) { destination_ = value; } |
| 3214 | |
| 3215 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 3216 | // destination (but not the source). |
| 3217 | Location MarkPending() { |
| 3218 | DCHECK(!IsPending()); |
| 3219 | Location dest = destination_; |
| 3220 | destination_ = Location::NoLocation(); |
| 3221 | return dest; |
| 3222 | } |
| 3223 | |
| 3224 | void ClearPending(Location dest) { |
| 3225 | DCHECK(IsPending()); |
| 3226 | destination_ = dest; |
| 3227 | } |
| 3228 | |
| 3229 | bool IsPending() const { |
| 3230 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3231 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 3232 | } |
| 3233 | |
| 3234 | // True if this blocks a move from the given location. |
| 3235 | bool Blocks(Location loc) const { |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3236 | return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | // A move is redundant if it's been eliminated, if its source and |
| 3240 | // destination are the same, or if its destination is unneeded. |
| 3241 | bool IsRedundant() const { |
| 3242 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 3243 | } |
| 3244 | |
| 3245 | // We clear both operands to indicate move that's been eliminated. |
| 3246 | void Eliminate() { |
| 3247 | source_ = destination_ = Location::NoLocation(); |
| 3248 | } |
| 3249 | |
| 3250 | bool IsEliminated() const { |
| 3251 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3252 | return source_.IsInvalid(); |
| 3253 | } |
| 3254 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3255 | HInstruction* GetInstruction() const { return instruction_; } |
| 3256 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3257 | private: |
| 3258 | Location source_; |
| 3259 | Location destination_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3260 | // The instruction this move is assocatied with. Null when this move is |
| 3261 | // for moving an input in the expected locations of user (including a phi user). |
| 3262 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 3263 | // in the same parallel move. |
| 3264 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3265 | }; |
| 3266 | |
| 3267 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 3268 | |
| 3269 | class HParallelMove : public HTemplateInstruction<0> { |
| 3270 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3271 | explicit HParallelMove(ArenaAllocator* arena) |
| 3272 | : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3273 | |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3274 | void AddMove(Location source, Location destination, HInstruction* instruction) { |
| 3275 | DCHECK(source.IsValid()); |
| 3276 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3277 | if (kIsDebugBuild) { |
| 3278 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3279 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3280 | DCHECK_NE(moves_.Get(i).GetInstruction(), instruction) |
| 3281 | << "Doing parallel moves for the same instruction."; |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3282 | } |
| 3283 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3284 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
| 3285 | DCHECK(!destination.Equals(moves_.Get(i).GetDestination())) |
| 3286 | << "Same destination for two moves in a parallel move."; |
| 3287 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3288 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3289 | moves_.Add(MoveOperands(source, destination, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3293 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | size_t NumMoves() const { return moves_.Size(); } |
| 3297 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3298 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3299 | |
| 3300 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3301 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3302 | |
| 3303 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 3304 | }; |
| 3305 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3306 | class HGraphVisitor : public ValueObject { |
| 3307 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3308 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 3309 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3310 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3311 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3312 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 3313 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3314 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3315 | void VisitInsertionOrder(); |
| 3316 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3317 | // Visit the graph following dominator tree reverse post-order. |
| 3318 | void VisitReversePostOrder(); |
| 3319 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 3320 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 3321 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3322 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3323 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3324 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 3325 | |
| 3326 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 3327 | |
| 3328 | #undef DECLARE_VISIT_INSTRUCTION |
| 3329 | |
| 3330 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 3331 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3332 | |
| 3333 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 3334 | }; |
| 3335 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3336 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 3337 | public: |
| 3338 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 3339 | virtual ~HGraphDelegateVisitor() {} |
| 3340 | |
| 3341 | // Visit functions that delegate to to super class. |
| 3342 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
| 3343 | virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
| 3344 | |
| 3345 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 3346 | |
| 3347 | #undef DECLARE_VISIT_INSTRUCTION |
| 3348 | |
| 3349 | private: |
| 3350 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 3351 | }; |
| 3352 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3353 | class HInsertionOrderIterator : public ValueObject { |
| 3354 | public: |
| 3355 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 3356 | |
| 3357 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 3358 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 3359 | void Advance() { ++index_; } |
| 3360 | |
| 3361 | private: |
| 3362 | const HGraph& graph_; |
| 3363 | size_t index_; |
| 3364 | |
| 3365 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 3366 | }; |
| 3367 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3368 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3369 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3370 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3371 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3372 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 3373 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3374 | void Advance() { ++index_; } |
| 3375 | |
| 3376 | private: |
| 3377 | const HGraph& graph_; |
| 3378 | size_t index_; |
| 3379 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3380 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3381 | }; |
| 3382 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3383 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3384 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3385 | explicit HPostOrderIterator(const HGraph& graph) |
| 3386 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3387 | |
| 3388 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3389 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3390 | void Advance() { --index_; } |
| 3391 | |
| 3392 | private: |
| 3393 | const HGraph& graph_; |
| 3394 | size_t index_; |
| 3395 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3396 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3397 | }; |
| 3398 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3399 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 3400 | // of an inner loop. The order in which the blocks are iterated is on their |
| 3401 | // block id. |
| 3402 | class HBlocksInLoopIterator : public ValueObject { |
| 3403 | public: |
| 3404 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 3405 | : blocks_in_loop_(info.GetBlocks()), |
| 3406 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 3407 | index_(0) { |
| 3408 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 3409 | Advance(); |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | bool Done() const { return index_ == blocks_.Size(); } |
| 3414 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 3415 | void Advance() { |
| 3416 | ++index_; |
| 3417 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 3418 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | private: |
| 3425 | const BitVector& blocks_in_loop_; |
| 3426 | const GrowableArray<HBasicBlock*>& blocks_; |
| 3427 | size_t index_; |
| 3428 | |
| 3429 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 3430 | }; |
| 3431 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3432 | } // namespace art |
| 3433 | |
| 3434 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |