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