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