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