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 | |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 62 | namespace mirror { |
| 63 | class DexCache; |
| 64 | } // namespace mirror |
| 65 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 66 | static const int kDefaultNumberOfBlocks = 8; |
| 67 | static const int kDefaultNumberOfSuccessors = 2; |
| 68 | static const int kDefaultNumberOfPredecessors = 2; |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 69 | static const int kDefaultNumberOfExceptionalPredecessors = 0; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 70 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 71 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 72 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 73 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 74 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 75 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 76 | static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1); |
| 77 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 78 | static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1); |
| 79 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 80 | static constexpr uint32_t kNoDexPc = -1; |
| 81 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 82 | enum IfCondition { |
| 83 | kCondEQ, |
| 84 | kCondNE, |
| 85 | kCondLT, |
| 86 | kCondLE, |
| 87 | kCondGT, |
| 88 | kCondGE, |
| 89 | }; |
| 90 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 91 | class HInstructionList : public ValueObject { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 92 | public: |
| 93 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 94 | |
| 95 | void AddInstruction(HInstruction* instruction); |
| 96 | void RemoveInstruction(HInstruction* instruction); |
| 97 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 98 | // Insert `instruction` before/after an existing instruction `cursor`. |
| 99 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
| 100 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
| 101 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 102 | // Return true if this list contains `instruction`. |
| 103 | bool Contains(HInstruction* instruction) const; |
| 104 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 105 | // Return true if `instruction1` is found before `instruction2` in |
| 106 | // this instruction list and false otherwise. Abort if none |
| 107 | // of these instructions is found. |
| 108 | bool FoundBefore(const HInstruction* instruction1, |
| 109 | const HInstruction* instruction2) const; |
| 110 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 111 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 112 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 113 | |
| 114 | // Update the block of all instructions to be `block`. |
| 115 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 116 | |
| 117 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
| 118 | void Add(const HInstructionList& instruction_list); |
| 119 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 120 | // Return the number of instructions in the list. This is an expensive operation. |
| 121 | size_t CountSize() const; |
| 122 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 123 | private: |
| 124 | HInstruction* first_instruction_; |
| 125 | HInstruction* last_instruction_; |
| 126 | |
| 127 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 128 | friend class HGraph; |
| 129 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 130 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 131 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 132 | |
| 133 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 134 | }; |
| 135 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 136 | // Control-flow graph of a method. Contains a list of basic blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 137 | class HGraph : public ArenaObject<kArenaAllocGraph> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 138 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 139 | HGraph(ArenaAllocator* arena, |
| 140 | const DexFile& dex_file, |
| 141 | uint32_t method_idx, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 142 | bool should_generate_constructor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | InstructionSet instruction_set, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 144 | InvokeType invoke_type = kInvalidInvokeType, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 145 | bool debuggable = false, |
| 146 | int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 147 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 148 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 149 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 150 | linear_order_(arena, kDefaultNumberOfBlocks), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 151 | entry_block_(nullptr), |
| 152 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 153 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 154 | number_of_vregs_(0), |
| 155 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 156 | temporaries_vreg_slots_(0), |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 157 | has_bounds_checks_(false), |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 158 | debuggable_(debuggable), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 159 | current_instruction_id_(start_instruction_id), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 160 | dex_file_(dex_file), |
| 161 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 162 | invoke_type_(invoke_type), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 163 | in_ssa_form_(false), |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 164 | should_generate_constructor_barrier_(should_generate_constructor_barrier), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 165 | instruction_set_(instruction_set), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 166 | cached_null_constant_(nullptr), |
| 167 | cached_int_constants_(std::less<int32_t>(), arena->Adapter()), |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 168 | cached_float_constants_(std::less<int32_t>(), arena->Adapter()), |
| 169 | cached_long_constants_(std::less<int64_t>(), arena->Adapter()), |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 170 | cached_double_constants_(std::less<int64_t>(), arena->Adapter()), |
| 171 | cached_current_method_(nullptr) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 172 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 173 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 174 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 175 | HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 176 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 177 | bool IsInSsaForm() const { return in_ssa_form_; } |
| 178 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 179 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 180 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 181 | bool HasExitBlock() const { return exit_block_ != nullptr; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 182 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 183 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 184 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 185 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 186 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 187 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 188 | // Try building the SSA form of this graph, with dominance computation and loop |
| 189 | // recognition. Returns whether it was successful in doing all these steps. |
| 190 | bool TryBuildingSsa() { |
| 191 | BuildDominatorTree(); |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 192 | // The SSA builder requires loops to all be natural. Specifically, the dead phi |
| 193 | // elimination phase checks the consistency of the graph when doing a post-order |
| 194 | // visit for eliminating dead phis: a dead phi can only have loop header phi |
| 195 | // users remaining when being visited. |
| 196 | if (!AnalyzeNaturalLoops()) return false; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 197 | // Precompute per-block try membership before entering the SSA builder, |
| 198 | // which needs the information to build catch block phis from values of |
| 199 | // locals at throwing instructions inside try blocks. |
| 200 | ComputeTryBlockInformation(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 201 | TransformToSsa(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 202 | in_ssa_form_ = true; |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 203 | return true; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 206 | void ComputeDominanceInformation(); |
| 207 | void ClearDominanceInformation(); |
| 208 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 209 | void BuildDominatorTree(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 210 | void TransformToSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 211 | void SimplifyCFG(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 212 | void SimplifyCatchBlocks(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 213 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 214 | // Analyze all natural loops in this graph. Returns false if one |
| 215 | // loop is not natural, that is the header does not dominate the |
| 216 | // back edge. |
| 217 | bool AnalyzeNaturalLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 218 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 219 | // Iterate over blocks to compute try block membership. Needs reverse post |
| 220 | // order and loop information. |
| 221 | void ComputeTryBlockInformation(); |
| 222 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 223 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 224 | // Returns the instruction used to replace the invoke expression or null if the |
| 225 | // invoke is for a void method. |
| 226 | HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 227 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 228 | // Need to add a couple of blocks to test if the loop body is entered and |
| 229 | // put deoptimization instructions, etc. |
| 230 | void TransformLoopHeaderForBCE(HBasicBlock* header); |
| 231 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 232 | // Removes `block` from the graph. |
| 233 | void DeleteDeadBlock(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 234 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 235 | // Splits the edge between `block` and `successor` while preserving the |
| 236 | // indices in the predecessor/successor lists. If there are multiple edges |
| 237 | // between the blocks, the lowest indices are used. |
| 238 | // Returns the new block which is empty and has the same dex pc as `successor`. |
| 239 | HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor); |
| 240 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 241 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 242 | void SimplifyLoop(HBasicBlock* header); |
| 243 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 244 | int32_t GetNextInstructionId() { |
| 245 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 246 | return current_instruction_id_++; |
| 247 | } |
| 248 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 249 | int32_t GetCurrentInstructionId() const { |
| 250 | return current_instruction_id_; |
| 251 | } |
| 252 | |
| 253 | void SetCurrentInstructionId(int32_t id) { |
| 254 | current_instruction_id_ = id; |
| 255 | } |
| 256 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 257 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 258 | return maximum_number_of_out_vregs_; |
| 259 | } |
| 260 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 261 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 262 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 265 | void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) { |
| 266 | maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value); |
| 267 | } |
| 268 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 269 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 270 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 273 | size_t GetTemporariesVRegSlots() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 274 | DCHECK(!in_ssa_form_); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 275 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 278 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 279 | number_of_vregs_ = number_of_vregs; |
| 280 | } |
| 281 | |
| 282 | uint16_t GetNumberOfVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 283 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 284 | return number_of_vregs_; |
| 285 | } |
| 286 | |
| 287 | void SetNumberOfInVRegs(uint16_t value) { |
| 288 | number_of_in_vregs_ = value; |
| 289 | } |
| 290 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 291 | uint16_t GetNumberOfLocalVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 292 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 293 | return number_of_vregs_ - number_of_in_vregs_; |
| 294 | } |
| 295 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 296 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 297 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 298 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 299 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 300 | const GrowableArray<HBasicBlock*>& GetLinearOrder() const { |
| 301 | return linear_order_; |
| 302 | } |
| 303 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 304 | bool HasBoundsChecks() const { |
| 305 | return has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 308 | void SetHasBoundsChecks(bool value) { |
| 309 | has_bounds_checks_ = value; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 312 | bool ShouldGenerateConstructorBarrier() const { |
| 313 | return should_generate_constructor_barrier_; |
| 314 | } |
| 315 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 316 | bool IsDebuggable() const { return debuggable_; } |
| 317 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 318 | // 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] | 319 | // already, it is created and inserted into the graph. This method is only for |
| 320 | // integral types. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 321 | HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 322 | |
| 323 | // TODO: This is problematic for the consistency of reference type propagation |
| 324 | // because it can be created anytime after the pass and thus it will be left |
| 325 | // with an invalid type. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 326 | HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 327 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 328 | HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) { |
| 329 | return CreateConstant(value, &cached_int_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 330 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 331 | HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) { |
| 332 | return CreateConstant(value, &cached_long_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 333 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 334 | HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) { |
| 335 | return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 336 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 337 | HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) { |
| 338 | return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 339 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 340 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 341 | HCurrentMethod* GetCurrentMethod(); |
| 342 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 343 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 344 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 345 | const DexFile& GetDexFile() const { |
| 346 | return dex_file_; |
| 347 | } |
| 348 | |
| 349 | uint32_t GetMethodIdx() const { |
| 350 | return method_idx_; |
| 351 | } |
| 352 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 353 | InvokeType GetInvokeType() const { |
| 354 | return invoke_type_; |
| 355 | } |
| 356 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 357 | InstructionSet GetInstructionSet() const { |
| 358 | return instruction_set_; |
| 359 | } |
| 360 | |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 361 | // TODO: Remove once the full compilation pipeline is enabled for try/catch. |
| 362 | bool HasTryCatch() const; |
| 363 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 364 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 365 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 366 | HBasicBlock* predecessor, |
| 367 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 368 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 369 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 370 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 371 | ArenaBitVector* visiting); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 372 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 373 | void RemoveDeadBlocks(const ArenaBitVector& visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 374 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 375 | template <class InstructionType, typename ValueType> |
| 376 | InstructionType* CreateConstant(ValueType value, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 377 | ArenaSafeMap<ValueType, InstructionType*>* cache, |
| 378 | uint32_t dex_pc = kNoDexPc) { |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 379 | // Try to find an existing constant of the given value. |
| 380 | InstructionType* constant = nullptr; |
| 381 | auto cached_constant = cache->find(value); |
| 382 | if (cached_constant != cache->end()) { |
| 383 | constant = cached_constant->second; |
| 384 | } |
| 385 | |
| 386 | // If not found or previously deleted, create and cache a new instruction. |
Nicolas Geoffray | f78848f | 2015-06-17 11:57:56 +0100 | [diff] [blame] | 387 | // Don't bother reviving a previously deleted instruction, for simplicity. |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 388 | if (constant == nullptr || constant->GetBlock() == nullptr) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 389 | constant = new (arena_) InstructionType(value, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 390 | cache->Overwrite(value, constant); |
| 391 | InsertConstant(constant); |
| 392 | } |
| 393 | return constant; |
| 394 | } |
| 395 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 396 | void InsertConstant(HConstant* instruction); |
| 397 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 398 | // Cache a float constant into the graph. This method should only be |
| 399 | // called by the SsaBuilder when creating "equivalent" instructions. |
| 400 | void CacheFloatConstant(HFloatConstant* constant); |
| 401 | |
| 402 | // See CacheFloatConstant comment. |
| 403 | void CacheDoubleConstant(HDoubleConstant* constant); |
| 404 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 405 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 406 | |
| 407 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 408 | GrowableArray<HBasicBlock*> blocks_; |
| 409 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 410 | // List of blocks to perform a reverse post order tree traversal. |
| 411 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 412 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 413 | // List of blocks to perform a linear order tree traversal. |
| 414 | GrowableArray<HBasicBlock*> linear_order_; |
| 415 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 416 | HBasicBlock* entry_block_; |
| 417 | HBasicBlock* exit_block_; |
| 418 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 419 | // 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] | 420 | uint16_t maximum_number_of_out_vregs_; |
| 421 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 422 | // The number of virtual registers in this method. Contains the parameters. |
| 423 | uint16_t number_of_vregs_; |
| 424 | |
| 425 | // The number of virtual registers used by parameters of this method. |
| 426 | uint16_t number_of_in_vregs_; |
| 427 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 428 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 429 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 430 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 431 | // Has bounds checks. We can totally skip BCE if it's false. |
| 432 | bool has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 433 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 434 | // Indicates whether the graph should be compiled in a way that |
| 435 | // ensures full debuggability. If false, we can apply more |
| 436 | // aggressive optimizations that may limit the level of debugging. |
| 437 | const bool debuggable_; |
| 438 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 439 | // 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] | 440 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 441 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 442 | // The dex file from which the method is from. |
| 443 | const DexFile& dex_file_; |
| 444 | |
| 445 | // The method index in the dex file. |
| 446 | const uint32_t method_idx_; |
| 447 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 448 | // If inlined, this encodes how the callee is being invoked. |
| 449 | const InvokeType invoke_type_; |
| 450 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 451 | // Whether the graph has been transformed to SSA form. Only used |
| 452 | // in debug mode to ensure we are not using properties only valid |
| 453 | // for non-SSA form (like the number of temporaries). |
| 454 | bool in_ssa_form_; |
| 455 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 456 | const bool should_generate_constructor_barrier_; |
| 457 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 458 | const InstructionSet instruction_set_; |
| 459 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 460 | // Cached constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 461 | HNullConstant* cached_null_constant_; |
| 462 | ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 463 | ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 464 | ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 465 | ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 466 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 467 | HCurrentMethod* cached_current_method_; |
| 468 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 469 | friend class SsaBuilder; // For caching constants. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 470 | friend class SsaLivenessAnalysis; // For the linear order. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 471 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 472 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 473 | }; |
| 474 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 475 | class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 476 | public: |
| 477 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 478 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 479 | suspend_check_(nullptr), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 480 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 481 | // Make bit vector growable, as the number of blocks may change. |
| 482 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 483 | |
| 484 | HBasicBlock* GetHeader() const { |
| 485 | return header_; |
| 486 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 487 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 488 | void SetHeader(HBasicBlock* block) { |
| 489 | header_ = block; |
| 490 | } |
| 491 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 492 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 493 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 494 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 495 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 496 | void AddBackEdge(HBasicBlock* back_edge) { |
| 497 | back_edges_.Add(back_edge); |
| 498 | } |
| 499 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 500 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 501 | back_edges_.Delete(back_edge); |
| 502 | } |
| 503 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 504 | bool IsBackEdge(const HBasicBlock& block) const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 505 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 506 | if (back_edges_.Get(i) == &block) return true; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 507 | } |
| 508 | return false; |
| 509 | } |
| 510 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 511 | size_t NumberOfBackEdges() const { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 512 | return back_edges_.Size(); |
| 513 | } |
| 514 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 515 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 516 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 517 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 518 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 521 | // Returns the lifetime position of the back edge that has the |
| 522 | // greatest lifetime position. |
| 523 | size_t GetLifetimeEnd() const; |
| 524 | |
| 525 | void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) { |
| 526 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 527 | if (back_edges_.Get(i) == existing) { |
| 528 | back_edges_.Put(i, new_back_edge); |
| 529 | return; |
| 530 | } |
| 531 | } |
| 532 | UNREACHABLE(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 535 | // 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] | 536 | // that is the header dominates the back edge. |
| 537 | bool Populate(); |
| 538 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 539 | // Reanalyzes the loop by removing loop info from its blocks and re-running |
| 540 | // Populate(). If there are no back edges left, the loop info is completely |
| 541 | // removed as well as its SuspendCheck instruction. It must be run on nested |
| 542 | // inner loops first. |
| 543 | void Update(); |
| 544 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 545 | // Returns whether this loop information contains `block`. |
| 546 | // Note that this loop information *must* be populated before entering this function. |
| 547 | bool Contains(const HBasicBlock& block) const; |
| 548 | |
| 549 | // Returns whether this loop information is an inner loop of `other`. |
| 550 | // Note that `other` *must* be populated before entering this function. |
| 551 | bool IsIn(const HLoopInformation& other) const; |
| 552 | |
| 553 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 554 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 555 | void Add(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 556 | void Remove(HBasicBlock* block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 557 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 558 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 559 | // Internal recursive implementation of `Populate`. |
| 560 | void PopulateRecursive(HBasicBlock* block); |
| 561 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 562 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 563 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 564 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 565 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 566 | |
| 567 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 568 | }; |
| 569 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 570 | // Stores try/catch information for basic blocks. |
| 571 | // Note that HGraph is constructed so that catch blocks cannot simultaneously |
| 572 | // be try blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 573 | class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 574 | public: |
| 575 | // Try block information constructor. |
| 576 | explicit TryCatchInformation(const HTryBoundary& try_entry) |
| 577 | : try_entry_(&try_entry), |
| 578 | catch_dex_file_(nullptr), |
| 579 | catch_type_index_(DexFile::kDexNoIndex16) { |
| 580 | DCHECK(try_entry_ != nullptr); |
| 581 | } |
| 582 | |
| 583 | // Catch block information constructor. |
| 584 | TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file) |
| 585 | : try_entry_(nullptr), |
| 586 | catch_dex_file_(&dex_file), |
| 587 | catch_type_index_(catch_type_index) {} |
| 588 | |
| 589 | bool IsTryBlock() const { return try_entry_ != nullptr; } |
| 590 | |
| 591 | const HTryBoundary& GetTryEntry() const { |
| 592 | DCHECK(IsTryBlock()); |
| 593 | return *try_entry_; |
| 594 | } |
| 595 | |
| 596 | bool IsCatchBlock() const { return catch_dex_file_ != nullptr; } |
| 597 | |
| 598 | bool IsCatchAllTypeIndex() const { |
| 599 | DCHECK(IsCatchBlock()); |
| 600 | return catch_type_index_ == DexFile::kDexNoIndex16; |
| 601 | } |
| 602 | |
| 603 | uint16_t GetCatchTypeIndex() const { |
| 604 | DCHECK(IsCatchBlock()); |
| 605 | return catch_type_index_; |
| 606 | } |
| 607 | |
| 608 | const DexFile& GetCatchDexFile() const { |
| 609 | DCHECK(IsCatchBlock()); |
| 610 | return *catch_dex_file_; |
| 611 | } |
| 612 | |
| 613 | private: |
| 614 | // One of possibly several TryBoundary instructions entering the block's try. |
| 615 | // Only set for try blocks. |
| 616 | const HTryBoundary* try_entry_; |
| 617 | |
| 618 | // Exception type information. Only set for catch blocks. |
| 619 | const DexFile* catch_dex_file_; |
| 620 | const uint16_t catch_type_index_; |
| 621 | }; |
| 622 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 623 | static constexpr size_t kNoLifetime = -1; |
| 624 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 625 | // A block in a method. Contains the list of instructions represented |
| 626 | // as a double linked list. Each block knows its predecessors and |
| 627 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 628 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 629 | class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 630 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 631 | HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 632 | : graph_(graph), |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 633 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 634 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 635 | loop_information_(nullptr), |
| 636 | dominator_(nullptr), |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 637 | dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 638 | block_id_(-1), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 639 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 640 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 641 | lifetime_end_(kNoLifetime), |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 642 | try_catch_information_(nullptr) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 643 | |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 644 | const GrowableArray<HBasicBlock*>& GetPredecessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 645 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 648 | const GrowableArray<HBasicBlock*>& GetSuccessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 649 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 652 | const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 653 | return dominated_blocks_; |
| 654 | } |
| 655 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 656 | bool IsEntryBlock() const { |
| 657 | return graph_->GetEntryBlock() == this; |
| 658 | } |
| 659 | |
| 660 | bool IsExitBlock() const { |
| 661 | return graph_->GetExitBlock() == this; |
| 662 | } |
| 663 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 664 | bool IsSingleGoto() const; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 665 | bool IsSingleTryBoundary() const; |
| 666 | |
| 667 | // Returns true if this block emits nothing but a jump. |
| 668 | bool IsSingleJump() const { |
| 669 | HLoopInformation* loop_info = GetLoopInformation(); |
| 670 | return (IsSingleGoto() || IsSingleTryBoundary()) |
| 671 | // Back edges generate a suspend check. |
| 672 | && (loop_info == nullptr || !loop_info->IsBackEdge(*this)); |
| 673 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 674 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 675 | void AddBackEdge(HBasicBlock* back_edge) { |
| 676 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 677 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 678 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 679 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 680 | loop_information_->AddBackEdge(back_edge); |
| 681 | } |
| 682 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 683 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 684 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 685 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 686 | int GetBlockId() const { return block_id_; } |
| 687 | void SetBlockId(int id) { block_id_ = id; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 688 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 689 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 690 | HBasicBlock* GetDominator() const { return dominator_; } |
| 691 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 692 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); } |
| 693 | void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); } |
Vladimir Marko | 91e11c0 | 2015-09-02 17:03:22 +0100 | [diff] [blame] | 694 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 695 | for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) { |
| 696 | if (dominated_blocks_.Get(i) == existing) { |
| 697 | dominated_blocks_.Put(i, new_block); |
| 698 | return; |
| 699 | } |
| 700 | } |
| 701 | LOG(FATAL) << "Unreachable"; |
| 702 | UNREACHABLE(); |
Vladimir Marko | 91e11c0 | 2015-09-02 17:03:22 +0100 | [diff] [blame] | 703 | } |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 704 | void ClearDominanceInformation(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 705 | |
| 706 | int NumberOfBackEdges() const { |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 707 | return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 710 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 711 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 712 | const HInstructionList& GetInstructions() const { return instructions_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 713 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 714 | HInstruction* GetLastPhi() const { return phis_.last_instruction_; } |
| 715 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 716 | |
| 717 | void AddSuccessor(HBasicBlock* block) { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 718 | successors_.Add(block); |
| 719 | block->predecessors_.Add(this); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 722 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 723 | size_t successor_index = GetSuccessorIndexOf(existing); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 724 | DCHECK_NE(successor_index, static_cast<size_t>(-1)); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 725 | existing->RemovePredecessor(this); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 726 | new_block->predecessors_.Add(this); |
| 727 | successors_.Put(successor_index, new_block); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 730 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 731 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 732 | DCHECK_NE(predecessor_index, static_cast<size_t>(-1)); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 733 | existing->RemoveSuccessor(this); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 734 | new_block->successors_.Add(this); |
| 735 | predecessors_.Put(predecessor_index, new_block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 738 | // Insert `this` between `predecessor` and `successor. This method |
| 739 | // preserves the indicies, and will update the first edge found between |
| 740 | // `predecessor` and `successor`. |
| 741 | void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) { |
| 742 | size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 743 | DCHECK_NE(predecessor_index, static_cast<size_t>(-1)); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 744 | size_t successor_index = predecessor->GetSuccessorIndexOf(successor); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 745 | DCHECK_NE(successor_index, static_cast<size_t>(-1)); |
| 746 | successor->predecessors_.Put(predecessor_index, this); |
| 747 | predecessor->successors_.Put(successor_index, this); |
| 748 | successors_.Add(successor); |
| 749 | predecessors_.Add(predecessor); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 752 | void RemovePredecessor(HBasicBlock* block) { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 753 | predecessors_.Delete(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 756 | void RemoveSuccessor(HBasicBlock* block) { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 757 | successors_.Delete(block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 760 | void ClearAllPredecessors() { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 761 | predecessors_.Reset(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void AddPredecessor(HBasicBlock* block) { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 765 | predecessors_.Add(block); |
| 766 | block->successors_.Add(this); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 769 | void SwapPredecessors() { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 770 | DCHECK_EQ(predecessors_.Size(), 2u); |
| 771 | HBasicBlock* temp = predecessors_.Get(0); |
| 772 | predecessors_.Put(0, predecessors_.Get(1)); |
| 773 | predecessors_.Put(1, temp); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 774 | } |
| 775 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 776 | void SwapSuccessors() { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 777 | DCHECK_EQ(successors_.Size(), 2u); |
| 778 | HBasicBlock* temp = successors_.Get(0); |
| 779 | successors_.Put(0, successors_.Get(1)); |
| 780 | successors_.Put(1, temp); |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 781 | } |
| 782 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 783 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 784 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
| 785 | if (predecessors_.Get(i) == predecessor) { |
| 786 | return i; |
| 787 | } |
| 788 | } |
| 789 | return -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 790 | } |
| 791 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 792 | size_t GetSuccessorIndexOf(HBasicBlock* successor) const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 793 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 794 | if (successors_.Get(i) == successor) { |
| 795 | return i; |
| 796 | } |
| 797 | } |
| 798 | return -1; |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 799 | } |
| 800 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 801 | HBasicBlock* GetSinglePredecessor() const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 802 | DCHECK_EQ(GetPredecessors().Size(), 1u); |
| 803 | return GetPredecessors().Get(0); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | HBasicBlock* GetSingleSuccessor() const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 807 | DCHECK_EQ(GetSuccessors().Size(), 1u); |
| 808 | return GetSuccessors().Get(0); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // Returns whether the first occurrence of `predecessor` in the list of |
| 812 | // predecessors is at index `idx`. |
| 813 | bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 814 | DCHECK_EQ(GetPredecessors().Get(idx), predecessor); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 815 | return GetPredecessorIndexOf(predecessor) == idx; |
| 816 | } |
| 817 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 818 | // Returns the number of non-exceptional successors. SsaChecker ensures that |
| 819 | // these are stored at the beginning of the successor list. |
| 820 | size_t NumberOfNormalSuccessors() const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 821 | return EndsWithTryBoundary() ? 1 : GetSuccessors().Size(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 822 | } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 823 | |
| 824 | // Split the block into two blocks just before `cursor`. Returns the newly |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 825 | // created, latter block. Note that this method will add the block to the |
| 826 | // graph, create a Goto at the end of the former block and will create an edge |
| 827 | // between the blocks. It will not, however, update the reverse post order or |
| 828 | // loop information. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 829 | HBasicBlock* SplitBefore(HInstruction* cursor); |
| 830 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 831 | // Split the block into two blocks just after `cursor`. Returns the newly |
| 832 | // created block. Note that this method just updates raw block information, |
| 833 | // like predecessors, successors, dominators, and instruction list. It does not |
| 834 | // update the graph, reverse post order, loop information, nor make sure the |
| 835 | // blocks are consistent (for example ending with a control flow instruction). |
| 836 | HBasicBlock* SplitAfter(HInstruction* cursor); |
| 837 | |
| 838 | // Merge `other` at the end of `this`. Successors and dominated blocks of |
| 839 | // `other` are changed to be successors and dominated blocks of `this`. Note |
| 840 | // that this method does not update the graph, reverse post order, loop |
| 841 | // information, nor make sure the blocks are consistent (for example ending |
| 842 | // with a control flow instruction). |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 843 | void MergeWithInlined(HBasicBlock* other); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 844 | |
| 845 | // Replace `this` with `other`. Predecessors, successors, and dominated blocks |
| 846 | // of `this` are moved to `other`. |
| 847 | // Note that this method does not update the graph, reverse post order, loop |
| 848 | // information, nor make sure the blocks are consistent (for example ending |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 849 | // with a control flow instruction). |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 850 | void ReplaceWith(HBasicBlock* other); |
| 851 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 852 | // Merge `other` at the end of `this`. This method updates loops, reverse post |
| 853 | // order, links to predecessors, successors, dominators and deletes the block |
| 854 | // from the graph. The two blocks must be successive, i.e. `this` the only |
| 855 | // predecessor of `other` and vice versa. |
| 856 | void MergeWith(HBasicBlock* other); |
| 857 | |
| 858 | // Disconnects `this` from all its predecessors, successors and dominator, |
| 859 | // removes it from all loops it is included in and eventually from the graph. |
| 860 | // The block must not dominate any other block. Predecessors and successors |
| 861 | // are safely updated. |
| 862 | void DisconnectAndDelete(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 863 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 864 | void AddInstruction(HInstruction* instruction); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 865 | // Insert `instruction` before/after an existing instruction `cursor`. |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 866 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 867 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 868 | // Replace instruction `initial` with `replacement` within this block. |
| 869 | void ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 870 | HInstruction* replacement); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 871 | void AddPhi(HPhi* phi); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 872 | void InsertPhiAfter(HPhi* instruction, HPhi* cursor); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 873 | // RemoveInstruction and RemovePhi delete a given instruction from the respective |
| 874 | // instruction list. With 'ensure_safety' set to true, it verifies that the |
| 875 | // instruction is not in use and removes it from the use lists of its inputs. |
| 876 | void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true); |
| 877 | void RemovePhi(HPhi* phi, bool ensure_safety = true); |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 878 | void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 879 | |
| 880 | bool IsLoopHeader() const { |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 881 | return IsInLoop() && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 884 | bool IsLoopPreHeaderFirstPredecessor() const { |
| 885 | DCHECK(IsLoopHeader()); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 886 | DCHECK(!GetPredecessors().IsEmpty()); |
| 887 | return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 890 | HLoopInformation* GetLoopInformation() const { |
| 891 | return loop_information_; |
| 892 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 893 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 894 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 895 | // 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] | 896 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 897 | void SetInLoop(HLoopInformation* info) { |
| 898 | if (IsLoopHeader()) { |
| 899 | // Nothing to do. This just means `info` is an outer loop. |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 900 | } else if (!IsInLoop()) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 901 | loop_information_ = info; |
| 902 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 903 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 904 | // Note that a non loop header having a loop information means this loop information |
| 905 | // has already been populated |
| 906 | loop_information_ = info; |
| 907 | } else { |
| 908 | // Block is part of an inner loop. Do not update the loop information. |
| 909 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 910 | // at this point, because this method is being called while populating `info`. |
| 911 | } |
| 912 | } |
| 913 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 914 | // Raw update of the loop information. |
| 915 | void SetLoopInformation(HLoopInformation* info) { |
| 916 | loop_information_ = info; |
| 917 | } |
| 918 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 919 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 920 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 921 | TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; } |
| 922 | |
| 923 | void SetTryCatchInformation(TryCatchInformation* try_catch_information) { |
| 924 | try_catch_information_ = try_catch_information; |
| 925 | } |
| 926 | |
| 927 | bool IsTryBlock() const { |
| 928 | return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock(); |
| 929 | } |
| 930 | |
| 931 | bool IsCatchBlock() const { |
| 932 | return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock(); |
| 933 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 934 | |
| 935 | // Returns the try entry that this block's successors should have. They will |
| 936 | // be in the same try, unless the block ends in a try boundary. In that case, |
| 937 | // the appropriate try entry will be returned. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 938 | const HTryBoundary* ComputeTryEntryOfSuccessors() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 939 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 940 | // Returns whether this block dominates the blocked passed as parameter. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 941 | bool Dominates(HBasicBlock* block) const; |
| 942 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 943 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 944 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 945 | |
| 946 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 947 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 948 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 949 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 950 | bool EndsWithControlFlowInstruction() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 951 | bool EndsWithIf() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 952 | bool EndsWithTryBoundary() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 953 | bool HasSinglePhi() const; |
| 954 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 955 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 956 | HGraph* graph_; |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 957 | GrowableArray<HBasicBlock*> predecessors_; |
| 958 | GrowableArray<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 959 | HInstructionList instructions_; |
| 960 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 961 | HLoopInformation* loop_information_; |
| 962 | HBasicBlock* dominator_; |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 963 | GrowableArray<HBasicBlock*> dominated_blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 964 | int block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 965 | // The dex program counter of the first instruction of this block. |
| 966 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 967 | size_t lifetime_start_; |
| 968 | size_t lifetime_end_; |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 969 | TryCatchInformation* try_catch_information_; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 970 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 971 | friend class HGraph; |
| 972 | friend class HInstruction; |
| 973 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 974 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 975 | }; |
| 976 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 977 | // Iterates over the LoopInformation of all loops which contain 'block' |
| 978 | // from the innermost to the outermost. |
| 979 | class HLoopInformationOutwardIterator : public ValueObject { |
| 980 | public: |
| 981 | explicit HLoopInformationOutwardIterator(const HBasicBlock& block) |
| 982 | : current_(block.GetLoopInformation()) {} |
| 983 | |
| 984 | bool Done() const { return current_ == nullptr; } |
| 985 | |
| 986 | void Advance() { |
| 987 | DCHECK(!Done()); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 988 | current_ = current_->GetPreHeader()->GetLoopInformation(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | HLoopInformation* Current() const { |
| 992 | DCHECK(!Done()); |
| 993 | return current_; |
| 994 | } |
| 995 | |
| 996 | private: |
| 997 | HLoopInformation* current_; |
| 998 | |
| 999 | DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator); |
| 1000 | }; |
| 1001 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1002 | #define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1003 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1004 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1005 | M(ArrayGet, Instruction) \ |
| 1006 | M(ArrayLength, Instruction) \ |
| 1007 | M(ArraySet, Instruction) \ |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 1008 | M(BooleanNot, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1009 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1010 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1011 | M(CheckCast, Instruction) \ |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 1012 | M(ClearException, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1013 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1014 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1015 | M(Condition, BinaryOperation) \ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1016 | M(CurrentMethod, Instruction) \ |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1017 | M(Deoptimize, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1018 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1019 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1020 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1021 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1022 | M(Exit, Instruction) \ |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1023 | M(FakeString, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1024 | M(FloatConstant, Constant) \ |
| 1025 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1026 | M(GreaterThan, Condition) \ |
| 1027 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1028 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1029 | M(InstanceFieldGet, Instruction) \ |
| 1030 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1031 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1032 | M(IntConstant, Constant) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 1033 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1034 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1035 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1036 | M(LessThan, Condition) \ |
| 1037 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1038 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1039 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1040 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1041 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1042 | M(Local, Instruction) \ |
| 1043 | M(LongConstant, Constant) \ |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 1044 | M(MemoryBarrier, Instruction) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 1045 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1046 | M(Mul, BinaryOperation) \ |
| 1047 | M(Neg, UnaryOperation) \ |
| 1048 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1049 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 1050 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1051 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1052 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1053 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1054 | M(Or, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1055 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1056 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1057 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1058 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1059 | M(Return, Instruction) \ |
| 1060 | M(ReturnVoid, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1061 | M(Shl, BinaryOperation) \ |
| 1062 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1063 | M(StaticFieldGet, Instruction) \ |
| 1064 | M(StaticFieldSet, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1065 | M(StoreLocal, Instruction) \ |
| 1066 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1067 | M(SuspendCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1068 | M(Temporary, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1069 | M(Throw, Instruction) \ |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1070 | M(TryBoundary, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 1071 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1072 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1073 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1074 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1075 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) |
| 1076 | |
| 1077 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) |
| 1078 | |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1079 | #define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) |
| 1080 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 1081 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1082 | M(X86ComputeBaseMethodAddress, Instruction) \ |
| 1083 | M(X86LoadFromConstantTable, Instruction) |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1084 | |
| 1085 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1086 | |
| 1087 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1088 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
| 1089 | FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \ |
| 1090 | FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \ |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1091 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \ |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1092 | FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1093 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1094 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1095 | #define FOR_EACH_INSTRUCTION(M) \ |
| 1096 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1097 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1098 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1099 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1100 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1101 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1102 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1103 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 1104 | #undef FORWARD_DECLARATION |
| 1105 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1106 | #define DECLARE_INSTRUCTION(type) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1107 | InstructionKind GetKind() const OVERRIDE { return k##type; } \ |
| 1108 | const char* DebugName() const OVERRIDE { return #type; } \ |
| 1109 | const H##type* As##type() const OVERRIDE { return this; } \ |
| 1110 | H##type* As##type() OVERRIDE { return this; } \ |
| 1111 | bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1112 | return other->Is##type(); \ |
| 1113 | } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1114 | void Accept(HGraphVisitor* visitor) OVERRIDE |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1115 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1116 | template <typename T> class HUseList; |
| 1117 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1118 | template <typename T> |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1119 | class HUseListNode : public ArenaObject<kArenaAllocUseListNode> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1120 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1121 | HUseListNode* GetPrevious() const { return prev_; } |
| 1122 | HUseListNode* GetNext() const { return next_; } |
| 1123 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1124 | size_t GetIndex() const { return index_; } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1125 | void SetIndex(size_t index) { index_ = index; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1126 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1127 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1128 | HUseListNode(T user, size_t index) |
| 1129 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 1130 | |
| 1131 | T const user_; |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1132 | size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1133 | HUseListNode<T>* prev_; |
| 1134 | HUseListNode<T>* next_; |
| 1135 | |
| 1136 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1137 | |
| 1138 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 1139 | }; |
| 1140 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1141 | template <typename T> |
| 1142 | class HUseList : public ValueObject { |
| 1143 | public: |
| 1144 | HUseList() : first_(nullptr) {} |
| 1145 | |
| 1146 | void Clear() { |
| 1147 | first_ = nullptr; |
| 1148 | } |
| 1149 | |
| 1150 | // Adds a new entry at the beginning of the use list and returns |
| 1151 | // the newly created node. |
| 1152 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 1153 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1154 | if (IsEmpty()) { |
| 1155 | first_ = new_node; |
| 1156 | } else { |
| 1157 | first_->prev_ = new_node; |
| 1158 | new_node->next_ = first_; |
| 1159 | first_ = new_node; |
| 1160 | } |
| 1161 | return new_node; |
| 1162 | } |
| 1163 | |
| 1164 | HUseListNode<T>* GetFirst() const { |
| 1165 | return first_; |
| 1166 | } |
| 1167 | |
| 1168 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1169 | DCHECK(node != nullptr); |
| 1170 | DCHECK(Contains(node)); |
| 1171 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1172 | if (node->prev_ != nullptr) { |
| 1173 | node->prev_->next_ = node->next_; |
| 1174 | } |
| 1175 | if (node->next_ != nullptr) { |
| 1176 | node->next_->prev_ = node->prev_; |
| 1177 | } |
| 1178 | if (node == first_) { |
| 1179 | first_ = node->next_; |
| 1180 | } |
| 1181 | } |
| 1182 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1183 | bool Contains(const HUseListNode<T>* node) const { |
| 1184 | if (node == nullptr) { |
| 1185 | return false; |
| 1186 | } |
| 1187 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1188 | if (current == node) { |
| 1189 | return true; |
| 1190 | } |
| 1191 | } |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1195 | bool IsEmpty() const { |
| 1196 | return first_ == nullptr; |
| 1197 | } |
| 1198 | |
| 1199 | bool HasOnlyOneUse() const { |
| 1200 | return first_ != nullptr && first_->next_ == nullptr; |
| 1201 | } |
| 1202 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1203 | size_t SizeSlow() const { |
| 1204 | size_t count = 0; |
| 1205 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1206 | ++count; |
| 1207 | } |
| 1208 | return count; |
| 1209 | } |
| 1210 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1211 | private: |
| 1212 | HUseListNode<T>* first_; |
| 1213 | }; |
| 1214 | |
| 1215 | template<typename T> |
| 1216 | class HUseIterator : public ValueObject { |
| 1217 | public: |
| 1218 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 1219 | |
| 1220 | bool Done() const { return current_ == nullptr; } |
| 1221 | |
| 1222 | void Advance() { |
| 1223 | DCHECK(!Done()); |
| 1224 | current_ = current_->GetNext(); |
| 1225 | } |
| 1226 | |
| 1227 | HUseListNode<T>* Current() const { |
| 1228 | DCHECK(!Done()); |
| 1229 | return current_; |
| 1230 | } |
| 1231 | |
| 1232 | private: |
| 1233 | HUseListNode<T>* current_; |
| 1234 | |
| 1235 | friend class HValue; |
| 1236 | }; |
| 1237 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1238 | // This class is used by HEnvironment and HInstruction classes to record the |
| 1239 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 1240 | // by the used instructions. |
| 1241 | template <typename T> |
| 1242 | class HUserRecord : public ValueObject { |
| 1243 | public: |
| 1244 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 1245 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 1246 | |
| 1247 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 1248 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 1249 | DCHECK(instruction_ != nullptr); |
| 1250 | DCHECK(use_node_ != nullptr); |
| 1251 | DCHECK(old_record.use_node_ == nullptr); |
| 1252 | } |
| 1253 | |
| 1254 | HInstruction* GetInstruction() const { return instruction_; } |
| 1255 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 1256 | |
| 1257 | private: |
| 1258 | // Instruction used by the user. |
| 1259 | HInstruction* instruction_; |
| 1260 | |
| 1261 | // Corresponding entry in the use list kept by 'instruction_'. |
| 1262 | HUseListNode<T>* use_node_; |
| 1263 | }; |
| 1264 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1265 | /** |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1266 | * Side-effects representation. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1267 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1268 | * For write/read dependences on fields/arrays, the dependence analysis uses |
| 1269 | * type disambiguation (e.g. a float field write cannot modify the value of an |
| 1270 | * integer field read) and the access type (e.g. a reference array write cannot |
| 1271 | * modify the value of a reference field read [although it may modify the |
| 1272 | * reference fetch prior to reading the field, which is represented by its own |
| 1273 | * write/read dependence]). The analysis makes conservative points-to |
| 1274 | * assumptions on reference types (e.g. two same typed arrays are assumed to be |
| 1275 | * the same, and any reference read depends on any reference read without |
| 1276 | * further regard of its type). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1277 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1278 | * The internal representation uses 38-bit and is described in the table below. |
| 1279 | * The first line indicates the side effect, and for field/array accesses the |
| 1280 | * second line indicates the type of the access (in the order of the |
| 1281 | * Primitive::Type enum). |
| 1282 | * The two numbered lines below indicate the bit position in the bitfield (read |
| 1283 | * vertically). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1284 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1285 | * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W | |
| 1286 | * +-------------+---------+---------+--------------+---------+---------+ |
| 1287 | * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL| |
| 1288 | * | 3 |333333322|222222221| 1 |111111110|000000000| |
| 1289 | * | 7 |654321098|765432109| 8 |765432109|876543210| |
| 1290 | * |
| 1291 | * Note that, to ease the implementation, 'changes' bits are least significant |
| 1292 | * bits, while 'dependency' bits are most significant bits. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1293 | */ |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1294 | class SideEffects : public ValueObject { |
| 1295 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1296 | SideEffects() : flags_(0) {} |
| 1297 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1298 | static SideEffects None() { |
| 1299 | return SideEffects(0); |
| 1300 | } |
| 1301 | |
| 1302 | static SideEffects All() { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1303 | return SideEffects(kAllChangeBits | kAllDependOnBits); |
| 1304 | } |
| 1305 | |
| 1306 | static SideEffects AllChanges() { |
| 1307 | return SideEffects(kAllChangeBits); |
| 1308 | } |
| 1309 | |
| 1310 | static SideEffects AllDependencies() { |
| 1311 | return SideEffects(kAllDependOnBits); |
| 1312 | } |
| 1313 | |
| 1314 | static SideEffects AllExceptGCDependency() { |
| 1315 | return AllWritesAndReads().Union(SideEffects::CanTriggerGC()); |
| 1316 | } |
| 1317 | |
| 1318 | static SideEffects AllWritesAndReads() { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1319 | return SideEffects(kAllWrites | kAllReads); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1322 | static SideEffects AllWrites() { |
| 1323 | return SideEffects(kAllWrites); |
| 1324 | } |
| 1325 | |
| 1326 | static SideEffects AllReads() { |
| 1327 | return SideEffects(kAllReads); |
| 1328 | } |
| 1329 | |
| 1330 | static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) { |
| 1331 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1332 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1333 | : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1336 | static SideEffects ArrayWriteOfType(Primitive::Type type) { |
| 1337 | return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1340 | static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) { |
| 1341 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1342 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1343 | : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset)); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static SideEffects ArrayReadOfType(Primitive::Type type) { |
| 1347 | return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset)); |
| 1348 | } |
| 1349 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1350 | static SideEffects CanTriggerGC() { |
| 1351 | return SideEffects(1ULL << kCanTriggerGCBit); |
| 1352 | } |
| 1353 | |
| 1354 | static SideEffects DependsOnGC() { |
| 1355 | return SideEffects(1ULL << kDependsOnGCBit); |
| 1356 | } |
| 1357 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1358 | // Combines the side-effects of this and the other. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1359 | SideEffects Union(SideEffects other) const { |
| 1360 | return SideEffects(flags_ | other.flags_); |
| 1361 | } |
| 1362 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1363 | SideEffects Exclusion(SideEffects other) const { |
| 1364 | return SideEffects(flags_ & ~other.flags_); |
| 1365 | } |
| 1366 | |
| 1367 | bool Includes(SideEffects other) const { |
| 1368 | return (other.flags_ & flags_) == other.flags_; |
| 1369 | } |
| 1370 | |
| 1371 | bool HasSideEffects() const { |
| 1372 | return (flags_ & kAllChangeBits); |
| 1373 | } |
| 1374 | |
| 1375 | bool HasDependencies() const { |
| 1376 | return (flags_ & kAllDependOnBits); |
| 1377 | } |
| 1378 | |
| 1379 | // Returns true if there are no side effects or dependencies. |
| 1380 | bool DoesNothing() const { |
| 1381 | return flags_ == 0; |
| 1382 | } |
| 1383 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1384 | // Returns true if something is written. |
| 1385 | bool DoesAnyWrite() const { |
| 1386 | return (flags_ & kAllWrites); |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1389 | // Returns true if something is read. |
| 1390 | bool DoesAnyRead() const { |
| 1391 | return (flags_ & kAllReads); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1394 | // Returns true if potentially everything is written and read |
| 1395 | // (every type and every kind of access). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1396 | bool DoesAllReadWrite() const { |
| 1397 | return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads); |
| 1398 | } |
| 1399 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1400 | bool DoesAll() const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1401 | return flags_ == (kAllChangeBits | kAllDependOnBits); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | // Returns true if this may read something written by other. |
| 1405 | bool MayDependOn(SideEffects other) const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1406 | const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits; |
| 1407 | return (other.flags_ & depends_on_flags); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // Returns string representation of flags (for debugging only). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1411 | // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL| |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1412 | std::string ToString() const { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1413 | std::string flags = "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1414 | for (int s = kLastBit; s >= 0; s--) { |
| 1415 | bool current_bit_is_set = ((flags_ >> s) & 1) != 0; |
| 1416 | if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) { |
| 1417 | // This is a bit for the GC side effect. |
| 1418 | if (current_bit_is_set) { |
| 1419 | flags += "GC"; |
| 1420 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1421 | flags += "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1422 | } else { |
| 1423 | // This is a bit for the array/field analysis. |
| 1424 | // The underscore character stands for the 'can trigger GC' bit. |
| 1425 | static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD"; |
| 1426 | if (current_bit_is_set) { |
| 1427 | flags += kDebug[s]; |
| 1428 | } |
| 1429 | if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) || |
| 1430 | (s == kFieldReadOffset) || (s == kArrayReadOffset)) { |
| 1431 | flags += "|"; |
| 1432 | } |
| 1433 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1434 | } |
| 1435 | return flags; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1436 | } |
| 1437 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1438 | bool Equals(const SideEffects& other) const { return flags_ == other.flags_; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1439 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1440 | private: |
| 1441 | static constexpr int kFieldArrayAnalysisBits = 9; |
| 1442 | |
| 1443 | static constexpr int kFieldWriteOffset = 0; |
| 1444 | static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits; |
| 1445 | static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1; |
| 1446 | static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1; |
| 1447 | |
| 1448 | static constexpr int kChangeBits = kCanTriggerGCBit + 1; |
| 1449 | |
| 1450 | static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1; |
| 1451 | static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits; |
| 1452 | static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1; |
| 1453 | static constexpr int kDependsOnGCBit = kLastBitForReads + 1; |
| 1454 | |
| 1455 | static constexpr int kLastBit = kDependsOnGCBit; |
| 1456 | static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits; |
| 1457 | |
| 1458 | // Aliases. |
| 1459 | |
| 1460 | static_assert(kChangeBits == kDependOnBits, |
| 1461 | "the 'change' bits should match the 'depend on' bits."); |
| 1462 | |
| 1463 | static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1); |
| 1464 | static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits; |
| 1465 | static constexpr uint64_t kAllWrites = |
| 1466 | ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset; |
| 1467 | static constexpr uint64_t kAllReads = |
| 1468 | ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1469 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1470 | // Work around the fact that HIR aliases I/F and J/D. |
| 1471 | // TODO: remove this interceptor once HIR types are clean |
| 1472 | static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) { |
| 1473 | switch (type) { |
| 1474 | case Primitive::kPrimInt: |
| 1475 | case Primitive::kPrimFloat: |
| 1476 | return TypeFlag(Primitive::kPrimInt, offset) | |
| 1477 | TypeFlag(Primitive::kPrimFloat, offset); |
| 1478 | case Primitive::kPrimLong: |
| 1479 | case Primitive::kPrimDouble: |
| 1480 | return TypeFlag(Primitive::kPrimLong, offset) | |
| 1481 | TypeFlag(Primitive::kPrimDouble, offset); |
| 1482 | default: |
| 1483 | return TypeFlag(type, offset); |
| 1484 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1487 | // Translates type to bit flag. |
| 1488 | static uint64_t TypeFlag(Primitive::Type type, int offset) { |
| 1489 | CHECK_NE(type, Primitive::kPrimVoid); |
| 1490 | const uint64_t one = 1; |
| 1491 | const int shift = type; // 0-based consecutive enum |
| 1492 | DCHECK_LE(kFieldWriteOffset, shift); |
| 1493 | DCHECK_LT(shift, kArrayWriteOffset); |
| 1494 | return one << (type + offset); |
| 1495 | } |
| 1496 | |
| 1497 | // Private constructor on direct flags value. |
| 1498 | explicit SideEffects(uint64_t flags) : flags_(flags) {} |
| 1499 | |
| 1500 | uint64_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1501 | }; |
| 1502 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1503 | // 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] | 1504 | class HEnvironment : public ArenaObject<kArenaAllocEnvironment> { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1505 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1506 | HEnvironment(ArenaAllocator* arena, |
| 1507 | size_t number_of_vregs, |
| 1508 | const DexFile& dex_file, |
| 1509 | uint32_t method_idx, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1510 | uint32_t dex_pc, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1511 | InvokeType invoke_type, |
| 1512 | HInstruction* holder) |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1513 | : vregs_(arena, number_of_vregs), |
| 1514 | locations_(arena, number_of_vregs), |
| 1515 | parent_(nullptr), |
| 1516 | dex_file_(dex_file), |
| 1517 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1518 | dex_pc_(dex_pc), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1519 | invoke_type_(invoke_type), |
| 1520 | holder_(holder) { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1521 | vregs_.SetSize(number_of_vregs); |
| 1522 | for (size_t i = 0; i < number_of_vregs; i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1523 | vregs_.Put(i, HUserRecord<HEnvironment*>()); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1524 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1525 | |
| 1526 | locations_.SetSize(number_of_vregs); |
| 1527 | for (size_t i = 0; i < number_of_vregs; ++i) { |
| 1528 | locations_.Put(i, Location()); |
| 1529 | } |
| 1530 | } |
| 1531 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1532 | HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1533 | : HEnvironment(arena, |
| 1534 | to_copy.Size(), |
| 1535 | to_copy.GetDexFile(), |
| 1536 | to_copy.GetMethodIdx(), |
| 1537 | to_copy.GetDexPc(), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1538 | to_copy.GetInvokeType(), |
| 1539 | holder) {} |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1540 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1541 | void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1542 | if (parent_ != nullptr) { |
| 1543 | parent_->SetAndCopyParentChain(allocator, parent); |
| 1544 | } else { |
| 1545 | parent_ = new (allocator) HEnvironment(allocator, *parent, holder_); |
| 1546 | parent_->CopyFrom(parent); |
| 1547 | if (parent->GetParent() != nullptr) { |
| 1548 | parent_->SetAndCopyParentChain(allocator, parent->GetParent()); |
| 1549 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1550 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 1553 | void CopyFrom(const GrowableArray<HInstruction*>& locals); |
| 1554 | void CopyFrom(HEnvironment* environment); |
| 1555 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1556 | // Copy from `env`. If it's a loop phi for `loop_header`, copy the first |
| 1557 | // input to the loop phi instead. This is for inserting instructions that |
| 1558 | // require an environment (like HDeoptimization) in the loop pre-header. |
| 1559 | void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1560 | |
| 1561 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1562 | vregs_.Put(index, HUserRecord<HEnvironment*>(instruction)); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | HInstruction* GetInstructionAt(size_t index) const { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1566 | return vregs_.Get(index).GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1569 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1570 | |
| 1571 | size_t Size() const { return vregs_.Size(); } |
| 1572 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1573 | HEnvironment* GetParent() const { return parent_; } |
| 1574 | |
| 1575 | void SetLocationAt(size_t index, Location location) { |
| 1576 | locations_.Put(index, location); |
| 1577 | } |
| 1578 | |
| 1579 | Location GetLocationAt(size_t index) const { |
| 1580 | return locations_.Get(index); |
| 1581 | } |
| 1582 | |
| 1583 | uint32_t GetDexPc() const { |
| 1584 | return dex_pc_; |
| 1585 | } |
| 1586 | |
| 1587 | uint32_t GetMethodIdx() const { |
| 1588 | return method_idx_; |
| 1589 | } |
| 1590 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1591 | InvokeType GetInvokeType() const { |
| 1592 | return invoke_type_; |
| 1593 | } |
| 1594 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1595 | const DexFile& GetDexFile() const { |
| 1596 | return dex_file_; |
| 1597 | } |
| 1598 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1599 | HInstruction* GetHolder() const { |
| 1600 | return holder_; |
| 1601 | } |
| 1602 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1603 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1604 | // Record instructions' use entries of this environment for constant-time removal. |
| 1605 | // It should only be called by HInstruction when a new environment use is added. |
| 1606 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 1607 | DCHECK(env_use->GetUser() == this); |
| 1608 | size_t index = env_use->GetIndex(); |
| 1609 | vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use)); |
| 1610 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1611 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1612 | GrowableArray<HUserRecord<HEnvironment*> > vregs_; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1613 | GrowableArray<Location> locations_; |
| 1614 | HEnvironment* parent_; |
| 1615 | const DexFile& dex_file_; |
| 1616 | const uint32_t method_idx_; |
| 1617 | const uint32_t dex_pc_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1618 | const InvokeType invoke_type_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1619 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1620 | // The instruction that holds this environment. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1621 | HInstruction* const holder_; |
| 1622 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1623 | friend class HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1624 | |
| 1625 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 1626 | }; |
| 1627 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1628 | class ReferenceTypeInfo : ValueObject { |
| 1629 | public: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1630 | typedef Handle<mirror::Class> TypeHandle; |
| 1631 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1632 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) { |
| 1633 | // The constructor will check that the type_handle is valid. |
| 1634 | return ReferenceTypeInfo(type_handle, is_exact); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1637 | static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); } |
| 1638 | |
| 1639 | static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1640 | return handle.GetReference() != nullptr; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1643 | bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1644 | return IsValidHandle(type_handle_); |
| 1645 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1646 | bool IsExact() const { return is_exact_; } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1647 | |
| 1648 | bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1649 | DCHECK(IsValid()); |
| 1650 | return GetTypeHandle()->IsObjectClass(); |
| 1651 | } |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1652 | bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1653 | DCHECK(IsValid()); |
| 1654 | return GetTypeHandle()->IsInterface(); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 1655 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1656 | |
| 1657 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 1658 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1659 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1660 | DCHECK(IsValid()); |
| 1661 | DCHECK(rti.IsValid()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1662 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 1663 | } |
| 1664 | |
| 1665 | // Returns true if the type information provide the same amount of details. |
| 1666 | // 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] | 1667 | // (because the type can be the result of a merge). |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1668 | bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1669 | if (!IsValid() && !rti.IsValid()) { |
| 1670 | // Invalid types are equal. |
Calin Juravle | 7733bd6 | 2015-07-22 17:14:50 +0000 | [diff] [blame] | 1671 | return true; |
| 1672 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1673 | if (!IsValid() || !rti.IsValid()) { |
| 1674 | // One is valid, the other not. |
Calin Juravle | 7733bd6 | 2015-07-22 17:14:50 +0000 | [diff] [blame] | 1675 | return false; |
| 1676 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1677 | return IsExact() == rti.IsExact() |
| 1678 | && GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | private: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1682 | ReferenceTypeInfo(); |
| 1683 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1684 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1685 | // The class of the object. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1686 | TypeHandle type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1687 | // 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] | 1688 | // Whether or not we have any information about this type. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1689 | bool is_exact_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1690 | }; |
| 1691 | |
| 1692 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 1693 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1694 | class HInstruction : public ArenaObject<kArenaAllocInstruction> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1695 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1696 | HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1697 | : previous_(nullptr), |
| 1698 | next_(nullptr), |
| 1699 | block_(nullptr), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1700 | dex_pc_(dex_pc), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1701 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1702 | ssa_index_(-1), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1703 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1704 | locations_(nullptr), |
| 1705 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1706 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1707 | side_effects_(side_effects), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1708 | reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1709 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1710 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1711 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1712 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1713 | enum InstructionKind { |
| 1714 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1715 | }; |
| 1716 | #undef DECLARE_KIND |
| 1717 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1718 | HInstruction* GetNext() const { return next_; } |
| 1719 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1720 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1721 | HInstruction* GetNextDisregardingMoves() const; |
| 1722 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1723 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1724 | HBasicBlock* GetBlock() const { return block_; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1725 | ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1726 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1727 | bool IsInBlock() const { return block_ != nullptr; } |
| 1728 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 1729 | bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1730 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1731 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1732 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1733 | |
| 1734 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1735 | virtual const char* DebugName() const = 0; |
| 1736 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1737 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1738 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1739 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1740 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1741 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1742 | virtual bool NeedsEnvironment() const { return false; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1743 | |
| 1744 | uint32_t GetDexPc() const { return dex_pc_; } |
| 1745 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1746 | virtual bool IsControlFlow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1747 | |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1748 | virtual bool CanThrow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1749 | bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1750 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1751 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1752 | bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1753 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1754 | // Does not apply for all instructions, but having this at top level greatly |
| 1755 | // simplifies the null check elimination. |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 1756 | // TODO: Consider merging can_be_null into ReferenceTypeInfo. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1757 | virtual bool CanBeNull() const { |
| 1758 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1759 | return true; |
| 1760 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1761 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1762 | virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const { |
| 1763 | UNUSED(obj); |
| 1764 | return false; |
| 1765 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1766 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1767 | void SetReferenceTypeInfo(ReferenceTypeInfo rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1768 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1769 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1770 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1771 | return reference_type_info_; |
| 1772 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1773 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1774 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1775 | DCHECK(user != nullptr); |
| 1776 | HUseListNode<HInstruction*>* use = |
| 1777 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1778 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1781 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1782 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1783 | HUseListNode<HEnvironment*>* env_use = |
| 1784 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1785 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1786 | } |
| 1787 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1788 | void RemoveAsUserOfInput(size_t input) { |
| 1789 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1790 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1791 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1792 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1793 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1794 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1795 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1796 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1797 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1798 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1799 | bool HasOnlyOneNonEnvironmentUse() const { |
| 1800 | return !HasEnvironmentUses() && GetUses().HasOnlyOneUse(); |
| 1801 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1802 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1803 | // Does this instruction strictly dominate `other_instruction`? |
| 1804 | // Returns false if this instruction and `other_instruction` are the same. |
| 1805 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1806 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1807 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1808 | int GetId() const { return id_; } |
| 1809 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1810 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1811 | int GetSsaIndex() const { return ssa_index_; } |
| 1812 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1813 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1814 | |
| 1815 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1816 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1817 | // Set the `environment_` field. Raw because this method does not |
| 1818 | // update the uses lists. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1819 | void SetRawEnvironment(HEnvironment* environment) { |
| 1820 | DCHECK(environment_ == nullptr); |
| 1821 | DCHECK_EQ(environment->GetHolder(), this); |
| 1822 | environment_ = environment; |
| 1823 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1824 | |
| 1825 | // Set the environment of this instruction, copying it from `environment`. While |
| 1826 | // copying, the uses lists are being updated. |
| 1827 | void CopyEnvironmentFrom(HEnvironment* environment) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1828 | DCHECK(environment_ == nullptr); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1829 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1830 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1831 | environment_->CopyFrom(environment); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1832 | if (environment->GetParent() != nullptr) { |
| 1833 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1834 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1835 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1836 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1837 | void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment, |
| 1838 | HBasicBlock* block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1839 | DCHECK(environment_ == nullptr); |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1840 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1841 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1842 | environment_->CopyFromWithLoopPhiAdjustment(environment, block); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1843 | if (environment->GetParent() != nullptr) { |
| 1844 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1845 | } |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1848 | // Returns the number of entries in the environment. Typically, that is the |
| 1849 | // number of dex registers in a method. It could be more in case of inlining. |
| 1850 | size_t EnvironmentSize() const; |
| 1851 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1852 | LocationSummary* GetLocations() const { return locations_; } |
| 1853 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1854 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1855 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1856 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1857 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1858 | // This is almost the same as doing `ReplaceWith()`. But in this helper, the |
| 1859 | // uses of this instruction by `other` are *not* updated. |
| 1860 | void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) { |
| 1861 | ReplaceWith(other); |
| 1862 | other->ReplaceInput(this, use_index); |
| 1863 | } |
| 1864 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1865 | // Move `this` instruction before `cursor`. |
| 1866 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1867 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1868 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1869 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1870 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1871 | virtual H##type* As##type() { return nullptr; } |
| 1872 | |
| 1873 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1874 | #undef INSTRUCTION_TYPE_CHECK |
| 1875 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1876 | // Returns whether the instruction can be moved within the graph. |
| 1877 | virtual bool CanBeMoved() const { return false; } |
| 1878 | |
| 1879 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1880 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1881 | UNUSED(other); |
| 1882 | return false; |
| 1883 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1884 | |
| 1885 | // Returns whether any data encoded in the two instructions is equal. |
| 1886 | // This method does not look at the inputs. Both instructions must be |
| 1887 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1888 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1889 | UNUSED(other); |
| 1890 | return false; |
| 1891 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1892 | |
| 1893 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1894 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1895 | // 2) Their inputs are identical. |
| 1896 | bool Equals(HInstruction* other) const; |
| 1897 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1898 | virtual InstructionKind GetKind() const = 0; |
| 1899 | |
| 1900 | virtual size_t ComputeHashCode() const { |
| 1901 | size_t result = GetKind(); |
| 1902 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1903 | result = (result * 31) + InputAt(i)->GetId(); |
| 1904 | } |
| 1905 | return result; |
| 1906 | } |
| 1907 | |
| 1908 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1909 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1910 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1911 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1912 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1913 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1914 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1915 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1916 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1917 | |
| 1918 | // Returns whether the code generation of the instruction will require to have access |
| 1919 | // to the current method. Such instructions are: |
| 1920 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1921 | // to walk the stack and have the current method stored at a specific stack address. |
| 1922 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1923 | // fields of the current method. |
| 1924 | bool NeedsCurrentMethod() const { |
| 1925 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1926 | } |
| 1927 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1928 | virtual bool NeedsDexCache() const { return false; } |
| 1929 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1930 | // Does this instruction have any use in an environment before |
| 1931 | // control flow hits 'other'? |
| 1932 | bool HasAnyEnvironmentUseBefore(HInstruction* other); |
| 1933 | |
| 1934 | // Remove all references to environment uses of this instruction. |
| 1935 | // The caller must ensure that this is safe to do. |
| 1936 | void RemoveEnvironmentUsers(); |
| 1937 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1938 | protected: |
| 1939 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1940 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1941 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1942 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1943 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1944 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1945 | HInstruction* previous_; |
| 1946 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1947 | HBasicBlock* block_; |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1948 | const uint32_t dex_pc_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1949 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1950 | // An instruction gets an id when it is added to the graph. |
| 1951 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1952 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1953 | int id_; |
| 1954 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1955 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1956 | int ssa_index_; |
| 1957 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1958 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1959 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1960 | |
| 1961 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1962 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1963 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1964 | // The environment associated with this instruction. Not null if the instruction |
| 1965 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1966 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1967 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1968 | // Set by the code generator. |
| 1969 | LocationSummary* locations_; |
| 1970 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1971 | // Set by the liveness analysis. |
| 1972 | LiveInterval* live_interval_; |
| 1973 | |
| 1974 | // Set by the liveness analysis, this is the position in a linear |
| 1975 | // order of blocks where this instruction's live interval start. |
| 1976 | size_t lifetime_position_; |
| 1977 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1978 | const SideEffects side_effects_; |
| 1979 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1980 | // TODO: for primitive types this should be marked as invalid. |
| 1981 | ReferenceTypeInfo reference_type_info_; |
| 1982 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1983 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1984 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1985 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1986 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1987 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1988 | |
| 1989 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1990 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1991 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1992 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1993 | class HInputIterator : public ValueObject { |
| 1994 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1995 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1996 | |
| 1997 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1998 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1999 | void Advance() { index_++; } |
| 2000 | |
| 2001 | private: |
| 2002 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2003 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2004 | |
| 2005 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 2006 | }; |
| 2007 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2008 | class HInstructionIterator : public ValueObject { |
| 2009 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2010 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 2011 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2012 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2015 | bool Done() const { return instruction_ == nullptr; } |
| 2016 | HInstruction* Current() const { return instruction_; } |
| 2017 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2018 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2019 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | private: |
| 2023 | HInstruction* instruction_; |
| 2024 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2025 | |
| 2026 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2027 | }; |
| 2028 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2029 | class HBackwardInstructionIterator : public ValueObject { |
| 2030 | public: |
| 2031 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 2032 | : instruction_(instructions.last_instruction_) { |
| 2033 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2034 | } |
| 2035 | |
| 2036 | bool Done() const { return instruction_ == nullptr; } |
| 2037 | HInstruction* Current() const { return instruction_; } |
| 2038 | void Advance() { |
| 2039 | instruction_ = next_; |
| 2040 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2041 | } |
| 2042 | |
| 2043 | private: |
| 2044 | HInstruction* instruction_; |
| 2045 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2046 | |
| 2047 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2048 | }; |
| 2049 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2050 | template<size_t N> |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2051 | class HTemplateInstruction: public HInstruction { |
| 2052 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2053 | HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2054 | : HInstruction(side_effects, dex_pc), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2055 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2056 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2057 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2058 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2059 | protected: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2060 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 2061 | DCHECK_LT(i, N); |
| 2062 | return inputs_[i]; |
| 2063 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2064 | |
| 2065 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2066 | DCHECK_LT(i, N); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2067 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2070 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2071 | std::array<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2072 | |
| 2073 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2074 | }; |
| 2075 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2076 | // HTemplateInstruction specialization for N=0. |
| 2077 | template<> |
| 2078 | class HTemplateInstruction<0>: public HInstruction { |
| 2079 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2080 | explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2081 | : HInstruction(side_effects, dex_pc) {} |
| 2082 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2083 | virtual ~HTemplateInstruction() {} |
| 2084 | |
| 2085 | size_t InputCount() const OVERRIDE { return 0; } |
| 2086 | |
| 2087 | protected: |
| 2088 | const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2089 | LOG(FATAL) << "Unreachable"; |
| 2090 | UNREACHABLE(); |
| 2091 | } |
| 2092 | |
| 2093 | void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED, |
| 2094 | const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE { |
| 2095 | LOG(FATAL) << "Unreachable"; |
| 2096 | UNREACHABLE(); |
| 2097 | } |
| 2098 | |
| 2099 | private: |
| 2100 | friend class SsaBuilder; |
| 2101 | }; |
| 2102 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2103 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2104 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2105 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2106 | HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2107 | : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2108 | virtual ~HExpression() {} |
| 2109 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2110 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2111 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2112 | protected: |
| 2113 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2114 | }; |
| 2115 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2116 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 2117 | // instruction that branches to the exit block. |
| 2118 | class HReturnVoid : public HTemplateInstruction<0> { |
| 2119 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2120 | explicit HReturnVoid(uint32_t dex_pc = kNoDexPc) |
| 2121 | : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2122 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2123 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2124 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2125 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2126 | |
| 2127 | private: |
| 2128 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 2129 | }; |
| 2130 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2131 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 2132 | // instruction that branches to the exit block. |
| 2133 | class HReturn : public HTemplateInstruction<1> { |
| 2134 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2135 | explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 2136 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2137 | SetRawInputAt(0, value); |
| 2138 | } |
| 2139 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2140 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2141 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2142 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2143 | |
| 2144 | private: |
| 2145 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 2146 | }; |
| 2147 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2148 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2149 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2150 | // exit block. |
| 2151 | class HExit : public HTemplateInstruction<0> { |
| 2152 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2153 | explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2154 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2155 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2156 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2157 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2158 | |
| 2159 | private: |
| 2160 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 2161 | }; |
| 2162 | |
| 2163 | // Jumps from one block to another. |
| 2164 | class HGoto : public HTemplateInstruction<0> { |
| 2165 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2166 | explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2167 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2168 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2169 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2170 | HBasicBlock* GetSuccessor() const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2171 | return GetBlock()->GetSingleSuccessor(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2174 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2175 | |
| 2176 | private: |
| 2177 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 2178 | }; |
| 2179 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2180 | class HConstant : public HExpression<0> { |
| 2181 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2182 | explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2183 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2184 | |
| 2185 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2186 | |
| 2187 | virtual bool IsMinusOne() const { return false; } |
| 2188 | virtual bool IsZero() const { return false; } |
| 2189 | virtual bool IsOne() const { return false; } |
| 2190 | |
| 2191 | DECLARE_INSTRUCTION(Constant); |
| 2192 | |
| 2193 | private: |
| 2194 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 2195 | }; |
| 2196 | |
| 2197 | class HNullConstant : public HConstant { |
| 2198 | public: |
| 2199 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2200 | return true; |
| 2201 | } |
| 2202 | |
| 2203 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 2204 | |
| 2205 | DECLARE_INSTRUCTION(NullConstant); |
| 2206 | |
| 2207 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2208 | explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2209 | |
| 2210 | friend class HGraph; |
| 2211 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 2212 | }; |
| 2213 | |
| 2214 | // Constants of the type int. Those can be from Dex instructions, or |
| 2215 | // synthesized (for example with the if-eqz instruction). |
| 2216 | class HIntConstant : public HConstant { |
| 2217 | public: |
| 2218 | int32_t GetValue() const { return value_; } |
| 2219 | |
| 2220 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2221 | DCHECK(other->IsIntConstant()); |
| 2222 | return other->AsIntConstant()->value_ == value_; |
| 2223 | } |
| 2224 | |
| 2225 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 2226 | |
| 2227 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2228 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2229 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2230 | |
| 2231 | DECLARE_INSTRUCTION(IntConstant); |
| 2232 | |
| 2233 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2234 | explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2235 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {} |
| 2236 | explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc) |
| 2237 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2238 | |
| 2239 | const int32_t value_; |
| 2240 | |
| 2241 | friend class HGraph; |
| 2242 | ART_FRIEND_TEST(GraphTest, InsertInstructionBefore); |
| 2243 | ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast); |
| 2244 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 2245 | }; |
| 2246 | |
| 2247 | class HLongConstant : public HConstant { |
| 2248 | public: |
| 2249 | int64_t GetValue() const { return value_; } |
| 2250 | |
| 2251 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2252 | DCHECK(other->IsLongConstant()); |
| 2253 | return other->AsLongConstant()->value_ == value_; |
| 2254 | } |
| 2255 | |
| 2256 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
| 2257 | |
| 2258 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2259 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2260 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2261 | |
| 2262 | DECLARE_INSTRUCTION(LongConstant); |
| 2263 | |
| 2264 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2265 | explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2266 | : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2267 | |
| 2268 | const int64_t value_; |
| 2269 | |
| 2270 | friend class HGraph; |
| 2271 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 2272 | }; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2273 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2274 | // Conditional branch. A block ending with an HIf instruction must have |
| 2275 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2276 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2277 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2278 | explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2279 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2280 | SetRawInputAt(0, input); |
| 2281 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2282 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2283 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2284 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2285 | HBasicBlock* IfTrueSuccessor() const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 2286 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | HBasicBlock* IfFalseSuccessor() const { |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 2290 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2293 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2294 | |
| 2295 | private: |
| 2296 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 2297 | }; |
| 2298 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Abstract instruction which marks the beginning and/or end of a try block and |
| 2301 | // links it to the respective exception handlers. Behaves the same as a Goto in |
| 2302 | // non-exceptional control flow. |
| 2303 | // Normal-flow successor is stored at index zero, exception handlers under |
| 2304 | // higher indices in no particular order. |
| 2305 | class HTryBoundary : public HTemplateInstruction<0> { |
| 2306 | public: |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2307 | enum BoundaryKind { |
| 2308 | kEntry, |
| 2309 | kExit, |
| 2310 | }; |
| 2311 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2312 | explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc) |
| 2313 | : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {} |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2314 | |
| 2315 | bool IsControlFlow() const OVERRIDE { return true; } |
| 2316 | |
| 2317 | // Returns the block's non-exceptional successor (index zero). |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 2318 | HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2319 | |
| 2320 | // Returns whether `handler` is among its exception handlers (non-zero index |
| 2321 | // successors). |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2322 | bool HasExceptionHandler(const HBasicBlock& handler) const { |
| 2323 | DCHECK(handler.IsCatchBlock()); |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 2324 | return GetBlock()->GetSuccessors().Contains( |
| 2325 | const_cast<HBasicBlock*>(&handler), /* start_from */ 1); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | // If not present already, adds `handler` to its block's list of exception |
| 2329 | // handlers. |
| 2330 | void AddExceptionHandler(HBasicBlock* handler) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2331 | if (!HasExceptionHandler(*handler)) { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2332 | GetBlock()->AddSuccessor(handler); |
| 2333 | } |
| 2334 | } |
| 2335 | |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2336 | bool IsEntry() const { return kind_ == BoundaryKind::kEntry; } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2337 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2338 | bool HasSameExceptionHandlersAs(const HTryBoundary& other) const; |
| 2339 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2340 | DECLARE_INSTRUCTION(TryBoundary); |
| 2341 | |
| 2342 | private: |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2343 | const BoundaryKind kind_; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2344 | |
| 2345 | DISALLOW_COPY_AND_ASSIGN(HTryBoundary); |
| 2346 | }; |
| 2347 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2348 | // Iterator over exception handlers of a given HTryBoundary, i.e. over |
| 2349 | // exceptional successors of its basic block. |
| 2350 | class HExceptionHandlerIterator : public ValueObject { |
| 2351 | public: |
| 2352 | explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary) |
| 2353 | : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {} |
| 2354 | |
Vladimir Marko | 145acc5 | 2015-09-03 13:33:25 +0000 | [diff] [blame] | 2355 | bool Done() const { return index_ == block_.GetSuccessors().Size(); } |
| 2356 | HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2357 | size_t CurrentSuccessorIndex() const { return index_; } |
| 2358 | void Advance() { ++index_; } |
| 2359 | |
| 2360 | private: |
| 2361 | const HBasicBlock& block_; |
| 2362 | size_t index_; |
| 2363 | |
| 2364 | DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator); |
| 2365 | }; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2366 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2367 | // Deoptimize to interpreter, upon checking a condition. |
| 2368 | class HDeoptimize : public HTemplateInstruction<1> { |
| 2369 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2370 | explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc) |
| 2371 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2372 | SetRawInputAt(0, cond); |
| 2373 | } |
| 2374 | |
| 2375 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2376 | bool CanThrow() const OVERRIDE { return true; } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2377 | |
| 2378 | DECLARE_INSTRUCTION(Deoptimize); |
| 2379 | |
| 2380 | private: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2381 | DISALLOW_COPY_AND_ASSIGN(HDeoptimize); |
| 2382 | }; |
| 2383 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2384 | // Represents the ArtMethod that was passed as a first argument to |
| 2385 | // the method. It is used by instructions that depend on it, like |
| 2386 | // instructions that work with the dex cache. |
| 2387 | class HCurrentMethod : public HExpression<0> { |
| 2388 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2389 | explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2390 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2391 | |
| 2392 | DECLARE_INSTRUCTION(CurrentMethod); |
| 2393 | |
| 2394 | private: |
| 2395 | DISALLOW_COPY_AND_ASSIGN(HCurrentMethod); |
| 2396 | }; |
| 2397 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2398 | class HUnaryOperation : public HExpression<1> { |
| 2399 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2400 | HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2401 | : HExpression(result_type, SideEffects::None(), dex_pc) { |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2402 | SetRawInputAt(0, input); |
| 2403 | } |
| 2404 | |
| 2405 | HInstruction* GetInput() const { return InputAt(0); } |
| 2406 | Primitive::Type GetResultType() const { return GetType(); } |
| 2407 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2408 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2409 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2410 | UNUSED(other); |
| 2411 | return true; |
| 2412 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2413 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2414 | // Try to statically evaluate `operation` and return a HConstant |
| 2415 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2416 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2417 | HConstant* TryStaticEvaluation() const; |
| 2418 | |
| 2419 | // Apply this operation to `x`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2420 | virtual HConstant* Evaluate(HIntConstant* x) const = 0; |
| 2421 | virtual HConstant* Evaluate(HLongConstant* x) const = 0; |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2422 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2423 | DECLARE_INSTRUCTION(UnaryOperation); |
| 2424 | |
| 2425 | private: |
| 2426 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 2427 | }; |
| 2428 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2429 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2430 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2431 | HBinaryOperation(Primitive::Type result_type, |
| 2432 | HInstruction* left, |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 2433 | HInstruction* right, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2434 | SideEffects side_effects = SideEffects::None(), |
| 2435 | uint32_t dex_pc = kNoDexPc) |
| 2436 | : HExpression(result_type, side_effects, dex_pc) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2437 | SetRawInputAt(0, left); |
| 2438 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2441 | HInstruction* GetLeft() const { return InputAt(0); } |
| 2442 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2443 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2444 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2445 | virtual bool IsCommutative() const { return false; } |
| 2446 | |
| 2447 | // Put constant on the right. |
| 2448 | // Returns whether order is changed. |
| 2449 | bool OrderInputsWithConstantOnTheRight() { |
| 2450 | HInstruction* left = InputAt(0); |
| 2451 | HInstruction* right = InputAt(1); |
| 2452 | if (left->IsConstant() && !right->IsConstant()) { |
| 2453 | ReplaceInput(right, 0); |
| 2454 | ReplaceInput(left, 1); |
| 2455 | return true; |
| 2456 | } |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | // Order inputs by instruction id, but favor constant on the right side. |
| 2461 | // This helps GVN for commutative ops. |
| 2462 | void OrderInputs() { |
| 2463 | DCHECK(IsCommutative()); |
| 2464 | HInstruction* left = InputAt(0); |
| 2465 | HInstruction* right = InputAt(1); |
| 2466 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 2467 | return; |
| 2468 | } |
| 2469 | if (OrderInputsWithConstantOnTheRight()) { |
| 2470 | return; |
| 2471 | } |
| 2472 | // Order according to instruction id. |
| 2473 | if (left->GetId() > right->GetId()) { |
| 2474 | ReplaceInput(right, 0); |
| 2475 | ReplaceInput(left, 1); |
| 2476 | } |
| 2477 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2478 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2479 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2480 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2481 | UNUSED(other); |
| 2482 | return true; |
| 2483 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2484 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2485 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2486 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2487 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2488 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2489 | |
| 2490 | // Apply this operation to `x` and `y`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2491 | virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0; |
| 2492 | virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0; |
| 2493 | virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED, |
| 2494 | HLongConstant* y ATTRIBUTE_UNUSED) const { |
| 2495 | VLOG(compiler) << DebugName() << " is not defined for the (int, long) case."; |
| 2496 | return nullptr; |
| 2497 | } |
| 2498 | virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED, |
| 2499 | HIntConstant* y ATTRIBUTE_UNUSED) const { |
| 2500 | VLOG(compiler) << DebugName() << " is not defined for the (long, int) case."; |
| 2501 | return nullptr; |
| 2502 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2503 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2504 | // 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] | 2505 | // constant, or null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2506 | HConstant* GetConstantRight() const; |
| 2507 | |
| 2508 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2509 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2510 | HInstruction* GetLeastConstantLeft() const; |
| 2511 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 2512 | DECLARE_INSTRUCTION(BinaryOperation); |
| 2513 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2514 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2515 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 2516 | }; |
| 2517 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2518 | // The comparison bias applies for floating point operations and indicates how NaN |
| 2519 | // comparisons are treated: |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2520 | enum class ComparisonBias { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2521 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 2522 | kGtBias, // return 1 for NaN comparisons |
| 2523 | kLtBias, // return -1 for NaN comparisons |
| 2524 | }; |
| 2525 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2526 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2527 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2528 | HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2529 | : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc), |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2530 | needs_materialization_(true), |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2531 | bias_(ComparisonBias::kNoBias) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2532 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2533 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 2534 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2535 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2536 | // For code generation purposes, returns whether this instruction is just before |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2537 | // `instruction`, and disregard moves in between. |
| 2538 | bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2539 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2540 | DECLARE_INSTRUCTION(Condition); |
| 2541 | |
| 2542 | virtual IfCondition GetCondition() const = 0; |
| 2543 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2544 | virtual IfCondition GetOppositeCondition() const = 0; |
| 2545 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2546 | bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2547 | |
| 2548 | void SetBias(ComparisonBias bias) { bias_ = bias; } |
| 2549 | |
| 2550 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2551 | return bias_ == other->AsCondition()->bias_; |
| 2552 | } |
| 2553 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2554 | bool IsFPConditionTrueIfNaN() const { |
| 2555 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())); |
| 2556 | IfCondition if_cond = GetCondition(); |
| 2557 | return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE); |
| 2558 | } |
| 2559 | |
| 2560 | bool IsFPConditionFalseIfNaN() const { |
| 2561 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())); |
| 2562 | IfCondition if_cond = GetCondition(); |
| 2563 | return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ); |
| 2564 | } |
| 2565 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2566 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2567 | // For register allocation purposes, returns whether this instruction needs to be |
| 2568 | // materialized (that is, not just be in the processor flags). |
| 2569 | bool needs_materialization_; |
| 2570 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2571 | // Needed if we merge a HCompare into a HCondition. |
| 2572 | ComparisonBias bias_; |
| 2573 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2574 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 2575 | }; |
| 2576 | |
| 2577 | // Instruction to check if two inputs are equal to each other. |
| 2578 | class HEqual : public HCondition { |
| 2579 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2580 | HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2581 | : HCondition(first, second, dex_pc) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2582 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2583 | bool IsCommutative() const OVERRIDE { return true; } |
| 2584 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2585 | template <typename T> bool Compute(T x, T y) const { return x == y; } |
| 2586 | |
| 2587 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2588 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2589 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2590 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2591 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2592 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2593 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2594 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2595 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2596 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2597 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2598 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2599 | return kCondEQ; |
| 2600 | } |
| 2601 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2602 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2603 | return kCondNE; |
| 2604 | } |
| 2605 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2606 | private: |
| 2607 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 2608 | }; |
| 2609 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2610 | class HNotEqual : public HCondition { |
| 2611 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2612 | HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2613 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2614 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2615 | bool IsCommutative() const OVERRIDE { return true; } |
| 2616 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2617 | template <typename T> bool Compute(T x, T y) const { return x != y; } |
| 2618 | |
| 2619 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2620 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2621 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2622 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2623 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2624 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2625 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2626 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2627 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2628 | DECLARE_INSTRUCTION(NotEqual); |
| 2629 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2630 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2631 | return kCondNE; |
| 2632 | } |
| 2633 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2634 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2635 | return kCondEQ; |
| 2636 | } |
| 2637 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2638 | private: |
| 2639 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 2640 | }; |
| 2641 | |
| 2642 | class HLessThan : public HCondition { |
| 2643 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2644 | HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2645 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2646 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2647 | template <typename T> bool Compute(T x, T y) const { return x < y; } |
| 2648 | |
| 2649 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2650 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2651 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2652 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2653 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2654 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2655 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2656 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2657 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2658 | DECLARE_INSTRUCTION(LessThan); |
| 2659 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2660 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2661 | return kCondLT; |
| 2662 | } |
| 2663 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2664 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2665 | return kCondGE; |
| 2666 | } |
| 2667 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2668 | private: |
| 2669 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 2670 | }; |
| 2671 | |
| 2672 | class HLessThanOrEqual : public HCondition { |
| 2673 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2674 | HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2675 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2676 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2677 | template <typename T> bool Compute(T x, T y) const { return x <= y; } |
| 2678 | |
| 2679 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2680 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2681 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2682 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2683 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2684 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2685 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2686 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2687 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2688 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 2689 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2690 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2691 | return kCondLE; |
| 2692 | } |
| 2693 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2694 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2695 | return kCondGT; |
| 2696 | } |
| 2697 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2698 | private: |
| 2699 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 2700 | }; |
| 2701 | |
| 2702 | class HGreaterThan : public HCondition { |
| 2703 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2704 | HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2705 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2706 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2707 | template <typename T> bool Compute(T x, T y) const { return x > y; } |
| 2708 | |
| 2709 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2710 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2711 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2712 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2713 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2714 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2715 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2716 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2717 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2718 | DECLARE_INSTRUCTION(GreaterThan); |
| 2719 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2720 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2721 | return kCondGT; |
| 2722 | } |
| 2723 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2724 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2725 | return kCondLE; |
| 2726 | } |
| 2727 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2728 | private: |
| 2729 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 2730 | }; |
| 2731 | |
| 2732 | class HGreaterThanOrEqual : public HCondition { |
| 2733 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2734 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2735 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2736 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2737 | template <typename T> bool Compute(T x, T y) const { return x >= y; } |
| 2738 | |
| 2739 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2740 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2741 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2742 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2743 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2744 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2745 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2746 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2747 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2748 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 2749 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2750 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2751 | return kCondGE; |
| 2752 | } |
| 2753 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2754 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2755 | return kCondLT; |
| 2756 | } |
| 2757 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2758 | private: |
| 2759 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 2760 | }; |
| 2761 | |
| 2762 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2763 | // Instruction to check how two inputs compare to each other. |
| 2764 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 2765 | class HCompare : public HBinaryOperation { |
| 2766 | public: |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2767 | HCompare(Primitive::Type type, |
| 2768 | HInstruction* first, |
| 2769 | HInstruction* second, |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2770 | ComparisonBias bias, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2771 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2772 | : HBinaryOperation(Primitive::kPrimInt, |
| 2773 | first, |
| 2774 | second, |
| 2775 | SideEffectsForArchRuntimeCalls(type), |
| 2776 | dex_pc), |
| 2777 | bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2778 | DCHECK_EQ(type, first->GetType()); |
| 2779 | DCHECK_EQ(type, second->GetType()); |
| 2780 | } |
| 2781 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2782 | template <typename T> |
| 2783 | 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] | 2784 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2785 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2786 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2787 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2788 | } |
| 2789 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2790 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2791 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2792 | } |
| 2793 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2794 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2795 | return bias_ == other->AsCompare()->bias_; |
| 2796 | } |
| 2797 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2798 | ComparisonBias GetBias() const { return bias_; } |
| 2799 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2800 | bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2801 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 2802 | |
| 2803 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) { |
| 2804 | // MIPS64 uses a runtime call for FP comparisons. |
| 2805 | return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
| 2806 | } |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2807 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2808 | DECLARE_INSTRUCTION(Compare); |
| 2809 | |
| 2810 | private: |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2811 | const ComparisonBias bias_; |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2812 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2813 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 2814 | }; |
| 2815 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2816 | // A local in the graph. Corresponds to a Dex register. |
| 2817 | class HLocal : public HTemplateInstruction<0> { |
| 2818 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2819 | explicit HLocal(uint16_t reg_number) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2820 | : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2821 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2822 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2823 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2824 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2825 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2826 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2827 | // The Dex register number. |
| 2828 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2829 | |
| 2830 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 2831 | }; |
| 2832 | |
| 2833 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2834 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2835 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2836 | HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2837 | : HExpression(type, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2838 | SetRawInputAt(0, local); |
| 2839 | } |
| 2840 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2841 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2842 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2843 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2844 | |
| 2845 | private: |
| 2846 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 2847 | }; |
| 2848 | |
| 2849 | // Store a value in a given local. This instruction has two inputs: the value |
| 2850 | // and the local. |
| 2851 | class HStoreLocal : public HTemplateInstruction<2> { |
| 2852 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2853 | HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 2854 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2855 | SetRawInputAt(0, local); |
| 2856 | SetRawInputAt(1, value); |
| 2857 | } |
| 2858 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2859 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2860 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2861 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2862 | |
| 2863 | private: |
| 2864 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 2865 | }; |
| 2866 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2867 | class HFloatConstant : public HConstant { |
| 2868 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2869 | float GetValue() const { return value_; } |
| 2870 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2871 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2872 | DCHECK(other->IsFloatConstant()); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2873 | return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) == |
| 2874 | bit_cast<uint32_t, float>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2875 | } |
| 2876 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2877 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2878 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2879 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2880 | 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] | 2881 | } |
| 2882 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2883 | return value_ == 0.0f; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2884 | } |
| 2885 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2886 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f); |
| 2887 | } |
| 2888 | bool IsNaN() const { |
| 2889 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2892 | DECLARE_INSTRUCTION(FloatConstant); |
| 2893 | |
| 2894 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2895 | explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc) |
| 2896 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {} |
| 2897 | explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2898 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2899 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2900 | const float value_; |
| 2901 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2902 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2903 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2904 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2905 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 2906 | }; |
| 2907 | |
| 2908 | class HDoubleConstant : public HConstant { |
| 2909 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2910 | double GetValue() const { return value_; } |
| 2911 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2912 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2913 | DCHECK(other->IsDoubleConstant()); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2914 | return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) == |
| 2915 | bit_cast<uint64_t, double>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2916 | } |
| 2917 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2918 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2919 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2920 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2921 | 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] | 2922 | } |
| 2923 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2924 | return value_ == 0.0; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2925 | } |
| 2926 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2927 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0); |
| 2928 | } |
| 2929 | bool IsNaN() const { |
| 2930 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2933 | DECLARE_INSTRUCTION(DoubleConstant); |
| 2934 | |
| 2935 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2936 | explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) |
| 2937 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {} |
| 2938 | explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2939 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2940 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2941 | const double value_; |
| 2942 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2943 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2944 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2945 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2946 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 2947 | }; |
| 2948 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2949 | enum class Intrinsics { |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2950 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2951 | #include "intrinsics_list.h" |
| 2952 | kNone, |
| 2953 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 2954 | #undef INTRINSICS_LIST |
| 2955 | #undef OPTIMIZING_INTRINSICS |
| 2956 | }; |
| 2957 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 2958 | |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2959 | enum IntrinsicNeedsEnvironmentOrCache { |
| 2960 | kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache. |
| 2961 | kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache. |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 2962 | }; |
| 2963 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2964 | class HInvoke : public HInstruction { |
| 2965 | public: |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2966 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2967 | |
| 2968 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 2969 | // know their environment. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2970 | bool NeedsEnvironment() const OVERRIDE { |
| 2971 | return needs_environment_or_cache_ == kNeedsEnvironmentOrCache; |
| 2972 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2973 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 2974 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2975 | SetRawInputAt(index, argument); |
| 2976 | } |
| 2977 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2978 | // Return the number of arguments. This number can be lower than |
| 2979 | // the number of inputs returned by InputCount(), as some invoke |
| 2980 | // instructions (e.g. HInvokeStaticOrDirect) can have non-argument |
| 2981 | // inputs at the end of their list of inputs. |
| 2982 | uint32_t GetNumberOfArguments() const { return number_of_arguments_; } |
| 2983 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2984 | Primitive::Type GetType() const OVERRIDE { return return_type_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2985 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2986 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2987 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2988 | const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2989 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2990 | InvokeType GetOriginalInvokeType() const { return original_invoke_type_; } |
| 2991 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 2992 | Intrinsics GetIntrinsic() const { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2993 | return intrinsic_; |
| 2994 | } |
| 2995 | |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2996 | void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2997 | intrinsic_ = intrinsic; |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2998 | needs_environment_or_cache_ = needs_env_or_cache; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2999 | } |
| 3000 | |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 3001 | bool IsFromInlinedInvoke() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 3002 | return GetEnvironment()->GetParent() != nullptr; |
| 3003 | } |
| 3004 | |
| 3005 | bool CanThrow() const OVERRIDE { return true; } |
| 3006 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3007 | DECLARE_INSTRUCTION(Invoke); |
| 3008 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3009 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3010 | HInvoke(ArenaAllocator* arena, |
| 3011 | uint32_t number_of_arguments, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3012 | uint32_t number_of_other_inputs, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3013 | Primitive::Type return_type, |
| 3014 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3015 | uint32_t dex_method_index, |
| 3016 | InvokeType original_invoke_type) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3017 | : HInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3018 | SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays. |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3019 | number_of_arguments_(number_of_arguments), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3020 | inputs_(arena, number_of_arguments), |
| 3021 | return_type_(return_type), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3022 | dex_method_index_(dex_method_index), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3023 | original_invoke_type_(original_invoke_type), |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 3024 | intrinsic_(Intrinsics::kNone), |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3025 | needs_environment_or_cache_(kNeedsEnvironmentOrCache) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3026 | uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs; |
| 3027 | inputs_.SetSize(number_of_inputs); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3030 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 3031 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 3032 | inputs_.Put(index, input); |
| 3033 | } |
| 3034 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3035 | uint32_t number_of_arguments_; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3036 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 3037 | const Primitive::Type return_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3038 | const uint32_t dex_method_index_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3039 | const InvokeType original_invoke_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3040 | Intrinsics intrinsic_; |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3041 | IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3042 | |
| 3043 | private: |
| 3044 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 3045 | }; |
| 3046 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3047 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3048 | public: |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3049 | // Requirements of this method call regarding the class |
| 3050 | // initialization (clinit) check of its declaring class. |
| 3051 | enum class ClinitCheckRequirement { |
| 3052 | kNone, // Class already initialized. |
| 3053 | kExplicit, // Static call having explicit clinit check as last input. |
| 3054 | kImplicit, // Static call implicitly requiring a clinit check. |
| 3055 | }; |
| 3056 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3057 | // Determines how to load the target ArtMethod*. |
| 3058 | enum class MethodLoadKind { |
| 3059 | // Use a String init ArtMethod* loaded from Thread entrypoints. |
| 3060 | kStringInit, |
| 3061 | |
| 3062 | // Use the method's own ArtMethod* loaded by the register allocator. |
| 3063 | kRecursive, |
| 3064 | |
| 3065 | // Use ArtMethod* at a known address, embed the direct address in the code. |
| 3066 | // Used for app->boot calls with non-relocatable image and for JIT-compiled calls. |
| 3067 | kDirectAddress, |
| 3068 | |
| 3069 | // Use ArtMethod* at an address that will be known at link time, embed the direct |
| 3070 | // address in the code. If the image is relocatable, emit .patch_oat entry. |
| 3071 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3072 | // the image relocatable or not. |
| 3073 | kDirectAddressWithFixup, |
| 3074 | |
| 3075 | // Load from resoved methods array in the dex cache using a PC-relative load. |
| 3076 | // Used when we need to use the dex cache, for example for invoke-static that |
| 3077 | // may cause class initialization (the entry may point to a resolution method), |
| 3078 | // and we know that we can access the dex cache arrays using a PC-relative load. |
| 3079 | kDexCachePcRelative, |
| 3080 | |
| 3081 | // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*. |
| 3082 | // Used for JIT when we need to use the dex cache. This is also the last-resort-kind |
| 3083 | // used when other kinds are unavailable (say, dex cache arrays are not PC-relative) |
| 3084 | // or unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3085 | kDexCacheViaMethod, |
| 3086 | }; |
| 3087 | |
| 3088 | // Determines the location of the code pointer. |
| 3089 | enum class CodePtrLocation { |
| 3090 | // Recursive call, use local PC-relative call instruction. |
| 3091 | kCallSelf, |
| 3092 | |
| 3093 | // Use PC-relative call instruction patched at link time. |
| 3094 | // Used for calls within an oat file, boot->boot or app->app. |
| 3095 | kCallPCRelative, |
| 3096 | |
| 3097 | // Call to a known target address, embed the direct address in code. |
| 3098 | // Used for app->boot call with non-relocatable image and for JIT-compiled calls. |
| 3099 | kCallDirect, |
| 3100 | |
| 3101 | // Call to a target address that will be known at link time, embed the direct |
| 3102 | // address in code. If the image is relocatable, emit .patch_oat entry. |
| 3103 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3104 | // the image relocatable or not. |
| 3105 | kCallDirectWithFixup, |
| 3106 | |
| 3107 | // Use code pointer from the ArtMethod*. |
| 3108 | // Used when we don't know the target code. This is also the last-resort-kind used when |
| 3109 | // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3110 | kCallArtMethod, |
| 3111 | }; |
| 3112 | |
| 3113 | struct DispatchInfo { |
| 3114 | const MethodLoadKind method_load_kind; |
| 3115 | const CodePtrLocation code_ptr_location; |
| 3116 | // The method load data holds |
| 3117 | // - thread entrypoint offset for kStringInit method if this is a string init invoke. |
| 3118 | // Note that there are multiple string init methods, each having its own offset. |
| 3119 | // - the method address for kDirectAddress |
| 3120 | // - the dex cache arrays offset for kDexCachePcRel. |
| 3121 | const uint64_t method_load_data; |
| 3122 | const uint64_t direct_code_ptr; |
| 3123 | }; |
| 3124 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3125 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 3126 | uint32_t number_of_arguments, |
| 3127 | Primitive::Type return_type, |
| 3128 | uint32_t dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3129 | uint32_t method_index, |
| 3130 | MethodReference target_method, |
| 3131 | DispatchInfo dispatch_info, |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 3132 | InvokeType original_invoke_type, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3133 | InvokeType invoke_type, |
| 3134 | ClinitCheckRequirement clinit_check_requirement) |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3135 | : HInvoke(arena, |
| 3136 | number_of_arguments, |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 3137 | // There is one extra argument for the HCurrentMethod node, and |
| 3138 | // potentially one other if the clinit check is explicit, and one other |
| 3139 | // if the method is a string factory. |
| 3140 | 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3141 | + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u), |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3142 | return_type, |
| 3143 | dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3144 | method_index, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3145 | original_invoke_type), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 3146 | invoke_type_(invoke_type), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3147 | clinit_check_requirement_(clinit_check_requirement), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3148 | target_method_(target_method), |
| 3149 | dispatch_info_(dispatch_info) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3150 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3151 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3152 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3153 | // We access the method via the dex cache so we can't do an implicit null check. |
| 3154 | // TODO: for intrinsics we can generate implicit null checks. |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 3158 | bool CanBeNull() const OVERRIDE { |
| 3159 | return return_type_ == Primitive::kPrimNot && !IsStringInit(); |
| 3160 | } |
| 3161 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3162 | InvokeType GetInvokeType() const { return invoke_type_; } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3163 | MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; } |
| 3164 | CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; } |
| 3165 | bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3166 | bool NeedsDexCache() const OVERRIDE { |
| 3167 | if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; } |
| 3168 | return !IsRecursive() && !IsStringInit(); |
| 3169 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3170 | bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; } |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 3171 | uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3172 | bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; } |
| 3173 | bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; } |
| 3174 | bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; } |
| 3175 | MethodReference GetTargetMethod() const { return target_method_; } |
| 3176 | |
| 3177 | int32_t GetStringInitOffset() const { |
| 3178 | DCHECK(IsStringInit()); |
| 3179 | return dispatch_info_.method_load_data; |
| 3180 | } |
| 3181 | |
| 3182 | uint64_t GetMethodAddress() const { |
| 3183 | DCHECK(HasMethodAddress()); |
| 3184 | return dispatch_info_.method_load_data; |
| 3185 | } |
| 3186 | |
| 3187 | uint32_t GetDexCacheArrayOffset() const { |
| 3188 | DCHECK(HasPcRelDexCache()); |
| 3189 | return dispatch_info_.method_load_data; |
| 3190 | } |
| 3191 | |
| 3192 | uint64_t GetDirectCodePtr() const { |
| 3193 | DCHECK(HasDirectCodePtr()); |
| 3194 | return dispatch_info_.direct_code_ptr; |
| 3195 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3196 | |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 3197 | ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; } |
| 3198 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3199 | // Is this instruction a call to a static method? |
| 3200 | bool IsStatic() const { |
| 3201 | return GetInvokeType() == kStatic; |
| 3202 | } |
| 3203 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3204 | // Remove the art::HLoadClass instruction set as last input by |
| 3205 | // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of |
| 3206 | // the initial art::HClinitCheck instruction (only relevant for |
| 3207 | // static calls with explicit clinit check). |
| 3208 | void RemoveLoadClassAsLastInput() { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3209 | DCHECK(IsStaticWithExplicitClinitCheck()); |
| 3210 | size_t last_input_index = InputCount() - 1; |
| 3211 | HInstruction* last_input = InputAt(last_input_index); |
| 3212 | DCHECK(last_input != nullptr); |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3213 | DCHECK(last_input->IsLoadClass()) << last_input->DebugName(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3214 | RemoveAsUserOfInput(last_input_index); |
| 3215 | inputs_.DeleteAt(last_input_index); |
| 3216 | clinit_check_requirement_ = ClinitCheckRequirement::kImplicit; |
| 3217 | DCHECK(IsStaticWithImplicitClinitCheck()); |
| 3218 | } |
| 3219 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 3220 | bool IsStringFactoryFor(HFakeString* str) const { |
| 3221 | if (!IsStringInit()) return false; |
| 3222 | // +1 for the current method. |
| 3223 | if (InputCount() == (number_of_arguments_ + 1)) return false; |
| 3224 | return InputAt(InputCount() - 1)->AsFakeString() == str; |
| 3225 | } |
| 3226 | |
| 3227 | void RemoveFakeStringArgumentAsLastInput() { |
| 3228 | DCHECK(IsStringInit()); |
| 3229 | size_t last_input_index = InputCount() - 1; |
| 3230 | HInstruction* last_input = InputAt(last_input_index); |
| 3231 | DCHECK(last_input != nullptr); |
| 3232 | DCHECK(last_input->IsFakeString()) << last_input->DebugName(); |
| 3233 | RemoveAsUserOfInput(last_input_index); |
| 3234 | inputs_.DeleteAt(last_input_index); |
| 3235 | } |
| 3236 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3237 | // Is this a call to a static method whose declaring class has an |
| 3238 | // explicit intialization check in the graph? |
| 3239 | bool IsStaticWithExplicitClinitCheck() const { |
| 3240 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit); |
| 3241 | } |
| 3242 | |
| 3243 | // Is this a call to a static method whose declaring class has an |
| 3244 | // implicit intialization check requirement? |
| 3245 | bool IsStaticWithImplicitClinitCheck() const { |
| 3246 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit); |
| 3247 | } |
| 3248 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3249 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3250 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3251 | protected: |
| 3252 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 3253 | const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i); |
| 3254 | if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) { |
| 3255 | HInstruction* input = input_record.GetInstruction(); |
| 3256 | // `input` is the last input of a static invoke marked as having |
| 3257 | // an explicit clinit check. It must either be: |
| 3258 | // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or |
| 3259 | // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation. |
| 3260 | DCHECK(input != nullptr); |
| 3261 | DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName(); |
| 3262 | } |
| 3263 | return input_record; |
| 3264 | } |
| 3265 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3266 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3267 | const InvokeType invoke_type_; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3268 | ClinitCheckRequirement clinit_check_requirement_; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3269 | // The target method may refer to different dex file or method index than the original |
| 3270 | // invoke. This happens for sharpened calls and for calls where a method was redeclared |
| 3271 | // in derived class to increase visibility. |
| 3272 | MethodReference target_method_; |
| 3273 | DispatchInfo dispatch_info_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3274 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3275 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3276 | }; |
| 3277 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3278 | class HInvokeVirtual : public HInvoke { |
| 3279 | public: |
| 3280 | HInvokeVirtual(ArenaAllocator* arena, |
| 3281 | uint32_t number_of_arguments, |
| 3282 | Primitive::Type return_type, |
| 3283 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3284 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3285 | uint32_t vtable_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3286 | : 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] | 3287 | vtable_index_(vtable_index) {} |
| 3288 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3289 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3290 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3291 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3294 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 3295 | |
| 3296 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 3297 | |
| 3298 | private: |
| 3299 | const uint32_t vtable_index_; |
| 3300 | |
| 3301 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 3302 | }; |
| 3303 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3304 | class HInvokeInterface : public HInvoke { |
| 3305 | public: |
| 3306 | HInvokeInterface(ArenaAllocator* arena, |
| 3307 | uint32_t number_of_arguments, |
| 3308 | Primitive::Type return_type, |
| 3309 | uint32_t dex_pc, |
| 3310 | uint32_t dex_method_index, |
| 3311 | uint32_t imt_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3312 | : 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] | 3313 | imt_index_(imt_index) {} |
| 3314 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3315 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3316 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3317 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3320 | uint32_t GetImtIndex() const { return imt_index_; } |
| 3321 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 3322 | |
| 3323 | DECLARE_INSTRUCTION(InvokeInterface); |
| 3324 | |
| 3325 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3326 | const uint32_t imt_index_; |
| 3327 | |
| 3328 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 3329 | }; |
| 3330 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3331 | class HNewInstance : public HExpression<1> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3332 | public: |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3333 | HNewInstance(HCurrentMethod* current_method, |
| 3334 | uint32_t dex_pc, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3335 | uint16_t type_index, |
| 3336 | const DexFile& dex_file, |
| 3337 | QuickEntrypointEnum entrypoint) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3338 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3339 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3340 | dex_file_(dex_file), |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3341 | entrypoint_(entrypoint) { |
| 3342 | SetRawInputAt(0, current_method); |
| 3343 | } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3344 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3345 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3346 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3347 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3348 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 3349 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3350 | // It may throw when called on: |
| 3351 | // - interfaces |
| 3352 | // - abstract/innaccessible/unknown classes |
| 3353 | // TODO: optimize when possible. |
| 3354 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3355 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3356 | bool CanBeNull() const OVERRIDE { return false; } |
| 3357 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3358 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 3359 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3360 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3361 | |
| 3362 | private: |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3363 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3364 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3365 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3366 | |
| 3367 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 3368 | }; |
| 3369 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3370 | class HNeg : public HUnaryOperation { |
| 3371 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3372 | HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3373 | : HUnaryOperation(result_type, input, dex_pc) {} |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3374 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3375 | template <typename T> T Compute(T x) const { return -x; } |
| 3376 | |
| 3377 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3378 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3379 | } |
| 3380 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3381 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3382 | } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 3383 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3384 | DECLARE_INSTRUCTION(Neg); |
| 3385 | |
| 3386 | private: |
| 3387 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 3388 | }; |
| 3389 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3390 | class HNewArray : public HExpression<2> { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3391 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3392 | HNewArray(HInstruction* length, |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3393 | HCurrentMethod* current_method, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3394 | uint32_t dex_pc, |
| 3395 | uint16_t type_index, |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3396 | const DexFile& dex_file, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3397 | QuickEntrypointEnum entrypoint) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3398 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3399 | type_index_(type_index), |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3400 | dex_file_(dex_file), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3401 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3402 | SetRawInputAt(0, length); |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3403 | SetRawInputAt(1, current_method); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3406 | uint16_t GetTypeIndex() const { return type_index_; } |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3407 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3408 | |
| 3409 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3410 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3411 | |
Mingyao Yang | 0c365e6 | 2015-03-31 15:09:29 -0700 | [diff] [blame] | 3412 | // May throw NegativeArraySizeException, OutOfMemoryError, etc. |
| 3413 | bool CanThrow() const OVERRIDE { return true; } |
| 3414 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3415 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3416 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3417 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 3418 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3419 | DECLARE_INSTRUCTION(NewArray); |
| 3420 | |
| 3421 | private: |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3422 | const uint16_t type_index_; |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3423 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3424 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3425 | |
| 3426 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 3427 | }; |
| 3428 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3429 | class HAdd : public HBinaryOperation { |
| 3430 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3431 | HAdd(Primitive::Type result_type, |
| 3432 | HInstruction* left, |
| 3433 | HInstruction* right, |
| 3434 | uint32_t dex_pc = kNoDexPc) |
| 3435 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3436 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3437 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3438 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3439 | template <typename T> T Compute(T x, T y) const { return x + y; } |
| 3440 | |
| 3441 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3442 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3443 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3444 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3445 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3446 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3447 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3448 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3449 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3450 | DECLARE_INSTRUCTION(Add); |
| 3451 | |
| 3452 | private: |
| 3453 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 3454 | }; |
| 3455 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3456 | class HSub : public HBinaryOperation { |
| 3457 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3458 | HSub(Primitive::Type result_type, |
| 3459 | HInstruction* left, |
| 3460 | HInstruction* right, |
| 3461 | uint32_t dex_pc = kNoDexPc) |
| 3462 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3463 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3464 | template <typename T> T Compute(T x, T y) const { return x - y; } |
| 3465 | |
| 3466 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3467 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3468 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3469 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3470 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3471 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3472 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3473 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3474 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3475 | DECLARE_INSTRUCTION(Sub); |
| 3476 | |
| 3477 | private: |
| 3478 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 3479 | }; |
| 3480 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3481 | class HMul : public HBinaryOperation { |
| 3482 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3483 | HMul(Primitive::Type result_type, |
| 3484 | HInstruction* left, |
| 3485 | HInstruction* right, |
| 3486 | uint32_t dex_pc = kNoDexPc) |
| 3487 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3488 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3489 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3490 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3491 | template <typename T> T Compute(T x, T y) const { return x * y; } |
| 3492 | |
| 3493 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3494 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3495 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3496 | } |
| 3497 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3498 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3499 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3500 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3501 | |
| 3502 | DECLARE_INSTRUCTION(Mul); |
| 3503 | |
| 3504 | private: |
| 3505 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 3506 | }; |
| 3507 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3508 | class HDiv : public HBinaryOperation { |
| 3509 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3510 | HDiv(Primitive::Type result_type, |
| 3511 | HInstruction* left, |
| 3512 | HInstruction* right, |
| 3513 | uint32_t dex_pc) |
| 3514 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3515 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3516 | template <typename T> |
| 3517 | T Compute(T x, T y) const { |
| 3518 | // Our graph structure ensures we never have 0 for `y` during |
| 3519 | // constant folding. |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 3520 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3521 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 3522 | return (y == -1) ? -x : x / y; |
| 3523 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3524 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3525 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3526 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3527 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3528 | } |
| 3529 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3530 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3531 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3532 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3533 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3534 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 3535 | // The generated code can use a runtime call. |
| 3536 | return SideEffects::CanTriggerGC(); |
| 3537 | } |
| 3538 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3539 | DECLARE_INSTRUCTION(Div); |
| 3540 | |
| 3541 | private: |
| 3542 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 3543 | }; |
| 3544 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3545 | class HRem : public HBinaryOperation { |
| 3546 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3547 | HRem(Primitive::Type result_type, |
| 3548 | HInstruction* left, |
| 3549 | HInstruction* right, |
| 3550 | uint32_t dex_pc) |
| 3551 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3552 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3553 | template <typename T> |
| 3554 | T Compute(T x, T y) const { |
| 3555 | // Our graph structure ensures we never have 0 for `y` during |
| 3556 | // constant folding. |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3557 | DCHECK_NE(y, 0); |
| 3558 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 3559 | return (y == -1) ? 0 : x % y; |
| 3560 | } |
| 3561 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3562 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3563 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3564 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3565 | } |
| 3566 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3567 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3568 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3571 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3572 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 3573 | return SideEffects::CanTriggerGC(); |
| 3574 | } |
| 3575 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3576 | DECLARE_INSTRUCTION(Rem); |
| 3577 | |
| 3578 | private: |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3579 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 3580 | }; |
| 3581 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3582 | class HDivZeroCheck : public HExpression<1> { |
| 3583 | public: |
| 3584 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3585 | : HExpression(value->GetType(), SideEffects::None(), dex_pc) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3586 | SetRawInputAt(0, value); |
| 3587 | } |
| 3588 | |
Serguei Katkov | 8c0676c | 2015-08-03 13:55:33 +0600 | [diff] [blame] | 3589 | Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); } |
| 3590 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3591 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3592 | |
| 3593 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3594 | UNUSED(other); |
| 3595 | return true; |
| 3596 | } |
| 3597 | |
| 3598 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3599 | bool CanThrow() const OVERRIDE { return true; } |
| 3600 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3601 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 3602 | |
| 3603 | private: |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3604 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 3605 | }; |
| 3606 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3607 | class HShl : public HBinaryOperation { |
| 3608 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3609 | HShl(Primitive::Type result_type, |
| 3610 | HInstruction* left, |
| 3611 | HInstruction* right, |
| 3612 | uint32_t dex_pc = kNoDexPc) |
| 3613 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3614 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3615 | template <typename T, typename U, typename V> |
| 3616 | T Compute(T x, U y, V max_shift_value) const { |
| 3617 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3618 | "V is not the unsigned integer type corresponding to T"); |
| 3619 | return x << (y & max_shift_value); |
| 3620 | } |
| 3621 | |
| 3622 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3623 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3624 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3625 | } |
| 3626 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3627 | // case is handled as `x << static_cast<int>(y)`. |
| 3628 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3629 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3630 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3631 | } |
| 3632 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3633 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3634 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3635 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3636 | |
| 3637 | DECLARE_INSTRUCTION(Shl); |
| 3638 | |
| 3639 | private: |
| 3640 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 3641 | }; |
| 3642 | |
| 3643 | class HShr : public HBinaryOperation { |
| 3644 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3645 | HShr(Primitive::Type result_type, |
| 3646 | HInstruction* left, |
| 3647 | HInstruction* right, |
| 3648 | uint32_t dex_pc = kNoDexPc) |
| 3649 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3650 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3651 | template <typename T, typename U, typename V> |
| 3652 | T Compute(T x, U y, V max_shift_value) const { |
| 3653 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3654 | "V is not the unsigned integer type corresponding to T"); |
| 3655 | return x >> (y & max_shift_value); |
| 3656 | } |
| 3657 | |
| 3658 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3659 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3660 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3661 | } |
| 3662 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3663 | // case is handled as `x >> static_cast<int>(y)`. |
| 3664 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3665 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3666 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3667 | } |
| 3668 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3669 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3670 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3671 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3672 | |
| 3673 | DECLARE_INSTRUCTION(Shr); |
| 3674 | |
| 3675 | private: |
| 3676 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 3677 | }; |
| 3678 | |
| 3679 | class HUShr : public HBinaryOperation { |
| 3680 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3681 | HUShr(Primitive::Type result_type, |
| 3682 | HInstruction* left, |
| 3683 | HInstruction* right, |
| 3684 | uint32_t dex_pc = kNoDexPc) |
| 3685 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3686 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3687 | template <typename T, typename U, typename V> |
| 3688 | T Compute(T x, U y, V max_shift_value) const { |
| 3689 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3690 | "V is not the unsigned integer type corresponding to T"); |
| 3691 | V ux = static_cast<V>(x); |
| 3692 | return static_cast<T>(ux >> (y & max_shift_value)); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3695 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3696 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3697 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3698 | } |
| 3699 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3700 | // case is handled as `x >>> static_cast<int>(y)`. |
| 3701 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3702 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3703 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3704 | } |
| 3705 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3706 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3707 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | DECLARE_INSTRUCTION(UShr); |
| 3711 | |
| 3712 | private: |
| 3713 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 3714 | }; |
| 3715 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3716 | class HAnd : public HBinaryOperation { |
| 3717 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3718 | HAnd(Primitive::Type result_type, |
| 3719 | HInstruction* left, |
| 3720 | HInstruction* right, |
| 3721 | uint32_t dex_pc = kNoDexPc) |
| 3722 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3723 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3724 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3725 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3726 | template <typename T, typename U> |
| 3727 | auto Compute(T x, U y) const -> decltype(x & y) { return x & y; } |
| 3728 | |
| 3729 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3730 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3731 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3732 | } |
| 3733 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3734 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3735 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3736 | } |
| 3737 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3738 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3739 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3740 | } |
| 3741 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3742 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3743 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3744 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3745 | |
| 3746 | DECLARE_INSTRUCTION(And); |
| 3747 | |
| 3748 | private: |
| 3749 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 3750 | }; |
| 3751 | |
| 3752 | class HOr : public HBinaryOperation { |
| 3753 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3754 | HOr(Primitive::Type result_type, |
| 3755 | HInstruction* left, |
| 3756 | HInstruction* right, |
| 3757 | uint32_t dex_pc = kNoDexPc) |
| 3758 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3759 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3760 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3761 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3762 | template <typename T, typename U> |
| 3763 | auto Compute(T x, U y) const -> decltype(x | y) { return x | y; } |
| 3764 | |
| 3765 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3766 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3767 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3768 | } |
| 3769 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3770 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3771 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3772 | } |
| 3773 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3774 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3775 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3776 | } |
| 3777 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3778 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3779 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3780 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3781 | |
| 3782 | DECLARE_INSTRUCTION(Or); |
| 3783 | |
| 3784 | private: |
| 3785 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 3786 | }; |
| 3787 | |
| 3788 | class HXor : public HBinaryOperation { |
| 3789 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3790 | HXor(Primitive::Type result_type, |
| 3791 | HInstruction* left, |
| 3792 | HInstruction* right, |
| 3793 | uint32_t dex_pc = kNoDexPc) |
| 3794 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3795 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3796 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3797 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3798 | template <typename T, typename U> |
| 3799 | auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; } |
| 3800 | |
| 3801 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3802 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3803 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3804 | } |
| 3805 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3806 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3807 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3808 | } |
| 3809 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3810 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3811 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3812 | } |
| 3813 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3814 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3815 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3816 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3817 | |
| 3818 | DECLARE_INSTRUCTION(Xor); |
| 3819 | |
| 3820 | private: |
| 3821 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 3822 | }; |
| 3823 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3824 | // The value of a parameter in this method. Its location depends on |
| 3825 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3826 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3827 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3828 | HParameterValue(uint8_t index, |
| 3829 | Primitive::Type parameter_type, |
| 3830 | bool is_this = false) |
| 3831 | : HExpression(parameter_type, SideEffects::None(), kNoDexPc), |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3832 | index_(index), |
| 3833 | is_this_(is_this), |
| 3834 | can_be_null_(!is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3835 | |
| 3836 | uint8_t GetIndex() const { return index_; } |
| 3837 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3838 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3839 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3840 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 3841 | bool IsThis() const { return is_this_; } |
| 3842 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3843 | DECLARE_INSTRUCTION(ParameterValue); |
| 3844 | |
| 3845 | private: |
| 3846 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3847 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3848 | const uint8_t index_; |
| 3849 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3850 | // Whether or not the parameter value corresponds to 'this' argument. |
| 3851 | const bool is_this_; |
| 3852 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3853 | bool can_be_null_; |
| 3854 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3855 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 3856 | }; |
| 3857 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3858 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3859 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3860 | HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3861 | : HUnaryOperation(result_type, input, dex_pc) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3862 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3863 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3864 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3865 | UNUSED(other); |
| 3866 | return true; |
| 3867 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3868 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3869 | template <typename T> T Compute(T x) const { return ~x; } |
| 3870 | |
| 3871 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3872 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3873 | } |
| 3874 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3875 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3876 | } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3877 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3878 | DECLARE_INSTRUCTION(Not); |
| 3879 | |
| 3880 | private: |
| 3881 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 3882 | }; |
| 3883 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3884 | class HBooleanNot : public HUnaryOperation { |
| 3885 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3886 | explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3887 | : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {} |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3888 | |
| 3889 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3890 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3891 | UNUSED(other); |
| 3892 | return true; |
| 3893 | } |
| 3894 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3895 | template <typename T> bool Compute(T x) const { |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3896 | DCHECK(IsUint<1>(x)); |
| 3897 | return !x; |
| 3898 | } |
| 3899 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3900 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3901 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3902 | } |
| 3903 | HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3904 | LOG(FATAL) << DebugName() << " is not defined for long values"; |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3905 | UNREACHABLE(); |
| 3906 | } |
| 3907 | |
| 3908 | DECLARE_INSTRUCTION(BooleanNot); |
| 3909 | |
| 3910 | private: |
| 3911 | DISALLOW_COPY_AND_ASSIGN(HBooleanNot); |
| 3912 | }; |
| 3913 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3914 | class HTypeConversion : public HExpression<1> { |
| 3915 | public: |
| 3916 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3917 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3918 | : HExpression(result_type, |
| 3919 | SideEffectsForArchRuntimeCalls(input->GetType(), result_type), |
| 3920 | dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3921 | SetRawInputAt(0, input); |
| 3922 | DCHECK_NE(input->GetType(), result_type); |
| 3923 | } |
| 3924 | |
| 3925 | HInstruction* GetInput() const { return InputAt(0); } |
| 3926 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 3927 | Primitive::Type GetResultType() const { return GetType(); } |
| 3928 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3929 | // Required by the x86 and ARM code generators when producing calls |
| 3930 | // to the runtime. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3931 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3932 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 3933 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3934 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 3935 | // Try to statically evaluate the conversion and return a HConstant |
| 3936 | // containing the result. If the input cannot be converted, return nullptr. |
| 3937 | HConstant* TryStaticEvaluation() const; |
| 3938 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3939 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type, |
| 3940 | Primitive::Type result_type) { |
| 3941 | // Some architectures may not require the 'GC' side effects, but at this point |
| 3942 | // in the compilation process we do not know what architecture we will |
| 3943 | // generate code for, so we must be conservative. |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 3944 | if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type)) |
| 3945 | || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3946 | return SideEffects::CanTriggerGC(); |
| 3947 | } |
| 3948 | return SideEffects::None(); |
| 3949 | } |
| 3950 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3951 | DECLARE_INSTRUCTION(TypeConversion); |
| 3952 | |
| 3953 | private: |
| 3954 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 3955 | }; |
| 3956 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 3957 | static constexpr uint32_t kNoRegNumber = -1; |
| 3958 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3959 | class HPhi : public HInstruction { |
| 3960 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3961 | HPhi(ArenaAllocator* arena, |
| 3962 | uint32_t reg_number, |
| 3963 | size_t number_of_inputs, |
| 3964 | Primitive::Type type, |
| 3965 | uint32_t dex_pc = kNoDexPc) |
| 3966 | : HInstruction(SideEffects::None(), dex_pc), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3967 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3968 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3969 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3970 | is_live_(false), |
| 3971 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3972 | inputs_.SetSize(number_of_inputs); |
| 3973 | } |
| 3974 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 3975 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 3976 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 3977 | switch (type) { |
| 3978 | case Primitive::kPrimBoolean: |
| 3979 | case Primitive::kPrimByte: |
| 3980 | case Primitive::kPrimShort: |
| 3981 | case Primitive::kPrimChar: |
| 3982 | return Primitive::kPrimInt; |
| 3983 | default: |
| 3984 | return type; |
| 3985 | } |
| 3986 | } |
| 3987 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 3988 | bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); } |
| 3989 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3990 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3991 | |
| 3992 | void AddInput(HInstruction* input); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 3993 | void RemoveInputAt(size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3994 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3995 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3996 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3997 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3998 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3999 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 4000 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4001 | uint32_t GetRegNumber() const { return reg_number_; } |
| 4002 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 4003 | void SetDead() { is_live_ = false; } |
| 4004 | void SetLive() { is_live_ = true; } |
| 4005 | bool IsDead() const { return !is_live_; } |
| 4006 | bool IsLive() const { return is_live_; } |
| 4007 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 4008 | // Returns the next equivalent phi (starting from the current one) or null if there is none. |
| 4009 | // An equivalent phi is a phi having the same dex register and type. |
| 4010 | // It assumes that phis with the same dex register are adjacent. |
| 4011 | HPhi* GetNextEquivalentPhiWithSameType() { |
| 4012 | HInstruction* next = GetNext(); |
| 4013 | while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) { |
| 4014 | if (next->GetType() == GetType()) { |
| 4015 | return next->AsPhi(); |
| 4016 | } |
| 4017 | next = next->GetNext(); |
| 4018 | } |
| 4019 | return nullptr; |
| 4020 | } |
| 4021 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4022 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4023 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4024 | protected: |
| 4025 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 4026 | |
| 4027 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 4028 | inputs_.Put(index, input); |
| 4029 | } |
| 4030 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 4031 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4032 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4033 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4034 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 4035 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4036 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4037 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4038 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 4039 | }; |
| 4040 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4041 | class HNullCheck : public HExpression<1> { |
| 4042 | public: |
| 4043 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4044 | : HExpression(value->GetType(), SideEffects::None(), dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4045 | SetRawInputAt(0, value); |
| 4046 | } |
| 4047 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4048 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4049 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4050 | UNUSED(other); |
| 4051 | return true; |
| 4052 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4053 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4054 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4055 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4056 | bool CanThrow() const OVERRIDE { return true; } |
| 4057 | |
| 4058 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 4059 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4060 | |
| 4061 | DECLARE_INSTRUCTION(NullCheck); |
| 4062 | |
| 4063 | private: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4064 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 4065 | }; |
| 4066 | |
| 4067 | class FieldInfo : public ValueObject { |
| 4068 | public: |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4069 | FieldInfo(MemberOffset field_offset, |
| 4070 | Primitive::Type field_type, |
| 4071 | bool is_volatile, |
| 4072 | uint32_t index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4073 | const DexFile& dex_file, |
| 4074 | Handle<mirror::DexCache> dex_cache) |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4075 | : field_offset_(field_offset), |
| 4076 | field_type_(field_type), |
| 4077 | is_volatile_(is_volatile), |
| 4078 | index_(index), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4079 | dex_file_(dex_file), |
| 4080 | dex_cache_(dex_cache) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4081 | |
| 4082 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4083 | Primitive::Type GetFieldType() const { return field_type_; } |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4084 | uint32_t GetFieldIndex() const { return index_; } |
| 4085 | const DexFile& GetDexFile() const { return dex_file_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4086 | bool IsVolatile() const { return is_volatile_; } |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4087 | Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4088 | |
| 4089 | private: |
| 4090 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4091 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4092 | const bool is_volatile_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4093 | const uint32_t index_; |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4094 | const DexFile& dex_file_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4095 | const Handle<mirror::DexCache> dex_cache_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4096 | }; |
| 4097 | |
| 4098 | class HInstanceFieldGet : public HExpression<1> { |
| 4099 | public: |
| 4100 | HInstanceFieldGet(HInstruction* value, |
| 4101 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4102 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4103 | bool is_volatile, |
| 4104 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4105 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4106 | Handle<mirror::DexCache> dex_cache, |
| 4107 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4108 | : HExpression( |
| 4109 | field_type, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4110 | SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4111 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4112 | SetRawInputAt(0, value); |
| 4113 | } |
| 4114 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 4115 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4116 | |
| 4117 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4118 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 4119 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4120 | } |
| 4121 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4122 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4123 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 4127 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 4128 | } |
| 4129 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4130 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4131 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4132 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4133 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4134 | |
| 4135 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 4136 | |
| 4137 | private: |
| 4138 | const FieldInfo field_info_; |
| 4139 | |
| 4140 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 4141 | }; |
| 4142 | |
| 4143 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 4144 | public: |
| 4145 | HInstanceFieldSet(HInstruction* object, |
| 4146 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4147 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4148 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4149 | bool is_volatile, |
| 4150 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4151 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4152 | Handle<mirror::DexCache> dex_cache, |
| 4153 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4154 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4155 | SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4156 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4157 | value_can_be_null_(true) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4158 | SetRawInputAt(0, object); |
| 4159 | SetRawInputAt(1, value); |
| 4160 | } |
| 4161 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4162 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4163 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4166 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4167 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4168 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4169 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4170 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4171 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 4172 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4173 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4174 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 4175 | |
| 4176 | private: |
| 4177 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4178 | bool value_can_be_null_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4179 | |
| 4180 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 4181 | }; |
| 4182 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4183 | class HArrayGet : public HExpression<2> { |
| 4184 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4185 | HArrayGet(HInstruction* array, |
| 4186 | HInstruction* index, |
| 4187 | Primitive::Type type, |
| 4188 | uint32_t dex_pc = kNoDexPc) |
| 4189 | : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4190 | SetRawInputAt(0, array); |
| 4191 | SetRawInputAt(1, index); |
| 4192 | } |
| 4193 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4194 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4195 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4196 | UNUSED(other); |
| 4197 | return true; |
| 4198 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4199 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4200 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4201 | // TODO: We can be smarter here. |
| 4202 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 4203 | // which generates the implicit null check. There are cases when these can be removed |
| 4204 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 4205 | // implicit check here (as long as the address falls in the first page). |
| 4206 | return false; |
| 4207 | } |
| 4208 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4209 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4210 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4211 | HInstruction* GetArray() const { return InputAt(0); } |
| 4212 | HInstruction* GetIndex() const { return InputAt(1); } |
| 4213 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4214 | DECLARE_INSTRUCTION(ArrayGet); |
| 4215 | |
| 4216 | private: |
| 4217 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 4218 | }; |
| 4219 | |
| 4220 | class HArraySet : public HTemplateInstruction<3> { |
| 4221 | public: |
| 4222 | HArraySet(HInstruction* array, |
| 4223 | HInstruction* index, |
| 4224 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4225 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4226 | uint32_t dex_pc) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4227 | : HTemplateInstruction( |
| 4228 | SideEffects::ArrayWriteOfType(expected_component_type).Union( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4229 | SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4230 | expected_component_type_(expected_component_type), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4231 | needs_type_check_(value->GetType() == Primitive::kPrimNot), |
| 4232 | value_can_be_null_(true) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4233 | SetRawInputAt(0, array); |
| 4234 | SetRawInputAt(1, index); |
| 4235 | SetRawInputAt(2, value); |
| 4236 | } |
| 4237 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4238 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4239 | // We currently always call a runtime method to catch array store |
| 4240 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4241 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4242 | } |
| 4243 | |
Mingyao Yang | 81014cb | 2015-06-02 03:16:27 -0700 | [diff] [blame] | 4244 | // Can throw ArrayStoreException. |
| 4245 | bool CanThrow() const OVERRIDE { return needs_type_check_; } |
| 4246 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4247 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4248 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4249 | // TODO: Same as for ArrayGet. |
| 4250 | return false; |
| 4251 | } |
| 4252 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4253 | void ClearNeedsTypeCheck() { |
| 4254 | needs_type_check_ = false; |
| 4255 | } |
| 4256 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4257 | void ClearValueCanBeNull() { |
| 4258 | value_can_be_null_ = false; |
| 4259 | } |
| 4260 | |
| 4261 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4262 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 4263 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4264 | HInstruction* GetArray() const { return InputAt(0); } |
| 4265 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4266 | HInstruction* GetValue() const { return InputAt(2); } |
| 4267 | |
| 4268 | Primitive::Type GetComponentType() const { |
| 4269 | // The Dex format does not type floating point index operations. Since the |
| 4270 | // `expected_component_type_` is set during building and can therefore not |
| 4271 | // be correct, we also check what is the value type. If it is a floating |
| 4272 | // point type, we must use that type. |
| 4273 | Primitive::Type value_type = GetValue()->GetType(); |
| 4274 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 4275 | ? value_type |
| 4276 | : expected_component_type_; |
| 4277 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4278 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4279 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) { |
| 4280 | return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
| 4281 | } |
| 4282 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4283 | DECLARE_INSTRUCTION(ArraySet); |
| 4284 | |
| 4285 | private: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4286 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4287 | bool needs_type_check_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4288 | bool value_can_be_null_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4289 | |
| 4290 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 4291 | }; |
| 4292 | |
| 4293 | class HArrayLength : public HExpression<1> { |
| 4294 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4295 | explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc) |
| 4296 | : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4297 | // Note that arrays do not change length, so the instruction does not |
| 4298 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4299 | SetRawInputAt(0, array); |
| 4300 | } |
| 4301 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4302 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4303 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4304 | UNUSED(other); |
| 4305 | return true; |
| 4306 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4307 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4308 | return obj == InputAt(0); |
| 4309 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4310 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4311 | DECLARE_INSTRUCTION(ArrayLength); |
| 4312 | |
| 4313 | private: |
| 4314 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 4315 | }; |
| 4316 | |
| 4317 | class HBoundsCheck : public HExpression<2> { |
| 4318 | public: |
| 4319 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4320 | : HExpression(index->GetType(), SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4321 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 4322 | SetRawInputAt(0, index); |
| 4323 | SetRawInputAt(1, length); |
| 4324 | } |
| 4325 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4326 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4327 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4328 | UNUSED(other); |
| 4329 | return true; |
| 4330 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4331 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4332 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4333 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4334 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 4335 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4336 | |
| 4337 | DECLARE_INSTRUCTION(BoundsCheck); |
| 4338 | |
| 4339 | private: |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4340 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 4341 | }; |
| 4342 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4343 | /** |
| 4344 | * Some DEX instructions are folded into multiple HInstructions that need |
| 4345 | * to stay live until the last HInstruction. This class |
| 4346 | * 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] | 4347 | * HInstruction stays live. `index` represents the stack location index of the |
| 4348 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4349 | */ |
| 4350 | class HTemporary : public HTemplateInstruction<0> { |
| 4351 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4352 | explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc) |
| 4353 | : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4354 | |
| 4355 | size_t GetIndex() const { return index_; } |
| 4356 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 4357 | Primitive::Type GetType() const OVERRIDE { |
| 4358 | // The previous instruction is the one that will be stored in the temporary location. |
| 4359 | DCHECK(GetPrevious() != nullptr); |
| 4360 | return GetPrevious()->GetType(); |
| 4361 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 4362 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4363 | DECLARE_INSTRUCTION(Temporary); |
| 4364 | |
| 4365 | private: |
| 4366 | const size_t index_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4367 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 4368 | }; |
| 4369 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4370 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 4371 | public: |
| 4372 | explicit HSuspendCheck(uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4373 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4374 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4375 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4376 | return true; |
| 4377 | } |
| 4378 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 4379 | void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; } |
| 4380 | SlowPathCode* GetSlowPath() const { return slow_path_; } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4381 | |
| 4382 | DECLARE_INSTRUCTION(SuspendCheck); |
| 4383 | |
| 4384 | private: |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 4385 | // Only used for code generation, in order to share the same slow path between back edges |
| 4386 | // of a same loop. |
| 4387 | SlowPathCode* slow_path_; |
| 4388 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4389 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 4390 | }; |
| 4391 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4392 | /** |
| 4393 | * Instruction to load a Class object. |
| 4394 | */ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4395 | class HLoadClass : public HExpression<1> { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4396 | public: |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4397 | HLoadClass(HCurrentMethod* current_method, |
| 4398 | uint16_t type_index, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4399 | const DexFile& dex_file, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4400 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4401 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4402 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4403 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4404 | dex_file_(dex_file), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4405 | is_referrers_class_(is_referrers_class), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4406 | generate_clinit_check_(false), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4407 | loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) { |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4408 | SetRawInputAt(0, current_method); |
| 4409 | } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4410 | |
| 4411 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4412 | |
| 4413 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4414 | return other->AsLoadClass()->type_index_ == type_index_; |
| 4415 | } |
| 4416 | |
| 4417 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 4418 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4419 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4420 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 4421 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4422 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4423 | bool NeedsEnvironment() const OVERRIDE { |
| 4424 | // Will call runtime and load the class if the class is not loaded yet. |
| 4425 | // TODO: finer grain decision. |
| 4426 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4429 | bool MustGenerateClinitCheck() const { |
| 4430 | return generate_clinit_check_; |
| 4431 | } |
| 4432 | |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 4433 | void SetMustGenerateClinitCheck(bool generate_clinit_check) { |
| 4434 | generate_clinit_check_ = generate_clinit_check; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | bool CanCallRuntime() const { |
| 4438 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 4439 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4440 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4441 | bool CanThrow() const OVERRIDE { |
| 4442 | // May call runtime and and therefore can throw. |
| 4443 | // TODO: finer grain decision. |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 4444 | return CanCallRuntime(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 4447 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 4448 | return loaded_class_rti_; |
| 4449 | } |
| 4450 | |
| 4451 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 4452 | // Make sure we only set exact types (the loaded class should never be merged). |
| 4453 | DCHECK(rti.IsExact()); |
| 4454 | loaded_class_rti_ = rti; |
| 4455 | } |
| 4456 | |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4457 | const DexFile& GetDexFile() { return dex_file_; } |
| 4458 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 4459 | bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; } |
| 4460 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4461 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4462 | return SideEffects::CanTriggerGC(); |
| 4463 | } |
| 4464 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4465 | DECLARE_INSTRUCTION(LoadClass); |
| 4466 | |
| 4467 | private: |
| 4468 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4469 | const DexFile& dex_file_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4470 | const bool is_referrers_class_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4471 | // Whether this instruction must generate the initialization check. |
| 4472 | // Used for code generation. |
| 4473 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4474 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 4475 | ReferenceTypeInfo loaded_class_rti_; |
| 4476 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4477 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 4478 | }; |
| 4479 | |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4480 | class HLoadString : public HExpression<1> { |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4481 | public: |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4482 | HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4483 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
| 4484 | string_index_(string_index) { |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4485 | SetRawInputAt(0, current_method); |
| 4486 | } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4487 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4488 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4489 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4490 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4491 | return other->AsLoadString()->string_index_ == string_index_; |
| 4492 | } |
| 4493 | |
| 4494 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 4495 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4496 | uint32_t GetStringIndex() const { return string_index_; } |
| 4497 | |
| 4498 | // TODO: Can we deopt or debug when we resolve a string? |
| 4499 | bool NeedsEnvironment() const OVERRIDE { return false; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 4500 | bool NeedsDexCache() const OVERRIDE { return true; } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 4501 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4502 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4503 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4504 | return SideEffects::CanTriggerGC(); |
| 4505 | } |
| 4506 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4507 | DECLARE_INSTRUCTION(LoadString); |
| 4508 | |
| 4509 | private: |
| 4510 | const uint32_t string_index_; |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4511 | |
| 4512 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 4513 | }; |
| 4514 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4515 | /** |
| 4516 | * Performs an initialization check on its Class object input. |
| 4517 | */ |
| 4518 | class HClinitCheck : public HExpression<1> { |
| 4519 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 4520 | HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 4521 | : HExpression( |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4522 | Primitive::kPrimNot, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4523 | SideEffects::AllChanges(), // Assume write/read on all fields/arrays. |
| 4524 | dex_pc) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4525 | SetRawInputAt(0, constant); |
| 4526 | } |
| 4527 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4528 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4529 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4530 | UNUSED(other); |
| 4531 | return true; |
| 4532 | } |
| 4533 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4534 | bool NeedsEnvironment() const OVERRIDE { |
| 4535 | // May call runtime to initialize the class. |
| 4536 | return true; |
| 4537 | } |
| 4538 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4539 | |
| 4540 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 4541 | |
| 4542 | DECLARE_INSTRUCTION(ClinitCheck); |
| 4543 | |
| 4544 | private: |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4545 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 4546 | }; |
| 4547 | |
| 4548 | class HStaticFieldGet : public HExpression<1> { |
| 4549 | public: |
| 4550 | HStaticFieldGet(HInstruction* cls, |
| 4551 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4552 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4553 | bool is_volatile, |
| 4554 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4555 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4556 | Handle<mirror::DexCache> dex_cache, |
| 4557 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4558 | : HExpression( |
| 4559 | field_type, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4560 | SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4561 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4562 | SetRawInputAt(0, cls); |
| 4563 | } |
| 4564 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4565 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 4566 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4567 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4568 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4569 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 4570 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | size_t ComputeHashCode() const OVERRIDE { |
| 4574 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 4575 | } |
| 4576 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4577 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4578 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 4579 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4580 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4581 | |
| 4582 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 4583 | |
| 4584 | private: |
| 4585 | const FieldInfo field_info_; |
| 4586 | |
| 4587 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 4588 | }; |
| 4589 | |
| 4590 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 4591 | public: |
| 4592 | HStaticFieldSet(HInstruction* cls, |
| 4593 | HInstruction* value, |
| 4594 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4595 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4596 | bool is_volatile, |
| 4597 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4598 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4599 | Handle<mirror::DexCache> dex_cache, |
| 4600 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4601 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4602 | SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4603 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4604 | value_can_be_null_(true) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4605 | SetRawInputAt(0, cls); |
| 4606 | SetRawInputAt(1, value); |
| 4607 | } |
| 4608 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4609 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4610 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 4611 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4612 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4613 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4614 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4615 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 4616 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4617 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4618 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 4619 | |
| 4620 | private: |
| 4621 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4622 | bool value_can_be_null_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4623 | |
| 4624 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 4625 | }; |
| 4626 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4627 | // Implement the move-exception DEX instruction. |
| 4628 | class HLoadException : public HExpression<0> { |
| 4629 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4630 | explicit HLoadException(uint32_t dex_pc = kNoDexPc) |
| 4631 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4632 | |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 4633 | bool CanBeNull() const OVERRIDE { return false; } |
| 4634 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4635 | DECLARE_INSTRUCTION(LoadException); |
| 4636 | |
| 4637 | private: |
| 4638 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 4639 | }; |
| 4640 | |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 4641 | // Implicit part of move-exception which clears thread-local exception storage. |
| 4642 | // Must not be removed because the runtime expects the TLS to get cleared. |
| 4643 | class HClearException : public HTemplateInstruction<0> { |
| 4644 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4645 | explicit HClearException(uint32_t dex_pc = kNoDexPc) |
| 4646 | : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {} |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 4647 | |
| 4648 | DECLARE_INSTRUCTION(ClearException); |
| 4649 | |
| 4650 | private: |
| 4651 | DISALLOW_COPY_AND_ASSIGN(HClearException); |
| 4652 | }; |
| 4653 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4654 | class HThrow : public HTemplateInstruction<1> { |
| 4655 | public: |
| 4656 | HThrow(HInstruction* exception, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4657 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4658 | SetRawInputAt(0, exception); |
| 4659 | } |
| 4660 | |
| 4661 | bool IsControlFlow() const OVERRIDE { return true; } |
| 4662 | |
| 4663 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 4664 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4665 | bool CanThrow() const OVERRIDE { return true; } |
| 4666 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4667 | |
| 4668 | DECLARE_INSTRUCTION(Throw); |
| 4669 | |
| 4670 | private: |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4671 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 4672 | }; |
| 4673 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4674 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4675 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4676 | HInstanceOf(HInstruction* object, |
| 4677 | HLoadClass* constant, |
| 4678 | bool class_is_final, |
| 4679 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4680 | : HExpression(Primitive::kPrimBoolean, |
| 4681 | SideEffectsForArchRuntimeCalls(class_is_final), |
| 4682 | dex_pc), |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4683 | class_is_final_(class_is_final), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4684 | must_do_null_check_(true) { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4685 | SetRawInputAt(0, object); |
| 4686 | SetRawInputAt(1, constant); |
| 4687 | } |
| 4688 | |
| 4689 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4690 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4691 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4692 | return true; |
| 4693 | } |
| 4694 | |
| 4695 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4696 | return false; |
| 4697 | } |
| 4698 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4699 | bool IsClassFinal() const { return class_is_final_; } |
| 4700 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4701 | // Used only in code generation. |
| 4702 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 4703 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 4704 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4705 | static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) { |
| 4706 | return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC(); |
| 4707 | } |
| 4708 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4709 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4710 | |
| 4711 | private: |
| 4712 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4713 | bool must_do_null_check_; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4714 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4715 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 4716 | }; |
| 4717 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4718 | class HBoundType : public HExpression<1> { |
| 4719 | public: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4720 | // Constructs an HBoundType with the given upper_bound. |
| 4721 | // Ensures that the upper_bound is valid. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4722 | HBoundType(HInstruction* input, |
| 4723 | ReferenceTypeInfo upper_bound, |
| 4724 | bool upper_can_be_null, |
| 4725 | uint32_t dex_pc = kNoDexPc) |
| 4726 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc), |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4727 | upper_bound_(upper_bound), |
| 4728 | upper_can_be_null_(upper_can_be_null), |
| 4729 | can_be_null_(upper_can_be_null) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 4730 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4731 | SetRawInputAt(0, input); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4732 | SetReferenceTypeInfo(upper_bound_); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4735 | // GetUpper* should only be used in reference type propagation. |
| 4736 | const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; } |
| 4737 | bool GetUpperCanBeNull() const { return upper_can_be_null_; } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4738 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4739 | void SetCanBeNull(bool can_be_null) { |
| 4740 | DCHECK(upper_can_be_null_ || !can_be_null); |
| 4741 | can_be_null_ = can_be_null; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4744 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 4745 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4746 | DECLARE_INSTRUCTION(BoundType); |
| 4747 | |
| 4748 | private: |
| 4749 | // 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] | 4750 | // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()). |
| 4751 | // It is used to bound the type in cases like: |
| 4752 | // if (x instanceof ClassX) { |
| 4753 | // // uper_bound_ will be ClassX |
| 4754 | // } |
| 4755 | const ReferenceTypeInfo upper_bound_; |
| 4756 | // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this |
| 4757 | // is false then can_be_null_ cannot be true). |
| 4758 | const bool upper_can_be_null_; |
| 4759 | bool can_be_null_; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4760 | |
| 4761 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 4762 | }; |
| 4763 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4764 | class HCheckCast : public HTemplateInstruction<2> { |
| 4765 | public: |
| 4766 | HCheckCast(HInstruction* object, |
| 4767 | HLoadClass* constant, |
| 4768 | bool class_is_final, |
| 4769 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4770 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4771 | class_is_final_(class_is_final), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4772 | must_do_null_check_(true) { |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4773 | SetRawInputAt(0, object); |
| 4774 | SetRawInputAt(1, constant); |
| 4775 | } |
| 4776 | |
| 4777 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4778 | |
| 4779 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4780 | return true; |
| 4781 | } |
| 4782 | |
| 4783 | bool NeedsEnvironment() const OVERRIDE { |
| 4784 | // Instruction may throw a CheckCastError. |
| 4785 | return true; |
| 4786 | } |
| 4787 | |
| 4788 | bool CanThrow() const OVERRIDE { return true; } |
| 4789 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4790 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 4791 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 4792 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4793 | |
| 4794 | bool IsClassFinal() const { return class_is_final_; } |
| 4795 | |
| 4796 | DECLARE_INSTRUCTION(CheckCast); |
| 4797 | |
| 4798 | private: |
| 4799 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4800 | bool must_do_null_check_; |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4801 | |
| 4802 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4803 | }; |
| 4804 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 4805 | class HMemoryBarrier : public HTemplateInstruction<0> { |
| 4806 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4807 | explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4808 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4809 | SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays. |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 4810 | barrier_kind_(barrier_kind) {} |
| 4811 | |
| 4812 | MemBarrierKind GetBarrierKind() { return barrier_kind_; } |
| 4813 | |
| 4814 | DECLARE_INSTRUCTION(MemoryBarrier); |
| 4815 | |
| 4816 | private: |
| 4817 | const MemBarrierKind barrier_kind_; |
| 4818 | |
| 4819 | DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier); |
| 4820 | }; |
| 4821 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4822 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 4823 | public: |
| 4824 | enum OperationKind { |
| 4825 | kEnter, |
| 4826 | kExit, |
| 4827 | }; |
| 4828 | |
| 4829 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4830 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4831 | SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays. |
| 4832 | kind_(kind) { |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4833 | SetRawInputAt(0, object); |
| 4834 | } |
| 4835 | |
| 4836 | // Instruction may throw a Java exception, so we need an environment. |
David Brazdil | bff7503 | 2015-07-08 17:26:51 +0000 | [diff] [blame] | 4837 | bool NeedsEnvironment() const OVERRIDE { return CanThrow(); } |
| 4838 | |
| 4839 | bool CanThrow() const OVERRIDE { |
| 4840 | // Verifier guarantees that monitor-exit cannot throw. |
| 4841 | // This is important because it allows the HGraphBuilder to remove |
| 4842 | // a dead throw-catch loop generated for `synchronized` blocks/methods. |
| 4843 | return IsEnter(); |
| 4844 | } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4845 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4846 | |
| 4847 | bool IsEnter() const { return kind_ == kEnter; } |
| 4848 | |
| 4849 | DECLARE_INSTRUCTION(MonitorOperation); |
| 4850 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4851 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4852 | const OperationKind kind_; |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4853 | |
| 4854 | private: |
| 4855 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 4856 | }; |
| 4857 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 4858 | /** |
| 4859 | * A HInstruction used as a marker for the replacement of new + <init> |
| 4860 | * of a String to a call to a StringFactory. Only baseline will see |
| 4861 | * the node at code generation, where it will be be treated as null. |
| 4862 | * When compiling non-baseline, `HFakeString` instructions are being removed |
| 4863 | * in the instruction simplifier. |
| 4864 | */ |
| 4865 | class HFakeString : public HTemplateInstruction<0> { |
| 4866 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4867 | explicit HFakeString(uint32_t dex_pc = kNoDexPc) |
| 4868 | : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 4869 | |
| 4870 | Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; } |
| 4871 | |
| 4872 | DECLARE_INSTRUCTION(FakeString); |
| 4873 | |
| 4874 | private: |
| 4875 | DISALLOW_COPY_AND_ASSIGN(HFakeString); |
| 4876 | }; |
| 4877 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 4878 | class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4879 | public: |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4880 | MoveOperands(Location source, |
| 4881 | Location destination, |
| 4882 | Primitive::Type type, |
| 4883 | HInstruction* instruction) |
| 4884 | : source_(source), destination_(destination), type_(type), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4885 | |
| 4886 | Location GetSource() const { return source_; } |
| 4887 | Location GetDestination() const { return destination_; } |
| 4888 | |
| 4889 | void SetSource(Location value) { source_ = value; } |
| 4890 | void SetDestination(Location value) { destination_ = value; } |
| 4891 | |
| 4892 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 4893 | // destination (but not the source). |
| 4894 | Location MarkPending() { |
| 4895 | DCHECK(!IsPending()); |
| 4896 | Location dest = destination_; |
| 4897 | destination_ = Location::NoLocation(); |
| 4898 | return dest; |
| 4899 | } |
| 4900 | |
| 4901 | void ClearPending(Location dest) { |
| 4902 | DCHECK(IsPending()); |
| 4903 | destination_ = dest; |
| 4904 | } |
| 4905 | |
| 4906 | bool IsPending() const { |
| 4907 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 4908 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 4909 | } |
| 4910 | |
| 4911 | // True if this blocks a move from the given location. |
| 4912 | bool Blocks(Location loc) const { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4913 | return !IsEliminated() && source_.OverlapsWith(loc); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | // A move is redundant if it's been eliminated, if its source and |
| 4917 | // destination are the same, or if its destination is unneeded. |
| 4918 | bool IsRedundant() const { |
| 4919 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 4920 | } |
| 4921 | |
| 4922 | // We clear both operands to indicate move that's been eliminated. |
| 4923 | void Eliminate() { |
| 4924 | source_ = destination_ = Location::NoLocation(); |
| 4925 | } |
| 4926 | |
| 4927 | bool IsEliminated() const { |
| 4928 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 4929 | return source_.IsInvalid(); |
| 4930 | } |
| 4931 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 4932 | Primitive::Type GetType() const { return type_; } |
| 4933 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4934 | bool Is64BitMove() const { |
| 4935 | return Primitive::Is64BitType(type_); |
| 4936 | } |
| 4937 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4938 | HInstruction* GetInstruction() const { return instruction_; } |
| 4939 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4940 | private: |
| 4941 | Location source_; |
| 4942 | Location destination_; |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4943 | // The type this move is for. |
| 4944 | Primitive::Type type_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4945 | // The instruction this move is assocatied with. Null when this move is |
| 4946 | // for moving an input in the expected locations of user (including a phi user). |
| 4947 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 4948 | // in the same parallel move. |
| 4949 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4950 | }; |
| 4951 | |
| 4952 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 4953 | |
| 4954 | class HParallelMove : public HTemplateInstruction<0> { |
| 4955 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4956 | explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc) |
| 4957 | : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4958 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4959 | void AddMove(Location source, |
| 4960 | Location destination, |
| 4961 | Primitive::Type type, |
| 4962 | HInstruction* instruction) { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4963 | DCHECK(source.IsValid()); |
| 4964 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4965 | if (kIsDebugBuild) { |
| 4966 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4967 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 4968 | if (moves_.Get(i).GetInstruction() == instruction) { |
| 4969 | // Special case the situation where the move is for the spill slot |
| 4970 | // of the instruction. |
| 4971 | if ((GetPrevious() == instruction) |
| 4972 | || ((GetPrevious() == nullptr) |
| 4973 | && instruction->IsPhi() |
| 4974 | && instruction->GetBlock() == GetBlock())) { |
| 4975 | DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind()) |
| 4976 | << "Doing parallel moves for the same instruction."; |
| 4977 | } else { |
| 4978 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 4979 | } |
| 4980 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4981 | } |
| 4982 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4983 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4984 | DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination())) |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 4985 | << "Overlapped destination for two moves in a parallel move: " |
| 4986 | << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and " |
| 4987 | << source << " ==> " << destination; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4988 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4989 | } |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4990 | moves_.Add(MoveOperands(source, destination, type, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4994 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | size_t NumMoves() const { return moves_.Size(); } |
| 4998 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4999 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 5000 | |
| 5001 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 5002 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 5003 | |
| 5004 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 5005 | }; |
| 5006 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 5007 | } // namespace art |
| 5008 | |
| 5009 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 5010 | #include "nodes_x86.h" |
| 5011 | #endif |
| 5012 | |
| 5013 | namespace art { |
| 5014 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5015 | class HGraphVisitor : public ValueObject { |
| 5016 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 5017 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 5018 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5019 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5020 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5021 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 5022 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 5023 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5024 | void VisitInsertionOrder(); |
| 5025 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 5026 | // Visit the graph following dominator tree reverse post-order. |
| 5027 | void VisitReversePostOrder(); |
| 5028 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 5029 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 5030 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5031 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5032 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5033 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 5034 | |
| 5035 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 5036 | |
| 5037 | #undef DECLARE_VISIT_INSTRUCTION |
| 5038 | |
| 5039 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 5040 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5041 | |
| 5042 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 5043 | }; |
| 5044 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5045 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 5046 | public: |
| 5047 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 5048 | virtual ~HGraphDelegateVisitor() {} |
| 5049 | |
| 5050 | // Visit functions that delegate to to super class. |
| 5051 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5052 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5053 | |
| 5054 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 5055 | |
| 5056 | #undef DECLARE_VISIT_INSTRUCTION |
| 5057 | |
| 5058 | private: |
| 5059 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 5060 | }; |
| 5061 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5062 | class HInsertionOrderIterator : public ValueObject { |
| 5063 | public: |
| 5064 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 5065 | |
| 5066 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 5067 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 5068 | void Advance() { ++index_; } |
| 5069 | |
| 5070 | private: |
| 5071 | const HGraph& graph_; |
| 5072 | size_t index_; |
| 5073 | |
| 5074 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 5075 | }; |
| 5076 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5077 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5078 | public: |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 5079 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) { |
| 5080 | // Check that reverse post order of the graph has been built. |
| 5081 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 5082 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5083 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5084 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 5085 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5086 | void Advance() { ++index_; } |
| 5087 | |
| 5088 | private: |
| 5089 | const HGraph& graph_; |
| 5090 | size_t index_; |
| 5091 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5092 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5093 | }; |
| 5094 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5095 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5096 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5097 | explicit HPostOrderIterator(const HGraph& graph) |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 5098 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) { |
| 5099 | // Check that reverse post order of the graph has been built. |
| 5100 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 5101 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5102 | |
| 5103 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5104 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5105 | void Advance() { --index_; } |
| 5106 | |
| 5107 | private: |
| 5108 | const HGraph& graph_; |
| 5109 | size_t index_; |
| 5110 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5111 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5112 | }; |
| 5113 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 5114 | class HLinearPostOrderIterator : public ValueObject { |
| 5115 | public: |
| 5116 | explicit HLinearPostOrderIterator(const HGraph& graph) |
| 5117 | : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {} |
| 5118 | |
| 5119 | bool Done() const { return index_ == 0; } |
| 5120 | |
| 5121 | HBasicBlock* Current() const { return order_.Get(index_ -1); } |
| 5122 | |
| 5123 | void Advance() { |
| 5124 | --index_; |
| 5125 | DCHECK_GE(index_, 0U); |
| 5126 | } |
| 5127 | |
| 5128 | private: |
| 5129 | const GrowableArray<HBasicBlock*>& order_; |
| 5130 | size_t index_; |
| 5131 | |
| 5132 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 5133 | }; |
| 5134 | |
| 5135 | class HLinearOrderIterator : public ValueObject { |
| 5136 | public: |
| 5137 | explicit HLinearOrderIterator(const HGraph& graph) |
| 5138 | : order_(graph.GetLinearOrder()), index_(0) {} |
| 5139 | |
| 5140 | bool Done() const { return index_ == order_.Size(); } |
| 5141 | HBasicBlock* Current() const { return order_.Get(index_); } |
| 5142 | void Advance() { ++index_; } |
| 5143 | |
| 5144 | private: |
| 5145 | const GrowableArray<HBasicBlock*>& order_; |
| 5146 | size_t index_; |
| 5147 | |
| 5148 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 5149 | }; |
| 5150 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 5151 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 5152 | // of an inner loop. The order in which the blocks are iterated is on their |
| 5153 | // block id. |
| 5154 | class HBlocksInLoopIterator : public ValueObject { |
| 5155 | public: |
| 5156 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 5157 | : blocks_in_loop_(info.GetBlocks()), |
| 5158 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 5159 | index_(0) { |
| 5160 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 5161 | Advance(); |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | bool Done() const { return index_ == blocks_.Size(); } |
| 5166 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 5167 | void Advance() { |
| 5168 | ++index_; |
| 5169 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 5170 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 5171 | break; |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | private: |
| 5177 | const BitVector& blocks_in_loop_; |
| 5178 | const GrowableArray<HBasicBlock*>& blocks_; |
| 5179 | size_t index_; |
| 5180 | |
| 5181 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 5182 | }; |
| 5183 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 5184 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 5185 | // of an inner loop. The order in which the blocks are iterated is reverse |
| 5186 | // post order. |
| 5187 | class HBlocksInLoopReversePostOrderIterator : public ValueObject { |
| 5188 | public: |
| 5189 | explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info) |
| 5190 | : blocks_in_loop_(info.GetBlocks()), |
| 5191 | blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()), |
| 5192 | index_(0) { |
| 5193 | if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) { |
| 5194 | Advance(); |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | bool Done() const { return index_ == blocks_.Size(); } |
| 5199 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 5200 | void Advance() { |
| 5201 | ++index_; |
| 5202 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 5203 | if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) { |
| 5204 | break; |
| 5205 | } |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | private: |
| 5210 | const BitVector& blocks_in_loop_; |
| 5211 | const GrowableArray<HBasicBlock*>& blocks_; |
| 5212 | size_t index_; |
| 5213 | |
| 5214 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator); |
| 5215 | }; |
| 5216 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 5217 | inline int64_t Int64FromConstant(HConstant* constant) { |
| 5218 | DCHECK(constant->IsIntConstant() || constant->IsLongConstant()); |
| 5219 | return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue() |
| 5220 | : constant->AsLongConstant()->GetValue(); |
| 5221 | } |
| 5222 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 5223 | inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) { |
| 5224 | // For the purposes of the compiler, the dex files must actually be the same object |
| 5225 | // if we want to safely treat them as the same. This is especially important for JIT |
| 5226 | // as custom class loaders can open the same underlying file (or memory) multiple |
| 5227 | // times and provide different class resolution but no two class loaders should ever |
| 5228 | // use the same DexFile object - doing so is an unsupported hack that can lead to |
| 5229 | // all sorts of weird failures. |
| 5230 | return &lhs == &rhs; |
| 5231 | } |
| 5232 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5233 | } // namespace art |
| 5234 | |
| 5235 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |