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