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