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