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