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