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