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 | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame^] | 1107 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1108 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1109 | // Does this instruction strictly dominate `other_instruction`? |
| 1110 | // Returns false if this instruction and `other_instruction` are the same. |
| 1111 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1112 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1113 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1114 | int GetId() const { return id_; } |
| 1115 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1116 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1117 | int GetSsaIndex() const { return ssa_index_; } |
| 1118 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1119 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1120 | |
| 1121 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1122 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1123 | void SetEnvironment(HEnvironment* environment) { environment_ = environment; } |
| 1124 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1125 | // Returns the number of entries in the environment. Typically, that is the |
| 1126 | // number of dex registers in a method. It could be more in case of inlining. |
| 1127 | size_t EnvironmentSize() const; |
| 1128 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1129 | LocationSummary* GetLocations() const { return locations_; } |
| 1130 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1131 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1132 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1133 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1134 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1135 | // Move `this` instruction before `cursor`. |
| 1136 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1137 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1138 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1139 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1140 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1141 | virtual H##type* As##type() { return nullptr; } |
| 1142 | |
| 1143 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1144 | #undef INSTRUCTION_TYPE_CHECK |
| 1145 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1146 | // Returns whether the instruction can be moved within the graph. |
| 1147 | virtual bool CanBeMoved() const { return false; } |
| 1148 | |
| 1149 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1150 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1151 | UNUSED(other); |
| 1152 | return false; |
| 1153 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1154 | |
| 1155 | // Returns whether any data encoded in the two instructions is equal. |
| 1156 | // This method does not look at the inputs. Both instructions must be |
| 1157 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1158 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1159 | UNUSED(other); |
| 1160 | return false; |
| 1161 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1162 | |
| 1163 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1164 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1165 | // 2) Their inputs are identical. |
| 1166 | bool Equals(HInstruction* other) const; |
| 1167 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1168 | virtual InstructionKind GetKind() const = 0; |
| 1169 | |
| 1170 | virtual size_t ComputeHashCode() const { |
| 1171 | size_t result = GetKind(); |
| 1172 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1173 | result = (result * 31) + InputAt(i)->GetId(); |
| 1174 | } |
| 1175 | return result; |
| 1176 | } |
| 1177 | |
| 1178 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1179 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1180 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1181 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1182 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1183 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1184 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1185 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1186 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1187 | |
| 1188 | // Returns whether the code generation of the instruction will require to have access |
| 1189 | // to the current method. Such instructions are: |
| 1190 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1191 | // to walk the stack and have the current method stored at a specific stack address. |
| 1192 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1193 | // fields of the current method. |
| 1194 | bool NeedsCurrentMethod() const { |
| 1195 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1196 | } |
| 1197 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1198 | protected: |
| 1199 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1200 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1201 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1202 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1203 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1204 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1205 | HInstruction* previous_; |
| 1206 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1207 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1208 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1209 | // An instruction gets an id when it is added to the graph. |
| 1210 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1211 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1212 | int id_; |
| 1213 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1214 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1215 | int ssa_index_; |
| 1216 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1217 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1218 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1219 | |
| 1220 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1221 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1222 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1223 | // The environment associated with this instruction. Not null if the instruction |
| 1224 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1225 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1226 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1227 | // Set by the code generator. |
| 1228 | LocationSummary* locations_; |
| 1229 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1230 | // Set by the liveness analysis. |
| 1231 | LiveInterval* live_interval_; |
| 1232 | |
| 1233 | // Set by the liveness analysis, this is the position in a linear |
| 1234 | // order of blocks where this instruction's live interval start. |
| 1235 | size_t lifetime_position_; |
| 1236 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1237 | const SideEffects side_effects_; |
| 1238 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1239 | // TODO: for primitive types this should be marked as invalid. |
| 1240 | ReferenceTypeInfo reference_type_info_; |
| 1241 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1242 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1243 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1244 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1245 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1246 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1247 | |
| 1248 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1249 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1250 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1251 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1252 | class HInputIterator : public ValueObject { |
| 1253 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1254 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1255 | |
| 1256 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1257 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1258 | void Advance() { index_++; } |
| 1259 | |
| 1260 | private: |
| 1261 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1262 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1263 | |
| 1264 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 1265 | }; |
| 1266 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1267 | class HInstructionIterator : public ValueObject { |
| 1268 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1269 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 1270 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1271 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1274 | bool Done() const { return instruction_ == nullptr; } |
| 1275 | HInstruction* Current() const { return instruction_; } |
| 1276 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1277 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1278 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | private: |
| 1282 | HInstruction* instruction_; |
| 1283 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1284 | |
| 1285 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1286 | }; |
| 1287 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1288 | class HBackwardInstructionIterator : public ValueObject { |
| 1289 | public: |
| 1290 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 1291 | : instruction_(instructions.last_instruction_) { |
| 1292 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1293 | } |
| 1294 | |
| 1295 | bool Done() const { return instruction_ == nullptr; } |
| 1296 | HInstruction* Current() const { return instruction_; } |
| 1297 | void Advance() { |
| 1298 | instruction_ = next_; |
| 1299 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1300 | } |
| 1301 | |
| 1302 | private: |
| 1303 | HInstruction* instruction_; |
| 1304 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1305 | |
| 1306 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1307 | }; |
| 1308 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1309 | // An embedded container with N elements of type T. Used (with partial |
| 1310 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 1311 | template<typename T, intptr_t N> |
| 1312 | class EmbeddedArray { |
| 1313 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1314 | EmbeddedArray() : elements_() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1315 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1316 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1317 | |
| 1318 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1319 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1320 | return elements_[i]; |
| 1321 | } |
| 1322 | |
| 1323 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1324 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1325 | return elements_[i]; |
| 1326 | } |
| 1327 | |
| 1328 | const T& At(intptr_t i) const { |
| 1329 | return (*this)[i]; |
| 1330 | } |
| 1331 | |
| 1332 | void SetAt(intptr_t i, const T& val) { |
| 1333 | (*this)[i] = val; |
| 1334 | } |
| 1335 | |
| 1336 | private: |
| 1337 | T elements_[N]; |
| 1338 | }; |
| 1339 | |
| 1340 | template<typename T> |
| 1341 | class EmbeddedArray<T, 0> { |
| 1342 | public: |
| 1343 | intptr_t length() const { return 0; } |
| 1344 | const T& operator[](intptr_t i) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1345 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1346 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1347 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1348 | } |
| 1349 | T& operator[](intptr_t i) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1350 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1351 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1352 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1353 | } |
| 1354 | }; |
| 1355 | |
| 1356 | template<intptr_t N> |
| 1357 | class HTemplateInstruction: public HInstruction { |
| 1358 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1359 | HTemplateInstruction<N>(SideEffects side_effects) |
| 1360 | : HInstruction(side_effects), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1361 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1362 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1363 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1364 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1365 | protected: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1366 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; } |
| 1367 | |
| 1368 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 1369 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1372 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1373 | EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1374 | |
| 1375 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1376 | }; |
| 1377 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1378 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1379 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1380 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1381 | HExpression<N>(Primitive::Type type, SideEffects side_effects) |
| 1382 | : HTemplateInstruction<N>(side_effects), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1383 | virtual ~HExpression() {} |
| 1384 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1385 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1386 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1387 | protected: |
| 1388 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1389 | }; |
| 1390 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1391 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 1392 | // instruction that branches to the exit block. |
| 1393 | class HReturnVoid : public HTemplateInstruction<0> { |
| 1394 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1395 | HReturnVoid() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1396 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1397 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1398 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1399 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1400 | |
| 1401 | private: |
| 1402 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 1403 | }; |
| 1404 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1405 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 1406 | // instruction that branches to the exit block. |
| 1407 | class HReturn : public HTemplateInstruction<1> { |
| 1408 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1409 | explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1410 | SetRawInputAt(0, value); |
| 1411 | } |
| 1412 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1413 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1414 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1415 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1416 | |
| 1417 | private: |
| 1418 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 1419 | }; |
| 1420 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1421 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1422 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1423 | // exit block. |
| 1424 | class HExit : public HTemplateInstruction<0> { |
| 1425 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1426 | HExit() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1427 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1428 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1429 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1430 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1431 | |
| 1432 | private: |
| 1433 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 1434 | }; |
| 1435 | |
| 1436 | // Jumps from one block to another. |
| 1437 | class HGoto : public HTemplateInstruction<0> { |
| 1438 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1439 | HGoto() : HTemplateInstruction(SideEffects::None()) {} |
| 1440 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1441 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1442 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1443 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1444 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1447 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1448 | |
| 1449 | private: |
| 1450 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 1451 | }; |
| 1452 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1453 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1454 | // Conditional branch. A block ending with an HIf instruction must have |
| 1455 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1456 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1457 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1458 | explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1459 | SetRawInputAt(0, input); |
| 1460 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1461 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1462 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1463 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1464 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1465 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1469 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1472 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1473 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1474 | virtual bool IsIfInstruction() const { return true; } |
| 1475 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1476 | private: |
| 1477 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 1478 | }; |
| 1479 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1480 | class HUnaryOperation : public HExpression<1> { |
| 1481 | public: |
| 1482 | HUnaryOperation(Primitive::Type result_type, HInstruction* input) |
| 1483 | : HExpression(result_type, SideEffects::None()) { |
| 1484 | SetRawInputAt(0, input); |
| 1485 | } |
| 1486 | |
| 1487 | HInstruction* GetInput() const { return InputAt(0); } |
| 1488 | Primitive::Type GetResultType() const { return GetType(); } |
| 1489 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1490 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1491 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1492 | UNUSED(other); |
| 1493 | return true; |
| 1494 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1495 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1496 | // Try to statically evaluate `operation` and return a HConstant |
| 1497 | // containing the result of this evaluation. If `operation` cannot |
| 1498 | // be evaluated as a constant, return nullptr. |
| 1499 | HConstant* TryStaticEvaluation() const; |
| 1500 | |
| 1501 | // Apply this operation to `x`. |
| 1502 | virtual int32_t Evaluate(int32_t x) const = 0; |
| 1503 | virtual int64_t Evaluate(int64_t x) const = 0; |
| 1504 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1505 | DECLARE_INSTRUCTION(UnaryOperation); |
| 1506 | |
| 1507 | private: |
| 1508 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 1509 | }; |
| 1510 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1511 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1512 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1513 | HBinaryOperation(Primitive::Type result_type, |
| 1514 | HInstruction* left, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1515 | HInstruction* right) : HExpression(result_type, SideEffects::None()) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1516 | SetRawInputAt(0, left); |
| 1517 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1520 | HInstruction* GetLeft() const { return InputAt(0); } |
| 1521 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1522 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1523 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1524 | virtual bool IsCommutative() const { return false; } |
| 1525 | |
| 1526 | // Put constant on the right. |
| 1527 | // Returns whether order is changed. |
| 1528 | bool OrderInputsWithConstantOnTheRight() { |
| 1529 | HInstruction* left = InputAt(0); |
| 1530 | HInstruction* right = InputAt(1); |
| 1531 | if (left->IsConstant() && !right->IsConstant()) { |
| 1532 | ReplaceInput(right, 0); |
| 1533 | ReplaceInput(left, 1); |
| 1534 | return true; |
| 1535 | } |
| 1536 | return false; |
| 1537 | } |
| 1538 | |
| 1539 | // Order inputs by instruction id, but favor constant on the right side. |
| 1540 | // This helps GVN for commutative ops. |
| 1541 | void OrderInputs() { |
| 1542 | DCHECK(IsCommutative()); |
| 1543 | HInstruction* left = InputAt(0); |
| 1544 | HInstruction* right = InputAt(1); |
| 1545 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 1546 | return; |
| 1547 | } |
| 1548 | if (OrderInputsWithConstantOnTheRight()) { |
| 1549 | return; |
| 1550 | } |
| 1551 | // Order according to instruction id. |
| 1552 | if (left->GetId() > right->GetId()) { |
| 1553 | ReplaceInput(right, 0); |
| 1554 | ReplaceInput(left, 1); |
| 1555 | } |
| 1556 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1557 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1558 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1559 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1560 | UNUSED(other); |
| 1561 | return true; |
| 1562 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1563 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1564 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1565 | // containing the result of this evaluation. If `operation` cannot |
| 1566 | // be evaluated as a constant, return nullptr. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1567 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1568 | |
| 1569 | // Apply this operation to `x` and `y`. |
| 1570 | virtual int32_t Evaluate(int32_t x, int32_t y) const = 0; |
| 1571 | virtual int64_t Evaluate(int64_t x, int64_t y) const = 0; |
| 1572 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1573 | DECLARE_INSTRUCTION(BinaryOperation); |
| 1574 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1575 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1576 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 1577 | }; |
| 1578 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1579 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1580 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1581 | HCondition(HInstruction* first, HInstruction* second) |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1582 | : HBinaryOperation(Primitive::kPrimBoolean, first, second), |
| 1583 | needs_materialization_(true) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1584 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1585 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 1586 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1587 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1588 | // For code generation purposes, returns whether this instruction is just before |
| 1589 | // `if_`, and disregard moves in between. |
| 1590 | bool IsBeforeWhenDisregardMoves(HIf* if_) const; |
| 1591 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1592 | DECLARE_INSTRUCTION(Condition); |
| 1593 | |
| 1594 | virtual IfCondition GetCondition() const = 0; |
| 1595 | |
| 1596 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1597 | // For register allocation purposes, returns whether this instruction needs to be |
| 1598 | // materialized (that is, not just be in the processor flags). |
| 1599 | bool needs_materialization_; |
| 1600 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1601 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 1602 | }; |
| 1603 | |
| 1604 | // Instruction to check if two inputs are equal to each other. |
| 1605 | class HEqual : public HCondition { |
| 1606 | public: |
| 1607 | HEqual(HInstruction* first, HInstruction* second) |
| 1608 | : HCondition(first, second) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1609 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1610 | bool IsCommutative() const OVERRIDE { return true; } |
| 1611 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1612 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1613 | return x == y ? 1 : 0; |
| 1614 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1615 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1616 | return x == y ? 1 : 0; |
| 1617 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1618 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1619 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1620 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1621 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1622 | return kCondEQ; |
| 1623 | } |
| 1624 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1625 | private: |
| 1626 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 1627 | }; |
| 1628 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1629 | class HNotEqual : public HCondition { |
| 1630 | public: |
| 1631 | HNotEqual(HInstruction* first, HInstruction* second) |
| 1632 | : HCondition(first, second) {} |
| 1633 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1634 | bool IsCommutative() const OVERRIDE { return true; } |
| 1635 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1636 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1637 | return x != y ? 1 : 0; |
| 1638 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1639 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1640 | return x != y ? 1 : 0; |
| 1641 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1642 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1643 | DECLARE_INSTRUCTION(NotEqual); |
| 1644 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1645 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1646 | return kCondNE; |
| 1647 | } |
| 1648 | |
| 1649 | private: |
| 1650 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 1651 | }; |
| 1652 | |
| 1653 | class HLessThan : public HCondition { |
| 1654 | public: |
| 1655 | HLessThan(HInstruction* first, HInstruction* second) |
| 1656 | : HCondition(first, second) {} |
| 1657 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1658 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1659 | return x < y ? 1 : 0; |
| 1660 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1661 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1662 | return x < y ? 1 : 0; |
| 1663 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1664 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1665 | DECLARE_INSTRUCTION(LessThan); |
| 1666 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1667 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1668 | return kCondLT; |
| 1669 | } |
| 1670 | |
| 1671 | private: |
| 1672 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 1673 | }; |
| 1674 | |
| 1675 | class HLessThanOrEqual : public HCondition { |
| 1676 | public: |
| 1677 | HLessThanOrEqual(HInstruction* first, HInstruction* second) |
| 1678 | : HCondition(first, second) {} |
| 1679 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1680 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1681 | return x <= y ? 1 : 0; |
| 1682 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1683 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1684 | return x <= y ? 1 : 0; |
| 1685 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1686 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1687 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 1688 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1689 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1690 | return kCondLE; |
| 1691 | } |
| 1692 | |
| 1693 | private: |
| 1694 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 1695 | }; |
| 1696 | |
| 1697 | class HGreaterThan : public HCondition { |
| 1698 | public: |
| 1699 | HGreaterThan(HInstruction* first, HInstruction* second) |
| 1700 | : HCondition(first, second) {} |
| 1701 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1702 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1703 | return x > y ? 1 : 0; |
| 1704 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1705 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1706 | return x > y ? 1 : 0; |
| 1707 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1708 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1709 | DECLARE_INSTRUCTION(GreaterThan); |
| 1710 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1711 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1712 | return kCondGT; |
| 1713 | } |
| 1714 | |
| 1715 | private: |
| 1716 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 1717 | }; |
| 1718 | |
| 1719 | class HGreaterThanOrEqual : public HCondition { |
| 1720 | public: |
| 1721 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second) |
| 1722 | : HCondition(first, second) {} |
| 1723 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1724 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1725 | return x >= y ? 1 : 0; |
| 1726 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1727 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1728 | return x >= y ? 1 : 0; |
| 1729 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1730 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1731 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 1732 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1733 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1734 | return kCondGE; |
| 1735 | } |
| 1736 | |
| 1737 | private: |
| 1738 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 1739 | }; |
| 1740 | |
| 1741 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1742 | // Instruction to check how two inputs compare to each other. |
| 1743 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 1744 | class HCompare : public HBinaryOperation { |
| 1745 | public: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1746 | // The bias applies for floating point operations and indicates how NaN |
| 1747 | // comparisons are treated: |
| 1748 | enum Bias { |
| 1749 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 1750 | kGtBias, // return 1 for NaN comparisons |
| 1751 | kLtBias, // return -1 for NaN comparisons |
| 1752 | }; |
| 1753 | |
| 1754 | HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias) |
| 1755 | : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1756 | DCHECK_EQ(type, first->GetType()); |
| 1757 | DCHECK_EQ(type, second->GetType()); |
| 1758 | } |
| 1759 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1760 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1761 | return |
| 1762 | x == y ? 0 : |
| 1763 | x > y ? 1 : |
| 1764 | -1; |
| 1765 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1766 | |
| 1767 | int64_t Evaluate(int64_t x, int64_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 | } |
| 1773 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1774 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 1775 | return bias_ == other->AsCompare()->bias_; |
| 1776 | } |
| 1777 | |
| 1778 | bool IsGtBias() { return bias_ == kGtBias; } |
| 1779 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1780 | DECLARE_INSTRUCTION(Compare); |
| 1781 | |
| 1782 | private: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1783 | const Bias bias_; |
| 1784 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1785 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 1786 | }; |
| 1787 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1788 | // A local in the graph. Corresponds to a Dex register. |
| 1789 | class HLocal : public HTemplateInstruction<0> { |
| 1790 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1791 | explicit HLocal(uint16_t reg_number) |
| 1792 | : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1793 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1794 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1795 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1796 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1797 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1798 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1799 | // The Dex register number. |
| 1800 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1801 | |
| 1802 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 1803 | }; |
| 1804 | |
| 1805 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1806 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1807 | public: |
Roland Levillain | 5799fc0 | 2014-09-25 12:15:20 +0100 | [diff] [blame] | 1808 | HLoadLocal(HLocal* local, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1809 | : HExpression(type, SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1810 | SetRawInputAt(0, local); |
| 1811 | } |
| 1812 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1813 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 1814 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1815 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1816 | |
| 1817 | private: |
| 1818 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 1819 | }; |
| 1820 | |
| 1821 | // Store a value in a given local. This instruction has two inputs: the value |
| 1822 | // and the local. |
| 1823 | class HStoreLocal : public HTemplateInstruction<2> { |
| 1824 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1825 | HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1826 | SetRawInputAt(0, local); |
| 1827 | SetRawInputAt(1, value); |
| 1828 | } |
| 1829 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1830 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 1831 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1832 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1833 | |
| 1834 | private: |
| 1835 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 1836 | }; |
| 1837 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1838 | class HConstant : public HExpression<0> { |
| 1839 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1840 | explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {} |
| 1841 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1842 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1843 | |
| 1844 | DECLARE_INSTRUCTION(Constant); |
| 1845 | |
| 1846 | private: |
| 1847 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 1848 | }; |
| 1849 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1850 | class HFloatConstant : public HConstant { |
| 1851 | public: |
| 1852 | explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {} |
| 1853 | |
| 1854 | float GetValue() const { return value_; } |
| 1855 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1856 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1857 | return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) == |
| 1858 | bit_cast<float, int32_t>(value_); |
| 1859 | } |
| 1860 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1861 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1862 | |
| 1863 | DECLARE_INSTRUCTION(FloatConstant); |
| 1864 | |
| 1865 | private: |
| 1866 | const float value_; |
| 1867 | |
| 1868 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 1869 | }; |
| 1870 | |
| 1871 | class HDoubleConstant : public HConstant { |
| 1872 | public: |
| 1873 | explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {} |
| 1874 | |
| 1875 | double GetValue() const { return value_; } |
| 1876 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1877 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1878 | return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) == |
| 1879 | bit_cast<double, int64_t>(value_); |
| 1880 | } |
| 1881 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1882 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1883 | |
| 1884 | DECLARE_INSTRUCTION(DoubleConstant); |
| 1885 | |
| 1886 | private: |
| 1887 | const double value_; |
| 1888 | |
| 1889 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 1890 | }; |
| 1891 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1892 | class HNullConstant : public HConstant { |
| 1893 | public: |
| 1894 | HNullConstant() : HConstant(Primitive::kPrimNot) {} |
| 1895 | |
| 1896 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 1901 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1902 | bool ActAsNullConstant() const OVERRIDE { return true; } |
| 1903 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1904 | DECLARE_INSTRUCTION(NullConstant); |
| 1905 | |
| 1906 | private: |
| 1907 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 1908 | }; |
| 1909 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1910 | // Constants of the type int. Those can be from Dex instructions, or |
| 1911 | // synthesized (for example with the if-eqz instruction). |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1912 | class HIntConstant : public HConstant { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1913 | public: |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1914 | explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1915 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1916 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1917 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1918 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1919 | return other->AsIntConstant()->value_ == value_; |
| 1920 | } |
| 1921 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1922 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 1923 | |
| 1924 | // TODO: Null is represented by the `0` constant. In most cases we replace it |
| 1925 | // with a HNullConstant but we don't do it when comparing (a != null). This |
| 1926 | // method is an workaround until we fix the above. |
| 1927 | bool ActAsNullConstant() const OVERRIDE { return value_ == 0; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1928 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1929 | DECLARE_INSTRUCTION(IntConstant); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1930 | |
| 1931 | private: |
| 1932 | const int32_t value_; |
| 1933 | |
| 1934 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 1935 | }; |
| 1936 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1937 | class HLongConstant : public HConstant { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1938 | public: |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1939 | explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1940 | |
| 1941 | int64_t GetValue() const { return value_; } |
| 1942 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1943 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1944 | return other->AsLongConstant()->value_ == value_; |
| 1945 | } |
| 1946 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1947 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1948 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1949 | DECLARE_INSTRUCTION(LongConstant); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1950 | |
| 1951 | private: |
| 1952 | const int64_t value_; |
| 1953 | |
| 1954 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 1955 | }; |
| 1956 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1957 | enum class Intrinsics { |
| 1958 | #define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name, |
| 1959 | #include "intrinsics_list.h" |
| 1960 | kNone, |
| 1961 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 1962 | #undef INTRINSICS_LIST |
| 1963 | #undef OPTIMIZING_INTRINSICS |
| 1964 | }; |
| 1965 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 1966 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1967 | class HInvoke : public HInstruction { |
| 1968 | public: |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1969 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1970 | |
| 1971 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 1972 | // know their environment. |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1973 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1974 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 1975 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1976 | SetRawInputAt(index, argument); |
| 1977 | } |
| 1978 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1979 | Primitive::Type GetType() const OVERRIDE { return return_type_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1980 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 1981 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1982 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1983 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 1984 | |
| 1985 | Intrinsics GetIntrinsic() { |
| 1986 | return intrinsic_; |
| 1987 | } |
| 1988 | |
| 1989 | void SetIntrinsic(Intrinsics intrinsic) { |
| 1990 | intrinsic_ = intrinsic; |
| 1991 | } |
| 1992 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1993 | DECLARE_INSTRUCTION(Invoke); |
| 1994 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 1995 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1996 | HInvoke(ArenaAllocator* arena, |
| 1997 | uint32_t number_of_arguments, |
| 1998 | Primitive::Type return_type, |
| 1999 | uint32_t dex_pc, |
| 2000 | uint32_t dex_method_index) |
| 2001 | : HInstruction(SideEffects::All()), |
| 2002 | inputs_(arena, number_of_arguments), |
| 2003 | return_type_(return_type), |
| 2004 | dex_pc_(dex_pc), |
| 2005 | dex_method_index_(dex_method_index), |
| 2006 | intrinsic_(Intrinsics::kNone) { |
| 2007 | inputs_.SetSize(number_of_arguments); |
| 2008 | } |
| 2009 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2010 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 2011 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 2012 | inputs_.Put(index, input); |
| 2013 | } |
| 2014 | |
| 2015 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2016 | const Primitive::Type return_type_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2017 | const uint32_t dex_pc_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2018 | const uint32_t dex_method_index_; |
| 2019 | Intrinsics intrinsic_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2020 | |
| 2021 | private: |
| 2022 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 2023 | }; |
| 2024 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2025 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2026 | public: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2027 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 2028 | uint32_t number_of_arguments, |
| 2029 | Primitive::Type return_type, |
| 2030 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2031 | uint32_t dex_method_index, |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2032 | bool is_recursive, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2033 | InvokeType invoke_type) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2034 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2035 | invoke_type_(invoke_type), |
| 2036 | is_recursive_(is_recursive) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2037 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2038 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2039 | // We access the method via the dex cache so we can't do an implicit null check. |
| 2040 | // TODO: for intrinsics we can generate implicit null checks. |
| 2041 | return false; |
| 2042 | } |
| 2043 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2044 | InvokeType GetInvokeType() const { return invoke_type_; } |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2045 | bool IsRecursive() const { return is_recursive_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2046 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2047 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2048 | |
| 2049 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2050 | const InvokeType invoke_type_; |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2051 | const bool is_recursive_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2052 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2053 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2054 | }; |
| 2055 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2056 | class HInvokeVirtual : public HInvoke { |
| 2057 | public: |
| 2058 | HInvokeVirtual(ArenaAllocator* arena, |
| 2059 | uint32_t number_of_arguments, |
| 2060 | Primitive::Type return_type, |
| 2061 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2062 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2063 | uint32_t vtable_index) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2064 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2065 | vtable_index_(vtable_index) {} |
| 2066 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2067 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2068 | // TODO: Add implicit null checks in intrinsics. |
| 2069 | return !GetLocations()->Intrinsified(); |
| 2070 | } |
| 2071 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2072 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 2073 | |
| 2074 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 2075 | |
| 2076 | private: |
| 2077 | const uint32_t vtable_index_; |
| 2078 | |
| 2079 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 2080 | }; |
| 2081 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2082 | class HInvokeInterface : public HInvoke { |
| 2083 | public: |
| 2084 | HInvokeInterface(ArenaAllocator* arena, |
| 2085 | uint32_t number_of_arguments, |
| 2086 | Primitive::Type return_type, |
| 2087 | uint32_t dex_pc, |
| 2088 | uint32_t dex_method_index, |
| 2089 | uint32_t imt_index) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2090 | : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index), |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2091 | imt_index_(imt_index) {} |
| 2092 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2093 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2094 | // TODO: Add implicit null checks in intrinsics. |
| 2095 | return !GetLocations()->Intrinsified(); |
| 2096 | } |
| 2097 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2098 | uint32_t GetImtIndex() const { return imt_index_; } |
| 2099 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 2100 | |
| 2101 | DECLARE_INSTRUCTION(InvokeInterface); |
| 2102 | |
| 2103 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2104 | const uint32_t imt_index_; |
| 2105 | |
| 2106 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 2107 | }; |
| 2108 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2109 | class HNewInstance : public HExpression<0> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2110 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2111 | HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2112 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2113 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2114 | type_index_(type_index), |
| 2115 | entrypoint_(entrypoint) {} |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2116 | |
| 2117 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2118 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2119 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2120 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 2121 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2122 | // It may throw when called on: |
| 2123 | // - interfaces |
| 2124 | // - abstract/innaccessible/unknown classes |
| 2125 | // TODO: optimize when possible. |
| 2126 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2127 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2128 | bool CanBeNull() const OVERRIDE { return false; } |
| 2129 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2130 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2131 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2132 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2133 | |
| 2134 | private: |
| 2135 | const uint32_t dex_pc_; |
| 2136 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2137 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2138 | |
| 2139 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 2140 | }; |
| 2141 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2142 | class HNeg : public HUnaryOperation { |
| 2143 | public: |
| 2144 | explicit HNeg(Primitive::Type result_type, HInstruction* input) |
| 2145 | : HUnaryOperation(result_type, input) {} |
| 2146 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2147 | int32_t Evaluate(int32_t x) const OVERRIDE { return -x; } |
| 2148 | int64_t Evaluate(int64_t x) const OVERRIDE { return -x; } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2149 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2150 | DECLARE_INSTRUCTION(Neg); |
| 2151 | |
| 2152 | private: |
| 2153 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 2154 | }; |
| 2155 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2156 | class HNewArray : public HExpression<1> { |
| 2157 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2158 | HNewArray(HInstruction* length, |
| 2159 | uint32_t dex_pc, |
| 2160 | uint16_t type_index, |
| 2161 | QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2162 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2163 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2164 | type_index_(type_index), |
| 2165 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2166 | SetRawInputAt(0, length); |
| 2167 | } |
| 2168 | |
| 2169 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2170 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2171 | |
| 2172 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2173 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2174 | |
| 2175 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2176 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2177 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2178 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2179 | DECLARE_INSTRUCTION(NewArray); |
| 2180 | |
| 2181 | private: |
| 2182 | const uint32_t dex_pc_; |
| 2183 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2184 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2185 | |
| 2186 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 2187 | }; |
| 2188 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2189 | class HAdd : public HBinaryOperation { |
| 2190 | public: |
| 2191 | HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2192 | : HBinaryOperation(result_type, left, right) {} |
| 2193 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2194 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2195 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2196 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2197 | return x + y; |
| 2198 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2199 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2200 | return x + y; |
| 2201 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2202 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2203 | DECLARE_INSTRUCTION(Add); |
| 2204 | |
| 2205 | private: |
| 2206 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 2207 | }; |
| 2208 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2209 | class HSub : public HBinaryOperation { |
| 2210 | public: |
| 2211 | HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2212 | : HBinaryOperation(result_type, left, right) {} |
| 2213 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2214 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2215 | return x - y; |
| 2216 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2217 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2218 | return x - y; |
| 2219 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2220 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2221 | DECLARE_INSTRUCTION(Sub); |
| 2222 | |
| 2223 | private: |
| 2224 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 2225 | }; |
| 2226 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2227 | class HMul : public HBinaryOperation { |
| 2228 | public: |
| 2229 | HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2230 | : HBinaryOperation(result_type, left, right) {} |
| 2231 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2232 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2233 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2234 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; } |
| 2235 | 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] | 2236 | |
| 2237 | DECLARE_INSTRUCTION(Mul); |
| 2238 | |
| 2239 | private: |
| 2240 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 2241 | }; |
| 2242 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2243 | class HDiv : public HBinaryOperation { |
| 2244 | public: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2245 | HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2246 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2247 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2248 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2249 | // Our graph structure ensures we never have 0 for `y` during constant folding. |
| 2250 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2251 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2252 | return (y == -1) ? -x : x / y; |
| 2253 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2254 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2255 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2256 | DCHECK_NE(y, 0); |
| 2257 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2258 | return (y == -1) ? -x : x / y; |
| 2259 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2260 | |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2261 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2262 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2263 | DECLARE_INSTRUCTION(Div); |
| 2264 | |
| 2265 | private: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2266 | const uint32_t dex_pc_; |
| 2267 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2268 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 2269 | }; |
| 2270 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2271 | class HRem : public HBinaryOperation { |
| 2272 | public: |
| 2273 | HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2274 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
| 2275 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2276 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2277 | DCHECK_NE(y, 0); |
| 2278 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2279 | return (y == -1) ? 0 : x % y; |
| 2280 | } |
| 2281 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2282 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2283 | DCHECK_NE(y, 0); |
| 2284 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2285 | return (y == -1) ? 0 : x % y; |
| 2286 | } |
| 2287 | |
| 2288 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2289 | |
| 2290 | DECLARE_INSTRUCTION(Rem); |
| 2291 | |
| 2292 | private: |
| 2293 | const uint32_t dex_pc_; |
| 2294 | |
| 2295 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 2296 | }; |
| 2297 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2298 | class HDivZeroCheck : public HExpression<1> { |
| 2299 | public: |
| 2300 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
| 2301 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
| 2302 | SetRawInputAt(0, value); |
| 2303 | } |
| 2304 | |
| 2305 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2306 | |
| 2307 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2308 | UNUSED(other); |
| 2309 | return true; |
| 2310 | } |
| 2311 | |
| 2312 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2313 | bool CanThrow() const OVERRIDE { return true; } |
| 2314 | |
| 2315 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2316 | |
| 2317 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 2318 | |
| 2319 | private: |
| 2320 | const uint32_t dex_pc_; |
| 2321 | |
| 2322 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 2323 | }; |
| 2324 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 2325 | class HShl : public HBinaryOperation { |
| 2326 | public: |
| 2327 | HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2328 | : HBinaryOperation(result_type, left, right) {} |
| 2329 | |
| 2330 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); } |
| 2331 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); } |
| 2332 | |
| 2333 | DECLARE_INSTRUCTION(Shl); |
| 2334 | |
| 2335 | private: |
| 2336 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 2337 | }; |
| 2338 | |
| 2339 | class HShr : public HBinaryOperation { |
| 2340 | public: |
| 2341 | HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2342 | : HBinaryOperation(result_type, left, right) {} |
| 2343 | |
| 2344 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); } |
| 2345 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); } |
| 2346 | |
| 2347 | DECLARE_INSTRUCTION(Shr); |
| 2348 | |
| 2349 | private: |
| 2350 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 2351 | }; |
| 2352 | |
| 2353 | class HUShr : public HBinaryOperation { |
| 2354 | public: |
| 2355 | HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2356 | : HBinaryOperation(result_type, left, right) {} |
| 2357 | |
| 2358 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2359 | uint32_t ux = static_cast<uint32_t>(x); |
| 2360 | uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue; |
| 2361 | return static_cast<int32_t>(ux >> uy); |
| 2362 | } |
| 2363 | |
| 2364 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2365 | uint64_t ux = static_cast<uint64_t>(x); |
| 2366 | uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue; |
| 2367 | return static_cast<int64_t>(ux >> uy); |
| 2368 | } |
| 2369 | |
| 2370 | DECLARE_INSTRUCTION(UShr); |
| 2371 | |
| 2372 | private: |
| 2373 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 2374 | }; |
| 2375 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2376 | class HAnd : public HBinaryOperation { |
| 2377 | public: |
| 2378 | HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2379 | : HBinaryOperation(result_type, left, right) {} |
| 2380 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2381 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2382 | |
| 2383 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; } |
| 2384 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; } |
| 2385 | |
| 2386 | DECLARE_INSTRUCTION(And); |
| 2387 | |
| 2388 | private: |
| 2389 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 2390 | }; |
| 2391 | |
| 2392 | class HOr : public HBinaryOperation { |
| 2393 | public: |
| 2394 | HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2395 | : HBinaryOperation(result_type, left, right) {} |
| 2396 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2397 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2398 | |
| 2399 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; } |
| 2400 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; } |
| 2401 | |
| 2402 | DECLARE_INSTRUCTION(Or); |
| 2403 | |
| 2404 | private: |
| 2405 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 2406 | }; |
| 2407 | |
| 2408 | class HXor : public HBinaryOperation { |
| 2409 | public: |
| 2410 | HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2411 | : HBinaryOperation(result_type, left, right) {} |
| 2412 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2413 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2414 | |
| 2415 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; } |
| 2416 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; } |
| 2417 | |
| 2418 | DECLARE_INSTRUCTION(Xor); |
| 2419 | |
| 2420 | private: |
| 2421 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 2422 | }; |
| 2423 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2424 | // The value of a parameter in this method. Its location depends on |
| 2425 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2426 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2427 | public: |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2428 | HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false) |
| 2429 | : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2430 | |
| 2431 | uint8_t GetIndex() const { return index_; } |
| 2432 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2433 | bool CanBeNull() const OVERRIDE { return !is_this_; } |
| 2434 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2435 | DECLARE_INSTRUCTION(ParameterValue); |
| 2436 | |
| 2437 | private: |
| 2438 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2439 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2440 | const uint8_t index_; |
| 2441 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2442 | // Whether or not the parameter value corresponds to 'this' argument. |
| 2443 | const bool is_this_; |
| 2444 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2445 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 2446 | }; |
| 2447 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2448 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2449 | public: |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2450 | explicit HNot(Primitive::Type result_type, HInstruction* input) |
| 2451 | : HUnaryOperation(result_type, input) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2452 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2453 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2454 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2455 | UNUSED(other); |
| 2456 | return true; |
| 2457 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2458 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2459 | int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; } |
| 2460 | int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2461 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2462 | DECLARE_INSTRUCTION(Not); |
| 2463 | |
| 2464 | private: |
| 2465 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 2466 | }; |
| 2467 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2468 | class HTypeConversion : public HExpression<1> { |
| 2469 | public: |
| 2470 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2471 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
| 2472 | : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2473 | SetRawInputAt(0, input); |
| 2474 | DCHECK_NE(input->GetType(), result_type); |
| 2475 | } |
| 2476 | |
| 2477 | HInstruction* GetInput() const { return InputAt(0); } |
| 2478 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 2479 | Primitive::Type GetResultType() const { return GetType(); } |
| 2480 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2481 | // Required by the x86 and ARM code generators when producing calls |
| 2482 | // to the runtime. |
| 2483 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2484 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2485 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 2486 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2487 | |
| 2488 | DECLARE_INSTRUCTION(TypeConversion); |
| 2489 | |
| 2490 | private: |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2491 | const uint32_t dex_pc_; |
| 2492 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2493 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 2494 | }; |
| 2495 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 2496 | static constexpr uint32_t kNoRegNumber = -1; |
| 2497 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2498 | class HPhi : public HInstruction { |
| 2499 | public: |
| 2500 | 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] | 2501 | : HInstruction(SideEffects::None()), |
| 2502 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2503 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2504 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2505 | is_live_(false), |
| 2506 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2507 | inputs_.SetSize(number_of_inputs); |
| 2508 | } |
| 2509 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 2510 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 2511 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 2512 | switch (type) { |
| 2513 | case Primitive::kPrimBoolean: |
| 2514 | case Primitive::kPrimByte: |
| 2515 | case Primitive::kPrimShort: |
| 2516 | case Primitive::kPrimChar: |
| 2517 | return Primitive::kPrimInt; |
| 2518 | default: |
| 2519 | return type; |
| 2520 | } |
| 2521 | } |
| 2522 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2523 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2524 | |
| 2525 | void AddInput(HInstruction* input); |
| 2526 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2527 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2528 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2529 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2530 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 2531 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 2532 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2533 | uint32_t GetRegNumber() const { return reg_number_; } |
| 2534 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2535 | void SetDead() { is_live_ = false; } |
| 2536 | void SetLive() { is_live_ = true; } |
| 2537 | bool IsDead() const { return !is_live_; } |
| 2538 | bool IsLive() const { return is_live_; } |
| 2539 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2540 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2541 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2542 | protected: |
| 2543 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 2544 | |
| 2545 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 2546 | inputs_.Put(index, input); |
| 2547 | } |
| 2548 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2549 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2550 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2551 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2552 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 2553 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2554 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2555 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2556 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 2557 | }; |
| 2558 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2559 | class HNullCheck : public HExpression<1> { |
| 2560 | public: |
| 2561 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2562 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2563 | SetRawInputAt(0, value); |
| 2564 | } |
| 2565 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2566 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2567 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2568 | UNUSED(other); |
| 2569 | return true; |
| 2570 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2571 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2572 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2573 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2574 | bool CanThrow() const OVERRIDE { return true; } |
| 2575 | |
| 2576 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 2577 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2578 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2579 | |
| 2580 | DECLARE_INSTRUCTION(NullCheck); |
| 2581 | |
| 2582 | private: |
| 2583 | const uint32_t dex_pc_; |
| 2584 | |
| 2585 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 2586 | }; |
| 2587 | |
| 2588 | class FieldInfo : public ValueObject { |
| 2589 | public: |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2590 | FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile) |
| 2591 | : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2592 | |
| 2593 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2594 | Primitive::Type GetFieldType() const { return field_type_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2595 | bool IsVolatile() const { return is_volatile_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2596 | |
| 2597 | private: |
| 2598 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2599 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2600 | const bool is_volatile_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2601 | }; |
| 2602 | |
| 2603 | class HInstanceFieldGet : public HExpression<1> { |
| 2604 | public: |
| 2605 | HInstanceFieldGet(HInstruction* value, |
| 2606 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2607 | MemberOffset field_offset, |
| 2608 | bool is_volatile) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2609 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2610 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2611 | SetRawInputAt(0, value); |
| 2612 | } |
| 2613 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 2614 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2615 | |
| 2616 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2617 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 2618 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2621 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2622 | return GetFieldOffset().Uint32Value() < kPageSize; |
| 2623 | } |
| 2624 | |
| 2625 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2626 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 2627 | } |
| 2628 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2629 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2630 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2631 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2632 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2633 | |
| 2634 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 2635 | |
| 2636 | private: |
| 2637 | const FieldInfo field_info_; |
| 2638 | |
| 2639 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 2640 | }; |
| 2641 | |
| 2642 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 2643 | public: |
| 2644 | HInstanceFieldSet(HInstruction* object, |
| 2645 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2646 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2647 | MemberOffset field_offset, |
| 2648 | bool is_volatile) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2649 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2650 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2651 | SetRawInputAt(0, object); |
| 2652 | SetRawInputAt(1, value); |
| 2653 | } |
| 2654 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2655 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2656 | return GetFieldOffset().Uint32Value() < kPageSize; |
| 2657 | } |
| 2658 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2659 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2660 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2661 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 2662 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2663 | HInstruction* GetValue() const { return InputAt(1); } |
| 2664 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2665 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 2666 | |
| 2667 | private: |
| 2668 | const FieldInfo field_info_; |
| 2669 | |
| 2670 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 2671 | }; |
| 2672 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2673 | class HArrayGet : public HExpression<2> { |
| 2674 | public: |
| 2675 | HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2676 | : HExpression(type, SideEffects::DependsOnSomething()) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2677 | SetRawInputAt(0, array); |
| 2678 | SetRawInputAt(1, index); |
| 2679 | } |
| 2680 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2681 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2682 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2683 | UNUSED(other); |
| 2684 | return true; |
| 2685 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2686 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2687 | // TODO: We can be smarter here. |
| 2688 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 2689 | // which generates the implicit null check. There are cases when these can be removed |
| 2690 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 2691 | // implicit check here (as long as the address falls in the first page). |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2695 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2696 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2697 | HInstruction* GetArray() const { return InputAt(0); } |
| 2698 | HInstruction* GetIndex() const { return InputAt(1); } |
| 2699 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2700 | DECLARE_INSTRUCTION(ArrayGet); |
| 2701 | |
| 2702 | private: |
| 2703 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 2704 | }; |
| 2705 | |
| 2706 | class HArraySet : public HTemplateInstruction<3> { |
| 2707 | public: |
| 2708 | HArraySet(HInstruction* array, |
| 2709 | HInstruction* index, |
| 2710 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2711 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2712 | uint32_t dex_pc) |
| 2713 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
| 2714 | dex_pc_(dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2715 | expected_component_type_(expected_component_type), |
| 2716 | needs_type_check_(value->GetType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2717 | SetRawInputAt(0, array); |
| 2718 | SetRawInputAt(1, index); |
| 2719 | SetRawInputAt(2, value); |
| 2720 | } |
| 2721 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2722 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2723 | // We currently always call a runtime method to catch array store |
| 2724 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2725 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2726 | } |
| 2727 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2728 | bool CanDoImplicitNullCheck() const OVERRIDE { |
| 2729 | // TODO: Same as for ArrayGet. |
| 2730 | return false; |
| 2731 | } |
| 2732 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2733 | void ClearNeedsTypeCheck() { |
| 2734 | needs_type_check_ = false; |
| 2735 | } |
| 2736 | |
| 2737 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 2738 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2739 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2740 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2741 | HInstruction* GetArray() const { return InputAt(0); } |
| 2742 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2743 | HInstruction* GetValue() const { return InputAt(2); } |
| 2744 | |
| 2745 | Primitive::Type GetComponentType() const { |
| 2746 | // The Dex format does not type floating point index operations. Since the |
| 2747 | // `expected_component_type_` is set during building and can therefore not |
| 2748 | // be correct, we also check what is the value type. If it is a floating |
| 2749 | // point type, we must use that type. |
| 2750 | Primitive::Type value_type = GetValue()->GetType(); |
| 2751 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 2752 | ? value_type |
| 2753 | : expected_component_type_; |
| 2754 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2755 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2756 | DECLARE_INSTRUCTION(ArraySet); |
| 2757 | |
| 2758 | private: |
| 2759 | const uint32_t dex_pc_; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2760 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 2761 | bool needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2762 | |
| 2763 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 2764 | }; |
| 2765 | |
| 2766 | class HArrayLength : public HExpression<1> { |
| 2767 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2768 | explicit HArrayLength(HInstruction* array) |
| 2769 | : HExpression(Primitive::kPrimInt, SideEffects::None()) { |
| 2770 | // Note that arrays do not change length, so the instruction does not |
| 2771 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2772 | SetRawInputAt(0, array); |
| 2773 | } |
| 2774 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2775 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2776 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2777 | UNUSED(other); |
| 2778 | return true; |
| 2779 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2780 | bool CanDoImplicitNullCheck() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2781 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2782 | DECLARE_INSTRUCTION(ArrayLength); |
| 2783 | |
| 2784 | private: |
| 2785 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 2786 | }; |
| 2787 | |
| 2788 | class HBoundsCheck : public HExpression<2> { |
| 2789 | public: |
| 2790 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2791 | : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2792 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 2793 | SetRawInputAt(0, index); |
| 2794 | SetRawInputAt(1, length); |
| 2795 | } |
| 2796 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2797 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2798 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2799 | UNUSED(other); |
| 2800 | return true; |
| 2801 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2802 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2803 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2804 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2805 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 2806 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 2807 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2808 | |
| 2809 | DECLARE_INSTRUCTION(BoundsCheck); |
| 2810 | |
| 2811 | private: |
| 2812 | const uint32_t dex_pc_; |
| 2813 | |
| 2814 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 2815 | }; |
| 2816 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2817 | /** |
| 2818 | * Some DEX instructions are folded into multiple HInstructions that need |
| 2819 | * to stay live until the last HInstruction. This class |
| 2820 | * 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] | 2821 | * HInstruction stays live. `index` represents the stack location index of the |
| 2822 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2823 | */ |
| 2824 | class HTemporary : public HTemplateInstruction<0> { |
| 2825 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2826 | explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2827 | |
| 2828 | size_t GetIndex() const { return index_; } |
| 2829 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 2830 | Primitive::Type GetType() const OVERRIDE { |
| 2831 | // The previous instruction is the one that will be stored in the temporary location. |
| 2832 | DCHECK(GetPrevious() != nullptr); |
| 2833 | return GetPrevious()->GetType(); |
| 2834 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 2835 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 2836 | DECLARE_INSTRUCTION(Temporary); |
| 2837 | |
| 2838 | private: |
| 2839 | const size_t index_; |
| 2840 | |
| 2841 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 2842 | }; |
| 2843 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 2844 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 2845 | public: |
| 2846 | explicit HSuspendCheck(uint32_t dex_pc) |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 2847 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 2848 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2849 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 2850 | return true; |
| 2851 | } |
| 2852 | |
| 2853 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2854 | |
| 2855 | DECLARE_INSTRUCTION(SuspendCheck); |
| 2856 | |
| 2857 | private: |
| 2858 | const uint32_t dex_pc_; |
| 2859 | |
| 2860 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 2861 | }; |
| 2862 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2863 | /** |
| 2864 | * Instruction to load a Class object. |
| 2865 | */ |
| 2866 | class HLoadClass : public HExpression<0> { |
| 2867 | public: |
| 2868 | HLoadClass(uint16_t type_index, |
| 2869 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2870 | uint32_t dex_pc) |
| 2871 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2872 | type_index_(type_index), |
| 2873 | is_referrers_class_(is_referrers_class), |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2874 | dex_pc_(dex_pc), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 2875 | generate_clinit_check_(false), |
| 2876 | loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2877 | |
| 2878 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2879 | |
| 2880 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2881 | return other->AsLoadClass()->type_index_ == type_index_; |
| 2882 | } |
| 2883 | |
| 2884 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 2885 | |
| 2886 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2887 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2888 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2889 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2890 | bool NeedsEnvironment() const OVERRIDE { |
| 2891 | // Will call runtime and load the class if the class is not loaded yet. |
| 2892 | // TODO: finer grain decision. |
| 2893 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2896 | bool MustGenerateClinitCheck() const { |
| 2897 | return generate_clinit_check_; |
| 2898 | } |
| 2899 | |
| 2900 | void SetMustGenerateClinitCheck() { |
| 2901 | generate_clinit_check_ = true; |
| 2902 | } |
| 2903 | |
| 2904 | bool CanCallRuntime() const { |
| 2905 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 2906 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2907 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 2908 | bool CanThrow() const OVERRIDE { |
| 2909 | // May call runtime and and therefore can throw. |
| 2910 | // TODO: finer grain decision. |
| 2911 | return !is_referrers_class_; |
| 2912 | } |
| 2913 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2914 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 2915 | return loaded_class_rti_; |
| 2916 | } |
| 2917 | |
| 2918 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 2919 | // Make sure we only set exact types (the loaded class should never be merged). |
| 2920 | DCHECK(rti.IsExact()); |
| 2921 | loaded_class_rti_ = rti; |
| 2922 | } |
| 2923 | |
| 2924 | bool IsResolved() { |
| 2925 | return loaded_class_rti_.IsExact(); |
| 2926 | } |
| 2927 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2928 | DECLARE_INSTRUCTION(LoadClass); |
| 2929 | |
| 2930 | private: |
| 2931 | const uint16_t type_index_; |
| 2932 | const bool is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2933 | const uint32_t dex_pc_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2934 | // Whether this instruction must generate the initialization check. |
| 2935 | // Used for code generation. |
| 2936 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2937 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2938 | ReferenceTypeInfo loaded_class_rti_; |
| 2939 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2940 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 2941 | }; |
| 2942 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 2943 | class HLoadString : public HExpression<0> { |
| 2944 | public: |
| 2945 | HLoadString(uint32_t string_index, uint32_t dex_pc) |
| 2946 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2947 | string_index_(string_index), |
| 2948 | dex_pc_(dex_pc) {} |
| 2949 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2950 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2951 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 2952 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2953 | return other->AsLoadString()->string_index_ == string_index_; |
| 2954 | } |
| 2955 | |
| 2956 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 2957 | |
| 2958 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2959 | uint32_t GetStringIndex() const { return string_index_; } |
| 2960 | |
| 2961 | // TODO: Can we deopt or debug when we resolve a string? |
| 2962 | bool NeedsEnvironment() const OVERRIDE { return false; } |
| 2963 | |
| 2964 | DECLARE_INSTRUCTION(LoadString); |
| 2965 | |
| 2966 | private: |
| 2967 | const uint32_t string_index_; |
| 2968 | const uint32_t dex_pc_; |
| 2969 | |
| 2970 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 2971 | }; |
| 2972 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2973 | // TODO: Pass this check to HInvokeStaticOrDirect nodes. |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2974 | /** |
| 2975 | * Performs an initialization check on its Class object input. |
| 2976 | */ |
| 2977 | class HClinitCheck : public HExpression<1> { |
| 2978 | public: |
| 2979 | explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
| 2980 | : HExpression(Primitive::kPrimNot, SideEffects::All()), |
| 2981 | dex_pc_(dex_pc) { |
| 2982 | SetRawInputAt(0, constant); |
| 2983 | } |
| 2984 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 2985 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2986 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2987 | UNUSED(other); |
| 2988 | return true; |
| 2989 | } |
| 2990 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 2991 | bool NeedsEnvironment() const OVERRIDE { |
| 2992 | // May call runtime to initialize the class. |
| 2993 | return true; |
| 2994 | } |
| 2995 | |
| 2996 | uint32_t GetDexPc() const { return dex_pc_; } |
| 2997 | |
| 2998 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 2999 | |
| 3000 | DECLARE_INSTRUCTION(ClinitCheck); |
| 3001 | |
| 3002 | private: |
| 3003 | const uint32_t dex_pc_; |
| 3004 | |
| 3005 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 3006 | }; |
| 3007 | |
| 3008 | class HStaticFieldGet : public HExpression<1> { |
| 3009 | public: |
| 3010 | HStaticFieldGet(HInstruction* cls, |
| 3011 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3012 | MemberOffset field_offset, |
| 3013 | bool is_volatile) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3014 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3015 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3016 | SetRawInputAt(0, cls); |
| 3017 | } |
| 3018 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3019 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 3020 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3021 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3022 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3023 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 3024 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | size_t ComputeHashCode() const OVERRIDE { |
| 3028 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 3029 | } |
| 3030 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3031 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3032 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3033 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3034 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3035 | |
| 3036 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 3037 | |
| 3038 | private: |
| 3039 | const FieldInfo field_info_; |
| 3040 | |
| 3041 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 3042 | }; |
| 3043 | |
| 3044 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 3045 | public: |
| 3046 | HStaticFieldSet(HInstruction* cls, |
| 3047 | HInstruction* value, |
| 3048 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3049 | MemberOffset field_offset, |
| 3050 | bool is_volatile) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3051 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3052 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3053 | SetRawInputAt(0, cls); |
| 3054 | SetRawInputAt(1, value); |
| 3055 | } |
| 3056 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3057 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3058 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3059 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3060 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3061 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3062 | HInstruction* GetValue() const { return InputAt(1); } |
| 3063 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3064 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 3065 | |
| 3066 | private: |
| 3067 | const FieldInfo field_info_; |
| 3068 | |
| 3069 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 3070 | }; |
| 3071 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3072 | // Implement the move-exception DEX instruction. |
| 3073 | class HLoadException : public HExpression<0> { |
| 3074 | public: |
| 3075 | HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {} |
| 3076 | |
| 3077 | DECLARE_INSTRUCTION(LoadException); |
| 3078 | |
| 3079 | private: |
| 3080 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 3081 | }; |
| 3082 | |
| 3083 | class HThrow : public HTemplateInstruction<1> { |
| 3084 | public: |
| 3085 | HThrow(HInstruction* exception, uint32_t dex_pc) |
| 3086 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) { |
| 3087 | SetRawInputAt(0, exception); |
| 3088 | } |
| 3089 | |
| 3090 | bool IsControlFlow() const OVERRIDE { return true; } |
| 3091 | |
| 3092 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3093 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3094 | bool CanThrow() const OVERRIDE { return true; } |
| 3095 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3096 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3097 | |
| 3098 | DECLARE_INSTRUCTION(Throw); |
| 3099 | |
| 3100 | private: |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3101 | const uint32_t dex_pc_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3102 | |
| 3103 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 3104 | }; |
| 3105 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3106 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3107 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3108 | HInstanceOf(HInstruction* object, |
| 3109 | HLoadClass* constant, |
| 3110 | bool class_is_final, |
| 3111 | uint32_t dex_pc) |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3112 | : HExpression(Primitive::kPrimBoolean, SideEffects::None()), |
| 3113 | class_is_final_(class_is_final), |
| 3114 | dex_pc_(dex_pc) { |
| 3115 | SetRawInputAt(0, object); |
| 3116 | SetRawInputAt(1, constant); |
| 3117 | } |
| 3118 | |
| 3119 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3120 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3121 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3122 | return true; |
| 3123 | } |
| 3124 | |
| 3125 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3126 | return false; |
| 3127 | } |
| 3128 | |
| 3129 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3130 | |
| 3131 | bool IsClassFinal() const { return class_is_final_; } |
| 3132 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3133 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3134 | |
| 3135 | private: |
| 3136 | const bool class_is_final_; |
| 3137 | const uint32_t dex_pc_; |
| 3138 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3139 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 3140 | }; |
| 3141 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3142 | class HBoundType : public HExpression<1> { |
| 3143 | public: |
| 3144 | HBoundType(HInstruction* input, ReferenceTypeInfo bound_type) |
| 3145 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3146 | bound_type_(bound_type) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 3147 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3148 | SetRawInputAt(0, input); |
| 3149 | } |
| 3150 | |
| 3151 | const ReferenceTypeInfo& GetBoundType() const { return bound_type_; } |
| 3152 | |
| 3153 | bool CanBeNull() const OVERRIDE { |
| 3154 | // `null instanceof ClassX` always return false so we can't be null. |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
| 3158 | DECLARE_INSTRUCTION(BoundType); |
| 3159 | |
| 3160 | private: |
| 3161 | // Encodes the most upper class that this instruction can have. In other words |
| 3162 | // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()). |
| 3163 | // It is used to bound the type in cases like `if (x instanceof ClassX) {}` |
| 3164 | const ReferenceTypeInfo bound_type_; |
| 3165 | |
| 3166 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 3167 | }; |
| 3168 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3169 | class HCheckCast : public HTemplateInstruction<2> { |
| 3170 | public: |
| 3171 | HCheckCast(HInstruction* object, |
| 3172 | HLoadClass* constant, |
| 3173 | bool class_is_final, |
| 3174 | uint32_t dex_pc) |
| 3175 | : HTemplateInstruction(SideEffects::None()), |
| 3176 | class_is_final_(class_is_final), |
| 3177 | dex_pc_(dex_pc) { |
| 3178 | SetRawInputAt(0, object); |
| 3179 | SetRawInputAt(1, constant); |
| 3180 | } |
| 3181 | |
| 3182 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3183 | |
| 3184 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3185 | return true; |
| 3186 | } |
| 3187 | |
| 3188 | bool NeedsEnvironment() const OVERRIDE { |
| 3189 | // Instruction may throw a CheckCastError. |
| 3190 | return true; |
| 3191 | } |
| 3192 | |
| 3193 | bool CanThrow() const OVERRIDE { return true; } |
| 3194 | |
| 3195 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3196 | |
| 3197 | bool IsClassFinal() const { return class_is_final_; } |
| 3198 | |
| 3199 | DECLARE_INSTRUCTION(CheckCast); |
| 3200 | |
| 3201 | private: |
| 3202 | const bool class_is_final_; |
| 3203 | const uint32_t dex_pc_; |
| 3204 | |
| 3205 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3206 | }; |
| 3207 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3208 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 3209 | public: |
| 3210 | enum OperationKind { |
| 3211 | kEnter, |
| 3212 | kExit, |
| 3213 | }; |
| 3214 | |
| 3215 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
| 3216 | : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) { |
| 3217 | SetRawInputAt(0, object); |
| 3218 | } |
| 3219 | |
| 3220 | // Instruction may throw a Java exception, so we need an environment. |
| 3221 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3222 | bool CanThrow() const OVERRIDE { return true; } |
| 3223 | |
| 3224 | uint32_t GetDexPc() const { return dex_pc_; } |
| 3225 | |
| 3226 | bool IsEnter() const { return kind_ == kEnter; } |
| 3227 | |
| 3228 | DECLARE_INSTRUCTION(MonitorOperation); |
| 3229 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3230 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3231 | const OperationKind kind_; |
| 3232 | const uint32_t dex_pc_; |
| 3233 | |
| 3234 | private: |
| 3235 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 3236 | }; |
| 3237 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3238 | class MoveOperands : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3239 | public: |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3240 | MoveOperands(Location source, Location destination, HInstruction* instruction) |
| 3241 | : source_(source), destination_(destination), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3242 | |
| 3243 | Location GetSource() const { return source_; } |
| 3244 | Location GetDestination() const { return destination_; } |
| 3245 | |
| 3246 | void SetSource(Location value) { source_ = value; } |
| 3247 | void SetDestination(Location value) { destination_ = value; } |
| 3248 | |
| 3249 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 3250 | // destination (but not the source). |
| 3251 | Location MarkPending() { |
| 3252 | DCHECK(!IsPending()); |
| 3253 | Location dest = destination_; |
| 3254 | destination_ = Location::NoLocation(); |
| 3255 | return dest; |
| 3256 | } |
| 3257 | |
| 3258 | void ClearPending(Location dest) { |
| 3259 | DCHECK(IsPending()); |
| 3260 | destination_ = dest; |
| 3261 | } |
| 3262 | |
| 3263 | bool IsPending() const { |
| 3264 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3265 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 3266 | } |
| 3267 | |
| 3268 | // True if this blocks a move from the given location. |
| 3269 | bool Blocks(Location loc) const { |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3270 | return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | // A move is redundant if it's been eliminated, if its source and |
| 3274 | // destination are the same, or if its destination is unneeded. |
| 3275 | bool IsRedundant() const { |
| 3276 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 3277 | } |
| 3278 | |
| 3279 | // We clear both operands to indicate move that's been eliminated. |
| 3280 | void Eliminate() { |
| 3281 | source_ = destination_ = Location::NoLocation(); |
| 3282 | } |
| 3283 | |
| 3284 | bool IsEliminated() const { |
| 3285 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3286 | return source_.IsInvalid(); |
| 3287 | } |
| 3288 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3289 | HInstruction* GetInstruction() const { return instruction_; } |
| 3290 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3291 | private: |
| 3292 | Location source_; |
| 3293 | Location destination_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3294 | // The instruction this move is assocatied with. Null when this move is |
| 3295 | // for moving an input in the expected locations of user (including a phi user). |
| 3296 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 3297 | // in the same parallel move. |
| 3298 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3299 | }; |
| 3300 | |
| 3301 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 3302 | |
| 3303 | class HParallelMove : public HTemplateInstruction<0> { |
| 3304 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3305 | explicit HParallelMove(ArenaAllocator* arena) |
| 3306 | : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3307 | |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3308 | void AddMove(Location source, Location destination, HInstruction* instruction) { |
| 3309 | DCHECK(source.IsValid()); |
| 3310 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3311 | if (kIsDebugBuild) { |
| 3312 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3313 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 3314 | if (moves_.Get(i).GetInstruction() == instruction) { |
| 3315 | // Special case the situation where the move is for the spill slot |
| 3316 | // of the instruction. |
| 3317 | if ((GetPrevious() == instruction) |
| 3318 | || ((GetPrevious() == nullptr) |
| 3319 | && instruction->IsPhi() |
| 3320 | && instruction->GetBlock() == GetBlock())) { |
| 3321 | DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind()) |
| 3322 | << "Doing parallel moves for the same instruction."; |
| 3323 | } else { |
| 3324 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 3325 | } |
| 3326 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3329 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
| 3330 | DCHECK(!destination.Equals(moves_.Get(i).GetDestination())) |
| 3331 | << "Same destination for two moves in a parallel move."; |
| 3332 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3333 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3334 | moves_.Add(MoveOperands(source, destination, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3338 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | size_t NumMoves() const { return moves_.Size(); } |
| 3342 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3343 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3344 | |
| 3345 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3346 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3347 | |
| 3348 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 3349 | }; |
| 3350 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3351 | class HGraphVisitor : public ValueObject { |
| 3352 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3353 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 3354 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3355 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3356 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3357 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 3358 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3359 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3360 | void VisitInsertionOrder(); |
| 3361 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3362 | // Visit the graph following dominator tree reverse post-order. |
| 3363 | void VisitReversePostOrder(); |
| 3364 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 3365 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 3366 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3367 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3368 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3369 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 3370 | |
| 3371 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 3372 | |
| 3373 | #undef DECLARE_VISIT_INSTRUCTION |
| 3374 | |
| 3375 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 3376 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3377 | |
| 3378 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 3379 | }; |
| 3380 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3381 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 3382 | public: |
| 3383 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 3384 | virtual ~HGraphDelegateVisitor() {} |
| 3385 | |
| 3386 | // Visit functions that delegate to to super class. |
| 3387 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3388 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3389 | |
| 3390 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 3391 | |
| 3392 | #undef DECLARE_VISIT_INSTRUCTION |
| 3393 | |
| 3394 | private: |
| 3395 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 3396 | }; |
| 3397 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3398 | class HInsertionOrderIterator : public ValueObject { |
| 3399 | public: |
| 3400 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 3401 | |
| 3402 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 3403 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 3404 | void Advance() { ++index_; } |
| 3405 | |
| 3406 | private: |
| 3407 | const HGraph& graph_; |
| 3408 | size_t index_; |
| 3409 | |
| 3410 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 3411 | }; |
| 3412 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3413 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3414 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3415 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3416 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3417 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 3418 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3419 | void Advance() { ++index_; } |
| 3420 | |
| 3421 | private: |
| 3422 | const HGraph& graph_; |
| 3423 | size_t index_; |
| 3424 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3425 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3426 | }; |
| 3427 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3428 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3429 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3430 | explicit HPostOrderIterator(const HGraph& graph) |
| 3431 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3432 | |
| 3433 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3434 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3435 | void Advance() { --index_; } |
| 3436 | |
| 3437 | private: |
| 3438 | const HGraph& graph_; |
| 3439 | size_t index_; |
| 3440 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 3441 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 3442 | }; |
| 3443 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3444 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 3445 | // of an inner loop. The order in which the blocks are iterated is on their |
| 3446 | // block id. |
| 3447 | class HBlocksInLoopIterator : public ValueObject { |
| 3448 | public: |
| 3449 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 3450 | : blocks_in_loop_(info.GetBlocks()), |
| 3451 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 3452 | index_(0) { |
| 3453 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 3454 | Advance(); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | bool Done() const { return index_ == blocks_.Size(); } |
| 3459 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 3460 | void Advance() { |
| 3461 | ++index_; |
| 3462 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 3463 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 3464 | break; |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | private: |
| 3470 | const BitVector& blocks_in_loop_; |
| 3471 | const GrowableArray<HBasicBlock*>& blocks_; |
| 3472 | size_t index_; |
| 3473 | |
| 3474 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 3475 | }; |
| 3476 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3477 | } // namespace art |
| 3478 | |
| 3479 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |