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 | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 21 | #include <array> |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 22 | #include <type_traits> |
| 23 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 24 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 25 | #include "base/arena_object.h" |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 27 | #include "dex/compiler_enums.h" |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 28 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 29 | #include "handle.h" |
| 30 | #include "handle_scope.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 31 | #include "invoke_type.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 32 | #include "locations.h" |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 33 | #include "method_reference.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 34 | #include "mirror/class.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 35 | #include "offsets.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 36 | #include "primitive.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 37 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 38 | #include "utils/growable_array.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 42 | class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 43 | class HBasicBlock; |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 44 | class HCurrentMethod; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 45 | class HDoubleConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 46 | class HEnvironment; |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 47 | class HFakeString; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 48 | class HFloatConstant; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 49 | class HGraphBuilder; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 50 | class HGraphVisitor; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 51 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | class HIntConstant; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 53 | class HInvoke; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 54 | class HLongConstant; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 55 | class HNullConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 56 | class HPhi; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 57 | class HSuspendCheck; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 58 | class HTryBoundary; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 59 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 60 | class LocationSummary; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 61 | class SlowPathCode; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 62 | class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 63 | |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 64 | namespace mirror { |
| 65 | class DexCache; |
| 66 | } // namespace mirror |
| 67 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 68 | static const int kDefaultNumberOfBlocks = 8; |
| 69 | static const int kDefaultNumberOfSuccessors = 2; |
| 70 | static const int kDefaultNumberOfPredecessors = 2; |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 71 | static const int kDefaultNumberOfExceptionalPredecessors = 0; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 72 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 73 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 74 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 75 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 76 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 77 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 78 | static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1); |
| 79 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 80 | static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1); |
| 81 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 82 | static constexpr uint32_t kNoDexPc = -1; |
| 83 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 84 | enum IfCondition { |
| 85 | kCondEQ, |
| 86 | kCondNE, |
| 87 | kCondLT, |
| 88 | kCondLE, |
| 89 | kCondGT, |
| 90 | kCondGE, |
| 91 | }; |
| 92 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 93 | class HInstructionList : public ValueObject { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 94 | public: |
| 95 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 96 | |
| 97 | void AddInstruction(HInstruction* instruction); |
| 98 | void RemoveInstruction(HInstruction* instruction); |
| 99 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 100 | // Insert `instruction` before/after an existing instruction `cursor`. |
| 101 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
| 102 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
| 103 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 104 | // Return true if this list contains `instruction`. |
| 105 | bool Contains(HInstruction* instruction) const; |
| 106 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 107 | // Return true if `instruction1` is found before `instruction2` in |
| 108 | // this instruction list and false otherwise. Abort if none |
| 109 | // of these instructions is found. |
| 110 | bool FoundBefore(const HInstruction* instruction1, |
| 111 | const HInstruction* instruction2) const; |
| 112 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 113 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 114 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 115 | |
| 116 | // Update the block of all instructions to be `block`. |
| 117 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 118 | |
| 119 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
| 120 | void Add(const HInstructionList& instruction_list); |
| 121 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 122 | // Return the number of instructions in the list. This is an expensive operation. |
| 123 | size_t CountSize() const; |
| 124 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 125 | private: |
| 126 | HInstruction* first_instruction_; |
| 127 | HInstruction* last_instruction_; |
| 128 | |
| 129 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 130 | friend class HGraph; |
| 131 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 132 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 133 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 136 | }; |
| 137 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 138 | // Control-flow graph of a method. Contains a list of basic blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 139 | class HGraph : public ArenaObject<kArenaAllocGraph> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 140 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 141 | HGraph(ArenaAllocator* arena, |
| 142 | const DexFile& dex_file, |
| 143 | uint32_t method_idx, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 144 | bool should_generate_constructor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 145 | InstructionSet instruction_set, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 146 | InvokeType invoke_type = kInvalidInvokeType, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 147 | bool debuggable = false, |
| 148 | int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 149 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 150 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 151 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 152 | linear_order_(arena, kDefaultNumberOfBlocks), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 153 | entry_block_(nullptr), |
| 154 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 155 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 156 | number_of_vregs_(0), |
| 157 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 158 | temporaries_vreg_slots_(0), |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 159 | has_bounds_checks_(false), |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 160 | debuggable_(debuggable), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 161 | current_instruction_id_(start_instruction_id), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 162 | dex_file_(dex_file), |
| 163 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 164 | invoke_type_(invoke_type), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 165 | in_ssa_form_(false), |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 166 | should_generate_constructor_barrier_(should_generate_constructor_barrier), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 167 | instruction_set_(instruction_set), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 168 | cached_null_constant_(nullptr), |
| 169 | cached_int_constants_(std::less<int32_t>(), arena->Adapter()), |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 170 | cached_float_constants_(std::less<int32_t>(), arena->Adapter()), |
| 171 | cached_long_constants_(std::less<int64_t>(), arena->Adapter()), |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 172 | cached_double_constants_(std::less<int64_t>(), arena->Adapter()), |
| 173 | cached_current_method_(nullptr) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 174 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 175 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 176 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 177 | HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 178 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 179 | bool IsInSsaForm() const { return in_ssa_form_; } |
| 180 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 181 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 182 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 183 | bool HasExitBlock() const { return exit_block_ != nullptr; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 184 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 185 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 186 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 187 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 188 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 189 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 190 | // Try building the SSA form of this graph, with dominance computation and loop |
| 191 | // recognition. Returns whether it was successful in doing all these steps. |
| 192 | bool TryBuildingSsa() { |
| 193 | BuildDominatorTree(); |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 194 | // The SSA builder requires loops to all be natural. Specifically, the dead phi |
| 195 | // elimination phase checks the consistency of the graph when doing a post-order |
| 196 | // visit for eliminating dead phis: a dead phi can only have loop header phi |
| 197 | // users remaining when being visited. |
| 198 | if (!AnalyzeNaturalLoops()) return false; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 199 | // Precompute per-block try membership before entering the SSA builder, |
| 200 | // which needs the information to build catch block phis from values of |
| 201 | // locals at throwing instructions inside try blocks. |
| 202 | ComputeTryBlockInformation(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 203 | TransformToSsa(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 204 | in_ssa_form_ = true; |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 205 | return true; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 208 | void ComputeDominanceInformation(); |
| 209 | void ClearDominanceInformation(); |
| 210 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 211 | void BuildDominatorTree(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 212 | void TransformToSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 213 | void SimplifyCFG(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 214 | void SimplifyCatchBlocks(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 215 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 216 | // Analyze all natural loops in this graph. Returns false if one |
| 217 | // loop is not natural, that is the header does not dominate the |
| 218 | // back edge. |
| 219 | bool AnalyzeNaturalLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 220 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 221 | // Iterate over blocks to compute try block membership. Needs reverse post |
| 222 | // order and loop information. |
| 223 | void ComputeTryBlockInformation(); |
| 224 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 225 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 226 | // Returns the instruction used to replace the invoke expression or null if the |
| 227 | // invoke is for a void method. |
| 228 | HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 229 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 230 | // Need to add a couple of blocks to test if the loop body is entered and |
| 231 | // put deoptimization instructions, etc. |
| 232 | void TransformLoopHeaderForBCE(HBasicBlock* header); |
| 233 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 234 | // Removes `block` from the graph. |
| 235 | void DeleteDeadBlock(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 236 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 237 | // Splits the edge between `block` and `successor` while preserving the |
| 238 | // indices in the predecessor/successor lists. If there are multiple edges |
| 239 | // between the blocks, the lowest indices are used. |
| 240 | // Returns the new block which is empty and has the same dex pc as `successor`. |
| 241 | HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor); |
| 242 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 243 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 244 | void SimplifyLoop(HBasicBlock* header); |
| 245 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 246 | int32_t GetNextInstructionId() { |
| 247 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 248 | return current_instruction_id_++; |
| 249 | } |
| 250 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 251 | int32_t GetCurrentInstructionId() const { |
| 252 | return current_instruction_id_; |
| 253 | } |
| 254 | |
| 255 | void SetCurrentInstructionId(int32_t id) { |
| 256 | current_instruction_id_ = id; |
| 257 | } |
| 258 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 259 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 260 | return maximum_number_of_out_vregs_; |
| 261 | } |
| 262 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 263 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 264 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 267 | void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) { |
| 268 | maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value); |
| 269 | } |
| 270 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 271 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 272 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 275 | size_t GetTemporariesVRegSlots() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 276 | DCHECK(!in_ssa_form_); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 277 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 280 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 281 | number_of_vregs_ = number_of_vregs; |
| 282 | } |
| 283 | |
| 284 | uint16_t GetNumberOfVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 285 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 286 | return number_of_vregs_; |
| 287 | } |
| 288 | |
| 289 | void SetNumberOfInVRegs(uint16_t value) { |
| 290 | number_of_in_vregs_ = value; |
| 291 | } |
| 292 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 293 | uint16_t GetNumberOfLocalVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 294 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 295 | return number_of_vregs_ - number_of_in_vregs_; |
| 296 | } |
| 297 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 298 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 299 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 300 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 301 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 302 | const GrowableArray<HBasicBlock*>& GetLinearOrder() const { |
| 303 | return linear_order_; |
| 304 | } |
| 305 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 306 | bool HasBoundsChecks() const { |
| 307 | return has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 310 | void SetHasBoundsChecks(bool value) { |
| 311 | has_bounds_checks_ = value; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 314 | bool ShouldGenerateConstructorBarrier() const { |
| 315 | return should_generate_constructor_barrier_; |
| 316 | } |
| 317 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 318 | bool IsDebuggable() const { return debuggable_; } |
| 319 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 320 | // 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] | 321 | // already, it is created and inserted into the graph. This method is only for |
| 322 | // integral types. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 323 | 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] | 324 | |
| 325 | // TODO: This is problematic for the consistency of reference type propagation |
| 326 | // because it can be created anytime after the pass and thus it will be left |
| 327 | // with an invalid type. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 328 | HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 329 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 330 | HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) { |
| 331 | return CreateConstant(value, &cached_int_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 332 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 333 | HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) { |
| 334 | return CreateConstant(value, &cached_long_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 335 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 336 | HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) { |
| 337 | 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] | 338 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 339 | HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) { |
| 340 | 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] | 341 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 342 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 343 | HCurrentMethod* GetCurrentMethod(); |
| 344 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 345 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 346 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 347 | const DexFile& GetDexFile() const { |
| 348 | return dex_file_; |
| 349 | } |
| 350 | |
| 351 | uint32_t GetMethodIdx() const { |
| 352 | return method_idx_; |
| 353 | } |
| 354 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 355 | InvokeType GetInvokeType() const { |
| 356 | return invoke_type_; |
| 357 | } |
| 358 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 359 | InstructionSet GetInstructionSet() const { |
| 360 | return instruction_set_; |
| 361 | } |
| 362 | |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 363 | // TODO: Remove once the full compilation pipeline is enabled for try/catch. |
| 364 | bool HasTryCatch() const; |
| 365 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 366 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 367 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 368 | HBasicBlock* predecessor, |
| 369 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 370 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 371 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 372 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 373 | ArenaBitVector* visiting); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 374 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 375 | void RemoveDeadBlocks(const ArenaBitVector& visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 376 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 377 | template <class InstructionType, typename ValueType> |
| 378 | InstructionType* CreateConstant(ValueType value, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 379 | ArenaSafeMap<ValueType, InstructionType*>* cache, |
| 380 | uint32_t dex_pc = kNoDexPc) { |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 381 | // Try to find an existing constant of the given value. |
| 382 | InstructionType* constant = nullptr; |
| 383 | auto cached_constant = cache->find(value); |
| 384 | if (cached_constant != cache->end()) { |
| 385 | constant = cached_constant->second; |
| 386 | } |
| 387 | |
| 388 | // If not found or previously deleted, create and cache a new instruction. |
Nicolas Geoffray | f78848f | 2015-06-17 11:57:56 +0100 | [diff] [blame] | 389 | // Don't bother reviving a previously deleted instruction, for simplicity. |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 390 | if (constant == nullptr || constant->GetBlock() == nullptr) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 391 | constant = new (arena_) InstructionType(value, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 392 | cache->Overwrite(value, constant); |
| 393 | InsertConstant(constant); |
| 394 | } |
| 395 | return constant; |
| 396 | } |
| 397 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 398 | void InsertConstant(HConstant* instruction); |
| 399 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 400 | // Cache a float constant into the graph. This method should only be |
| 401 | // called by the SsaBuilder when creating "equivalent" instructions. |
| 402 | void CacheFloatConstant(HFloatConstant* constant); |
| 403 | |
| 404 | // See CacheFloatConstant comment. |
| 405 | void CacheDoubleConstant(HDoubleConstant* constant); |
| 406 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 407 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 408 | |
| 409 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 410 | GrowableArray<HBasicBlock*> blocks_; |
| 411 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 412 | // List of blocks to perform a reverse post order tree traversal. |
| 413 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 414 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 415 | // List of blocks to perform a linear order tree traversal. |
| 416 | GrowableArray<HBasicBlock*> linear_order_; |
| 417 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 418 | HBasicBlock* entry_block_; |
| 419 | HBasicBlock* exit_block_; |
| 420 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 421 | // 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] | 422 | uint16_t maximum_number_of_out_vregs_; |
| 423 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 424 | // The number of virtual registers in this method. Contains the parameters. |
| 425 | uint16_t number_of_vregs_; |
| 426 | |
| 427 | // The number of virtual registers used by parameters of this method. |
| 428 | uint16_t number_of_in_vregs_; |
| 429 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 430 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 431 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 432 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 433 | // Has bounds checks. We can totally skip BCE if it's false. |
| 434 | bool has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 435 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 436 | // Indicates whether the graph should be compiled in a way that |
| 437 | // ensures full debuggability. If false, we can apply more |
| 438 | // aggressive optimizations that may limit the level of debugging. |
| 439 | const bool debuggable_; |
| 440 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 441 | // 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] | 442 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 443 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 444 | // The dex file from which the method is from. |
| 445 | const DexFile& dex_file_; |
| 446 | |
| 447 | // The method index in the dex file. |
| 448 | const uint32_t method_idx_; |
| 449 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 450 | // If inlined, this encodes how the callee is being invoked. |
| 451 | const InvokeType invoke_type_; |
| 452 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 453 | // Whether the graph has been transformed to SSA form. Only used |
| 454 | // in debug mode to ensure we are not using properties only valid |
| 455 | // for non-SSA form (like the number of temporaries). |
| 456 | bool in_ssa_form_; |
| 457 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 458 | const bool should_generate_constructor_barrier_; |
| 459 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 460 | const InstructionSet instruction_set_; |
| 461 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 462 | // Cached constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 463 | HNullConstant* cached_null_constant_; |
| 464 | ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 465 | ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 466 | ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 467 | ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 468 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 469 | HCurrentMethod* cached_current_method_; |
| 470 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 471 | friend class SsaBuilder; // For caching constants. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 472 | friend class SsaLivenessAnalysis; // For the linear order. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 473 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 474 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 475 | }; |
| 476 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 477 | class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 478 | public: |
| 479 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 480 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 481 | suspend_check_(nullptr), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 482 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 483 | // Make bit vector growable, as the number of blocks may change. |
| 484 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 485 | |
| 486 | HBasicBlock* GetHeader() const { |
| 487 | return header_; |
| 488 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 489 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 490 | void SetHeader(HBasicBlock* block) { |
| 491 | header_ = block; |
| 492 | } |
| 493 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 494 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 495 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 496 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 497 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 498 | void AddBackEdge(HBasicBlock* back_edge) { |
| 499 | back_edges_.Add(back_edge); |
| 500 | } |
| 501 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 502 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 503 | back_edges_.Delete(back_edge); |
| 504 | } |
| 505 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 506 | bool IsBackEdge(const HBasicBlock& block) const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 507 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 508 | if (back_edges_.Get(i) == &block) return true; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 509 | } |
| 510 | return false; |
| 511 | } |
| 512 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 513 | size_t NumberOfBackEdges() const { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 514 | return back_edges_.Size(); |
| 515 | } |
| 516 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 517 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 518 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 519 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 520 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 523 | // Returns the lifetime position of the back edge that has the |
| 524 | // greatest lifetime position. |
| 525 | size_t GetLifetimeEnd() const; |
| 526 | |
| 527 | void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) { |
| 528 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 529 | if (back_edges_.Get(i) == existing) { |
| 530 | back_edges_.Put(i, new_back_edge); |
| 531 | return; |
| 532 | } |
| 533 | } |
| 534 | UNREACHABLE(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 537 | // 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] | 538 | // that is the header dominates the back edge. |
| 539 | bool Populate(); |
| 540 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 541 | // Reanalyzes the loop by removing loop info from its blocks and re-running |
| 542 | // Populate(). If there are no back edges left, the loop info is completely |
| 543 | // removed as well as its SuspendCheck instruction. It must be run on nested |
| 544 | // inner loops first. |
| 545 | void Update(); |
| 546 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 547 | // Returns whether this loop information contains `block`. |
| 548 | // Note that this loop information *must* be populated before entering this function. |
| 549 | bool Contains(const HBasicBlock& block) const; |
| 550 | |
| 551 | // Returns whether this loop information is an inner loop of `other`. |
| 552 | // Note that `other` *must* be populated before entering this function. |
| 553 | bool IsIn(const HLoopInformation& other) const; |
| 554 | |
| 555 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 556 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 557 | void Add(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 558 | void Remove(HBasicBlock* block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 559 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 560 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 561 | // Internal recursive implementation of `Populate`. |
| 562 | void PopulateRecursive(HBasicBlock* block); |
| 563 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 564 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 565 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 566 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 567 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 568 | |
| 569 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 570 | }; |
| 571 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 572 | // Stores try/catch information for basic blocks. |
| 573 | // Note that HGraph is constructed so that catch blocks cannot simultaneously |
| 574 | // be try blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 575 | class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 576 | public: |
| 577 | // Try block information constructor. |
| 578 | explicit TryCatchInformation(const HTryBoundary& try_entry) |
| 579 | : try_entry_(&try_entry), |
| 580 | catch_dex_file_(nullptr), |
| 581 | catch_type_index_(DexFile::kDexNoIndex16) { |
| 582 | DCHECK(try_entry_ != nullptr); |
| 583 | } |
| 584 | |
| 585 | // Catch block information constructor. |
| 586 | TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file) |
| 587 | : try_entry_(nullptr), |
| 588 | catch_dex_file_(&dex_file), |
| 589 | catch_type_index_(catch_type_index) {} |
| 590 | |
| 591 | bool IsTryBlock() const { return try_entry_ != nullptr; } |
| 592 | |
| 593 | const HTryBoundary& GetTryEntry() const { |
| 594 | DCHECK(IsTryBlock()); |
| 595 | return *try_entry_; |
| 596 | } |
| 597 | |
| 598 | bool IsCatchBlock() const { return catch_dex_file_ != nullptr; } |
| 599 | |
| 600 | bool IsCatchAllTypeIndex() const { |
| 601 | DCHECK(IsCatchBlock()); |
| 602 | return catch_type_index_ == DexFile::kDexNoIndex16; |
| 603 | } |
| 604 | |
| 605 | uint16_t GetCatchTypeIndex() const { |
| 606 | DCHECK(IsCatchBlock()); |
| 607 | return catch_type_index_; |
| 608 | } |
| 609 | |
| 610 | const DexFile& GetCatchDexFile() const { |
| 611 | DCHECK(IsCatchBlock()); |
| 612 | return *catch_dex_file_; |
| 613 | } |
| 614 | |
| 615 | private: |
| 616 | // One of possibly several TryBoundary instructions entering the block's try. |
| 617 | // Only set for try blocks. |
| 618 | const HTryBoundary* try_entry_; |
| 619 | |
| 620 | // Exception type information. Only set for catch blocks. |
| 621 | const DexFile* catch_dex_file_; |
| 622 | const uint16_t catch_type_index_; |
| 623 | }; |
| 624 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 625 | static constexpr size_t kNoLifetime = -1; |
| 626 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 627 | // A block in a method. Contains the list of instructions represented |
| 628 | // as a double linked list. Each block knows its predecessors and |
| 629 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 630 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 631 | class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 632 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 633 | HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 634 | : graph_(graph), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 635 | predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)), |
| 636 | successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 637 | loop_information_(nullptr), |
| 638 | dominator_(nullptr), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 639 | dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 640 | block_id_(-1), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 641 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 642 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 643 | lifetime_end_(kNoLifetime), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 644 | try_catch_information_(nullptr) { |
| 645 | predecessors_.reserve(kDefaultNumberOfPredecessors); |
| 646 | successors_.reserve(kDefaultNumberOfSuccessors); |
| 647 | dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks); |
| 648 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 649 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 650 | const ArenaVector<HBasicBlock*>& GetPredecessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 651 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 654 | HBasicBlock* GetPredecessor(size_t pred_idx) const { |
| 655 | DCHECK_LT(pred_idx, predecessors_.size()); |
| 656 | return predecessors_[pred_idx]; |
| 657 | } |
| 658 | |
| 659 | const ArenaVector<HBasicBlock*>& GetSuccessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 660 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 663 | HBasicBlock* GetSuccessor(size_t succ_idx) const { |
| 664 | DCHECK_LT(succ_idx, successors_.size()); |
| 665 | return successors_[succ_idx]; |
| 666 | } |
| 667 | |
| 668 | bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) { |
| 669 | return ContainsElement(successors_, block, start_from); |
| 670 | } |
| 671 | |
| 672 | const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 673 | return dominated_blocks_; |
| 674 | } |
| 675 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 676 | bool IsEntryBlock() const { |
| 677 | return graph_->GetEntryBlock() == this; |
| 678 | } |
| 679 | |
| 680 | bool IsExitBlock() const { |
| 681 | return graph_->GetExitBlock() == this; |
| 682 | } |
| 683 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 684 | bool IsSingleGoto() const; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 685 | bool IsSingleTryBoundary() const; |
| 686 | |
| 687 | // Returns true if this block emits nothing but a jump. |
| 688 | bool IsSingleJump() const { |
| 689 | HLoopInformation* loop_info = GetLoopInformation(); |
| 690 | return (IsSingleGoto() || IsSingleTryBoundary()) |
| 691 | // Back edges generate a suspend check. |
| 692 | && (loop_info == nullptr || !loop_info->IsBackEdge(*this)); |
| 693 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 694 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 695 | void AddBackEdge(HBasicBlock* back_edge) { |
| 696 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 697 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 698 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 699 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 700 | loop_information_->AddBackEdge(back_edge); |
| 701 | } |
| 702 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 703 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 704 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 705 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 706 | int GetBlockId() const { return block_id_; } |
| 707 | void SetBlockId(int id) { block_id_ = id; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 708 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 709 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 710 | HBasicBlock* GetDominator() const { return dominator_; } |
| 711 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 712 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); } |
| 713 | |
| 714 | void RemoveDominatedBlock(HBasicBlock* block) { |
| 715 | RemoveElement(dominated_blocks_, block); |
Vladimir Marko | 91e11c0 | 2015-09-02 17:03:22 +0100 | [diff] [blame] | 716 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 717 | |
| 718 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
| 719 | ReplaceElement(dominated_blocks_, existing, new_block); |
| 720 | } |
| 721 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 722 | void ClearDominanceInformation(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 723 | |
| 724 | int NumberOfBackEdges() const { |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 725 | return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 728 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 729 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 730 | const HInstructionList& GetInstructions() const { return instructions_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 731 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 732 | HInstruction* GetLastPhi() const { return phis_.last_instruction_; } |
| 733 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 734 | |
| 735 | void AddSuccessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 736 | successors_.push_back(block); |
| 737 | block->predecessors_.push_back(this); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 740 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 741 | size_t successor_index = GetSuccessorIndexOf(existing); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 742 | existing->RemovePredecessor(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 743 | new_block->predecessors_.push_back(this); |
| 744 | successors_[successor_index] = new_block; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 747 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 748 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 749 | existing->RemoveSuccessor(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 750 | new_block->successors_.push_back(this); |
| 751 | predecessors_[predecessor_index] = new_block; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 754 | // Insert `this` between `predecessor` and `successor. This method |
| 755 | // preserves the indicies, and will update the first edge found between |
| 756 | // `predecessor` and `successor`. |
| 757 | void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) { |
| 758 | size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 759 | size_t successor_index = predecessor->GetSuccessorIndexOf(successor); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 760 | successor->predecessors_[predecessor_index] = this; |
| 761 | predecessor->successors_[successor_index] = this; |
| 762 | successors_.push_back(successor); |
| 763 | predecessors_.push_back(predecessor); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 766 | void RemovePredecessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 767 | predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block)); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 770 | void RemoveSuccessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 771 | successors_.erase(successors_.begin() + GetSuccessorIndexOf(block)); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 774 | void ClearAllPredecessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 775 | predecessors_.clear(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void AddPredecessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 779 | predecessors_.push_back(block); |
| 780 | block->successors_.push_back(this); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 783 | void SwapPredecessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 784 | DCHECK_EQ(predecessors_.size(), 2u); |
| 785 | std::swap(predecessors_[0], predecessors_[1]); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 786 | } |
| 787 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 788 | void SwapSuccessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 789 | DCHECK_EQ(successors_.size(), 2u); |
| 790 | std::swap(successors_[0], successors_[1]); |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 791 | } |
| 792 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 793 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 794 | return IndexOfElement(predecessors_, predecessor); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 795 | } |
| 796 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 797 | size_t GetSuccessorIndexOf(HBasicBlock* successor) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 798 | return IndexOfElement(successors_, successor); |
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 | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 802 | DCHECK_EQ(GetPredecessors().size(), 1u); |
| 803 | return GetPredecessor(0); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | HBasicBlock* GetSingleSuccessor() const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 807 | DCHECK_EQ(GetSuccessors().size(), 1u); |
| 808 | return GetSuccessor(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 | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 814 | DCHECK_EQ(GetPredecessor(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 | 6058455 | 2015-09-03 13:35:12 +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 | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 886 | return GetPredecessor(0) == GetLoopInformation()->GetPreHeader(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 889 | HLoopInformation* GetLoopInformation() const { |
| 890 | return loop_information_; |
| 891 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 892 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 893 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 894 | // 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] | 895 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 896 | void SetInLoop(HLoopInformation* info) { |
| 897 | if (IsLoopHeader()) { |
| 898 | // Nothing to do. This just means `info` is an outer loop. |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 899 | } else if (!IsInLoop()) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 900 | loop_information_ = info; |
| 901 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 902 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 903 | // Note that a non loop header having a loop information means this loop information |
| 904 | // has already been populated |
| 905 | loop_information_ = info; |
| 906 | } else { |
| 907 | // Block is part of an inner loop. Do not update the loop information. |
| 908 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 909 | // at this point, because this method is being called while populating `info`. |
| 910 | } |
| 911 | } |
| 912 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 913 | // Raw update of the loop information. |
| 914 | void SetLoopInformation(HLoopInformation* info) { |
| 915 | loop_information_ = info; |
| 916 | } |
| 917 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 918 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 919 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 920 | TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; } |
| 921 | |
| 922 | void SetTryCatchInformation(TryCatchInformation* try_catch_information) { |
| 923 | try_catch_information_ = try_catch_information; |
| 924 | } |
| 925 | |
| 926 | bool IsTryBlock() const { |
| 927 | return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock(); |
| 928 | } |
| 929 | |
| 930 | bool IsCatchBlock() const { |
| 931 | return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock(); |
| 932 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 933 | |
| 934 | // Returns the try entry that this block's successors should have. They will |
| 935 | // be in the same try, unless the block ends in a try boundary. In that case, |
| 936 | // the appropriate try entry will be returned. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 937 | const HTryBoundary* ComputeTryEntryOfSuccessors() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 938 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 939 | // Returns whether this block dominates the blocked passed as parameter. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 940 | bool Dominates(HBasicBlock* block) const; |
| 941 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 942 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 943 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 944 | |
| 945 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 946 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 947 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 948 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 949 | bool EndsWithControlFlowInstruction() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 950 | bool EndsWithIf() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 951 | bool EndsWithTryBoundary() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 952 | bool HasSinglePhi() const; |
| 953 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 954 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 955 | HGraph* graph_; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 956 | ArenaVector<HBasicBlock*> predecessors_; |
| 957 | ArenaVector<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 958 | HInstructionList instructions_; |
| 959 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 960 | HLoopInformation* loop_information_; |
| 961 | HBasicBlock* dominator_; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 962 | ArenaVector<HBasicBlock*> dominated_blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 963 | int block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 964 | // The dex program counter of the first instruction of this block. |
| 965 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 966 | size_t lifetime_start_; |
| 967 | size_t lifetime_end_; |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 968 | TryCatchInformation* try_catch_information_; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 969 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 970 | friend class HGraph; |
| 971 | friend class HInstruction; |
| 972 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 973 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 974 | }; |
| 975 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 976 | // Iterates over the LoopInformation of all loops which contain 'block' |
| 977 | // from the innermost to the outermost. |
| 978 | class HLoopInformationOutwardIterator : public ValueObject { |
| 979 | public: |
| 980 | explicit HLoopInformationOutwardIterator(const HBasicBlock& block) |
| 981 | : current_(block.GetLoopInformation()) {} |
| 982 | |
| 983 | bool Done() const { return current_ == nullptr; } |
| 984 | |
| 985 | void Advance() { |
| 986 | DCHECK(!Done()); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 987 | current_ = current_->GetPreHeader()->GetLoopInformation(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | HLoopInformation* Current() const { |
| 991 | DCHECK(!Done()); |
| 992 | return current_; |
| 993 | } |
| 994 | |
| 995 | private: |
| 996 | HLoopInformation* current_; |
| 997 | |
| 998 | DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator); |
| 999 | }; |
| 1000 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1001 | #define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1002 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1003 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1004 | M(ArrayGet, Instruction) \ |
| 1005 | M(ArrayLength, Instruction) \ |
| 1006 | M(ArraySet, Instruction) \ |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 1007 | M(BooleanNot, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1008 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1009 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1010 | M(CheckCast, Instruction) \ |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 1011 | M(ClearException, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1012 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1013 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1014 | M(Condition, BinaryOperation) \ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1015 | M(CurrentMethod, Instruction) \ |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1016 | M(Deoptimize, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1017 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1018 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1019 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1020 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1021 | M(Exit, Instruction) \ |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1022 | M(FakeString, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1023 | M(FloatConstant, Constant) \ |
| 1024 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1025 | M(GreaterThan, Condition) \ |
| 1026 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1027 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1028 | M(InstanceFieldGet, Instruction) \ |
| 1029 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1030 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1031 | M(IntConstant, Constant) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 1032 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1033 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1034 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1035 | M(LessThan, Condition) \ |
| 1036 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1037 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1038 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1039 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1040 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1041 | M(Local, Instruction) \ |
| 1042 | M(LongConstant, Constant) \ |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 1043 | M(MemoryBarrier, Instruction) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 1044 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1045 | M(Mul, BinaryOperation) \ |
| 1046 | M(Neg, UnaryOperation) \ |
| 1047 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1048 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 1049 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1050 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1051 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1052 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1053 | M(Or, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1054 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1055 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1056 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1057 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1058 | M(Return, Instruction) \ |
| 1059 | M(ReturnVoid, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1060 | M(Shl, BinaryOperation) \ |
| 1061 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1062 | M(StaticFieldGet, Instruction) \ |
| 1063 | M(StaticFieldSet, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1064 | M(StoreLocal, Instruction) \ |
| 1065 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1066 | M(SuspendCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1067 | M(Temporary, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1068 | M(Throw, Instruction) \ |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1069 | M(TryBoundary, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 1070 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1071 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1072 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1073 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1074 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) |
| 1075 | |
| 1076 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) |
| 1077 | |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1078 | #define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) |
| 1079 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 1080 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1081 | M(X86ComputeBaseMethodAddress, Instruction) \ |
| 1082 | M(X86LoadFromConstantTable, Instruction) |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1083 | |
| 1084 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1085 | |
| 1086 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1087 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
| 1088 | FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \ |
| 1089 | FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \ |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1090 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \ |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1091 | FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1092 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1093 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1094 | #define FOR_EACH_INSTRUCTION(M) \ |
| 1095 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1096 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1097 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1098 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1099 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1100 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1101 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1102 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 1103 | #undef FORWARD_DECLARATION |
| 1104 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1105 | #define DECLARE_INSTRUCTION(type) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1106 | InstructionKind GetKind() const OVERRIDE { return k##type; } \ |
| 1107 | const char* DebugName() const OVERRIDE { return #type; } \ |
| 1108 | const H##type* As##type() const OVERRIDE { return this; } \ |
| 1109 | H##type* As##type() OVERRIDE { return this; } \ |
| 1110 | bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1111 | return other->Is##type(); \ |
| 1112 | } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1113 | void Accept(HGraphVisitor* visitor) OVERRIDE |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1114 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1115 | template <typename T> class HUseList; |
| 1116 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1117 | template <typename T> |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1118 | class HUseListNode : public ArenaObject<kArenaAllocUseListNode> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1119 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1120 | HUseListNode* GetPrevious() const { return prev_; } |
| 1121 | HUseListNode* GetNext() const { return next_; } |
| 1122 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1123 | size_t GetIndex() const { return index_; } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1124 | void SetIndex(size_t index) { index_ = index; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1125 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1126 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1127 | HUseListNode(T user, size_t index) |
| 1128 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 1129 | |
| 1130 | T const user_; |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1131 | size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1132 | HUseListNode<T>* prev_; |
| 1133 | HUseListNode<T>* next_; |
| 1134 | |
| 1135 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1136 | |
| 1137 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 1138 | }; |
| 1139 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1140 | template <typename T> |
| 1141 | class HUseList : public ValueObject { |
| 1142 | public: |
| 1143 | HUseList() : first_(nullptr) {} |
| 1144 | |
| 1145 | void Clear() { |
| 1146 | first_ = nullptr; |
| 1147 | } |
| 1148 | |
| 1149 | // Adds a new entry at the beginning of the use list and returns |
| 1150 | // the newly created node. |
| 1151 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 1152 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1153 | if (IsEmpty()) { |
| 1154 | first_ = new_node; |
| 1155 | } else { |
| 1156 | first_->prev_ = new_node; |
| 1157 | new_node->next_ = first_; |
| 1158 | first_ = new_node; |
| 1159 | } |
| 1160 | return new_node; |
| 1161 | } |
| 1162 | |
| 1163 | HUseListNode<T>* GetFirst() const { |
| 1164 | return first_; |
| 1165 | } |
| 1166 | |
| 1167 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1168 | DCHECK(node != nullptr); |
| 1169 | DCHECK(Contains(node)); |
| 1170 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1171 | if (node->prev_ != nullptr) { |
| 1172 | node->prev_->next_ = node->next_; |
| 1173 | } |
| 1174 | if (node->next_ != nullptr) { |
| 1175 | node->next_->prev_ = node->prev_; |
| 1176 | } |
| 1177 | if (node == first_) { |
| 1178 | first_ = node->next_; |
| 1179 | } |
| 1180 | } |
| 1181 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1182 | bool Contains(const HUseListNode<T>* node) const { |
| 1183 | if (node == nullptr) { |
| 1184 | return false; |
| 1185 | } |
| 1186 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1187 | if (current == node) { |
| 1188 | return true; |
| 1189 | } |
| 1190 | } |
| 1191 | return false; |
| 1192 | } |
| 1193 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1194 | bool IsEmpty() const { |
| 1195 | return first_ == nullptr; |
| 1196 | } |
| 1197 | |
| 1198 | bool HasOnlyOneUse() const { |
| 1199 | return first_ != nullptr && first_->next_ == nullptr; |
| 1200 | } |
| 1201 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1202 | size_t SizeSlow() const { |
| 1203 | size_t count = 0; |
| 1204 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1205 | ++count; |
| 1206 | } |
| 1207 | return count; |
| 1208 | } |
| 1209 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1210 | private: |
| 1211 | HUseListNode<T>* first_; |
| 1212 | }; |
| 1213 | |
| 1214 | template<typename T> |
| 1215 | class HUseIterator : public ValueObject { |
| 1216 | public: |
| 1217 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 1218 | |
| 1219 | bool Done() const { return current_ == nullptr; } |
| 1220 | |
| 1221 | void Advance() { |
| 1222 | DCHECK(!Done()); |
| 1223 | current_ = current_->GetNext(); |
| 1224 | } |
| 1225 | |
| 1226 | HUseListNode<T>* Current() const { |
| 1227 | DCHECK(!Done()); |
| 1228 | return current_; |
| 1229 | } |
| 1230 | |
| 1231 | private: |
| 1232 | HUseListNode<T>* current_; |
| 1233 | |
| 1234 | friend class HValue; |
| 1235 | }; |
| 1236 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1237 | // This class is used by HEnvironment and HInstruction classes to record the |
| 1238 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 1239 | // by the used instructions. |
| 1240 | template <typename T> |
| 1241 | class HUserRecord : public ValueObject { |
| 1242 | public: |
| 1243 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 1244 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 1245 | |
| 1246 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 1247 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 1248 | DCHECK(instruction_ != nullptr); |
| 1249 | DCHECK(use_node_ != nullptr); |
| 1250 | DCHECK(old_record.use_node_ == nullptr); |
| 1251 | } |
| 1252 | |
| 1253 | HInstruction* GetInstruction() const { return instruction_; } |
| 1254 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 1255 | |
| 1256 | private: |
| 1257 | // Instruction used by the user. |
| 1258 | HInstruction* instruction_; |
| 1259 | |
| 1260 | // Corresponding entry in the use list kept by 'instruction_'. |
| 1261 | HUseListNode<T>* use_node_; |
| 1262 | }; |
| 1263 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1264 | /** |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1265 | * Side-effects representation. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1266 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1267 | * For write/read dependences on fields/arrays, the dependence analysis uses |
| 1268 | * type disambiguation (e.g. a float field write cannot modify the value of an |
| 1269 | * integer field read) and the access type (e.g. a reference array write cannot |
| 1270 | * modify the value of a reference field read [although it may modify the |
| 1271 | * reference fetch prior to reading the field, which is represented by its own |
| 1272 | * write/read dependence]). The analysis makes conservative points-to |
| 1273 | * assumptions on reference types (e.g. two same typed arrays are assumed to be |
| 1274 | * the same, and any reference read depends on any reference read without |
| 1275 | * further regard of its type). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1276 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1277 | * The internal representation uses 38-bit and is described in the table below. |
| 1278 | * The first line indicates the side effect, and for field/array accesses the |
| 1279 | * second line indicates the type of the access (in the order of the |
| 1280 | * Primitive::Type enum). |
| 1281 | * The two numbered lines below indicate the bit position in the bitfield (read |
| 1282 | * vertically). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1283 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1284 | * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W | |
| 1285 | * +-------------+---------+---------+--------------+---------+---------+ |
| 1286 | * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL| |
| 1287 | * | 3 |333333322|222222221| 1 |111111110|000000000| |
| 1288 | * | 7 |654321098|765432109| 8 |765432109|876543210| |
| 1289 | * |
| 1290 | * Note that, to ease the implementation, 'changes' bits are least significant |
| 1291 | * bits, while 'dependency' bits are most significant bits. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1292 | */ |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1293 | class SideEffects : public ValueObject { |
| 1294 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1295 | SideEffects() : flags_(0) {} |
| 1296 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1297 | static SideEffects None() { |
| 1298 | return SideEffects(0); |
| 1299 | } |
| 1300 | |
| 1301 | static SideEffects All() { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1302 | return SideEffects(kAllChangeBits | kAllDependOnBits); |
| 1303 | } |
| 1304 | |
| 1305 | static SideEffects AllChanges() { |
| 1306 | return SideEffects(kAllChangeBits); |
| 1307 | } |
| 1308 | |
| 1309 | static SideEffects AllDependencies() { |
| 1310 | return SideEffects(kAllDependOnBits); |
| 1311 | } |
| 1312 | |
| 1313 | static SideEffects AllExceptGCDependency() { |
| 1314 | return AllWritesAndReads().Union(SideEffects::CanTriggerGC()); |
| 1315 | } |
| 1316 | |
| 1317 | static SideEffects AllWritesAndReads() { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1318 | return SideEffects(kAllWrites | kAllReads); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1321 | static SideEffects AllWrites() { |
| 1322 | return SideEffects(kAllWrites); |
| 1323 | } |
| 1324 | |
| 1325 | static SideEffects AllReads() { |
| 1326 | return SideEffects(kAllReads); |
| 1327 | } |
| 1328 | |
| 1329 | static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) { |
| 1330 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1331 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1332 | : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1335 | static SideEffects ArrayWriteOfType(Primitive::Type type) { |
| 1336 | return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1339 | static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) { |
| 1340 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1341 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1342 | : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset)); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static SideEffects ArrayReadOfType(Primitive::Type type) { |
| 1346 | return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset)); |
| 1347 | } |
| 1348 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1349 | static SideEffects CanTriggerGC() { |
| 1350 | return SideEffects(1ULL << kCanTriggerGCBit); |
| 1351 | } |
| 1352 | |
| 1353 | static SideEffects DependsOnGC() { |
| 1354 | return SideEffects(1ULL << kDependsOnGCBit); |
| 1355 | } |
| 1356 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1357 | // Combines the side-effects of this and the other. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1358 | SideEffects Union(SideEffects other) const { |
| 1359 | return SideEffects(flags_ | other.flags_); |
| 1360 | } |
| 1361 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1362 | SideEffects Exclusion(SideEffects other) const { |
| 1363 | return SideEffects(flags_ & ~other.flags_); |
| 1364 | } |
| 1365 | |
| 1366 | bool Includes(SideEffects other) const { |
| 1367 | return (other.flags_ & flags_) == other.flags_; |
| 1368 | } |
| 1369 | |
| 1370 | bool HasSideEffects() const { |
| 1371 | return (flags_ & kAllChangeBits); |
| 1372 | } |
| 1373 | |
| 1374 | bool HasDependencies() const { |
| 1375 | return (flags_ & kAllDependOnBits); |
| 1376 | } |
| 1377 | |
| 1378 | // Returns true if there are no side effects or dependencies. |
| 1379 | bool DoesNothing() const { |
| 1380 | return flags_ == 0; |
| 1381 | } |
| 1382 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1383 | // Returns true if something is written. |
| 1384 | bool DoesAnyWrite() const { |
| 1385 | return (flags_ & kAllWrites); |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1388 | // Returns true if something is read. |
| 1389 | bool DoesAnyRead() const { |
| 1390 | return (flags_ & kAllReads); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1393 | // Returns true if potentially everything is written and read |
| 1394 | // (every type and every kind of access). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1395 | bool DoesAllReadWrite() const { |
| 1396 | return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads); |
| 1397 | } |
| 1398 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1399 | bool DoesAll() const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1400 | return flags_ == (kAllChangeBits | kAllDependOnBits); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | // Returns true if this may read something written by other. |
| 1404 | bool MayDependOn(SideEffects other) const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1405 | const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits; |
| 1406 | return (other.flags_ & depends_on_flags); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | // Returns string representation of flags (for debugging only). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1410 | // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL| |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1411 | std::string ToString() const { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1412 | std::string flags = "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1413 | for (int s = kLastBit; s >= 0; s--) { |
| 1414 | bool current_bit_is_set = ((flags_ >> s) & 1) != 0; |
| 1415 | if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) { |
| 1416 | // This is a bit for the GC side effect. |
| 1417 | if (current_bit_is_set) { |
| 1418 | flags += "GC"; |
| 1419 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1420 | flags += "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1421 | } else { |
| 1422 | // This is a bit for the array/field analysis. |
| 1423 | // The underscore character stands for the 'can trigger GC' bit. |
| 1424 | static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD"; |
| 1425 | if (current_bit_is_set) { |
| 1426 | flags += kDebug[s]; |
| 1427 | } |
| 1428 | if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) || |
| 1429 | (s == kFieldReadOffset) || (s == kArrayReadOffset)) { |
| 1430 | flags += "|"; |
| 1431 | } |
| 1432 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1433 | } |
| 1434 | return flags; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1437 | bool Equals(const SideEffects& other) const { return flags_ == other.flags_; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1438 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1439 | private: |
| 1440 | static constexpr int kFieldArrayAnalysisBits = 9; |
| 1441 | |
| 1442 | static constexpr int kFieldWriteOffset = 0; |
| 1443 | static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits; |
| 1444 | static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1; |
| 1445 | static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1; |
| 1446 | |
| 1447 | static constexpr int kChangeBits = kCanTriggerGCBit + 1; |
| 1448 | |
| 1449 | static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1; |
| 1450 | static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits; |
| 1451 | static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1; |
| 1452 | static constexpr int kDependsOnGCBit = kLastBitForReads + 1; |
| 1453 | |
| 1454 | static constexpr int kLastBit = kDependsOnGCBit; |
| 1455 | static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits; |
| 1456 | |
| 1457 | // Aliases. |
| 1458 | |
| 1459 | static_assert(kChangeBits == kDependOnBits, |
| 1460 | "the 'change' bits should match the 'depend on' bits."); |
| 1461 | |
| 1462 | static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1); |
| 1463 | static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits; |
| 1464 | static constexpr uint64_t kAllWrites = |
| 1465 | ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset; |
| 1466 | static constexpr uint64_t kAllReads = |
| 1467 | ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1468 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1469 | // Work around the fact that HIR aliases I/F and J/D. |
| 1470 | // TODO: remove this interceptor once HIR types are clean |
| 1471 | static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) { |
| 1472 | switch (type) { |
| 1473 | case Primitive::kPrimInt: |
| 1474 | case Primitive::kPrimFloat: |
| 1475 | return TypeFlag(Primitive::kPrimInt, offset) | |
| 1476 | TypeFlag(Primitive::kPrimFloat, offset); |
| 1477 | case Primitive::kPrimLong: |
| 1478 | case Primitive::kPrimDouble: |
| 1479 | return TypeFlag(Primitive::kPrimLong, offset) | |
| 1480 | TypeFlag(Primitive::kPrimDouble, offset); |
| 1481 | default: |
| 1482 | return TypeFlag(type, offset); |
| 1483 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1486 | // Translates type to bit flag. |
| 1487 | static uint64_t TypeFlag(Primitive::Type type, int offset) { |
| 1488 | CHECK_NE(type, Primitive::kPrimVoid); |
| 1489 | const uint64_t one = 1; |
| 1490 | const int shift = type; // 0-based consecutive enum |
| 1491 | DCHECK_LE(kFieldWriteOffset, shift); |
| 1492 | DCHECK_LT(shift, kArrayWriteOffset); |
| 1493 | return one << (type + offset); |
| 1494 | } |
| 1495 | |
| 1496 | // Private constructor on direct flags value. |
| 1497 | explicit SideEffects(uint64_t flags) : flags_(flags) {} |
| 1498 | |
| 1499 | uint64_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1500 | }; |
| 1501 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1502 | // 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] | 1503 | class HEnvironment : public ArenaObject<kArenaAllocEnvironment> { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1504 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1505 | HEnvironment(ArenaAllocator* arena, |
| 1506 | size_t number_of_vregs, |
| 1507 | const DexFile& dex_file, |
| 1508 | uint32_t method_idx, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1509 | uint32_t dex_pc, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1510 | InvokeType invoke_type, |
| 1511 | HInstruction* holder) |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1512 | : vregs_(arena, number_of_vregs), |
| 1513 | locations_(arena, number_of_vregs), |
| 1514 | parent_(nullptr), |
| 1515 | dex_file_(dex_file), |
| 1516 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1517 | dex_pc_(dex_pc), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1518 | invoke_type_(invoke_type), |
| 1519 | holder_(holder) { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1520 | vregs_.SetSize(number_of_vregs); |
| 1521 | for (size_t i = 0; i < number_of_vregs; i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1522 | vregs_.Put(i, HUserRecord<HEnvironment*>()); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1523 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1524 | |
| 1525 | locations_.SetSize(number_of_vregs); |
| 1526 | for (size_t i = 0; i < number_of_vregs; ++i) { |
| 1527 | locations_.Put(i, Location()); |
| 1528 | } |
| 1529 | } |
| 1530 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1531 | HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1532 | : HEnvironment(arena, |
| 1533 | to_copy.Size(), |
| 1534 | to_copy.GetDexFile(), |
| 1535 | to_copy.GetMethodIdx(), |
| 1536 | to_copy.GetDexPc(), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1537 | to_copy.GetInvokeType(), |
| 1538 | holder) {} |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1539 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1540 | void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1541 | if (parent_ != nullptr) { |
| 1542 | parent_->SetAndCopyParentChain(allocator, parent); |
| 1543 | } else { |
| 1544 | parent_ = new (allocator) HEnvironment(allocator, *parent, holder_); |
| 1545 | parent_->CopyFrom(parent); |
| 1546 | if (parent->GetParent() != nullptr) { |
| 1547 | parent_->SetAndCopyParentChain(allocator, parent->GetParent()); |
| 1548 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1549 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 1552 | void CopyFrom(const GrowableArray<HInstruction*>& locals); |
| 1553 | void CopyFrom(HEnvironment* environment); |
| 1554 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1555 | // Copy from `env`. If it's a loop phi for `loop_header`, copy the first |
| 1556 | // input to the loop phi instead. This is for inserting instructions that |
| 1557 | // require an environment (like HDeoptimization) in the loop pre-header. |
| 1558 | void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1559 | |
| 1560 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1561 | vregs_.Put(index, HUserRecord<HEnvironment*>(instruction)); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | HInstruction* GetInstructionAt(size_t index) const { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1565 | return vregs_.Get(index).GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1568 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1569 | |
| 1570 | size_t Size() const { return vregs_.Size(); } |
| 1571 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1572 | HEnvironment* GetParent() const { return parent_; } |
| 1573 | |
| 1574 | void SetLocationAt(size_t index, Location location) { |
| 1575 | locations_.Put(index, location); |
| 1576 | } |
| 1577 | |
| 1578 | Location GetLocationAt(size_t index) const { |
| 1579 | return locations_.Get(index); |
| 1580 | } |
| 1581 | |
| 1582 | uint32_t GetDexPc() const { |
| 1583 | return dex_pc_; |
| 1584 | } |
| 1585 | |
| 1586 | uint32_t GetMethodIdx() const { |
| 1587 | return method_idx_; |
| 1588 | } |
| 1589 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1590 | InvokeType GetInvokeType() const { |
| 1591 | return invoke_type_; |
| 1592 | } |
| 1593 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1594 | const DexFile& GetDexFile() const { |
| 1595 | return dex_file_; |
| 1596 | } |
| 1597 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1598 | HInstruction* GetHolder() const { |
| 1599 | return holder_; |
| 1600 | } |
| 1601 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1602 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1603 | // Record instructions' use entries of this environment for constant-time removal. |
| 1604 | // It should only be called by HInstruction when a new environment use is added. |
| 1605 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 1606 | DCHECK(env_use->GetUser() == this); |
| 1607 | size_t index = env_use->GetIndex(); |
| 1608 | vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use)); |
| 1609 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1610 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1611 | GrowableArray<HUserRecord<HEnvironment*> > vregs_; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1612 | GrowableArray<Location> locations_; |
| 1613 | HEnvironment* parent_; |
| 1614 | const DexFile& dex_file_; |
| 1615 | const uint32_t method_idx_; |
| 1616 | const uint32_t dex_pc_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1617 | const InvokeType invoke_type_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1618 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1619 | // The instruction that holds this environment. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1620 | HInstruction* const holder_; |
| 1621 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1622 | friend class HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1623 | |
| 1624 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 1625 | }; |
| 1626 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1627 | class ReferenceTypeInfo : ValueObject { |
| 1628 | public: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1629 | typedef Handle<mirror::Class> TypeHandle; |
| 1630 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1631 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) { |
| 1632 | // The constructor will check that the type_handle is valid. |
| 1633 | return ReferenceTypeInfo(type_handle, is_exact); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1636 | static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); } |
| 1637 | |
| 1638 | static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1639 | return handle.GetReference() != nullptr; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1642 | bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1643 | return IsValidHandle(type_handle_); |
| 1644 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1645 | bool IsExact() const { return is_exact_; } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1646 | |
| 1647 | bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1648 | DCHECK(IsValid()); |
| 1649 | return GetTypeHandle()->IsObjectClass(); |
| 1650 | } |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1651 | bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1652 | DCHECK(IsValid()); |
| 1653 | return GetTypeHandle()->IsInterface(); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 1654 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1655 | |
| 1656 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 1657 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1658 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1659 | DCHECK(IsValid()); |
| 1660 | DCHECK(rti.IsValid()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1661 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 1662 | } |
| 1663 | |
| 1664 | // Returns true if the type information provide the same amount of details. |
| 1665 | // 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] | 1666 | // (because the type can be the result of a merge). |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1667 | bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1668 | if (!IsValid() && !rti.IsValid()) { |
| 1669 | // Invalid types are equal. |
Calin Juravle | 7733bd6 | 2015-07-22 17:14:50 +0000 | [diff] [blame] | 1670 | return true; |
| 1671 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1672 | if (!IsValid() || !rti.IsValid()) { |
| 1673 | // One is valid, the other not. |
Calin Juravle | 7733bd6 | 2015-07-22 17:14:50 +0000 | [diff] [blame] | 1674 | return false; |
| 1675 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1676 | return IsExact() == rti.IsExact() |
| 1677 | && GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | private: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1681 | ReferenceTypeInfo(); |
| 1682 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1683 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1684 | // The class of the object. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1685 | TypeHandle type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1686 | // 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] | 1687 | // Whether or not we have any information about this type. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1688 | bool is_exact_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 1692 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1693 | class HInstruction : public ArenaObject<kArenaAllocInstruction> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1694 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1695 | HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1696 | : previous_(nullptr), |
| 1697 | next_(nullptr), |
| 1698 | block_(nullptr), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1699 | dex_pc_(dex_pc), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1700 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1701 | ssa_index_(-1), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1702 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1703 | locations_(nullptr), |
| 1704 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1705 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1706 | side_effects_(side_effects), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1707 | reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1708 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1709 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1710 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1711 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1712 | enum InstructionKind { |
| 1713 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1714 | }; |
| 1715 | #undef DECLARE_KIND |
| 1716 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1717 | HInstruction* GetNext() const { return next_; } |
| 1718 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1719 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1720 | HInstruction* GetNextDisregardingMoves() const; |
| 1721 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1722 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1723 | HBasicBlock* GetBlock() const { return block_; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1724 | ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1725 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1726 | bool IsInBlock() const { return block_ != nullptr; } |
| 1727 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 1728 | bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1729 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1730 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1731 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1732 | |
| 1733 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1734 | virtual const char* DebugName() const = 0; |
| 1735 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1736 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1737 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1738 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1739 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1740 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1741 | virtual bool NeedsEnvironment() const { return false; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1742 | |
| 1743 | uint32_t GetDexPc() const { return dex_pc_; } |
| 1744 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1745 | virtual bool IsControlFlow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1746 | |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1747 | virtual bool CanThrow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1748 | bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1749 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1750 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1751 | bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1752 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1753 | // Does not apply for all instructions, but having this at top level greatly |
| 1754 | // simplifies the null check elimination. |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 1755 | // TODO: Consider merging can_be_null into ReferenceTypeInfo. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1756 | virtual bool CanBeNull() const { |
| 1757 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1758 | return true; |
| 1759 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1760 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1761 | virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const { |
| 1762 | UNUSED(obj); |
| 1763 | return false; |
| 1764 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1765 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1766 | void SetReferenceTypeInfo(ReferenceTypeInfo rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1767 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1768 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1769 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1770 | return reference_type_info_; |
| 1771 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1772 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1773 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1774 | DCHECK(user != nullptr); |
| 1775 | HUseListNode<HInstruction*>* use = |
| 1776 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1777 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1780 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1781 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1782 | HUseListNode<HEnvironment*>* env_use = |
| 1783 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1784 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1785 | } |
| 1786 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1787 | void RemoveAsUserOfInput(size_t input) { |
| 1788 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1789 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1790 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1791 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1792 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1793 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1794 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1795 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1796 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1797 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1798 | bool HasOnlyOneNonEnvironmentUse() const { |
| 1799 | return !HasEnvironmentUses() && GetUses().HasOnlyOneUse(); |
| 1800 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1801 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1802 | // Does this instruction strictly dominate `other_instruction`? |
| 1803 | // Returns false if this instruction and `other_instruction` are the same. |
| 1804 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1805 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1806 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1807 | int GetId() const { return id_; } |
| 1808 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1809 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1810 | int GetSsaIndex() const { return ssa_index_; } |
| 1811 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1812 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1813 | |
| 1814 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1815 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1816 | // Set the `environment_` field. Raw because this method does not |
| 1817 | // update the uses lists. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1818 | void SetRawEnvironment(HEnvironment* environment) { |
| 1819 | DCHECK(environment_ == nullptr); |
| 1820 | DCHECK_EQ(environment->GetHolder(), this); |
| 1821 | environment_ = environment; |
| 1822 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1823 | |
| 1824 | // Set the environment of this instruction, copying it from `environment`. While |
| 1825 | // copying, the uses lists are being updated. |
| 1826 | void CopyEnvironmentFrom(HEnvironment* environment) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1827 | DCHECK(environment_ == nullptr); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1828 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1829 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1830 | environment_->CopyFrom(environment); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1831 | if (environment->GetParent() != nullptr) { |
| 1832 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1833 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1834 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1835 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1836 | void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment, |
| 1837 | HBasicBlock* block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1838 | DCHECK(environment_ == nullptr); |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1839 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1840 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1841 | environment_->CopyFromWithLoopPhiAdjustment(environment, block); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1842 | if (environment->GetParent() != nullptr) { |
| 1843 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1844 | } |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1847 | // Returns the number of entries in the environment. Typically, that is the |
| 1848 | // number of dex registers in a method. It could be more in case of inlining. |
| 1849 | size_t EnvironmentSize() const; |
| 1850 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1851 | LocationSummary* GetLocations() const { return locations_; } |
| 1852 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1853 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1854 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1855 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1856 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1857 | // This is almost the same as doing `ReplaceWith()`. But in this helper, the |
| 1858 | // uses of this instruction by `other` are *not* updated. |
| 1859 | void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) { |
| 1860 | ReplaceWith(other); |
| 1861 | other->ReplaceInput(this, use_index); |
| 1862 | } |
| 1863 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1864 | // Move `this` instruction before `cursor`. |
| 1865 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1866 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1867 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1868 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1869 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1870 | virtual H##type* As##type() { return nullptr; } |
| 1871 | |
| 1872 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1873 | #undef INSTRUCTION_TYPE_CHECK |
| 1874 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1875 | // Returns whether the instruction can be moved within the graph. |
| 1876 | virtual bool CanBeMoved() const { return false; } |
| 1877 | |
| 1878 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1879 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1880 | UNUSED(other); |
| 1881 | return false; |
| 1882 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1883 | |
| 1884 | // Returns whether any data encoded in the two instructions is equal. |
| 1885 | // This method does not look at the inputs. Both instructions must be |
| 1886 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1887 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1888 | UNUSED(other); |
| 1889 | return false; |
| 1890 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1891 | |
| 1892 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1893 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1894 | // 2) Their inputs are identical. |
| 1895 | bool Equals(HInstruction* other) const; |
| 1896 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1897 | virtual InstructionKind GetKind() const = 0; |
| 1898 | |
| 1899 | virtual size_t ComputeHashCode() const { |
| 1900 | size_t result = GetKind(); |
| 1901 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1902 | result = (result * 31) + InputAt(i)->GetId(); |
| 1903 | } |
| 1904 | return result; |
| 1905 | } |
| 1906 | |
| 1907 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1908 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1909 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1910 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1911 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1912 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1913 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1914 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1915 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1916 | |
| 1917 | // Returns whether the code generation of the instruction will require to have access |
| 1918 | // to the current method. Such instructions are: |
| 1919 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1920 | // to walk the stack and have the current method stored at a specific stack address. |
| 1921 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1922 | // fields of the current method. |
| 1923 | bool NeedsCurrentMethod() const { |
| 1924 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1925 | } |
| 1926 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1927 | virtual bool NeedsDexCache() const { return false; } |
| 1928 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1929 | // Does this instruction have any use in an environment before |
| 1930 | // control flow hits 'other'? |
| 1931 | bool HasAnyEnvironmentUseBefore(HInstruction* other); |
| 1932 | |
| 1933 | // Remove all references to environment uses of this instruction. |
| 1934 | // The caller must ensure that this is safe to do. |
| 1935 | void RemoveEnvironmentUsers(); |
| 1936 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1937 | protected: |
| 1938 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1939 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1940 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1941 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1942 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1943 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1944 | HInstruction* previous_; |
| 1945 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1946 | HBasicBlock* block_; |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1947 | const uint32_t dex_pc_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1948 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1949 | // An instruction gets an id when it is added to the graph. |
| 1950 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1951 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1952 | int id_; |
| 1953 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1954 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1955 | int ssa_index_; |
| 1956 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1957 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1958 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1959 | |
| 1960 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1961 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1962 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1963 | // The environment associated with this instruction. Not null if the instruction |
| 1964 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1965 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1966 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1967 | // Set by the code generator. |
| 1968 | LocationSummary* locations_; |
| 1969 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1970 | // Set by the liveness analysis. |
| 1971 | LiveInterval* live_interval_; |
| 1972 | |
| 1973 | // Set by the liveness analysis, this is the position in a linear |
| 1974 | // order of blocks where this instruction's live interval start. |
| 1975 | size_t lifetime_position_; |
| 1976 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1977 | const SideEffects side_effects_; |
| 1978 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1979 | // TODO: for primitive types this should be marked as invalid. |
| 1980 | ReferenceTypeInfo reference_type_info_; |
| 1981 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1982 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1983 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1984 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1985 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1986 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1987 | |
| 1988 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1989 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1990 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1991 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1992 | class HInputIterator : public ValueObject { |
| 1993 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1994 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1995 | |
| 1996 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1997 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1998 | void Advance() { index_++; } |
| 1999 | |
| 2000 | private: |
| 2001 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2002 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2003 | |
| 2004 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 2005 | }; |
| 2006 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2007 | class HInstructionIterator : public ValueObject { |
| 2008 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2009 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 2010 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2011 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2014 | bool Done() const { return instruction_ == nullptr; } |
| 2015 | HInstruction* Current() const { return instruction_; } |
| 2016 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2017 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2018 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | private: |
| 2022 | HInstruction* instruction_; |
| 2023 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2024 | |
| 2025 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2026 | }; |
| 2027 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2028 | class HBackwardInstructionIterator : public ValueObject { |
| 2029 | public: |
| 2030 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 2031 | : instruction_(instructions.last_instruction_) { |
| 2032 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2033 | } |
| 2034 | |
| 2035 | bool Done() const { return instruction_ == nullptr; } |
| 2036 | HInstruction* Current() const { return instruction_; } |
| 2037 | void Advance() { |
| 2038 | instruction_ = next_; |
| 2039 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2040 | } |
| 2041 | |
| 2042 | private: |
| 2043 | HInstruction* instruction_; |
| 2044 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2045 | |
| 2046 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2047 | }; |
| 2048 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2049 | template<size_t N> |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2050 | class HTemplateInstruction: public HInstruction { |
| 2051 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2052 | HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2053 | : HInstruction(side_effects, dex_pc), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2054 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2055 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2056 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2057 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2058 | protected: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2059 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 2060 | DCHECK_LT(i, N); |
| 2061 | return inputs_[i]; |
| 2062 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2063 | |
| 2064 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2065 | DCHECK_LT(i, N); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2066 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2069 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2070 | std::array<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2073 | }; |
| 2074 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2075 | // HTemplateInstruction specialization for N=0. |
| 2076 | template<> |
| 2077 | class HTemplateInstruction<0>: public HInstruction { |
| 2078 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2079 | explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2080 | : HInstruction(side_effects, dex_pc) {} |
| 2081 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2082 | virtual ~HTemplateInstruction() {} |
| 2083 | |
| 2084 | size_t InputCount() const OVERRIDE { return 0; } |
| 2085 | |
| 2086 | protected: |
| 2087 | const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2088 | LOG(FATAL) << "Unreachable"; |
| 2089 | UNREACHABLE(); |
| 2090 | } |
| 2091 | |
| 2092 | void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED, |
| 2093 | const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE { |
| 2094 | LOG(FATAL) << "Unreachable"; |
| 2095 | UNREACHABLE(); |
| 2096 | } |
| 2097 | |
| 2098 | private: |
| 2099 | friend class SsaBuilder; |
| 2100 | }; |
| 2101 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2102 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2103 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2104 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2105 | HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc) |
| 2106 | : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2107 | virtual ~HExpression() {} |
| 2108 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2109 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2110 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2111 | protected: |
| 2112 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2113 | }; |
| 2114 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2115 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 2116 | // instruction that branches to the exit block. |
| 2117 | class HReturnVoid : public HTemplateInstruction<0> { |
| 2118 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2119 | explicit HReturnVoid(uint32_t dex_pc = kNoDexPc) |
| 2120 | : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2121 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2122 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2123 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2124 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2125 | |
| 2126 | private: |
| 2127 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 2128 | }; |
| 2129 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2130 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 2131 | // instruction that branches to the exit block. |
| 2132 | class HReturn : public HTemplateInstruction<1> { |
| 2133 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2134 | explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 2135 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2136 | SetRawInputAt(0, value); |
| 2137 | } |
| 2138 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2139 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2140 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2141 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2142 | |
| 2143 | private: |
| 2144 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 2145 | }; |
| 2146 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2147 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2148 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2149 | // exit block. |
| 2150 | class HExit : public HTemplateInstruction<0> { |
| 2151 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2152 | explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2153 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2154 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2155 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2156 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2157 | |
| 2158 | private: |
| 2159 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 2160 | }; |
| 2161 | |
| 2162 | // Jumps from one block to another. |
| 2163 | class HGoto : public HTemplateInstruction<0> { |
| 2164 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2165 | explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2166 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2167 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2168 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2169 | HBasicBlock* GetSuccessor() const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2170 | return GetBlock()->GetSingleSuccessor(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2173 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2174 | |
| 2175 | private: |
| 2176 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 2177 | }; |
| 2178 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2179 | class HConstant : public HExpression<0> { |
| 2180 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2181 | explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2182 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2183 | |
| 2184 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2185 | |
| 2186 | virtual bool IsMinusOne() const { return false; } |
| 2187 | virtual bool IsZero() const { return false; } |
| 2188 | virtual bool IsOne() const { return false; } |
| 2189 | |
| 2190 | DECLARE_INSTRUCTION(Constant); |
| 2191 | |
| 2192 | private: |
| 2193 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 2194 | }; |
| 2195 | |
| 2196 | class HNullConstant : public HConstant { |
| 2197 | public: |
| 2198 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2199 | return true; |
| 2200 | } |
| 2201 | |
| 2202 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 2203 | |
| 2204 | DECLARE_INSTRUCTION(NullConstant); |
| 2205 | |
| 2206 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2207 | explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2208 | |
| 2209 | friend class HGraph; |
| 2210 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 2211 | }; |
| 2212 | |
| 2213 | // Constants of the type int. Those can be from Dex instructions, or |
| 2214 | // synthesized (for example with the if-eqz instruction). |
| 2215 | class HIntConstant : public HConstant { |
| 2216 | public: |
| 2217 | int32_t GetValue() const { return value_; } |
| 2218 | |
| 2219 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2220 | DCHECK(other->IsIntConstant()); |
| 2221 | return other->AsIntConstant()->value_ == value_; |
| 2222 | } |
| 2223 | |
| 2224 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 2225 | |
| 2226 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2227 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2228 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2229 | |
| 2230 | DECLARE_INSTRUCTION(IntConstant); |
| 2231 | |
| 2232 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2233 | explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2234 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {} |
| 2235 | explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc) |
| 2236 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2237 | |
| 2238 | const int32_t value_; |
| 2239 | |
| 2240 | friend class HGraph; |
| 2241 | ART_FRIEND_TEST(GraphTest, InsertInstructionBefore); |
| 2242 | ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast); |
| 2243 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 2244 | }; |
| 2245 | |
| 2246 | class HLongConstant : public HConstant { |
| 2247 | public: |
| 2248 | int64_t GetValue() const { return value_; } |
| 2249 | |
| 2250 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2251 | DCHECK(other->IsLongConstant()); |
| 2252 | return other->AsLongConstant()->value_ == value_; |
| 2253 | } |
| 2254 | |
| 2255 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
| 2256 | |
| 2257 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2258 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2259 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2260 | |
| 2261 | DECLARE_INSTRUCTION(LongConstant); |
| 2262 | |
| 2263 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2264 | explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2265 | : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2266 | |
| 2267 | const int64_t value_; |
| 2268 | |
| 2269 | friend class HGraph; |
| 2270 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 2271 | }; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2272 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2273 | // Conditional branch. A block ending with an HIf instruction must have |
| 2274 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2275 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2276 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2277 | explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2278 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2279 | SetRawInputAt(0, input); |
| 2280 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2281 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2282 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2283 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2284 | HBasicBlock* IfTrueSuccessor() const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2285 | return GetBlock()->GetSuccessor(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | HBasicBlock* IfFalseSuccessor() const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2289 | return GetBlock()->GetSuccessor(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2292 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2293 | |
| 2294 | private: |
| 2295 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 2296 | }; |
| 2297 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2298 | |
| 2299 | // Abstract instruction which marks the beginning and/or end of a try block and |
| 2300 | // links it to the respective exception handlers. Behaves the same as a Goto in |
| 2301 | // non-exceptional control flow. |
| 2302 | // Normal-flow successor is stored at index zero, exception handlers under |
| 2303 | // higher indices in no particular order. |
| 2304 | class HTryBoundary : public HTemplateInstruction<0> { |
| 2305 | public: |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2306 | enum BoundaryKind { |
| 2307 | kEntry, |
| 2308 | kExit, |
| 2309 | }; |
| 2310 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2311 | explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc) |
| 2312 | : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {} |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2313 | |
| 2314 | bool IsControlFlow() const OVERRIDE { return true; } |
| 2315 | |
| 2316 | // Returns the block's non-exceptional successor (index zero). |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2317 | HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2318 | |
| 2319 | // Returns whether `handler` is among its exception handlers (non-zero index |
| 2320 | // successors). |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2321 | bool HasExceptionHandler(const HBasicBlock& handler) const { |
| 2322 | DCHECK(handler.IsCatchBlock()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2323 | return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | // If not present already, adds `handler` to its block's list of exception |
| 2327 | // handlers. |
| 2328 | void AddExceptionHandler(HBasicBlock* handler) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2329 | if (!HasExceptionHandler(*handler)) { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2330 | GetBlock()->AddSuccessor(handler); |
| 2331 | } |
| 2332 | } |
| 2333 | |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2334 | bool IsEntry() const { return kind_ == BoundaryKind::kEntry; } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2335 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2336 | bool HasSameExceptionHandlersAs(const HTryBoundary& other) const; |
| 2337 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2338 | DECLARE_INSTRUCTION(TryBoundary); |
| 2339 | |
| 2340 | private: |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2341 | const BoundaryKind kind_; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2342 | |
| 2343 | DISALLOW_COPY_AND_ASSIGN(HTryBoundary); |
| 2344 | }; |
| 2345 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2346 | // Iterator over exception handlers of a given HTryBoundary, i.e. over |
| 2347 | // exceptional successors of its basic block. |
| 2348 | class HExceptionHandlerIterator : public ValueObject { |
| 2349 | public: |
| 2350 | explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary) |
| 2351 | : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {} |
| 2352 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2353 | bool Done() const { return index_ == block_.GetSuccessors().size(); } |
| 2354 | HBasicBlock* Current() const { return block_.GetSuccessor(index_); } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2355 | size_t CurrentSuccessorIndex() const { return index_; } |
| 2356 | void Advance() { ++index_; } |
| 2357 | |
| 2358 | private: |
| 2359 | const HBasicBlock& block_; |
| 2360 | size_t index_; |
| 2361 | |
| 2362 | DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator); |
| 2363 | }; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2364 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2365 | // Deoptimize to interpreter, upon checking a condition. |
| 2366 | class HDeoptimize : public HTemplateInstruction<1> { |
| 2367 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2368 | explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc) |
| 2369 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2370 | SetRawInputAt(0, cond); |
| 2371 | } |
| 2372 | |
| 2373 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2374 | bool CanThrow() const OVERRIDE { return true; } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2375 | |
| 2376 | DECLARE_INSTRUCTION(Deoptimize); |
| 2377 | |
| 2378 | private: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2379 | DISALLOW_COPY_AND_ASSIGN(HDeoptimize); |
| 2380 | }; |
| 2381 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2382 | // Represents the ArtMethod that was passed as a first argument to |
| 2383 | // the method. It is used by instructions that depend on it, like |
| 2384 | // instructions that work with the dex cache. |
| 2385 | class HCurrentMethod : public HExpression<0> { |
| 2386 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2387 | explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2388 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2389 | |
| 2390 | DECLARE_INSTRUCTION(CurrentMethod); |
| 2391 | |
| 2392 | private: |
| 2393 | DISALLOW_COPY_AND_ASSIGN(HCurrentMethod); |
| 2394 | }; |
| 2395 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2396 | class HUnaryOperation : public HExpression<1> { |
| 2397 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2398 | HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2399 | : HExpression(result_type, SideEffects::None(), dex_pc) { |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2400 | SetRawInputAt(0, input); |
| 2401 | } |
| 2402 | |
| 2403 | HInstruction* GetInput() const { return InputAt(0); } |
| 2404 | Primitive::Type GetResultType() const { return GetType(); } |
| 2405 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2406 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2407 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2408 | UNUSED(other); |
| 2409 | return true; |
| 2410 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2411 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2412 | // Try to statically evaluate `operation` and return a HConstant |
| 2413 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2414 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2415 | HConstant* TryStaticEvaluation() const; |
| 2416 | |
| 2417 | // Apply this operation to `x`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2418 | virtual HConstant* Evaluate(HIntConstant* x) const = 0; |
| 2419 | virtual HConstant* Evaluate(HLongConstant* x) const = 0; |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2420 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2421 | DECLARE_INSTRUCTION(UnaryOperation); |
| 2422 | |
| 2423 | private: |
| 2424 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 2425 | }; |
| 2426 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2427 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2428 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2429 | HBinaryOperation(Primitive::Type result_type, |
| 2430 | HInstruction* left, |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 2431 | HInstruction* right, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2432 | SideEffects side_effects = SideEffects::None(), |
| 2433 | uint32_t dex_pc = kNoDexPc) |
| 2434 | : HExpression(result_type, side_effects, dex_pc) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2435 | SetRawInputAt(0, left); |
| 2436 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2439 | HInstruction* GetLeft() const { return InputAt(0); } |
| 2440 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2441 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2442 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2443 | virtual bool IsCommutative() const { return false; } |
| 2444 | |
| 2445 | // Put constant on the right. |
| 2446 | // Returns whether order is changed. |
| 2447 | bool OrderInputsWithConstantOnTheRight() { |
| 2448 | HInstruction* left = InputAt(0); |
| 2449 | HInstruction* right = InputAt(1); |
| 2450 | if (left->IsConstant() && !right->IsConstant()) { |
| 2451 | ReplaceInput(right, 0); |
| 2452 | ReplaceInput(left, 1); |
| 2453 | return true; |
| 2454 | } |
| 2455 | return false; |
| 2456 | } |
| 2457 | |
| 2458 | // Order inputs by instruction id, but favor constant on the right side. |
| 2459 | // This helps GVN for commutative ops. |
| 2460 | void OrderInputs() { |
| 2461 | DCHECK(IsCommutative()); |
| 2462 | HInstruction* left = InputAt(0); |
| 2463 | HInstruction* right = InputAt(1); |
| 2464 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 2465 | return; |
| 2466 | } |
| 2467 | if (OrderInputsWithConstantOnTheRight()) { |
| 2468 | return; |
| 2469 | } |
| 2470 | // Order according to instruction id. |
| 2471 | if (left->GetId() > right->GetId()) { |
| 2472 | ReplaceInput(right, 0); |
| 2473 | ReplaceInput(left, 1); |
| 2474 | } |
| 2475 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2476 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2477 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2478 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2479 | UNUSED(other); |
| 2480 | return true; |
| 2481 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2482 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2483 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2484 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2485 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2486 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2487 | |
| 2488 | // Apply this operation to `x` and `y`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2489 | virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0; |
| 2490 | virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0; |
| 2491 | virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED, |
| 2492 | HLongConstant* y ATTRIBUTE_UNUSED) const { |
| 2493 | VLOG(compiler) << DebugName() << " is not defined for the (int, long) case."; |
| 2494 | return nullptr; |
| 2495 | } |
| 2496 | virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED, |
| 2497 | HIntConstant* y ATTRIBUTE_UNUSED) const { |
| 2498 | VLOG(compiler) << DebugName() << " is not defined for the (long, int) case."; |
| 2499 | return nullptr; |
| 2500 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2501 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2502 | // 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] | 2503 | // constant, or null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2504 | HConstant* GetConstantRight() const; |
| 2505 | |
| 2506 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2507 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2508 | HInstruction* GetLeastConstantLeft() const; |
| 2509 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 2510 | DECLARE_INSTRUCTION(BinaryOperation); |
| 2511 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2512 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2513 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 2514 | }; |
| 2515 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2516 | // The comparison bias applies for floating point operations and indicates how NaN |
| 2517 | // comparisons are treated: |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2518 | enum class ComparisonBias { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2519 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 2520 | kGtBias, // return 1 for NaN comparisons |
| 2521 | kLtBias, // return -1 for NaN comparisons |
| 2522 | }; |
| 2523 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2524 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2525 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2526 | HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2527 | : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc), |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2528 | needs_materialization_(true), |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2529 | bias_(ComparisonBias::kNoBias) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2530 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2531 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 2532 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2533 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2534 | // For code generation purposes, returns whether this instruction is just before |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2535 | // `instruction`, and disregard moves in between. |
| 2536 | bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2537 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2538 | DECLARE_INSTRUCTION(Condition); |
| 2539 | |
| 2540 | virtual IfCondition GetCondition() const = 0; |
| 2541 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2542 | virtual IfCondition GetOppositeCondition() const = 0; |
| 2543 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2544 | bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2545 | |
| 2546 | void SetBias(ComparisonBias bias) { bias_ = bias; } |
| 2547 | |
| 2548 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2549 | return bias_ == other->AsCondition()->bias_; |
| 2550 | } |
| 2551 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2552 | bool IsFPConditionTrueIfNaN() const { |
| 2553 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())); |
| 2554 | IfCondition if_cond = GetCondition(); |
| 2555 | return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE); |
| 2556 | } |
| 2557 | |
| 2558 | bool IsFPConditionFalseIfNaN() const { |
| 2559 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())); |
| 2560 | IfCondition if_cond = GetCondition(); |
| 2561 | return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ); |
| 2562 | } |
| 2563 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2564 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2565 | // For register allocation purposes, returns whether this instruction needs to be |
| 2566 | // materialized (that is, not just be in the processor flags). |
| 2567 | bool needs_materialization_; |
| 2568 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2569 | // Needed if we merge a HCompare into a HCondition. |
| 2570 | ComparisonBias bias_; |
| 2571 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2572 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 2573 | }; |
| 2574 | |
| 2575 | // Instruction to check if two inputs are equal to each other. |
| 2576 | class HEqual : public HCondition { |
| 2577 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2578 | HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2579 | : HCondition(first, second, dex_pc) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2580 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2581 | bool IsCommutative() const OVERRIDE { return true; } |
| 2582 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2583 | template <typename T> bool Compute(T x, T y) const { return x == y; } |
| 2584 | |
| 2585 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2586 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2587 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2588 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2589 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2590 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2591 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2592 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2593 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2594 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2595 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2596 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2597 | return kCondEQ; |
| 2598 | } |
| 2599 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2600 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2601 | return kCondNE; |
| 2602 | } |
| 2603 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2604 | private: |
| 2605 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 2606 | }; |
| 2607 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2608 | class HNotEqual : public HCondition { |
| 2609 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2610 | HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2611 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2612 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2613 | bool IsCommutative() const OVERRIDE { return true; } |
| 2614 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2615 | template <typename T> bool Compute(T x, T y) const { return x != y; } |
| 2616 | |
| 2617 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2618 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2619 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2620 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2621 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2622 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2623 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2624 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2625 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2626 | DECLARE_INSTRUCTION(NotEqual); |
| 2627 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2628 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2629 | return kCondNE; |
| 2630 | } |
| 2631 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2632 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2633 | return kCondEQ; |
| 2634 | } |
| 2635 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2636 | private: |
| 2637 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 2638 | }; |
| 2639 | |
| 2640 | class HLessThan : public HCondition { |
| 2641 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2642 | HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2643 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2644 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2645 | template <typename T> bool Compute(T x, T y) const { return x < y; } |
| 2646 | |
| 2647 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2648 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2649 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2650 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2651 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2652 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2653 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2654 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2655 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2656 | DECLARE_INSTRUCTION(LessThan); |
| 2657 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2658 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2659 | return kCondLT; |
| 2660 | } |
| 2661 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2662 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2663 | return kCondGE; |
| 2664 | } |
| 2665 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2666 | private: |
| 2667 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 2668 | }; |
| 2669 | |
| 2670 | class HLessThanOrEqual : public HCondition { |
| 2671 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2672 | HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2673 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2674 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2675 | template <typename T> bool Compute(T x, T y) const { return x <= y; } |
| 2676 | |
| 2677 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2678 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2679 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2680 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2681 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2682 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2683 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2684 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2685 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2686 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 2687 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2688 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2689 | return kCondLE; |
| 2690 | } |
| 2691 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2692 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2693 | return kCondGT; |
| 2694 | } |
| 2695 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2696 | private: |
| 2697 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 2698 | }; |
| 2699 | |
| 2700 | class HGreaterThan : public HCondition { |
| 2701 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2702 | HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2703 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2704 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2705 | template <typename T> bool Compute(T x, T y) const { return x > y; } |
| 2706 | |
| 2707 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2708 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2709 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2710 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2711 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2712 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2713 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2714 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2715 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2716 | DECLARE_INSTRUCTION(GreaterThan); |
| 2717 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2718 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2719 | return kCondGT; |
| 2720 | } |
| 2721 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2722 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2723 | return kCondLE; |
| 2724 | } |
| 2725 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2726 | private: |
| 2727 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 2728 | }; |
| 2729 | |
| 2730 | class HGreaterThanOrEqual : public HCondition { |
| 2731 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2732 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 2733 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2734 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2735 | template <typename T> bool Compute(T x, T y) const { return x >= y; } |
| 2736 | |
| 2737 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2738 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2739 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2740 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2741 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2742 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2743 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2744 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2745 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2746 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 2747 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2748 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2749 | return kCondGE; |
| 2750 | } |
| 2751 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2752 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 2753 | return kCondLT; |
| 2754 | } |
| 2755 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2756 | private: |
| 2757 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 2758 | }; |
| 2759 | |
| 2760 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2761 | // Instruction to check how two inputs compare to each other. |
| 2762 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 2763 | class HCompare : public HBinaryOperation { |
| 2764 | public: |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2765 | HCompare(Primitive::Type type, |
| 2766 | HInstruction* first, |
| 2767 | HInstruction* second, |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2768 | ComparisonBias bias, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2769 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2770 | : HBinaryOperation(Primitive::kPrimInt, |
| 2771 | first, |
| 2772 | second, |
| 2773 | SideEffectsForArchRuntimeCalls(type), |
| 2774 | dex_pc), |
| 2775 | bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2776 | DCHECK_EQ(type, first->GetType()); |
| 2777 | DCHECK_EQ(type, second->GetType()); |
| 2778 | } |
| 2779 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2780 | template <typename T> |
| 2781 | 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] | 2782 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2783 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2784 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2785 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2786 | } |
| 2787 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2788 | return GetBlock()->GetGraph()->GetIntConstant( |
| 2789 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2792 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2793 | return bias_ == other->AsCompare()->bias_; |
| 2794 | } |
| 2795 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2796 | ComparisonBias GetBias() const { return bias_; } |
| 2797 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2798 | bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2799 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 2800 | |
| 2801 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) { |
| 2802 | // MIPS64 uses a runtime call for FP comparisons. |
| 2803 | return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
| 2804 | } |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 2805 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2806 | DECLARE_INSTRUCTION(Compare); |
| 2807 | |
| 2808 | private: |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2809 | const ComparisonBias bias_; |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2810 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2811 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 2812 | }; |
| 2813 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2814 | // A local in the graph. Corresponds to a Dex register. |
| 2815 | class HLocal : public HTemplateInstruction<0> { |
| 2816 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2817 | explicit HLocal(uint16_t reg_number) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2818 | : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2819 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2820 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2821 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2822 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2823 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2824 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2825 | // The Dex register number. |
| 2826 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2827 | |
| 2828 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 2829 | }; |
| 2830 | |
| 2831 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2832 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2833 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2834 | HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2835 | : HExpression(type, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2836 | SetRawInputAt(0, local); |
| 2837 | } |
| 2838 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2839 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2840 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2841 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2842 | |
| 2843 | private: |
| 2844 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 2845 | }; |
| 2846 | |
| 2847 | // Store a value in a given local. This instruction has two inputs: the value |
| 2848 | // and the local. |
| 2849 | class HStoreLocal : public HTemplateInstruction<2> { |
| 2850 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2851 | HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 2852 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2853 | SetRawInputAt(0, local); |
| 2854 | SetRawInputAt(1, value); |
| 2855 | } |
| 2856 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2857 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2858 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2859 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2860 | |
| 2861 | private: |
| 2862 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 2863 | }; |
| 2864 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2865 | class HFloatConstant : public HConstant { |
| 2866 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2867 | float GetValue() const { return value_; } |
| 2868 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2869 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2870 | DCHECK(other->IsFloatConstant()); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2871 | return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) == |
| 2872 | bit_cast<uint32_t, float>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2873 | } |
| 2874 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2875 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2876 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2877 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2878 | 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] | 2879 | } |
| 2880 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2881 | return value_ == 0.0f; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2882 | } |
| 2883 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2884 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f); |
| 2885 | } |
| 2886 | bool IsNaN() const { |
| 2887 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2890 | DECLARE_INSTRUCTION(FloatConstant); |
| 2891 | |
| 2892 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2893 | explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc) |
| 2894 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {} |
| 2895 | explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2896 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2897 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2898 | const float value_; |
| 2899 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2900 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2901 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2902 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2903 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 2904 | }; |
| 2905 | |
| 2906 | class HDoubleConstant : public HConstant { |
| 2907 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2908 | double GetValue() const { return value_; } |
| 2909 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2910 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2911 | DCHECK(other->IsDoubleConstant()); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2912 | return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) == |
| 2913 | bit_cast<uint64_t, double>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2914 | } |
| 2915 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2916 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2917 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2918 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2919 | 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] | 2920 | } |
| 2921 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2922 | return value_ == 0.0; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2923 | } |
| 2924 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2925 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0); |
| 2926 | } |
| 2927 | bool IsNaN() const { |
| 2928 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2931 | DECLARE_INSTRUCTION(DoubleConstant); |
| 2932 | |
| 2933 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2934 | explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) |
| 2935 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {} |
| 2936 | explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2937 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2938 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2939 | const double value_; |
| 2940 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2941 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2942 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2943 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2944 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 2945 | }; |
| 2946 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2947 | enum class Intrinsics { |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2948 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2949 | #include "intrinsics_list.h" |
| 2950 | kNone, |
| 2951 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 2952 | #undef INTRINSICS_LIST |
| 2953 | #undef OPTIMIZING_INTRINSICS |
| 2954 | }; |
| 2955 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 2956 | |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2957 | enum IntrinsicNeedsEnvironmentOrCache { |
| 2958 | kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache. |
| 2959 | kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache. |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 2960 | }; |
| 2961 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2962 | class HInvoke : public HInstruction { |
| 2963 | public: |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2964 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2965 | |
| 2966 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 2967 | // know their environment. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2968 | bool NeedsEnvironment() const OVERRIDE { |
| 2969 | return needs_environment_or_cache_ == kNeedsEnvironmentOrCache; |
| 2970 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2971 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 2972 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2973 | SetRawInputAt(index, argument); |
| 2974 | } |
| 2975 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2976 | // Return the number of arguments. This number can be lower than |
| 2977 | // the number of inputs returned by InputCount(), as some invoke |
| 2978 | // instructions (e.g. HInvokeStaticOrDirect) can have non-argument |
| 2979 | // inputs at the end of their list of inputs. |
| 2980 | uint32_t GetNumberOfArguments() const { return number_of_arguments_; } |
| 2981 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2982 | Primitive::Type GetType() const OVERRIDE { return return_type_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2983 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2984 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2985 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2986 | const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2987 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2988 | InvokeType GetOriginalInvokeType() const { return original_invoke_type_; } |
| 2989 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 2990 | Intrinsics GetIntrinsic() const { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2991 | return intrinsic_; |
| 2992 | } |
| 2993 | |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2994 | void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2995 | intrinsic_ = intrinsic; |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 2996 | needs_environment_or_cache_ = needs_env_or_cache; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2997 | } |
| 2998 | |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 2999 | bool IsFromInlinedInvoke() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 3000 | return GetEnvironment()->GetParent() != nullptr; |
| 3001 | } |
| 3002 | |
| 3003 | bool CanThrow() const OVERRIDE { return true; } |
| 3004 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3005 | DECLARE_INSTRUCTION(Invoke); |
| 3006 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3007 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3008 | HInvoke(ArenaAllocator* arena, |
| 3009 | uint32_t number_of_arguments, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3010 | uint32_t number_of_other_inputs, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3011 | Primitive::Type return_type, |
| 3012 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3013 | uint32_t dex_method_index, |
| 3014 | InvokeType original_invoke_type) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3015 | : HInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3016 | SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays. |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3017 | number_of_arguments_(number_of_arguments), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3018 | inputs_(arena, number_of_arguments), |
| 3019 | return_type_(return_type), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3020 | dex_method_index_(dex_method_index), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3021 | original_invoke_type_(original_invoke_type), |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 3022 | intrinsic_(Intrinsics::kNone), |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3023 | needs_environment_or_cache_(kNeedsEnvironmentOrCache) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3024 | uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs; |
| 3025 | inputs_.SetSize(number_of_inputs); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3026 | } |
| 3027 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3028 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 3029 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 3030 | inputs_.Put(index, input); |
| 3031 | } |
| 3032 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3033 | uint32_t number_of_arguments_; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3034 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 3035 | const Primitive::Type return_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3036 | const uint32_t dex_method_index_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3037 | const InvokeType original_invoke_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3038 | Intrinsics intrinsic_; |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3039 | IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3040 | |
| 3041 | private: |
| 3042 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 3043 | }; |
| 3044 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3045 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3046 | public: |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3047 | // Requirements of this method call regarding the class |
| 3048 | // initialization (clinit) check of its declaring class. |
| 3049 | enum class ClinitCheckRequirement { |
| 3050 | kNone, // Class already initialized. |
| 3051 | kExplicit, // Static call having explicit clinit check as last input. |
| 3052 | kImplicit, // Static call implicitly requiring a clinit check. |
| 3053 | }; |
| 3054 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3055 | // Determines how to load the target ArtMethod*. |
| 3056 | enum class MethodLoadKind { |
| 3057 | // Use a String init ArtMethod* loaded from Thread entrypoints. |
| 3058 | kStringInit, |
| 3059 | |
| 3060 | // Use the method's own ArtMethod* loaded by the register allocator. |
| 3061 | kRecursive, |
| 3062 | |
| 3063 | // Use ArtMethod* at a known address, embed the direct address in the code. |
| 3064 | // Used for app->boot calls with non-relocatable image and for JIT-compiled calls. |
| 3065 | kDirectAddress, |
| 3066 | |
| 3067 | // Use ArtMethod* at an address that will be known at link time, embed the direct |
| 3068 | // address in the code. If the image is relocatable, emit .patch_oat entry. |
| 3069 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3070 | // the image relocatable or not. |
| 3071 | kDirectAddressWithFixup, |
| 3072 | |
| 3073 | // Load from resoved methods array in the dex cache using a PC-relative load. |
| 3074 | // Used when we need to use the dex cache, for example for invoke-static that |
| 3075 | // may cause class initialization (the entry may point to a resolution method), |
| 3076 | // and we know that we can access the dex cache arrays using a PC-relative load. |
| 3077 | kDexCachePcRelative, |
| 3078 | |
| 3079 | // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*. |
| 3080 | // Used for JIT when we need to use the dex cache. This is also the last-resort-kind |
| 3081 | // used when other kinds are unavailable (say, dex cache arrays are not PC-relative) |
| 3082 | // or unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3083 | kDexCacheViaMethod, |
| 3084 | }; |
| 3085 | |
| 3086 | // Determines the location of the code pointer. |
| 3087 | enum class CodePtrLocation { |
| 3088 | // Recursive call, use local PC-relative call instruction. |
| 3089 | kCallSelf, |
| 3090 | |
| 3091 | // Use PC-relative call instruction patched at link time. |
| 3092 | // Used for calls within an oat file, boot->boot or app->app. |
| 3093 | kCallPCRelative, |
| 3094 | |
| 3095 | // Call to a known target address, embed the direct address in code. |
| 3096 | // Used for app->boot call with non-relocatable image and for JIT-compiled calls. |
| 3097 | kCallDirect, |
| 3098 | |
| 3099 | // Call to a target address that will be known at link time, embed the direct |
| 3100 | // address in code. If the image is relocatable, emit .patch_oat entry. |
| 3101 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3102 | // the image relocatable or not. |
| 3103 | kCallDirectWithFixup, |
| 3104 | |
| 3105 | // Use code pointer from the ArtMethod*. |
| 3106 | // Used when we don't know the target code. This is also the last-resort-kind used when |
| 3107 | // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3108 | kCallArtMethod, |
| 3109 | }; |
| 3110 | |
| 3111 | struct DispatchInfo { |
| 3112 | const MethodLoadKind method_load_kind; |
| 3113 | const CodePtrLocation code_ptr_location; |
| 3114 | // The method load data holds |
| 3115 | // - thread entrypoint offset for kStringInit method if this is a string init invoke. |
| 3116 | // Note that there are multiple string init methods, each having its own offset. |
| 3117 | // - the method address for kDirectAddress |
| 3118 | // - the dex cache arrays offset for kDexCachePcRel. |
| 3119 | const uint64_t method_load_data; |
| 3120 | const uint64_t direct_code_ptr; |
| 3121 | }; |
| 3122 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3123 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 3124 | uint32_t number_of_arguments, |
| 3125 | Primitive::Type return_type, |
| 3126 | uint32_t dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3127 | uint32_t method_index, |
| 3128 | MethodReference target_method, |
| 3129 | DispatchInfo dispatch_info, |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 3130 | InvokeType original_invoke_type, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3131 | InvokeType invoke_type, |
| 3132 | ClinitCheckRequirement clinit_check_requirement) |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3133 | : HInvoke(arena, |
| 3134 | number_of_arguments, |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 3135 | // There is one extra argument for the HCurrentMethod node, and |
| 3136 | // potentially one other if the clinit check is explicit, and one other |
| 3137 | // if the method is a string factory. |
| 3138 | 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3139 | + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u), |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3140 | return_type, |
| 3141 | dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3142 | method_index, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3143 | original_invoke_type), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 3144 | invoke_type_(invoke_type), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3145 | clinit_check_requirement_(clinit_check_requirement), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3146 | target_method_(target_method), |
| 3147 | dispatch_info_(dispatch_info) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3148 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3149 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3150 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3151 | // We access the method via the dex cache so we can't do an implicit null check. |
| 3152 | // TODO: for intrinsics we can generate implicit null checks. |
| 3153 | return false; |
| 3154 | } |
| 3155 | |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 3156 | bool CanBeNull() const OVERRIDE { |
| 3157 | return return_type_ == Primitive::kPrimNot && !IsStringInit(); |
| 3158 | } |
| 3159 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3160 | InvokeType GetInvokeType() const { return invoke_type_; } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3161 | MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; } |
| 3162 | CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; } |
| 3163 | bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3164 | bool NeedsDexCache() const OVERRIDE { |
| 3165 | if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; } |
| 3166 | return !IsRecursive() && !IsStringInit(); |
| 3167 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3168 | bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; } |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 3169 | uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3170 | bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; } |
| 3171 | bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; } |
| 3172 | bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; } |
| 3173 | MethodReference GetTargetMethod() const { return target_method_; } |
| 3174 | |
| 3175 | int32_t GetStringInitOffset() const { |
| 3176 | DCHECK(IsStringInit()); |
| 3177 | return dispatch_info_.method_load_data; |
| 3178 | } |
| 3179 | |
| 3180 | uint64_t GetMethodAddress() const { |
| 3181 | DCHECK(HasMethodAddress()); |
| 3182 | return dispatch_info_.method_load_data; |
| 3183 | } |
| 3184 | |
| 3185 | uint32_t GetDexCacheArrayOffset() const { |
| 3186 | DCHECK(HasPcRelDexCache()); |
| 3187 | return dispatch_info_.method_load_data; |
| 3188 | } |
| 3189 | |
| 3190 | uint64_t GetDirectCodePtr() const { |
| 3191 | DCHECK(HasDirectCodePtr()); |
| 3192 | return dispatch_info_.direct_code_ptr; |
| 3193 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3194 | |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 3195 | ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; } |
| 3196 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3197 | // Is this instruction a call to a static method? |
| 3198 | bool IsStatic() const { |
| 3199 | return GetInvokeType() == kStatic; |
| 3200 | } |
| 3201 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3202 | // Remove the art::HLoadClass instruction set as last input by |
| 3203 | // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of |
| 3204 | // the initial art::HClinitCheck instruction (only relevant for |
| 3205 | // static calls with explicit clinit check). |
| 3206 | void RemoveLoadClassAsLastInput() { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3207 | DCHECK(IsStaticWithExplicitClinitCheck()); |
| 3208 | size_t last_input_index = InputCount() - 1; |
| 3209 | HInstruction* last_input = InputAt(last_input_index); |
| 3210 | DCHECK(last_input != nullptr); |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3211 | DCHECK(last_input->IsLoadClass()) << last_input->DebugName(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3212 | RemoveAsUserOfInput(last_input_index); |
| 3213 | inputs_.DeleteAt(last_input_index); |
| 3214 | clinit_check_requirement_ = ClinitCheckRequirement::kImplicit; |
| 3215 | DCHECK(IsStaticWithImplicitClinitCheck()); |
| 3216 | } |
| 3217 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 3218 | bool IsStringFactoryFor(HFakeString* str) const { |
| 3219 | if (!IsStringInit()) return false; |
| 3220 | // +1 for the current method. |
| 3221 | if (InputCount() == (number_of_arguments_ + 1)) return false; |
| 3222 | return InputAt(InputCount() - 1)->AsFakeString() == str; |
| 3223 | } |
| 3224 | |
| 3225 | void RemoveFakeStringArgumentAsLastInput() { |
| 3226 | DCHECK(IsStringInit()); |
| 3227 | size_t last_input_index = InputCount() - 1; |
| 3228 | HInstruction* last_input = InputAt(last_input_index); |
| 3229 | DCHECK(last_input != nullptr); |
| 3230 | DCHECK(last_input->IsFakeString()) << last_input->DebugName(); |
| 3231 | RemoveAsUserOfInput(last_input_index); |
| 3232 | inputs_.DeleteAt(last_input_index); |
| 3233 | } |
| 3234 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3235 | // Is this a call to a static method whose declaring class has an |
| 3236 | // explicit intialization check in the graph? |
| 3237 | bool IsStaticWithExplicitClinitCheck() const { |
| 3238 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit); |
| 3239 | } |
| 3240 | |
| 3241 | // Is this a call to a static method whose declaring class has an |
| 3242 | // implicit intialization check requirement? |
| 3243 | bool IsStaticWithImplicitClinitCheck() const { |
| 3244 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit); |
| 3245 | } |
| 3246 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3247 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3248 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3249 | protected: |
| 3250 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 3251 | const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i); |
| 3252 | if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) { |
| 3253 | HInstruction* input = input_record.GetInstruction(); |
| 3254 | // `input` is the last input of a static invoke marked as having |
| 3255 | // an explicit clinit check. It must either be: |
| 3256 | // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or |
| 3257 | // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation. |
| 3258 | DCHECK(input != nullptr); |
| 3259 | DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName(); |
| 3260 | } |
| 3261 | return input_record; |
| 3262 | } |
| 3263 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3264 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3265 | const InvokeType invoke_type_; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3266 | ClinitCheckRequirement clinit_check_requirement_; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3267 | // The target method may refer to different dex file or method index than the original |
| 3268 | // invoke. This happens for sharpened calls and for calls where a method was redeclared |
| 3269 | // in derived class to increase visibility. |
| 3270 | MethodReference target_method_; |
| 3271 | DispatchInfo dispatch_info_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3272 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3273 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3274 | }; |
| 3275 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3276 | class HInvokeVirtual : public HInvoke { |
| 3277 | public: |
| 3278 | HInvokeVirtual(ArenaAllocator* arena, |
| 3279 | uint32_t number_of_arguments, |
| 3280 | Primitive::Type return_type, |
| 3281 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3282 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3283 | uint32_t vtable_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3284 | : 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] | 3285 | vtable_index_(vtable_index) {} |
| 3286 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3287 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3288 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3289 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 3292 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 3293 | |
| 3294 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 3295 | |
| 3296 | private: |
| 3297 | const uint32_t vtable_index_; |
| 3298 | |
| 3299 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 3300 | }; |
| 3301 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3302 | class HInvokeInterface : public HInvoke { |
| 3303 | public: |
| 3304 | HInvokeInterface(ArenaAllocator* arena, |
| 3305 | uint32_t number_of_arguments, |
| 3306 | Primitive::Type return_type, |
| 3307 | uint32_t dex_pc, |
| 3308 | uint32_t dex_method_index, |
| 3309 | uint32_t imt_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3310 | : 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] | 3311 | imt_index_(imt_index) {} |
| 3312 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3313 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3314 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3315 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3318 | uint32_t GetImtIndex() const { return imt_index_; } |
| 3319 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 3320 | |
| 3321 | DECLARE_INSTRUCTION(InvokeInterface); |
| 3322 | |
| 3323 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 3324 | const uint32_t imt_index_; |
| 3325 | |
| 3326 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 3327 | }; |
| 3328 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3329 | class HNewInstance : public HExpression<1> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3330 | public: |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3331 | HNewInstance(HCurrentMethod* current_method, |
| 3332 | uint32_t dex_pc, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3333 | uint16_t type_index, |
| 3334 | const DexFile& dex_file, |
| 3335 | QuickEntrypointEnum entrypoint) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3336 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3337 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3338 | dex_file_(dex_file), |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3339 | entrypoint_(entrypoint) { |
| 3340 | SetRawInputAt(0, current_method); |
| 3341 | } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3342 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3343 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3344 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3345 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3346 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 3347 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3348 | // It may throw when called on: |
| 3349 | // - interfaces |
| 3350 | // - abstract/innaccessible/unknown classes |
| 3351 | // TODO: optimize when possible. |
| 3352 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3353 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3354 | bool CanBeNull() const OVERRIDE { return false; } |
| 3355 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3356 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 3357 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3358 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3359 | |
| 3360 | private: |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3361 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3362 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3363 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 3364 | |
| 3365 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 3366 | }; |
| 3367 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3368 | class HNeg : public HUnaryOperation { |
| 3369 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3370 | HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3371 | : HUnaryOperation(result_type, input, dex_pc) {} |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3372 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3373 | template <typename T> T Compute(T x) const { return -x; } |
| 3374 | |
| 3375 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3376 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3377 | } |
| 3378 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3379 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3380 | } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 3381 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 3382 | DECLARE_INSTRUCTION(Neg); |
| 3383 | |
| 3384 | private: |
| 3385 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 3386 | }; |
| 3387 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3388 | class HNewArray : public HExpression<2> { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3389 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3390 | HNewArray(HInstruction* length, |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3391 | HCurrentMethod* current_method, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3392 | uint32_t dex_pc, |
| 3393 | uint16_t type_index, |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3394 | const DexFile& dex_file, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3395 | QuickEntrypointEnum entrypoint) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3396 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3397 | type_index_(type_index), |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3398 | dex_file_(dex_file), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3399 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3400 | SetRawInputAt(0, length); |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 3401 | SetRawInputAt(1, current_method); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3404 | uint16_t GetTypeIndex() const { return type_index_; } |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3405 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3406 | |
| 3407 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3408 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3409 | |
Mingyao Yang | 0c365e6 | 2015-03-31 15:09:29 -0700 | [diff] [blame] | 3410 | // May throw NegativeArraySizeException, OutOfMemoryError, etc. |
| 3411 | bool CanThrow() const OVERRIDE { return true; } |
| 3412 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3413 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3414 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3415 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 3416 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3417 | DECLARE_INSTRUCTION(NewArray); |
| 3418 | |
| 3419 | private: |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3420 | const uint16_t type_index_; |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 3421 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 3422 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 3423 | |
| 3424 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 3425 | }; |
| 3426 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3427 | class HAdd : public HBinaryOperation { |
| 3428 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3429 | HAdd(Primitive::Type result_type, |
| 3430 | HInstruction* left, |
| 3431 | HInstruction* right, |
| 3432 | uint32_t dex_pc = kNoDexPc) |
| 3433 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3434 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3435 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3436 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3437 | template <typename T> T Compute(T x, T y) const { return x + y; } |
| 3438 | |
| 3439 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3440 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3441 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3442 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3443 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3444 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3445 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3446 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3447 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 3448 | DECLARE_INSTRUCTION(Add); |
| 3449 | |
| 3450 | private: |
| 3451 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 3452 | }; |
| 3453 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3454 | class HSub : public HBinaryOperation { |
| 3455 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3456 | HSub(Primitive::Type result_type, |
| 3457 | HInstruction* left, |
| 3458 | HInstruction* right, |
| 3459 | uint32_t dex_pc = kNoDexPc) |
| 3460 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3461 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3462 | template <typename T> T Compute(T x, T y) const { return x - y; } |
| 3463 | |
| 3464 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3465 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3466 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3467 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3468 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3469 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3470 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3471 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3472 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3473 | DECLARE_INSTRUCTION(Sub); |
| 3474 | |
| 3475 | private: |
| 3476 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 3477 | }; |
| 3478 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3479 | class HMul : public HBinaryOperation { |
| 3480 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3481 | HMul(Primitive::Type result_type, |
| 3482 | HInstruction* left, |
| 3483 | HInstruction* right, |
| 3484 | uint32_t dex_pc = kNoDexPc) |
| 3485 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3486 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3487 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3488 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3489 | template <typename T> T Compute(T x, T y) const { return x * y; } |
| 3490 | |
| 3491 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3492 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3493 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3494 | } |
| 3495 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3496 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3497 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3498 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3499 | |
| 3500 | DECLARE_INSTRUCTION(Mul); |
| 3501 | |
| 3502 | private: |
| 3503 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 3504 | }; |
| 3505 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3506 | class HDiv : public HBinaryOperation { |
| 3507 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3508 | HDiv(Primitive::Type result_type, |
| 3509 | HInstruction* left, |
| 3510 | HInstruction* right, |
| 3511 | uint32_t dex_pc) |
| 3512 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3513 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3514 | template <typename T> |
| 3515 | T Compute(T x, T y) const { |
| 3516 | // Our graph structure ensures we never have 0 for `y` during |
| 3517 | // constant folding. |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 3518 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3519 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 3520 | return (y == -1) ? -x : x / y; |
| 3521 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3522 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3523 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3524 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3525 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3526 | } |
| 3527 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3528 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3529 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3530 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3531 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3532 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 3533 | // The generated code can use a runtime call. |
| 3534 | return SideEffects::CanTriggerGC(); |
| 3535 | } |
| 3536 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3537 | DECLARE_INSTRUCTION(Div); |
| 3538 | |
| 3539 | private: |
| 3540 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 3541 | }; |
| 3542 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3543 | class HRem : public HBinaryOperation { |
| 3544 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3545 | HRem(Primitive::Type result_type, |
| 3546 | HInstruction* left, |
| 3547 | HInstruction* right, |
| 3548 | uint32_t dex_pc) |
| 3549 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3550 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3551 | template <typename T> |
| 3552 | T Compute(T x, T y) const { |
| 3553 | // Our graph structure ensures we never have 0 for `y` during |
| 3554 | // constant folding. |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3555 | DCHECK_NE(y, 0); |
| 3556 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 3557 | return (y == -1) ? 0 : x % y; |
| 3558 | } |
| 3559 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3560 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3561 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3562 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3563 | } |
| 3564 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3565 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3566 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3569 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3570 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 3571 | return SideEffects::CanTriggerGC(); |
| 3572 | } |
| 3573 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3574 | DECLARE_INSTRUCTION(Rem); |
| 3575 | |
| 3576 | private: |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3577 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 3578 | }; |
| 3579 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3580 | class HDivZeroCheck : public HExpression<1> { |
| 3581 | public: |
| 3582 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3583 | : HExpression(value->GetType(), SideEffects::None(), dex_pc) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3584 | SetRawInputAt(0, value); |
| 3585 | } |
| 3586 | |
Serguei Katkov | 8c0676c | 2015-08-03 13:55:33 +0600 | [diff] [blame] | 3587 | Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); } |
| 3588 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3589 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3590 | |
| 3591 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3592 | UNUSED(other); |
| 3593 | return true; |
| 3594 | } |
| 3595 | |
| 3596 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3597 | bool CanThrow() const OVERRIDE { return true; } |
| 3598 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3599 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 3600 | |
| 3601 | private: |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3602 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 3603 | }; |
| 3604 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3605 | class HShl : public HBinaryOperation { |
| 3606 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3607 | HShl(Primitive::Type result_type, |
| 3608 | HInstruction* left, |
| 3609 | HInstruction* right, |
| 3610 | uint32_t dex_pc = kNoDexPc) |
| 3611 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3612 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3613 | template <typename T, typename U, typename V> |
| 3614 | T Compute(T x, U y, V max_shift_value) const { |
| 3615 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3616 | "V is not the unsigned integer type corresponding to T"); |
| 3617 | return x << (y & max_shift_value); |
| 3618 | } |
| 3619 | |
| 3620 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3621 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3622 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3623 | } |
| 3624 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3625 | // case is handled as `x << static_cast<int>(y)`. |
| 3626 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3627 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3628 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3629 | } |
| 3630 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3631 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3632 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3633 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3634 | |
| 3635 | DECLARE_INSTRUCTION(Shl); |
| 3636 | |
| 3637 | private: |
| 3638 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 3639 | }; |
| 3640 | |
| 3641 | class HShr : public HBinaryOperation { |
| 3642 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3643 | HShr(Primitive::Type result_type, |
| 3644 | HInstruction* left, |
| 3645 | HInstruction* right, |
| 3646 | uint32_t dex_pc = kNoDexPc) |
| 3647 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3648 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3649 | template <typename T, typename U, typename V> |
| 3650 | T Compute(T x, U y, V max_shift_value) const { |
| 3651 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3652 | "V is not the unsigned integer type corresponding to T"); |
| 3653 | return x >> (y & max_shift_value); |
| 3654 | } |
| 3655 | |
| 3656 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3657 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3658 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3659 | } |
| 3660 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3661 | // case is handled as `x >> static_cast<int>(y)`. |
| 3662 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3663 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3664 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3665 | } |
| 3666 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3667 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3668 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3669 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3670 | |
| 3671 | DECLARE_INSTRUCTION(Shr); |
| 3672 | |
| 3673 | private: |
| 3674 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 3675 | }; |
| 3676 | |
| 3677 | class HUShr : public HBinaryOperation { |
| 3678 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3679 | HUShr(Primitive::Type result_type, |
| 3680 | HInstruction* left, |
| 3681 | HInstruction* right, |
| 3682 | uint32_t dex_pc = kNoDexPc) |
| 3683 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3684 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3685 | template <typename T, typename U, typename V> |
| 3686 | T Compute(T x, U y, V max_shift_value) const { |
| 3687 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 3688 | "V is not the unsigned integer type corresponding to T"); |
| 3689 | V ux = static_cast<V>(x); |
| 3690 | return static_cast<T>(ux >> (y & max_shift_value)); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3693 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3694 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3695 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3696 | } |
| 3697 | // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this |
| 3698 | // case is handled as `x >>> static_cast<int>(y)`. |
| 3699 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 3700 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3701 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3702 | } |
| 3703 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3704 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3705 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | DECLARE_INSTRUCTION(UShr); |
| 3709 | |
| 3710 | private: |
| 3711 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 3712 | }; |
| 3713 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3714 | class HAnd : public HBinaryOperation { |
| 3715 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3716 | HAnd(Primitive::Type result_type, |
| 3717 | HInstruction* left, |
| 3718 | HInstruction* right, |
| 3719 | uint32_t dex_pc = kNoDexPc) |
| 3720 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3721 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3722 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3723 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3724 | template <typename T, typename U> |
| 3725 | auto Compute(T x, U y) const -> decltype(x & y) { return x & y; } |
| 3726 | |
| 3727 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3728 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3729 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3730 | } |
| 3731 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3732 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3733 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3734 | } |
| 3735 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3736 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3737 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3738 | } |
| 3739 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3740 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3741 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3742 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3743 | |
| 3744 | DECLARE_INSTRUCTION(And); |
| 3745 | |
| 3746 | private: |
| 3747 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 3748 | }; |
| 3749 | |
| 3750 | class HOr : public HBinaryOperation { |
| 3751 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3752 | HOr(Primitive::Type result_type, |
| 3753 | HInstruction* left, |
| 3754 | HInstruction* right, |
| 3755 | uint32_t dex_pc = kNoDexPc) |
| 3756 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3757 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3758 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3759 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3760 | template <typename T, typename U> |
| 3761 | auto Compute(T x, U y) const -> decltype(x | y) { return x | y; } |
| 3762 | |
| 3763 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3764 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3765 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3766 | } |
| 3767 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3768 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3769 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3770 | } |
| 3771 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3772 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3773 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3774 | } |
| 3775 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3776 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3777 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3778 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3779 | |
| 3780 | DECLARE_INSTRUCTION(Or); |
| 3781 | |
| 3782 | private: |
| 3783 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 3784 | }; |
| 3785 | |
| 3786 | class HXor : public HBinaryOperation { |
| 3787 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3788 | HXor(Primitive::Type result_type, |
| 3789 | HInstruction* left, |
| 3790 | HInstruction* right, |
| 3791 | uint32_t dex_pc = kNoDexPc) |
| 3792 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3793 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3794 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3795 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3796 | template <typename T, typename U> |
| 3797 | auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; } |
| 3798 | |
| 3799 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3800 | return GetBlock()->GetGraph()->GetIntConstant( |
| 3801 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3802 | } |
| 3803 | HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3804 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3805 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3806 | } |
| 3807 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3808 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3809 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3810 | } |
| 3811 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3812 | return GetBlock()->GetGraph()->GetLongConstant( |
| 3813 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3814 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3815 | |
| 3816 | DECLARE_INSTRUCTION(Xor); |
| 3817 | |
| 3818 | private: |
| 3819 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 3820 | }; |
| 3821 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3822 | // The value of a parameter in this method. Its location depends on |
| 3823 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3824 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3825 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3826 | HParameterValue(uint8_t index, |
| 3827 | Primitive::Type parameter_type, |
| 3828 | bool is_this = false) |
| 3829 | : HExpression(parameter_type, SideEffects::None(), kNoDexPc), |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3830 | index_(index), |
| 3831 | is_this_(is_this), |
| 3832 | can_be_null_(!is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3833 | |
| 3834 | uint8_t GetIndex() const { return index_; } |
| 3835 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3836 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3837 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3838 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 3839 | bool IsThis() const { return is_this_; } |
| 3840 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3841 | DECLARE_INSTRUCTION(ParameterValue); |
| 3842 | |
| 3843 | private: |
| 3844 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3845 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3846 | const uint8_t index_; |
| 3847 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3848 | // Whether or not the parameter value corresponds to 'this' argument. |
| 3849 | const bool is_this_; |
| 3850 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 3851 | bool can_be_null_; |
| 3852 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3853 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 3854 | }; |
| 3855 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3856 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3857 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3858 | HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3859 | : HUnaryOperation(result_type, input, dex_pc) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3860 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3861 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3862 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3863 | UNUSED(other); |
| 3864 | return true; |
| 3865 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3866 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3867 | template <typename T> T Compute(T x) const { return ~x; } |
| 3868 | |
| 3869 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3870 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3871 | } |
| 3872 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3873 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3874 | } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3875 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3876 | DECLARE_INSTRUCTION(Not); |
| 3877 | |
| 3878 | private: |
| 3879 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 3880 | }; |
| 3881 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3882 | class HBooleanNot : public HUnaryOperation { |
| 3883 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3884 | explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 3885 | : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {} |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3886 | |
| 3887 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3888 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3889 | UNUSED(other); |
| 3890 | return true; |
| 3891 | } |
| 3892 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3893 | template <typename T> bool Compute(T x) const { |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3894 | DCHECK(IsUint<1>(x)); |
| 3895 | return !x; |
| 3896 | } |
| 3897 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3898 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3899 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3900 | } |
| 3901 | HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3902 | LOG(FATAL) << DebugName() << " is not defined for long values"; |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3903 | UNREACHABLE(); |
| 3904 | } |
| 3905 | |
| 3906 | DECLARE_INSTRUCTION(BooleanNot); |
| 3907 | |
| 3908 | private: |
| 3909 | DISALLOW_COPY_AND_ASSIGN(HBooleanNot); |
| 3910 | }; |
| 3911 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3912 | class HTypeConversion : public HExpression<1> { |
| 3913 | public: |
| 3914 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3915 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3916 | : HExpression(result_type, |
| 3917 | SideEffectsForArchRuntimeCalls(input->GetType(), result_type), |
| 3918 | dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3919 | SetRawInputAt(0, input); |
| 3920 | DCHECK_NE(input->GetType(), result_type); |
| 3921 | } |
| 3922 | |
| 3923 | HInstruction* GetInput() const { return InputAt(0); } |
| 3924 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 3925 | Primitive::Type GetResultType() const { return GetType(); } |
| 3926 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3927 | // Required by the x86 and ARM code generators when producing calls |
| 3928 | // to the runtime. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3929 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3930 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 3931 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3932 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 3933 | // Try to statically evaluate the conversion and return a HConstant |
| 3934 | // containing the result. If the input cannot be converted, return nullptr. |
| 3935 | HConstant* TryStaticEvaluation() const; |
| 3936 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3937 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type, |
| 3938 | Primitive::Type result_type) { |
| 3939 | // Some architectures may not require the 'GC' side effects, but at this point |
| 3940 | // in the compilation process we do not know what architecture we will |
| 3941 | // generate code for, so we must be conservative. |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 3942 | if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type)) |
| 3943 | || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3944 | return SideEffects::CanTriggerGC(); |
| 3945 | } |
| 3946 | return SideEffects::None(); |
| 3947 | } |
| 3948 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3949 | DECLARE_INSTRUCTION(TypeConversion); |
| 3950 | |
| 3951 | private: |
| 3952 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 3953 | }; |
| 3954 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 3955 | static constexpr uint32_t kNoRegNumber = -1; |
| 3956 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3957 | class HPhi : public HInstruction { |
| 3958 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3959 | HPhi(ArenaAllocator* arena, |
| 3960 | uint32_t reg_number, |
| 3961 | size_t number_of_inputs, |
| 3962 | Primitive::Type type, |
| 3963 | uint32_t dex_pc = kNoDexPc) |
| 3964 | : HInstruction(SideEffects::None(), dex_pc), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3965 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3966 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3967 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3968 | is_live_(false), |
| 3969 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3970 | inputs_.SetSize(number_of_inputs); |
| 3971 | } |
| 3972 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 3973 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 3974 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 3975 | switch (type) { |
| 3976 | case Primitive::kPrimBoolean: |
| 3977 | case Primitive::kPrimByte: |
| 3978 | case Primitive::kPrimShort: |
| 3979 | case Primitive::kPrimChar: |
| 3980 | return Primitive::kPrimInt; |
| 3981 | default: |
| 3982 | return type; |
| 3983 | } |
| 3984 | } |
| 3985 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 3986 | bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); } |
| 3987 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3988 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3989 | |
| 3990 | void AddInput(HInstruction* input); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 3991 | void RemoveInputAt(size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3992 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3993 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3994 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3995 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3996 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3997 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 3998 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3999 | uint32_t GetRegNumber() const { return reg_number_; } |
| 4000 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 4001 | void SetDead() { is_live_ = false; } |
| 4002 | void SetLive() { is_live_ = true; } |
| 4003 | bool IsDead() const { return !is_live_; } |
| 4004 | bool IsLive() const { return is_live_; } |
| 4005 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 4006 | // Returns the next equivalent phi (starting from the current one) or null if there is none. |
| 4007 | // An equivalent phi is a phi having the same dex register and type. |
| 4008 | // It assumes that phis with the same dex register are adjacent. |
| 4009 | HPhi* GetNextEquivalentPhiWithSameType() { |
| 4010 | HInstruction* next = GetNext(); |
| 4011 | while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) { |
| 4012 | if (next->GetType() == GetType()) { |
| 4013 | return next->AsPhi(); |
| 4014 | } |
| 4015 | next = next->GetNext(); |
| 4016 | } |
| 4017 | return nullptr; |
| 4018 | } |
| 4019 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4020 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4021 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4022 | protected: |
| 4023 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 4024 | |
| 4025 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 4026 | inputs_.Put(index, input); |
| 4027 | } |
| 4028 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 4029 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4030 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4031 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4032 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 4033 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4034 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4035 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4036 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 4037 | }; |
| 4038 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4039 | class HNullCheck : public HExpression<1> { |
| 4040 | public: |
| 4041 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4042 | : HExpression(value->GetType(), SideEffects::None(), dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4043 | SetRawInputAt(0, value); |
| 4044 | } |
| 4045 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4046 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4047 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4048 | UNUSED(other); |
| 4049 | return true; |
| 4050 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4051 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4052 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4053 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4054 | bool CanThrow() const OVERRIDE { return true; } |
| 4055 | |
| 4056 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 4057 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4058 | |
| 4059 | DECLARE_INSTRUCTION(NullCheck); |
| 4060 | |
| 4061 | private: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4062 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 4063 | }; |
| 4064 | |
| 4065 | class FieldInfo : public ValueObject { |
| 4066 | public: |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4067 | FieldInfo(MemberOffset field_offset, |
| 4068 | Primitive::Type field_type, |
| 4069 | bool is_volatile, |
| 4070 | uint32_t index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4071 | const DexFile& dex_file, |
| 4072 | Handle<mirror::DexCache> dex_cache) |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4073 | : field_offset_(field_offset), |
| 4074 | field_type_(field_type), |
| 4075 | is_volatile_(is_volatile), |
| 4076 | index_(index), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4077 | dex_file_(dex_file), |
| 4078 | dex_cache_(dex_cache) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4079 | |
| 4080 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4081 | Primitive::Type GetFieldType() const { return field_type_; } |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4082 | uint32_t GetFieldIndex() const { return index_; } |
| 4083 | const DexFile& GetDexFile() const { return dex_file_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4084 | bool IsVolatile() const { return is_volatile_; } |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4085 | Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4086 | |
| 4087 | private: |
| 4088 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4089 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4090 | const bool is_volatile_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4091 | const uint32_t index_; |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4092 | const DexFile& dex_file_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4093 | const Handle<mirror::DexCache> dex_cache_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4094 | }; |
| 4095 | |
| 4096 | class HInstanceFieldGet : public HExpression<1> { |
| 4097 | public: |
| 4098 | HInstanceFieldGet(HInstruction* value, |
| 4099 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4100 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4101 | bool is_volatile, |
| 4102 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4103 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4104 | Handle<mirror::DexCache> dex_cache, |
| 4105 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4106 | : HExpression( |
| 4107 | field_type, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4108 | SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4109 | 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] | 4110 | SetRawInputAt(0, value); |
| 4111 | } |
| 4112 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 4113 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4114 | |
| 4115 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4116 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 4117 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4120 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4121 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 4125 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 4126 | } |
| 4127 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4128 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4129 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4130 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4131 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4132 | |
| 4133 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 4134 | |
| 4135 | private: |
| 4136 | const FieldInfo field_info_; |
| 4137 | |
| 4138 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 4139 | }; |
| 4140 | |
| 4141 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 4142 | public: |
| 4143 | HInstanceFieldSet(HInstruction* object, |
| 4144 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4145 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4146 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4147 | bool is_volatile, |
| 4148 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4149 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4150 | Handle<mirror::DexCache> dex_cache, |
| 4151 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4152 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4153 | SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4154 | 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] | 4155 | value_can_be_null_(true) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4156 | SetRawInputAt(0, object); |
| 4157 | SetRawInputAt(1, value); |
| 4158 | } |
| 4159 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4160 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4161 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4164 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4165 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4166 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4167 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4168 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4169 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 4170 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4171 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4172 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 4173 | |
| 4174 | private: |
| 4175 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4176 | bool value_can_be_null_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4177 | |
| 4178 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 4179 | }; |
| 4180 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4181 | class HArrayGet : public HExpression<2> { |
| 4182 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4183 | HArrayGet(HInstruction* array, |
| 4184 | HInstruction* index, |
| 4185 | Primitive::Type type, |
| 4186 | uint32_t dex_pc = kNoDexPc) |
| 4187 | : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4188 | SetRawInputAt(0, array); |
| 4189 | SetRawInputAt(1, index); |
| 4190 | } |
| 4191 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4192 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4193 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4194 | UNUSED(other); |
| 4195 | return true; |
| 4196 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4197 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4198 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4199 | // TODO: We can be smarter here. |
| 4200 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 4201 | // which generates the implicit null check. There are cases when these can be removed |
| 4202 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 4203 | // implicit check here (as long as the address falls in the first page). |
| 4204 | return false; |
| 4205 | } |
| 4206 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4207 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4208 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4209 | HInstruction* GetArray() const { return InputAt(0); } |
| 4210 | HInstruction* GetIndex() const { return InputAt(1); } |
| 4211 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4212 | DECLARE_INSTRUCTION(ArrayGet); |
| 4213 | |
| 4214 | private: |
| 4215 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 4216 | }; |
| 4217 | |
| 4218 | class HArraySet : public HTemplateInstruction<3> { |
| 4219 | public: |
| 4220 | HArraySet(HInstruction* array, |
| 4221 | HInstruction* index, |
| 4222 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4223 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4224 | uint32_t dex_pc) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4225 | : HTemplateInstruction( |
| 4226 | SideEffects::ArrayWriteOfType(expected_component_type).Union( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4227 | SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4228 | expected_component_type_(expected_component_type), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4229 | needs_type_check_(value->GetType() == Primitive::kPrimNot), |
| 4230 | value_can_be_null_(true) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4231 | SetRawInputAt(0, array); |
| 4232 | SetRawInputAt(1, index); |
| 4233 | SetRawInputAt(2, value); |
| 4234 | } |
| 4235 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4236 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4237 | // We currently always call a runtime method to catch array store |
| 4238 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4239 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
Mingyao Yang | 81014cb | 2015-06-02 03:16:27 -0700 | [diff] [blame] | 4242 | // Can throw ArrayStoreException. |
| 4243 | bool CanThrow() const OVERRIDE { return needs_type_check_; } |
| 4244 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4245 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4246 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4247 | // TODO: Same as for ArrayGet. |
| 4248 | return false; |
| 4249 | } |
| 4250 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4251 | void ClearNeedsTypeCheck() { |
| 4252 | needs_type_check_ = false; |
| 4253 | } |
| 4254 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4255 | void ClearValueCanBeNull() { |
| 4256 | value_can_be_null_ = false; |
| 4257 | } |
| 4258 | |
| 4259 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4260 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 4261 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4262 | HInstruction* GetArray() const { return InputAt(0); } |
| 4263 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4264 | HInstruction* GetValue() const { return InputAt(2); } |
| 4265 | |
| 4266 | Primitive::Type GetComponentType() const { |
| 4267 | // The Dex format does not type floating point index operations. Since the |
| 4268 | // `expected_component_type_` is set during building and can therefore not |
| 4269 | // be correct, we also check what is the value type. If it is a floating |
| 4270 | // point type, we must use that type. |
| 4271 | Primitive::Type value_type = GetValue()->GetType(); |
| 4272 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 4273 | ? value_type |
| 4274 | : expected_component_type_; |
| 4275 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4276 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4277 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) { |
| 4278 | return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
| 4279 | } |
| 4280 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4281 | DECLARE_INSTRUCTION(ArraySet); |
| 4282 | |
| 4283 | private: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4284 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4285 | bool needs_type_check_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4286 | bool value_can_be_null_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4287 | |
| 4288 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 4289 | }; |
| 4290 | |
| 4291 | class HArrayLength : public HExpression<1> { |
| 4292 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4293 | explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc) |
| 4294 | : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4295 | // Note that arrays do not change length, so the instruction does not |
| 4296 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4297 | SetRawInputAt(0, array); |
| 4298 | } |
| 4299 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4300 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4301 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4302 | UNUSED(other); |
| 4303 | return true; |
| 4304 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4305 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 4306 | return obj == InputAt(0); |
| 4307 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4308 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4309 | DECLARE_INSTRUCTION(ArrayLength); |
| 4310 | |
| 4311 | private: |
| 4312 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 4313 | }; |
| 4314 | |
| 4315 | class HBoundsCheck : public HExpression<2> { |
| 4316 | public: |
| 4317 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4318 | : HExpression(index->GetType(), SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4319 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 4320 | SetRawInputAt(0, index); |
| 4321 | SetRawInputAt(1, length); |
| 4322 | } |
| 4323 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4324 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4325 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4326 | UNUSED(other); |
| 4327 | return true; |
| 4328 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4329 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4330 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4331 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4332 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 4333 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4334 | |
| 4335 | DECLARE_INSTRUCTION(BoundsCheck); |
| 4336 | |
| 4337 | private: |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4338 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 4339 | }; |
| 4340 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4341 | /** |
| 4342 | * Some DEX instructions are folded into multiple HInstructions that need |
| 4343 | * to stay live until the last HInstruction. This class |
| 4344 | * 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] | 4345 | * HInstruction stays live. `index` represents the stack location index of the |
| 4346 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4347 | */ |
| 4348 | class HTemporary : public HTemplateInstruction<0> { |
| 4349 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4350 | explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc) |
| 4351 | : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4352 | |
| 4353 | size_t GetIndex() const { return index_; } |
| 4354 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 4355 | Primitive::Type GetType() const OVERRIDE { |
| 4356 | // The previous instruction is the one that will be stored in the temporary location. |
| 4357 | DCHECK(GetPrevious() != nullptr); |
| 4358 | return GetPrevious()->GetType(); |
| 4359 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 4360 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4361 | DECLARE_INSTRUCTION(Temporary); |
| 4362 | |
| 4363 | private: |
| 4364 | const size_t index_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4365 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 4366 | }; |
| 4367 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4368 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 4369 | public: |
| 4370 | explicit HSuspendCheck(uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4371 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4372 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4373 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4374 | return true; |
| 4375 | } |
| 4376 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 4377 | void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; } |
| 4378 | SlowPathCode* GetSlowPath() const { return slow_path_; } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4379 | |
| 4380 | DECLARE_INSTRUCTION(SuspendCheck); |
| 4381 | |
| 4382 | private: |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 4383 | // Only used for code generation, in order to share the same slow path between back edges |
| 4384 | // of a same loop. |
| 4385 | SlowPathCode* slow_path_; |
| 4386 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 4387 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 4388 | }; |
| 4389 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4390 | /** |
| 4391 | * Instruction to load a Class object. |
| 4392 | */ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4393 | class HLoadClass : public HExpression<1> { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4394 | public: |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4395 | HLoadClass(HCurrentMethod* current_method, |
| 4396 | uint16_t type_index, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4397 | const DexFile& dex_file, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4398 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4399 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4400 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4401 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4402 | dex_file_(dex_file), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4403 | is_referrers_class_(is_referrers_class), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4404 | generate_clinit_check_(false), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4405 | loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) { |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4406 | SetRawInputAt(0, current_method); |
| 4407 | } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4408 | |
| 4409 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4410 | |
| 4411 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4412 | return other->AsLoadClass()->type_index_ == type_index_; |
| 4413 | } |
| 4414 | |
| 4415 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 4416 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4417 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4418 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 4419 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4420 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4421 | bool NeedsEnvironment() const OVERRIDE { |
| 4422 | // Will call runtime and load the class if the class is not loaded yet. |
| 4423 | // TODO: finer grain decision. |
| 4424 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4427 | bool MustGenerateClinitCheck() const { |
| 4428 | return generate_clinit_check_; |
| 4429 | } |
| 4430 | |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 4431 | void SetMustGenerateClinitCheck(bool generate_clinit_check) { |
| 4432 | generate_clinit_check_ = generate_clinit_check; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | bool CanCallRuntime() const { |
| 4436 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 4437 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4438 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4439 | bool CanThrow() const OVERRIDE { |
| 4440 | // May call runtime and and therefore can throw. |
| 4441 | // TODO: finer grain decision. |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 4442 | return CanCallRuntime(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 4445 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 4446 | return loaded_class_rti_; |
| 4447 | } |
| 4448 | |
| 4449 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 4450 | // Make sure we only set exact types (the loaded class should never be merged). |
| 4451 | DCHECK(rti.IsExact()); |
| 4452 | loaded_class_rti_ = rti; |
| 4453 | } |
| 4454 | |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4455 | const DexFile& GetDexFile() { return dex_file_; } |
| 4456 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 4457 | bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; } |
| 4458 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4459 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4460 | return SideEffects::CanTriggerGC(); |
| 4461 | } |
| 4462 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4463 | DECLARE_INSTRUCTION(LoadClass); |
| 4464 | |
| 4465 | private: |
| 4466 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 4467 | const DexFile& dex_file_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4468 | const bool is_referrers_class_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4469 | // Whether this instruction must generate the initialization check. |
| 4470 | // Used for code generation. |
| 4471 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4472 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 4473 | ReferenceTypeInfo loaded_class_rti_; |
| 4474 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4475 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 4476 | }; |
| 4477 | |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4478 | class HLoadString : public HExpression<1> { |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4479 | public: |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4480 | HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4481 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
| 4482 | string_index_(string_index) { |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 4483 | SetRawInputAt(0, current_method); |
| 4484 | } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4485 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4486 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4487 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4488 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4489 | return other->AsLoadString()->string_index_ == string_index_; |
| 4490 | } |
| 4491 | |
| 4492 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 4493 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4494 | uint32_t GetStringIndex() const { return string_index_; } |
| 4495 | |
| 4496 | // TODO: Can we deopt or debug when we resolve a string? |
| 4497 | bool NeedsEnvironment() const OVERRIDE { return false; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 4498 | bool NeedsDexCache() const OVERRIDE { return true; } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 4499 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4500 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4501 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4502 | return SideEffects::CanTriggerGC(); |
| 4503 | } |
| 4504 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4505 | DECLARE_INSTRUCTION(LoadString); |
| 4506 | |
| 4507 | private: |
| 4508 | const uint32_t string_index_; |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 4509 | |
| 4510 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 4511 | }; |
| 4512 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4513 | /** |
| 4514 | * Performs an initialization check on its Class object input. |
| 4515 | */ |
| 4516 | class HClinitCheck : public HExpression<1> { |
| 4517 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 4518 | HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 4519 | : HExpression( |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4520 | Primitive::kPrimNot, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4521 | SideEffects::AllChanges(), // Assume write/read on all fields/arrays. |
| 4522 | dex_pc) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4523 | SetRawInputAt(0, constant); |
| 4524 | } |
| 4525 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 4526 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4527 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 4528 | UNUSED(other); |
| 4529 | return true; |
| 4530 | } |
| 4531 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4532 | bool NeedsEnvironment() const OVERRIDE { |
| 4533 | // May call runtime to initialize the class. |
| 4534 | return true; |
| 4535 | } |
| 4536 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4537 | |
| 4538 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 4539 | |
| 4540 | DECLARE_INSTRUCTION(ClinitCheck); |
| 4541 | |
| 4542 | private: |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4543 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 4544 | }; |
| 4545 | |
| 4546 | class HStaticFieldGet : public HExpression<1> { |
| 4547 | public: |
| 4548 | HStaticFieldGet(HInstruction* cls, |
| 4549 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4550 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4551 | bool is_volatile, |
| 4552 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4553 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4554 | Handle<mirror::DexCache> dex_cache, |
| 4555 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4556 | : HExpression( |
| 4557 | field_type, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4558 | SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4559 | 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] | 4560 | SetRawInputAt(0, cls); |
| 4561 | } |
| 4562 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4563 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 4564 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4565 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4566 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4567 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 4568 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | size_t ComputeHashCode() const OVERRIDE { |
| 4572 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 4573 | } |
| 4574 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4575 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4576 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 4577 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4578 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4579 | |
| 4580 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 4581 | |
| 4582 | private: |
| 4583 | const FieldInfo field_info_; |
| 4584 | |
| 4585 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 4586 | }; |
| 4587 | |
| 4588 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 4589 | public: |
| 4590 | HStaticFieldSet(HInstruction* cls, |
| 4591 | HInstruction* value, |
| 4592 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4593 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 4594 | bool is_volatile, |
| 4595 | uint32_t field_idx, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4596 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4597 | Handle<mirror::DexCache> dex_cache, |
| 4598 | uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4599 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4600 | SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 4601 | 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] | 4602 | value_can_be_null_(true) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4603 | SetRawInputAt(0, cls); |
| 4604 | SetRawInputAt(1, value); |
| 4605 | } |
| 4606 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4607 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4608 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 4609 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4610 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4611 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4612 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4613 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 4614 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4615 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4616 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 4617 | |
| 4618 | private: |
| 4619 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4620 | bool value_can_be_null_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 4621 | |
| 4622 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 4623 | }; |
| 4624 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4625 | // Implement the move-exception DEX instruction. |
| 4626 | class HLoadException : public HExpression<0> { |
| 4627 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4628 | explicit HLoadException(uint32_t dex_pc = kNoDexPc) |
| 4629 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4630 | |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 4631 | bool CanBeNull() const OVERRIDE { return false; } |
| 4632 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4633 | DECLARE_INSTRUCTION(LoadException); |
| 4634 | |
| 4635 | private: |
| 4636 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 4637 | }; |
| 4638 | |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 4639 | // Implicit part of move-exception which clears thread-local exception storage. |
| 4640 | // Must not be removed because the runtime expects the TLS to get cleared. |
| 4641 | class HClearException : public HTemplateInstruction<0> { |
| 4642 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4643 | explicit HClearException(uint32_t dex_pc = kNoDexPc) |
| 4644 | : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {} |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 4645 | |
| 4646 | DECLARE_INSTRUCTION(ClearException); |
| 4647 | |
| 4648 | private: |
| 4649 | DISALLOW_COPY_AND_ASSIGN(HClearException); |
| 4650 | }; |
| 4651 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4652 | class HThrow : public HTemplateInstruction<1> { |
| 4653 | public: |
| 4654 | HThrow(HInstruction* exception, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4655 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4656 | SetRawInputAt(0, exception); |
| 4657 | } |
| 4658 | |
| 4659 | bool IsControlFlow() const OVERRIDE { return true; } |
| 4660 | |
| 4661 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 4662 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4663 | bool CanThrow() const OVERRIDE { return true; } |
| 4664 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4665 | |
| 4666 | DECLARE_INSTRUCTION(Throw); |
| 4667 | |
| 4668 | private: |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 4669 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 4670 | }; |
| 4671 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4672 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4673 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4674 | HInstanceOf(HInstruction* object, |
| 4675 | HLoadClass* constant, |
| 4676 | bool class_is_final, |
| 4677 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4678 | : HExpression(Primitive::kPrimBoolean, |
| 4679 | SideEffectsForArchRuntimeCalls(class_is_final), |
| 4680 | dex_pc), |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4681 | class_is_final_(class_is_final), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4682 | must_do_null_check_(true) { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4683 | SetRawInputAt(0, object); |
| 4684 | SetRawInputAt(1, constant); |
| 4685 | } |
| 4686 | |
| 4687 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4688 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4689 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4690 | return true; |
| 4691 | } |
| 4692 | |
| 4693 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4694 | return false; |
| 4695 | } |
| 4696 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4697 | bool IsClassFinal() const { return class_is_final_; } |
| 4698 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4699 | // Used only in code generation. |
| 4700 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 4701 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 4702 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4703 | static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) { |
| 4704 | return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC(); |
| 4705 | } |
| 4706 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4707 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4708 | |
| 4709 | private: |
| 4710 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4711 | bool must_do_null_check_; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4712 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4713 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 4714 | }; |
| 4715 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4716 | class HBoundType : public HExpression<1> { |
| 4717 | public: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4718 | // Constructs an HBoundType with the given upper_bound. |
| 4719 | // Ensures that the upper_bound is valid. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4720 | HBoundType(HInstruction* input, |
| 4721 | ReferenceTypeInfo upper_bound, |
| 4722 | bool upper_can_be_null, |
| 4723 | uint32_t dex_pc = kNoDexPc) |
| 4724 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc), |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4725 | upper_bound_(upper_bound), |
| 4726 | upper_can_be_null_(upper_can_be_null), |
| 4727 | can_be_null_(upper_can_be_null) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 4728 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4729 | SetRawInputAt(0, input); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 4730 | SetReferenceTypeInfo(upper_bound_); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4733 | // GetUpper* should only be used in reference type propagation. |
| 4734 | const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; } |
| 4735 | bool GetUpperCanBeNull() const { return upper_can_be_null_; } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4736 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4737 | void SetCanBeNull(bool can_be_null) { |
| 4738 | DCHECK(upper_can_be_null_ || !can_be_null); |
| 4739 | can_be_null_ = can_be_null; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 4742 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 4743 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4744 | DECLARE_INSTRUCTION(BoundType); |
| 4745 | |
| 4746 | private: |
| 4747 | // 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] | 4748 | // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()). |
| 4749 | // It is used to bound the type in cases like: |
| 4750 | // if (x instanceof ClassX) { |
| 4751 | // // uper_bound_ will be ClassX |
| 4752 | // } |
| 4753 | const ReferenceTypeInfo upper_bound_; |
| 4754 | // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this |
| 4755 | // is false then can_be_null_ cannot be true). |
| 4756 | const bool upper_can_be_null_; |
| 4757 | bool can_be_null_; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 4758 | |
| 4759 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 4760 | }; |
| 4761 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4762 | class HCheckCast : public HTemplateInstruction<2> { |
| 4763 | public: |
| 4764 | HCheckCast(HInstruction* object, |
| 4765 | HLoadClass* constant, |
| 4766 | bool class_is_final, |
| 4767 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4768 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4769 | class_is_final_(class_is_final), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4770 | must_do_null_check_(true) { |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4771 | SetRawInputAt(0, object); |
| 4772 | SetRawInputAt(1, constant); |
| 4773 | } |
| 4774 | |
| 4775 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4776 | |
| 4777 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4778 | return true; |
| 4779 | } |
| 4780 | |
| 4781 | bool NeedsEnvironment() const OVERRIDE { |
| 4782 | // Instruction may throw a CheckCastError. |
| 4783 | return true; |
| 4784 | } |
| 4785 | |
| 4786 | bool CanThrow() const OVERRIDE { return true; } |
| 4787 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4788 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 4789 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 4790 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4791 | |
| 4792 | bool IsClassFinal() const { return class_is_final_; } |
| 4793 | |
| 4794 | DECLARE_INSTRUCTION(CheckCast); |
| 4795 | |
| 4796 | private: |
| 4797 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 4798 | bool must_do_null_check_; |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 4799 | |
| 4800 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 4801 | }; |
| 4802 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 4803 | class HMemoryBarrier : public HTemplateInstruction<0> { |
| 4804 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4805 | explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 4806 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4807 | SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays. |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 4808 | barrier_kind_(barrier_kind) {} |
| 4809 | |
| 4810 | MemBarrierKind GetBarrierKind() { return barrier_kind_; } |
| 4811 | |
| 4812 | DECLARE_INSTRUCTION(MemoryBarrier); |
| 4813 | |
| 4814 | private: |
| 4815 | const MemBarrierKind barrier_kind_; |
| 4816 | |
| 4817 | DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier); |
| 4818 | }; |
| 4819 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4820 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 4821 | public: |
| 4822 | enum OperationKind { |
| 4823 | kEnter, |
| 4824 | kExit, |
| 4825 | }; |
| 4826 | |
| 4827 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4828 | : HTemplateInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4829 | SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays. |
| 4830 | kind_(kind) { |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4831 | SetRawInputAt(0, object); |
| 4832 | } |
| 4833 | |
| 4834 | // Instruction may throw a Java exception, so we need an environment. |
David Brazdil | bff7503 | 2015-07-08 17:26:51 +0000 | [diff] [blame] | 4835 | bool NeedsEnvironment() const OVERRIDE { return CanThrow(); } |
| 4836 | |
| 4837 | bool CanThrow() const OVERRIDE { |
| 4838 | // Verifier guarantees that monitor-exit cannot throw. |
| 4839 | // This is important because it allows the HGraphBuilder to remove |
| 4840 | // a dead throw-catch loop generated for `synchronized` blocks/methods. |
| 4841 | return IsEnter(); |
| 4842 | } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4843 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4844 | |
| 4845 | bool IsEnter() const { return kind_ == kEnter; } |
| 4846 | |
| 4847 | DECLARE_INSTRUCTION(MonitorOperation); |
| 4848 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4849 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4850 | const OperationKind kind_; |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 4851 | |
| 4852 | private: |
| 4853 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 4854 | }; |
| 4855 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 4856 | /** |
| 4857 | * A HInstruction used as a marker for the replacement of new + <init> |
| 4858 | * of a String to a call to a StringFactory. Only baseline will see |
| 4859 | * the node at code generation, where it will be be treated as null. |
| 4860 | * When compiling non-baseline, `HFakeString` instructions are being removed |
| 4861 | * in the instruction simplifier. |
| 4862 | */ |
| 4863 | class HFakeString : public HTemplateInstruction<0> { |
| 4864 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4865 | explicit HFakeString(uint32_t dex_pc = kNoDexPc) |
| 4866 | : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 4867 | |
| 4868 | Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; } |
| 4869 | |
| 4870 | DECLARE_INSTRUCTION(FakeString); |
| 4871 | |
| 4872 | private: |
| 4873 | DISALLOW_COPY_AND_ASSIGN(HFakeString); |
| 4874 | }; |
| 4875 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 4876 | class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4877 | public: |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4878 | MoveOperands(Location source, |
| 4879 | Location destination, |
| 4880 | Primitive::Type type, |
| 4881 | HInstruction* instruction) |
| 4882 | : source_(source), destination_(destination), type_(type), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4883 | |
| 4884 | Location GetSource() const { return source_; } |
| 4885 | Location GetDestination() const { return destination_; } |
| 4886 | |
| 4887 | void SetSource(Location value) { source_ = value; } |
| 4888 | void SetDestination(Location value) { destination_ = value; } |
| 4889 | |
| 4890 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 4891 | // destination (but not the source). |
| 4892 | Location MarkPending() { |
| 4893 | DCHECK(!IsPending()); |
| 4894 | Location dest = destination_; |
| 4895 | destination_ = Location::NoLocation(); |
| 4896 | return dest; |
| 4897 | } |
| 4898 | |
| 4899 | void ClearPending(Location dest) { |
| 4900 | DCHECK(IsPending()); |
| 4901 | destination_ = dest; |
| 4902 | } |
| 4903 | |
| 4904 | bool IsPending() const { |
| 4905 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 4906 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 4907 | } |
| 4908 | |
| 4909 | // True if this blocks a move from the given location. |
| 4910 | bool Blocks(Location loc) const { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4911 | return !IsEliminated() && source_.OverlapsWith(loc); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | // A move is redundant if it's been eliminated, if its source and |
| 4915 | // destination are the same, or if its destination is unneeded. |
| 4916 | bool IsRedundant() const { |
| 4917 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 4918 | } |
| 4919 | |
| 4920 | // We clear both operands to indicate move that's been eliminated. |
| 4921 | void Eliminate() { |
| 4922 | source_ = destination_ = Location::NoLocation(); |
| 4923 | } |
| 4924 | |
| 4925 | bool IsEliminated() const { |
| 4926 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 4927 | return source_.IsInvalid(); |
| 4928 | } |
| 4929 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 4930 | Primitive::Type GetType() const { return type_; } |
| 4931 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4932 | bool Is64BitMove() const { |
| 4933 | return Primitive::Is64BitType(type_); |
| 4934 | } |
| 4935 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4936 | HInstruction* GetInstruction() const { return instruction_; } |
| 4937 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4938 | private: |
| 4939 | Location source_; |
| 4940 | Location destination_; |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4941 | // The type this move is for. |
| 4942 | Primitive::Type type_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4943 | // The instruction this move is assocatied with. Null when this move is |
| 4944 | // for moving an input in the expected locations of user (including a phi user). |
| 4945 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 4946 | // in the same parallel move. |
| 4947 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4948 | }; |
| 4949 | |
| 4950 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 4951 | |
| 4952 | class HParallelMove : public HTemplateInstruction<0> { |
| 4953 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4954 | explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc) |
| 4955 | : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4956 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4957 | void AddMove(Location source, |
| 4958 | Location destination, |
| 4959 | Primitive::Type type, |
| 4960 | HInstruction* instruction) { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4961 | DCHECK(source.IsValid()); |
| 4962 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4963 | if (kIsDebugBuild) { |
| 4964 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4965 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 4966 | if (moves_.Get(i).GetInstruction() == instruction) { |
| 4967 | // Special case the situation where the move is for the spill slot |
| 4968 | // of the instruction. |
| 4969 | if ((GetPrevious() == instruction) |
| 4970 | || ((GetPrevious() == nullptr) |
| 4971 | && instruction->IsPhi() |
| 4972 | && instruction->GetBlock() == GetBlock())) { |
| 4973 | DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind()) |
| 4974 | << "Doing parallel moves for the same instruction."; |
| 4975 | } else { |
| 4976 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 4977 | } |
| 4978 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4979 | } |
| 4980 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4981 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4982 | DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination())) |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 4983 | << "Overlapped destination for two moves in a parallel move: " |
| 4984 | << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and " |
| 4985 | << source << " ==> " << destination; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4986 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4987 | } |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4988 | moves_.Add(MoveOperands(source, destination, type, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4989 | } |
| 4990 | |
| 4991 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4992 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | size_t NumMoves() const { return moves_.Size(); } |
| 4996 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4997 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4998 | |
| 4999 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 5000 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 5001 | |
| 5002 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 5003 | }; |
| 5004 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 5005 | } // namespace art |
| 5006 | |
| 5007 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 5008 | #include "nodes_x86.h" |
| 5009 | #endif |
| 5010 | |
| 5011 | namespace art { |
| 5012 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5013 | class HGraphVisitor : public ValueObject { |
| 5014 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 5015 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 5016 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5017 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5018 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5019 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 5020 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 5021 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5022 | void VisitInsertionOrder(); |
| 5023 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 5024 | // Visit the graph following dominator tree reverse post-order. |
| 5025 | void VisitReversePostOrder(); |
| 5026 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 5027 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 5028 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5029 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5030 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5031 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 5032 | |
| 5033 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 5034 | |
| 5035 | #undef DECLARE_VISIT_INSTRUCTION |
| 5036 | |
| 5037 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 5038 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5039 | |
| 5040 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 5041 | }; |
| 5042 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5043 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 5044 | public: |
| 5045 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 5046 | virtual ~HGraphDelegateVisitor() {} |
| 5047 | |
| 5048 | // Visit functions that delegate to to super class. |
| 5049 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5050 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 5051 | |
| 5052 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 5053 | |
| 5054 | #undef DECLARE_VISIT_INSTRUCTION |
| 5055 | |
| 5056 | private: |
| 5057 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 5058 | }; |
| 5059 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5060 | class HInsertionOrderIterator : public ValueObject { |
| 5061 | public: |
| 5062 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 5063 | |
| 5064 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 5065 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 5066 | void Advance() { ++index_; } |
| 5067 | |
| 5068 | private: |
| 5069 | const HGraph& graph_; |
| 5070 | size_t index_; |
| 5071 | |
| 5072 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 5073 | }; |
| 5074 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5075 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5076 | public: |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 5077 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) { |
| 5078 | // Check that reverse post order of the graph has been built. |
| 5079 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 5080 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5081 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5082 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 5083 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5084 | void Advance() { ++index_; } |
| 5085 | |
| 5086 | private: |
| 5087 | const HGraph& graph_; |
| 5088 | size_t index_; |
| 5089 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5090 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5091 | }; |
| 5092 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5093 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5094 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5095 | explicit HPostOrderIterator(const HGraph& graph) |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 5096 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) { |
| 5097 | // Check that reverse post order of the graph has been built. |
| 5098 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 5099 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5100 | |
| 5101 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5102 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5103 | void Advance() { --index_; } |
| 5104 | |
| 5105 | private: |
| 5106 | const HGraph& graph_; |
| 5107 | size_t index_; |
| 5108 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 5109 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 5110 | }; |
| 5111 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 5112 | class HLinearPostOrderIterator : public ValueObject { |
| 5113 | public: |
| 5114 | explicit HLinearPostOrderIterator(const HGraph& graph) |
| 5115 | : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {} |
| 5116 | |
| 5117 | bool Done() const { return index_ == 0; } |
| 5118 | |
| 5119 | HBasicBlock* Current() const { return order_.Get(index_ -1); } |
| 5120 | |
| 5121 | void Advance() { |
| 5122 | --index_; |
| 5123 | DCHECK_GE(index_, 0U); |
| 5124 | } |
| 5125 | |
| 5126 | private: |
| 5127 | const GrowableArray<HBasicBlock*>& order_; |
| 5128 | size_t index_; |
| 5129 | |
| 5130 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 5131 | }; |
| 5132 | |
| 5133 | class HLinearOrderIterator : public ValueObject { |
| 5134 | public: |
| 5135 | explicit HLinearOrderIterator(const HGraph& graph) |
| 5136 | : order_(graph.GetLinearOrder()), index_(0) {} |
| 5137 | |
| 5138 | bool Done() const { return index_ == order_.Size(); } |
| 5139 | HBasicBlock* Current() const { return order_.Get(index_); } |
| 5140 | void Advance() { ++index_; } |
| 5141 | |
| 5142 | private: |
| 5143 | const GrowableArray<HBasicBlock*>& order_; |
| 5144 | size_t index_; |
| 5145 | |
| 5146 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 5147 | }; |
| 5148 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 5149 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 5150 | // of an inner loop. The order in which the blocks are iterated is on their |
| 5151 | // block id. |
| 5152 | class HBlocksInLoopIterator : public ValueObject { |
| 5153 | public: |
| 5154 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 5155 | : blocks_in_loop_(info.GetBlocks()), |
| 5156 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 5157 | index_(0) { |
| 5158 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 5159 | Advance(); |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | bool Done() const { return index_ == blocks_.Size(); } |
| 5164 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 5165 | void Advance() { |
| 5166 | ++index_; |
| 5167 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 5168 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 5169 | break; |
| 5170 | } |
| 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | private: |
| 5175 | const BitVector& blocks_in_loop_; |
| 5176 | const GrowableArray<HBasicBlock*>& blocks_; |
| 5177 | size_t index_; |
| 5178 | |
| 5179 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 5180 | }; |
| 5181 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 5182 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 5183 | // of an inner loop. The order in which the blocks are iterated is reverse |
| 5184 | // post order. |
| 5185 | class HBlocksInLoopReversePostOrderIterator : public ValueObject { |
| 5186 | public: |
| 5187 | explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info) |
| 5188 | : blocks_in_loop_(info.GetBlocks()), |
| 5189 | blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()), |
| 5190 | index_(0) { |
| 5191 | if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) { |
| 5192 | Advance(); |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | bool Done() const { return index_ == blocks_.Size(); } |
| 5197 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 5198 | void Advance() { |
| 5199 | ++index_; |
| 5200 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 5201 | if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) { |
| 5202 | break; |
| 5203 | } |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | private: |
| 5208 | const BitVector& blocks_in_loop_; |
| 5209 | const GrowableArray<HBasicBlock*>& blocks_; |
| 5210 | size_t index_; |
| 5211 | |
| 5212 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator); |
| 5213 | }; |
| 5214 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 5215 | inline int64_t Int64FromConstant(HConstant* constant) { |
| 5216 | DCHECK(constant->IsIntConstant() || constant->IsLongConstant()); |
| 5217 | return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue() |
| 5218 | : constant->AsLongConstant()->GetValue(); |
| 5219 | } |
| 5220 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 5221 | inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) { |
| 5222 | // For the purposes of the compiler, the dex files must actually be the same object |
| 5223 | // if we want to safely treat them as the same. This is especially important for JIT |
| 5224 | // as custom class loaders can open the same underlying file (or memory) multiple |
| 5225 | // times and provide different class resolution but no two class loaders should ever |
| 5226 | // use the same DexFile object - doing so is an unsupported hack that can lead to |
| 5227 | // all sorts of weird failures. |
| 5228 | return &lhs == &rhs; |
| 5229 | } |
| 5230 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 5231 | } // namespace art |
| 5232 | |
| 5233 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |