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