blob: fef6f21b4665eefb0e9b17aa9d27f0bb896040fd [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Markof9f64412015-09-02 14:05:49 +010020#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010021#include <type_traits>
22
David Brazdil8d5b8b22015-03-24 10:51:52 +000023#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080024#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010025#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000026#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000027#include "handle.h"
28#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000029#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010030#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000031#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000032#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010033#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070034#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000035#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036#include "utils/growable_array.h"
37
38namespace art {
39
David Brazdil1abb4192015-02-17 18:33:36 +000040class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010042class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000043class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010044class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010045class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000047class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000050class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000051class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000053class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010055class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010056class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010057class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000058class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010059class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061
Mathieu Chartier736b5602015-09-02 14:54:11 -070062namespace mirror {
63class DexCache;
64} // namespace mirror
65
Nicolas Geoffray818f2102014-02-18 16:43:35 +000066static const int kDefaultNumberOfBlocks = 8;
67static const int kDefaultNumberOfSuccessors = 2;
68static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010069static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010070static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000071static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Calin Juravle9aec02f2014-11-18 23:06:35 +000073static constexpr uint32_t kMaxIntShiftValue = 0x1f;
74static constexpr uint64_t kMaxLongShiftValue = 0x3f;
75
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010076static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
77
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010078static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
79
Dave Allison20dfc792014-06-16 20:44:29 -070080enum IfCondition {
81 kCondEQ,
82 kCondNE,
83 kCondLT,
84 kCondLE,
85 kCondGT,
86 kCondGE,
87};
88
Vladimir Markof9f64412015-09-02 14:05:49 +010089class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 public:
91 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
92
93 void AddInstruction(HInstruction* instruction);
94 void RemoveInstruction(HInstruction* instruction);
95
David Brazdilc3d743f2015-04-22 13:40:50 +010096 // Insert `instruction` before/after an existing instruction `cursor`.
97 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
98 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
99
Roland Levillain6b469232014-09-25 10:10:38 +0100100 // Return true if this list contains `instruction`.
101 bool Contains(HInstruction* instruction) const;
102
Roland Levillainccc07a92014-09-16 14:48:16 +0100103 // Return true if `instruction1` is found before `instruction2` in
104 // this instruction list and false otherwise. Abort if none
105 // of these instructions is found.
106 bool FoundBefore(const HInstruction* instruction1,
107 const HInstruction* instruction2) const;
108
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000109 bool IsEmpty() const { return first_instruction_ == nullptr; }
110 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
111
112 // Update the block of all instructions to be `block`.
113 void SetBlockOfInstructions(HBasicBlock* block) const;
114
115 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
116 void Add(const HInstructionList& instruction_list);
117
David Brazdil2d7352b2015-04-20 14:52:42 +0100118 // Return the number of instructions in the list. This is an expensive operation.
119 size_t CountSize() const;
120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100121 private:
122 HInstruction* first_instruction_;
123 HInstruction* last_instruction_;
124
125 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000126 friend class HGraph;
127 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100128 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100129 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100130
131 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
132};
133
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000134// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100135class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100137 HGraph(ArenaAllocator* arena,
138 const DexFile& dex_file,
139 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100140 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100142 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100143 bool debuggable = false,
144 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100147 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100148 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700149 entry_block_(nullptr),
150 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100151 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 number_of_vregs_(0),
153 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000154 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400155 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000156 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000157 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100158 dex_file_(dex_file),
159 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100160 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100161 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100162 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000164 cached_null_constant_(nullptr),
165 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000166 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
167 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100168 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
169 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100173 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174
David Brazdil69ba7b72015-06-23 18:27:30 +0100175 bool IsInSsaForm() const { return in_ssa_form_; }
176
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000177 HBasicBlock* GetEntryBlock() const { return entry_block_; }
178 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100179 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000180
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
182 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 // Try building the SSA form of this graph, with dominance computation and loop
187 // recognition. Returns whether it was successful in doing all these steps.
188 bool TryBuildingSsa() {
189 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000190 // The SSA builder requires loops to all be natural. Specifically, the dead phi
191 // elimination phase checks the consistency of the graph when doing a post-order
192 // visit for eliminating dead phis: a dead phi can only have loop header phi
193 // users remaining when being visited.
194 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100195 // Precompute per-block try membership before entering the SSA builder,
196 // which needs the information to build catch block phis from values of
197 // locals at throwing instructions inside try blocks.
198 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100200 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000201 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 }
203
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100204 void ComputeDominanceInformation();
205 void ClearDominanceInformation();
206
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100209 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100210 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000212 // Analyze all natural loops in this graph. Returns false if one
213 // loop is not natural, that is the header does not dominate the
214 // back edge.
215 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100216
David Brazdilffee3d32015-07-06 11:48:53 +0100217 // Iterate over blocks to compute try block membership. Needs reverse post
218 // order and loop information.
219 void ComputeTryBlockInformation();
220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000222 // Returns the instruction used to replace the invoke expression or null if the
223 // invoke is for a void method.
224 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225
Mingyao Yang3584bce2015-05-19 16:01:59 -0700226 // Need to add a couple of blocks to test if the loop body is entered and
227 // put deoptimization instructions, etc.
228 void TransformLoopHeaderForBCE(HBasicBlock* header);
229
David Brazdil2d7352b2015-04-20 14:52:42 +0100230 // Removes `block` from the graph.
231 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000232
David Brazdilfc6a86a2015-06-26 10:33:45 +0000233 // Splits the edge between `block` and `successor` while preserving the
234 // indices in the predecessor/successor lists. If there are multiple edges
235 // between the blocks, the lowest indices are used.
236 // Returns the new block which is empty and has the same dex pc as `successor`.
237 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
238
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100239 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
240 void SimplifyLoop(HBasicBlock* header);
241
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 int32_t GetNextInstructionId() {
243 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244 return current_instruction_id_++;
245 }
246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000247 int32_t GetCurrentInstructionId() const {
248 return current_instruction_id_;
249 }
250
251 void SetCurrentInstructionId(int32_t id) {
252 current_instruction_id_ = id;
253 }
254
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100255 uint16_t GetMaximumNumberOfOutVRegs() const {
256 return maximum_number_of_out_vregs_;
257 }
258
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000259 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
260 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100261 }
262
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100263 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
264 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
265 }
266
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000267 void UpdateTemporariesVRegSlots(size_t slots) {
268 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 }
270
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000271 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100272 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000273 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100274 }
275
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100276 void SetNumberOfVRegs(uint16_t number_of_vregs) {
277 number_of_vregs_ = number_of_vregs;
278 }
279
280 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100281 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100282 return number_of_vregs_;
283 }
284
285 void SetNumberOfInVRegs(uint16_t value) {
286 number_of_in_vregs_ = value;
287 }
288
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100289 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100290 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100291 return number_of_vregs_ - number_of_in_vregs_;
292 }
293
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100294 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
295 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100296 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100297
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100298 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
299 return linear_order_;
300 }
301
Mark Mendell1152c922015-04-24 17:06:35 -0400302 bool HasBoundsChecks() const {
303 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800304 }
305
Mark Mendell1152c922015-04-24 17:06:35 -0400306 void SetHasBoundsChecks(bool value) {
307 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800308 }
309
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100310 bool ShouldGenerateConstructorBarrier() const {
311 return should_generate_constructor_barrier_;
312 }
313
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000314 bool IsDebuggable() const { return debuggable_; }
315
David Brazdil8d5b8b22015-03-24 10:51:52 +0000316 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000317 // already, it is created and inserted into the graph. This method is only for
318 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000319 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000320
321 // TODO: This is problematic for the consistency of reference type propagation
322 // because it can be created anytime after the pass and thus it will be left
323 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000324 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000325
David Brazdil8d5b8b22015-03-24 10:51:52 +0000326 HIntConstant* GetIntConstant(int32_t value) {
327 return CreateConstant(value, &cached_int_constants_);
328 }
329 HLongConstant* GetLongConstant(int64_t value) {
330 return CreateConstant(value, &cached_long_constants_);
331 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000332 HFloatConstant* GetFloatConstant(float value) {
333 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
334 }
335 HDoubleConstant* GetDoubleConstant(double value) {
336 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
337 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000338
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100339 HCurrentMethod* GetCurrentMethod();
340
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000341 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100342
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100343 const DexFile& GetDexFile() const {
344 return dex_file_;
345 }
346
347 uint32_t GetMethodIdx() const {
348 return method_idx_;
349 }
350
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100351 InvokeType GetInvokeType() const {
352 return invoke_type_;
353 }
354
Mark Mendellc4701932015-04-10 13:18:51 -0400355 InstructionSet GetInstructionSet() const {
356 return instruction_set_;
357 }
358
David Brazdilbbd733e2015-08-18 17:48:17 +0100359 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
360 bool HasTryCatch() const;
361
David Brazdil2d7352b2015-04-20 14:52:42 +0100362 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363 void VisitBlockForDominatorTree(HBasicBlock* block,
364 HBasicBlock* predecessor,
365 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100366 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367 void VisitBlockForBackEdges(HBasicBlock* block,
368 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100369 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000370 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100371 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000373 template <class InstructionType, typename ValueType>
374 InstructionType* CreateConstant(ValueType value,
375 ArenaSafeMap<ValueType, InstructionType*>* cache) {
376 // Try to find an existing constant of the given value.
377 InstructionType* constant = nullptr;
378 auto cached_constant = cache->find(value);
379 if (cached_constant != cache->end()) {
380 constant = cached_constant->second;
381 }
382
383 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100384 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000385 if (constant == nullptr || constant->GetBlock() == nullptr) {
386 constant = new (arena_) InstructionType(value);
387 cache->Overwrite(value, constant);
388 InsertConstant(constant);
389 }
390 return constant;
391 }
392
David Brazdil8d5b8b22015-03-24 10:51:52 +0000393 void InsertConstant(HConstant* instruction);
394
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000395 // Cache a float constant into the graph. This method should only be
396 // called by the SsaBuilder when creating "equivalent" instructions.
397 void CacheFloatConstant(HFloatConstant* constant);
398
399 // See CacheFloatConstant comment.
400 void CacheDoubleConstant(HDoubleConstant* constant);
401
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000403
404 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000405 GrowableArray<HBasicBlock*> blocks_;
406
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 // List of blocks to perform a reverse post order tree traversal.
408 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100410 // List of blocks to perform a linear order tree traversal.
411 GrowableArray<HBasicBlock*> linear_order_;
412
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000413 HBasicBlock* entry_block_;
414 HBasicBlock* exit_block_;
415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100417 uint16_t maximum_number_of_out_vregs_;
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 // The number of virtual registers in this method. Contains the parameters.
420 uint16_t number_of_vregs_;
421
422 // The number of virtual registers used by parameters of this method.
423 uint16_t number_of_in_vregs_;
424
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000425 // Number of vreg size slots that the temporaries use (used in baseline compiler).
426 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100427
Mark Mendell1152c922015-04-24 17:06:35 -0400428 // Has bounds checks. We can totally skip BCE if it's false.
429 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800430
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000431 // Indicates whether the graph should be compiled in a way that
432 // ensures full debuggability. If false, we can apply more
433 // aggressive optimizations that may limit the level of debugging.
434 const bool debuggable_;
435
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000436 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000437 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000438
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100439 // The dex file from which the method is from.
440 const DexFile& dex_file_;
441
442 // The method index in the dex file.
443 const uint32_t method_idx_;
444
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100445 // If inlined, this encodes how the callee is being invoked.
446 const InvokeType invoke_type_;
447
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100448 // Whether the graph has been transformed to SSA form. Only used
449 // in debug mode to ensure we are not using properties only valid
450 // for non-SSA form (like the number of temporaries).
451 bool in_ssa_form_;
452
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100453 const bool should_generate_constructor_barrier_;
454
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 const InstructionSet instruction_set_;
456
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000457 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000458 HNullConstant* cached_null_constant_;
459 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000460 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100464 HCurrentMethod* cached_current_method_;
465
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000466 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100467 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000468 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000469 DISALLOW_COPY_AND_ASSIGN(HGraph);
470};
471
Vladimir Markof9f64412015-09-02 14:05:49 +0100472class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000473 public:
474 HLoopInformation(HBasicBlock* header, HGraph* graph)
475 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100476 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100477 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100478 // Make bit vector growable, as the number of blocks may change.
479 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480
481 HBasicBlock* GetHeader() const {
482 return header_;
483 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000485 void SetHeader(HBasicBlock* block) {
486 header_ = block;
487 }
488
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100489 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
490 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
491 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
492
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000493 void AddBackEdge(HBasicBlock* back_edge) {
494 back_edges_.Add(back_edge);
495 }
496
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 void RemoveBackEdge(HBasicBlock* back_edge) {
498 back_edges_.Delete(back_edge);
499 }
500
David Brazdil46e2a392015-03-16 17:31:52 +0000501 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000503 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 }
505 return false;
506 }
507
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000508 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000509 return back_edges_.Size();
510 }
511
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
515 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516 }
517
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100518 // Returns the lifetime position of the back edge that has the
519 // greatest lifetime position.
520 size_t GetLifetimeEnd() const;
521
522 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
523 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
524 if (back_edges_.Get(i) == existing) {
525 back_edges_.Put(i, new_back_edge);
526 return;
527 }
528 }
529 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100530 }
531
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100532 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 // that is the header dominates the back edge.
534 bool Populate();
535
David Brazdila4b8c212015-05-07 09:59:30 +0100536 // Reanalyzes the loop by removing loop info from its blocks and re-running
537 // Populate(). If there are no back edges left, the loop info is completely
538 // removed as well as its SuspendCheck instruction. It must be run on nested
539 // inner loops first.
540 void Update();
541
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // Returns whether this loop information contains `block`.
543 // Note that this loop information *must* be populated before entering this function.
544 bool Contains(const HBasicBlock& block) const;
545
546 // Returns whether this loop information is an inner loop of `other`.
547 // Note that `other` *must* be populated before entering this function.
548 bool IsIn(const HLoopInformation& other) const;
549
550 const ArenaBitVector& GetBlocks() const { return blocks_; }
551
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000552 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000553 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000554
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100556 // Internal recursive implementation of `Populate`.
557 void PopulateRecursive(HBasicBlock* block);
558
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100560 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100562 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563
564 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
565};
566
David Brazdilec16f792015-08-19 15:04:01 +0100567// Stores try/catch information for basic blocks.
568// Note that HGraph is constructed so that catch blocks cannot simultaneously
569// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100570class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100571 public:
572 // Try block information constructor.
573 explicit TryCatchInformation(const HTryBoundary& try_entry)
574 : try_entry_(&try_entry),
575 catch_dex_file_(nullptr),
576 catch_type_index_(DexFile::kDexNoIndex16) {
577 DCHECK(try_entry_ != nullptr);
578 }
579
580 // Catch block information constructor.
581 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
582 : try_entry_(nullptr),
583 catch_dex_file_(&dex_file),
584 catch_type_index_(catch_type_index) {}
585
586 bool IsTryBlock() const { return try_entry_ != nullptr; }
587
588 const HTryBoundary& GetTryEntry() const {
589 DCHECK(IsTryBlock());
590 return *try_entry_;
591 }
592
593 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
594
595 bool IsCatchAllTypeIndex() const {
596 DCHECK(IsCatchBlock());
597 return catch_type_index_ == DexFile::kDexNoIndex16;
598 }
599
600 uint16_t GetCatchTypeIndex() const {
601 DCHECK(IsCatchBlock());
602 return catch_type_index_;
603 }
604
605 const DexFile& GetCatchDexFile() const {
606 DCHECK(IsCatchBlock());
607 return *catch_dex_file_;
608 }
609
610 private:
611 // One of possibly several TryBoundary instructions entering the block's try.
612 // Only set for try blocks.
613 const HTryBoundary* try_entry_;
614
615 // Exception type information. Only set for catch blocks.
616 const DexFile* catch_dex_file_;
617 const uint16_t catch_type_index_;
618};
619
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100620static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100621static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100622
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000623// A block in a method. Contains the list of instructions represented
624// as a double linked list. Each block knows its predecessors and
625// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100626
Vladimir Markof9f64412015-09-02 14:05:49 +0100627class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000628 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100629 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630 : graph_(graph),
Vladimir Marko145acc52015-09-03 13:33:25 +0000631 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
632 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633 loop_information_(nullptr),
634 dominator_(nullptr),
Vladimir Marko145acc52015-09-03 13:33:25 +0000635 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100636 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100637 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100638 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 lifetime_end_(kNoLifetime),
Vladimir Marko145acc52015-09-03 13:33:25 +0000640 try_catch_information_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Vladimir Marko145acc52015-09-03 13:33:25 +0000642 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100643 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 }
645
Vladimir Marko145acc52015-09-03 13:33:25 +0000646 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 }
649
Vladimir Marko145acc52015-09-03 13:33:25 +0000650 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100651 return dominated_blocks_;
652 }
653
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100654 bool IsEntryBlock() const {
655 return graph_->GetEntryBlock() == this;
656 }
657
658 bool IsExitBlock() const {
659 return graph_->GetExitBlock() == this;
660 }
661
David Brazdil46e2a392015-03-16 17:31:52 +0000662 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000663 bool IsSingleTryBoundary() const;
664
665 // Returns true if this block emits nothing but a jump.
666 bool IsSingleJump() const {
667 HLoopInformation* loop_info = GetLoopInformation();
668 return (IsSingleGoto() || IsSingleTryBoundary())
669 // Back edges generate a suspend check.
670 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
671 }
David Brazdil46e2a392015-03-16 17:31:52 +0000672
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000673 void AddBackEdge(HBasicBlock* back_edge) {
674 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000676 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 loop_information_->AddBackEdge(back_edge);
679 }
680
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000681 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000682 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000683
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000684 int GetBlockId() const { return block_id_; }
685 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000686
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 HBasicBlock* GetDominator() const { return dominator_; }
688 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko145acc52015-09-03 13:33:25 +0000689 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
690 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Vladimir Marko91e11c02015-09-02 17:03:22 +0100691 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000692 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
693 if (dominated_blocks_.Get(i) == existing) {
694 dominated_blocks_.Put(i, new_block);
695 return;
696 }
697 }
698 LOG(FATAL) << "Unreachable";
699 UNREACHABLE();
Vladimir Marko91e11c02015-09-02 17:03:22 +0100700 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100701 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702
703 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100704 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705 }
706
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
708 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100709 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100710 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100711 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
712 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
714 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000715 successors_.Add(block);
716 block->predecessors_.Add(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000717 }
718
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100719 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
720 size_t successor_index = GetSuccessorIndexOf(existing);
Vladimir Marko145acc52015-09-03 13:33:25 +0000721 DCHECK_NE(successor_index, static_cast<size_t>(-1));
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100722 existing->RemovePredecessor(this);
Vladimir Marko145acc52015-09-03 13:33:25 +0000723 new_block->predecessors_.Add(this);
724 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000725 }
726
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000727 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
728 size_t predecessor_index = GetPredecessorIndexOf(existing);
Vladimir Marko145acc52015-09-03 13:33:25 +0000729 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000730 existing->RemoveSuccessor(this);
Vladimir Marko145acc52015-09-03 13:33:25 +0000731 new_block->successors_.Add(this);
732 predecessors_.Put(predecessor_index, new_block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000733 }
734
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100735 // Insert `this` between `predecessor` and `successor. This method
736 // preserves the indicies, and will update the first edge found between
737 // `predecessor` and `successor`.
738 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
739 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Vladimir Marko145acc52015-09-03 13:33:25 +0000740 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100741 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko145acc52015-09-03 13:33:25 +0000742 DCHECK_NE(successor_index, static_cast<size_t>(-1));
743 successor->predecessors_.Put(predecessor_index, this);
744 predecessor->successors_.Put(successor_index, this);
745 successors_.Add(successor);
746 predecessors_.Add(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100747 }
748
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100749 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000750 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100751 }
752
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000754 successors_.Delete(block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000755 }
756
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 void ClearAllPredecessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000758 predecessors_.Reset();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 }
760
761 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000762 predecessors_.Add(block);
763 block->successors_.Add(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 }
765
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100766 void SwapPredecessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000767 DCHECK_EQ(predecessors_.Size(), 2u);
768 HBasicBlock* temp = predecessors_.Get(0);
769 predecessors_.Put(0, predecessors_.Get(1));
770 predecessors_.Put(1, temp);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100771 }
772
David Brazdil769c9e52015-04-27 13:54:09 +0100773 void SwapSuccessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000774 DCHECK_EQ(successors_.Size(), 2u);
775 HBasicBlock* temp = successors_.Get(0);
776 successors_.Put(0, successors_.Get(1));
777 successors_.Put(1, temp);
David Brazdil769c9e52015-04-27 13:54:09 +0100778 }
779
David Brazdilfc6a86a2015-06-26 10:33:45 +0000780 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000781 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
782 if (predecessors_.Get(i) == predecessor) {
783 return i;
784 }
785 }
786 return -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100787 }
788
David Brazdilfc6a86a2015-06-26 10:33:45 +0000789 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000790 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
791 if (successors_.Get(i) == successor) {
792 return i;
793 }
794 }
795 return -1;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100796 }
797
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000799 DCHECK_EQ(GetPredecessors().Size(), 1u);
800 return GetPredecessors().Get(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 }
802
803 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000804 DCHECK_EQ(GetSuccessors().Size(), 1u);
805 return GetSuccessors().Get(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 }
807
808 // Returns whether the first occurrence of `predecessor` in the list of
809 // predecessors is at index `idx`.
810 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000811 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 return GetPredecessorIndexOf(predecessor) == idx;
813 }
814
David Brazdilffee3d32015-07-06 11:48:53 +0100815 // Returns the number of non-exceptional successors. SsaChecker ensures that
816 // these are stored at the beginning of the successor list.
817 size_t NumberOfNormalSuccessors() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000818 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
David Brazdilffee3d32015-07-06 11:48:53 +0100819 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820
821 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100822 // created, latter block. Note that this method will add the block to the
823 // graph, create a Goto at the end of the former block and will create an edge
824 // between the blocks. It will not, however, update the reverse post order or
825 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000826 HBasicBlock* SplitBefore(HInstruction* cursor);
827
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 // Split the block into two blocks just after `cursor`. Returns the newly
829 // created block. Note that this method just updates raw block information,
830 // like predecessors, successors, dominators, and instruction list. It does not
831 // update the graph, reverse post order, loop information, nor make sure the
832 // blocks are consistent (for example ending with a control flow instruction).
833 HBasicBlock* SplitAfter(HInstruction* cursor);
834
835 // Merge `other` at the end of `this`. Successors and dominated blocks of
836 // `other` are changed to be successors and dominated blocks of `this`. Note
837 // that this method does not update the graph, reverse post order, loop
838 // information, nor make sure the blocks are consistent (for example ending
839 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100840 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000841
842 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
843 // of `this` are moved to `other`.
844 // Note that this method does not update the graph, reverse post order, loop
845 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000846 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 void ReplaceWith(HBasicBlock* other);
848
David Brazdil2d7352b2015-04-20 14:52:42 +0100849 // Merge `other` at the end of `this`. This method updates loops, reverse post
850 // order, links to predecessors, successors, dominators and deletes the block
851 // from the graph. The two blocks must be successive, i.e. `this` the only
852 // predecessor of `other` and vice versa.
853 void MergeWith(HBasicBlock* other);
854
855 // Disconnects `this` from all its predecessors, successors and dominator,
856 // removes it from all loops it is included in and eventually from the graph.
857 // The block must not dominate any other block. Predecessors and successors
858 // are safely updated.
859 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000860
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100862 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100863 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100864 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100865 // Replace instruction `initial` with `replacement` within this block.
866 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
867 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100869 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000870 // RemoveInstruction and RemovePhi delete a given instruction from the respective
871 // instruction list. With 'ensure_safety' set to true, it verifies that the
872 // instruction is not in use and removes it from the use lists of its inputs.
873 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
874 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100875 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876
877 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100878 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 }
880
Roland Levillain6b879dd2014-09-22 17:13:44 +0100881 bool IsLoopPreHeaderFirstPredecessor() const {
882 DCHECK(IsLoopHeader());
Vladimir Marko145acc52015-09-03 13:33:25 +0000883 DCHECK(!GetPredecessors().IsEmpty());
884 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100885 }
886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 HLoopInformation* GetLoopInformation() const {
888 return loop_information_;
889 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 void SetInLoop(HLoopInformation* info) {
895 if (IsLoopHeader()) {
896 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100897 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 loop_information_ = info;
899 } else if (loop_information_->Contains(*info->GetHeader())) {
900 // Block is currently part of an outer loop. Make it part of this inner loop.
901 // Note that a non loop header having a loop information means this loop information
902 // has already been populated
903 loop_information_ = info;
904 } else {
905 // Block is part of an inner loop. Do not update the loop information.
906 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
907 // at this point, because this method is being called while populating `info`.
908 }
909 }
910
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 // Raw update of the loop information.
912 void SetLoopInformation(HLoopInformation* info) {
913 loop_information_ = info;
914 }
915
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100916 bool IsInLoop() const { return loop_information_ != nullptr; }
917
David Brazdilec16f792015-08-19 15:04:01 +0100918 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
919
920 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
921 try_catch_information_ = try_catch_information;
922 }
923
924 bool IsTryBlock() const {
925 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
926 }
927
928 bool IsCatchBlock() const {
929 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
930 }
David Brazdilffee3d32015-07-06 11:48:53 +0100931
932 // Returns the try entry that this block's successors should have. They will
933 // be in the same try, unless the block ends in a try boundary. In that case,
934 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100935 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100936
David Brazdila4b8c212015-05-07 09:59:30 +0100937 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 bool Dominates(HBasicBlock* block) const;
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940 size_t GetLifetimeStart() const { return lifetime_start_; }
941 size_t GetLifetimeEnd() const { return lifetime_end_; }
942
943 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
944 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
945
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100946 uint32_t GetDexPc() const { return dex_pc_; }
947
David Brazdil8d5b8b22015-03-24 10:51:52 +0000948 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000949 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100950 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000951 bool HasSinglePhi() const;
952
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000954 HGraph* graph_;
Vladimir Marko145acc52015-09-03 13:33:25 +0000955 GrowableArray<HBasicBlock*> predecessors_;
956 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100957 HInstructionList instructions_;
958 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000959 HLoopInformation* loop_information_;
960 HBasicBlock* dominator_;
Vladimir Marko145acc52015-09-03 13:33:25 +0000961 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100963 // The dex program counter of the first instruction of this block.
964 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100965 size_t lifetime_start_;
966 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100967 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100968
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000969 friend class HGraph;
970 friend class HInstruction;
971
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
973};
974
David Brazdilb2bd1c52015-03-25 11:17:37 +0000975// Iterates over the LoopInformation of all loops which contain 'block'
976// from the innermost to the outermost.
977class HLoopInformationOutwardIterator : public ValueObject {
978 public:
979 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
980 : current_(block.GetLoopInformation()) {}
981
982 bool Done() const { return current_ == nullptr; }
983
984 void Advance() {
985 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100986 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000987 }
988
989 HLoopInformation* Current() const {
990 DCHECK(!Done());
991 return current_;
992 }
993
994 private:
995 HLoopInformation* current_;
996
997 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
998};
999
Alexandre Ramesef20f712015-06-09 10:29:30 +01001000#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001002 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(ArrayGet, Instruction) \
1004 M(ArrayLength, Instruction) \
1005 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001006 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001008 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001009 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001010 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001011 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001013 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001014 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001015 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001016 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001017 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001019 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001021 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(FloatConstant, Constant) \
1023 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(GreaterThan, Condition) \
1025 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001027 M(InstanceFieldGet, Instruction) \
1028 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001029 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001030 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001031 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001032 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(LessThan, Condition) \
1035 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001036 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001037 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001039 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(Local, Instruction) \
1041 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001042 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001043 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 M(Mul, BinaryOperation) \
1045 M(Neg, UnaryOperation) \
1046 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001048 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001050 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001052 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001056 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(Return, Instruction) \
1058 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001059 M(Shl, BinaryOperation) \
1060 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001061 M(StaticFieldGet, Instruction) \
1062 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(StoreLocal, Instruction) \
1064 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001066 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001067 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001068 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001069 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001070 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001071 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
Alexandre Ramesef20f712015-06-09 10:29:30 +01001073#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1074
1075#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1076
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001077#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1078
Alexandre Ramesef20f712015-06-09 10:29:30 +01001079#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1080
1081#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1082
1083#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1085 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1086 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001087 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001088 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1089 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1090
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001091#define FOR_EACH_INSTRUCTION(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1093 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001094 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001095 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001097
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001098#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1100#undef FORWARD_DECLARATION
1101
Roland Levillainccc07a92014-09-16 14:48:16 +01001102#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001103 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1104 const char* DebugName() const OVERRIDE { return #type; } \
1105 const H##type* As##type() const OVERRIDE { return this; } \
1106 H##type* As##type() OVERRIDE { return this; } \
1107 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001108 return other->Is##type(); \
1109 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001110 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111
David Brazdiled596192015-01-23 10:39:45 +00001112template <typename T> class HUseList;
1113
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001115class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001116 public:
David Brazdiled596192015-01-23 10:39:45 +00001117 HUseListNode* GetPrevious() const { return prev_; }
1118 HUseListNode* GetNext() const { return next_; }
1119 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001121 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001123 private:
David Brazdiled596192015-01-23 10:39:45 +00001124 HUseListNode(T user, size_t index)
1125 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1126
1127 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001128 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001129 HUseListNode<T>* prev_;
1130 HUseListNode<T>* next_;
1131
1132 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133
1134 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1135};
1136
David Brazdiled596192015-01-23 10:39:45 +00001137template <typename T>
1138class HUseList : public ValueObject {
1139 public:
1140 HUseList() : first_(nullptr) {}
1141
1142 void Clear() {
1143 first_ = nullptr;
1144 }
1145
1146 // Adds a new entry at the beginning of the use list and returns
1147 // the newly created node.
1148 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001149 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001150 if (IsEmpty()) {
1151 first_ = new_node;
1152 } else {
1153 first_->prev_ = new_node;
1154 new_node->next_ = first_;
1155 first_ = new_node;
1156 }
1157 return new_node;
1158 }
1159
1160 HUseListNode<T>* GetFirst() const {
1161 return first_;
1162 }
1163
1164 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001165 DCHECK(node != nullptr);
1166 DCHECK(Contains(node));
1167
David Brazdiled596192015-01-23 10:39:45 +00001168 if (node->prev_ != nullptr) {
1169 node->prev_->next_ = node->next_;
1170 }
1171 if (node->next_ != nullptr) {
1172 node->next_->prev_ = node->prev_;
1173 }
1174 if (node == first_) {
1175 first_ = node->next_;
1176 }
1177 }
1178
David Brazdil1abb4192015-02-17 18:33:36 +00001179 bool Contains(const HUseListNode<T>* node) const {
1180 if (node == nullptr) {
1181 return false;
1182 }
1183 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1184 if (current == node) {
1185 return true;
1186 }
1187 }
1188 return false;
1189 }
1190
David Brazdiled596192015-01-23 10:39:45 +00001191 bool IsEmpty() const {
1192 return first_ == nullptr;
1193 }
1194
1195 bool HasOnlyOneUse() const {
1196 return first_ != nullptr && first_->next_ == nullptr;
1197 }
1198
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001199 size_t SizeSlow() const {
1200 size_t count = 0;
1201 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1202 ++count;
1203 }
1204 return count;
1205 }
1206
David Brazdiled596192015-01-23 10:39:45 +00001207 private:
1208 HUseListNode<T>* first_;
1209};
1210
1211template<typename T>
1212class HUseIterator : public ValueObject {
1213 public:
1214 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1215
1216 bool Done() const { return current_ == nullptr; }
1217
1218 void Advance() {
1219 DCHECK(!Done());
1220 current_ = current_->GetNext();
1221 }
1222
1223 HUseListNode<T>* Current() const {
1224 DCHECK(!Done());
1225 return current_;
1226 }
1227
1228 private:
1229 HUseListNode<T>* current_;
1230
1231 friend class HValue;
1232};
1233
David Brazdil1abb4192015-02-17 18:33:36 +00001234// This class is used by HEnvironment and HInstruction classes to record the
1235// instructions they use and pointers to the corresponding HUseListNodes kept
1236// by the used instructions.
1237template <typename T>
1238class HUserRecord : public ValueObject {
1239 public:
1240 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1241 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1242
1243 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1244 : instruction_(old_record.instruction_), use_node_(use_node) {
1245 DCHECK(instruction_ != nullptr);
1246 DCHECK(use_node_ != nullptr);
1247 DCHECK(old_record.use_node_ == nullptr);
1248 }
1249
1250 HInstruction* GetInstruction() const { return instruction_; }
1251 HUseListNode<T>* GetUseNode() const { return use_node_; }
1252
1253 private:
1254 // Instruction used by the user.
1255 HInstruction* instruction_;
1256
1257 // Corresponding entry in the use list kept by 'instruction_'.
1258 HUseListNode<T>* use_node_;
1259};
1260
Aart Bik854a02b2015-07-14 16:07:00 -07001261/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001262 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001263 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001264 * For write/read dependences on fields/arrays, the dependence analysis uses
1265 * type disambiguation (e.g. a float field write cannot modify the value of an
1266 * integer field read) and the access type (e.g. a reference array write cannot
1267 * modify the value of a reference field read [although it may modify the
1268 * reference fetch prior to reading the field, which is represented by its own
1269 * write/read dependence]). The analysis makes conservative points-to
1270 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1271 * the same, and any reference read depends on any reference read without
1272 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001273 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 * The internal representation uses 38-bit and is described in the table below.
1275 * The first line indicates the side effect, and for field/array accesses the
1276 * second line indicates the type of the access (in the order of the
1277 * Primitive::Type enum).
1278 * The two numbered lines below indicate the bit position in the bitfield (read
1279 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001280 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1282 * +-------------+---------+---------+--------------+---------+---------+
1283 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1284 * | 3 |333333322|222222221| 1 |111111110|000000000|
1285 * | 7 |654321098|765432109| 8 |765432109|876543210|
1286 *
1287 * Note that, to ease the implementation, 'changes' bits are least significant
1288 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001289 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001290class SideEffects : public ValueObject {
1291 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001292 SideEffects() : flags_(0) {}
1293
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001294 static SideEffects None() {
1295 return SideEffects(0);
1296 }
1297
1298 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 return SideEffects(kAllChangeBits | kAllDependOnBits);
1300 }
1301
1302 static SideEffects AllChanges() {
1303 return SideEffects(kAllChangeBits);
1304 }
1305
1306 static SideEffects AllDependencies() {
1307 return SideEffects(kAllDependOnBits);
1308 }
1309
1310 static SideEffects AllExceptGCDependency() {
1311 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1312 }
1313
1314 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001315 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001316 }
1317
Aart Bik34c3ba92015-07-20 14:08:59 -07001318 static SideEffects AllWrites() {
1319 return SideEffects(kAllWrites);
1320 }
1321
1322 static SideEffects AllReads() {
1323 return SideEffects(kAllReads);
1324 }
1325
1326 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1327 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001329 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 }
1331
Aart Bik854a02b2015-07-14 16:07:00 -07001332 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1333 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 }
1335
Aart Bik34c3ba92015-07-20 14:08:59 -07001336 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1337 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001339 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001340 }
1341
1342 static SideEffects ArrayReadOfType(Primitive::Type type) {
1343 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1344 }
1345
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001346 static SideEffects CanTriggerGC() {
1347 return SideEffects(1ULL << kCanTriggerGCBit);
1348 }
1349
1350 static SideEffects DependsOnGC() {
1351 return SideEffects(1ULL << kDependsOnGCBit);
1352 }
1353
Aart Bik854a02b2015-07-14 16:07:00 -07001354 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001355 SideEffects Union(SideEffects other) const {
1356 return SideEffects(flags_ | other.flags_);
1357 }
1358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 SideEffects Exclusion(SideEffects other) const {
1360 return SideEffects(flags_ & ~other.flags_);
1361 }
1362
1363 bool Includes(SideEffects other) const {
1364 return (other.flags_ & flags_) == other.flags_;
1365 }
1366
1367 bool HasSideEffects() const {
1368 return (flags_ & kAllChangeBits);
1369 }
1370
1371 bool HasDependencies() const {
1372 return (flags_ & kAllDependOnBits);
1373 }
1374
1375 // Returns true if there are no side effects or dependencies.
1376 bool DoesNothing() const {
1377 return flags_ == 0;
1378 }
1379
Aart Bik854a02b2015-07-14 16:07:00 -07001380 // Returns true if something is written.
1381 bool DoesAnyWrite() const {
1382 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001383 }
1384
Aart Bik854a02b2015-07-14 16:07:00 -07001385 // Returns true if something is read.
1386 bool DoesAnyRead() const {
1387 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Returns true if potentially everything is written and read
1391 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001392 bool DoesAllReadWrite() const {
1393 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1394 }
1395
Aart Bik854a02b2015-07-14 16:07:00 -07001396 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001398 }
1399
1400 // Returns true if this may read something written by other.
1401 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1403 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001404 }
1405
1406 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001408 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001409 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 for (int s = kLastBit; s >= 0; s--) {
1411 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1412 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1413 // This is a bit for the GC side effect.
1414 if (current_bit_is_set) {
1415 flags += "GC";
1416 }
Aart Bik854a02b2015-07-14 16:07:00 -07001417 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001418 } else {
1419 // This is a bit for the array/field analysis.
1420 // The underscore character stands for the 'can trigger GC' bit.
1421 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1422 if (current_bit_is_set) {
1423 flags += kDebug[s];
1424 }
1425 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1426 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1427 flags += "|";
1428 }
1429 }
Aart Bik854a02b2015-07-14 16:07:00 -07001430 }
1431 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001432 }
1433
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001435
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 private:
1437 static constexpr int kFieldArrayAnalysisBits = 9;
1438
1439 static constexpr int kFieldWriteOffset = 0;
1440 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1441 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1442 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1443
1444 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1445
1446 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1447 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1448 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1449 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1450
1451 static constexpr int kLastBit = kDependsOnGCBit;
1452 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1453
1454 // Aliases.
1455
1456 static_assert(kChangeBits == kDependOnBits,
1457 "the 'change' bits should match the 'depend on' bits.");
1458
1459 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1460 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1461 static constexpr uint64_t kAllWrites =
1462 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1463 static constexpr uint64_t kAllReads =
1464 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465
Aart Bik854a02b2015-07-14 16:07:00 -07001466 // Work around the fact that HIR aliases I/F and J/D.
1467 // TODO: remove this interceptor once HIR types are clean
1468 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1469 switch (type) {
1470 case Primitive::kPrimInt:
1471 case Primitive::kPrimFloat:
1472 return TypeFlag(Primitive::kPrimInt, offset) |
1473 TypeFlag(Primitive::kPrimFloat, offset);
1474 case Primitive::kPrimLong:
1475 case Primitive::kPrimDouble:
1476 return TypeFlag(Primitive::kPrimLong, offset) |
1477 TypeFlag(Primitive::kPrimDouble, offset);
1478 default:
1479 return TypeFlag(type, offset);
1480 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001481 }
1482
Aart Bik854a02b2015-07-14 16:07:00 -07001483 // Translates type to bit flag.
1484 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1485 CHECK_NE(type, Primitive::kPrimVoid);
1486 const uint64_t one = 1;
1487 const int shift = type; // 0-based consecutive enum
1488 DCHECK_LE(kFieldWriteOffset, shift);
1489 DCHECK_LT(shift, kArrayWriteOffset);
1490 return one << (type + offset);
1491 }
1492
1493 // Private constructor on direct flags value.
1494 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1495
1496 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497};
1498
David Brazdiled596192015-01-23 10:39:45 +00001499// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001500class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001501 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001502 HEnvironment(ArenaAllocator* arena,
1503 size_t number_of_vregs,
1504 const DexFile& dex_file,
1505 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001506 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001507 InvokeType invoke_type,
1508 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 : vregs_(arena, number_of_vregs),
1510 locations_(arena, number_of_vregs),
1511 parent_(nullptr),
1512 dex_file_(dex_file),
1513 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001514 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001515 invoke_type_(invoke_type),
1516 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001517 vregs_.SetSize(number_of_vregs);
1518 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001519 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001520 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001521
1522 locations_.SetSize(number_of_vregs);
1523 for (size_t i = 0; i < number_of_vregs; ++i) {
1524 locations_.Put(i, Location());
1525 }
1526 }
1527
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001529 : HEnvironment(arena,
1530 to_copy.Size(),
1531 to_copy.GetDexFile(),
1532 to_copy.GetMethodIdx(),
1533 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 to_copy.GetInvokeType(),
1535 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001536
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001537 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 if (parent_ != nullptr) {
1539 parent_->SetAndCopyParentChain(allocator, parent);
1540 } else {
1541 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1542 parent_->CopyFrom(parent);
1543 if (parent->GetParent() != nullptr) {
1544 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1545 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001546 }
David Brazdiled596192015-01-23 10:39:45 +00001547 }
1548
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001549 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1550 void CopyFrom(HEnvironment* environment);
1551
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001552 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1553 // input to the loop phi instead. This is for inserting instructions that
1554 // require an environment (like HDeoptimization) in the loop pre-header.
1555 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001556
1557 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001558 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001559 }
1560
1561 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001562 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001563 }
1564
David Brazdil1abb4192015-02-17 18:33:36 +00001565 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001566
1567 size_t Size() const { return vregs_.Size(); }
1568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001569 HEnvironment* GetParent() const { return parent_; }
1570
1571 void SetLocationAt(size_t index, Location location) {
1572 locations_.Put(index, location);
1573 }
1574
1575 Location GetLocationAt(size_t index) const {
1576 return locations_.Get(index);
1577 }
1578
1579 uint32_t GetDexPc() const {
1580 return dex_pc_;
1581 }
1582
1583 uint32_t GetMethodIdx() const {
1584 return method_idx_;
1585 }
1586
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001587 InvokeType GetInvokeType() const {
1588 return invoke_type_;
1589 }
1590
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 const DexFile& GetDexFile() const {
1592 return dex_file_;
1593 }
1594
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001595 HInstruction* GetHolder() const {
1596 return holder_;
1597 }
1598
David Brazdiled596192015-01-23 10:39:45 +00001599 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001600 // Record instructions' use entries of this environment for constant-time removal.
1601 // It should only be called by HInstruction when a new environment use is added.
1602 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1603 DCHECK(env_use->GetUser() == this);
1604 size_t index = env_use->GetIndex();
1605 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1606 }
David Brazdiled596192015-01-23 10:39:45 +00001607
David Brazdil1abb4192015-02-17 18:33:36 +00001608 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 GrowableArray<Location> locations_;
1610 HEnvironment* parent_;
1611 const DexFile& dex_file_;
1612 const uint32_t method_idx_;
1613 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001614 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001615
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001616 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001617 HInstruction* const holder_;
1618
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001619 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001620
1621 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1622};
1623
Calin Juravleacf735c2015-02-12 15:25:22 +00001624class ReferenceTypeInfo : ValueObject {
1625 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001626 typedef Handle<mirror::Class> TypeHandle;
1627
Calin Juravle2e768302015-07-28 14:41:11 +00001628 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1629 // The constructor will check that the type_handle is valid.
1630 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001631 }
1632
Calin Juravle2e768302015-07-28 14:41:11 +00001633 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1634
1635 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1636 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001637 }
1638
Calin Juravle2e768302015-07-28 14:41:11 +00001639 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1640 return IsValidHandle(type_handle_);
1641 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001642 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001643
1644 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1645 DCHECK(IsValid());
1646 return GetTypeHandle()->IsObjectClass();
1647 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001648 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001649 DCHECK(IsValid());
1650 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001651 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001652
1653 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1654
Mathieu Chartier90443472015-07-16 20:32:27 -07001655 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001656 DCHECK(IsValid());
1657 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001658 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1659 }
1660
1661 // Returns true if the type information provide the same amount of details.
1662 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001663 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001664 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001665 if (!IsValid() && !rti.IsValid()) {
1666 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001667 return true;
1668 }
Calin Juravle2e768302015-07-28 14:41:11 +00001669 if (!IsValid() || !rti.IsValid()) {
1670 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001671 return false;
1672 }
Calin Juravle2e768302015-07-28 14:41:11 +00001673 return IsExact() == rti.IsExact()
1674 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001675 }
1676
1677 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001678 ReferenceTypeInfo();
1679 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001680
Calin Juravleacf735c2015-02-12 15:25:22 +00001681 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001682 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001683 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001684 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001686};
1687
1688std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1689
Vladimir Markof9f64412015-09-02 14:05:49 +01001690class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001691 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001692 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001693 : previous_(nullptr),
1694 next_(nullptr),
1695 block_(nullptr),
1696 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001697 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001698 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001699 locations_(nullptr),
1700 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001702 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001703 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704
Dave Allison20dfc792014-06-16 20:44:29 -07001705 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001707#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001708 enum InstructionKind {
1709 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1710 };
1711#undef DECLARE_KIND
1712
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001713 HInstruction* GetNext() const { return next_; }
1714 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
Calin Juravle77520bc2015-01-12 18:45:46 +00001716 HInstruction* GetNextDisregardingMoves() const;
1717 HInstruction* GetPreviousDisregardingMoves() const;
1718
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001719 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001720 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001721 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001722 bool IsInBlock() const { return block_ != nullptr; }
1723 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001724 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001725
Roland Levillain6b879dd2014-09-22 17:13:44 +01001726 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001727 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728
1729 virtual void Accept(HGraphVisitor* visitor) = 0;
1730 virtual const char* DebugName() const = 0;
1731
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001732 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001733 void SetRawInputAt(size_t index, HInstruction* input) {
1734 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001737 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001738 virtual uint32_t GetDexPc() const {
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +06001739 return kNoDexPc;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001741 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001742
Roland Levillaine161a2a2014-10-03 12:45:18 +01001743 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001744 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001745
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001747 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001748
Calin Juravle10e244f2015-01-26 18:54:32 +00001749 // Does not apply for all instructions, but having this at top level greatly
1750 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001751 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001752 virtual bool CanBeNull() const {
1753 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1754 return true;
1755 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001756
Calin Juravle641547a2015-04-21 22:08:51 +01001757 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1758 UNUSED(obj);
1759 return false;
1760 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001761
Calin Juravle2e768302015-07-28 14:41:11 +00001762 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001763
Calin Juravle61d544b2015-02-23 16:46:57 +00001764 ReferenceTypeInfo GetReferenceTypeInfo() const {
1765 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1766 return reference_type_info_;
1767 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001768
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001769 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001770 DCHECK(user != nullptr);
1771 HUseListNode<HInstruction*>* use =
1772 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1773 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001774 }
1775
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001776 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001777 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001778 HUseListNode<HEnvironment*>* env_use =
1779 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1780 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781 }
1782
David Brazdil1abb4192015-02-17 18:33:36 +00001783 void RemoveAsUserOfInput(size_t input) {
1784 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1785 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1786 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1789 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001790
David Brazdiled596192015-01-23 10:39:45 +00001791 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1792 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001793 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001794 bool HasOnlyOneNonEnvironmentUse() const {
1795 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1796 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797
Roland Levillain6c82d402014-10-13 16:10:27 +01001798 // Does this instruction strictly dominate `other_instruction`?
1799 // Returns false if this instruction and `other_instruction` are the same.
1800 // Aborts if this instruction and `other_instruction` are both phis.
1801 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 int GetId() const { return id_; }
1804 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001806 int GetSsaIndex() const { return ssa_index_; }
1807 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1808 bool HasSsaIndex() const { return ssa_index_ != -1; }
1809
1810 bool HasEnvironment() const { return environment_ != nullptr; }
1811 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001812 // Set the `environment_` field. Raw because this method does not
1813 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001814 void SetRawEnvironment(HEnvironment* environment) {
1815 DCHECK(environment_ == nullptr);
1816 DCHECK_EQ(environment->GetHolder(), this);
1817 environment_ = environment;
1818 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001819
1820 // Set the environment of this instruction, copying it from `environment`. While
1821 // copying, the uses lists are being updated.
1822 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001824 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001826 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001827 if (environment->GetParent() != nullptr) {
1828 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1829 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001832 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1833 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001835 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001836 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001837 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 if (environment->GetParent() != nullptr) {
1839 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1840 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001841 }
1842
Nicolas Geoffray39468442014-09-02 15:17:15 +01001843 // Returns the number of entries in the environment. Typically, that is the
1844 // number of dex registers in a method. It could be more in case of inlining.
1845 size_t EnvironmentSize() const;
1846
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001847 LocationSummary* GetLocations() const { return locations_; }
1848 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852
Alexandre Rames188d4312015-04-09 18:30:21 +01001853 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1854 // uses of this instruction by `other` are *not* updated.
1855 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1856 ReplaceWith(other);
1857 other->ReplaceInput(this, use_index);
1858 }
1859
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001860 // Move `this` instruction before `cursor`.
1861 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001862
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001863#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001864 bool Is##type() const { return (As##type() != nullptr); } \
1865 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866 virtual H##type* As##type() { return nullptr; }
1867
1868 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1869#undef INSTRUCTION_TYPE_CHECK
1870
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001871 // Returns whether the instruction can be moved within the graph.
1872 virtual bool CanBeMoved() const { return false; }
1873
1874 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001875 virtual bool InstructionTypeEquals(HInstruction* other) const {
1876 UNUSED(other);
1877 return false;
1878 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879
1880 // Returns whether any data encoded in the two instructions is equal.
1881 // This method does not look at the inputs. Both instructions must be
1882 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001883 virtual bool InstructionDataEquals(HInstruction* other) const {
1884 UNUSED(other);
1885 return false;
1886 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887
1888 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001889 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 // 2) Their inputs are identical.
1891 bool Equals(HInstruction* other) const;
1892
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001893 virtual InstructionKind GetKind() const = 0;
1894
1895 virtual size_t ComputeHashCode() const {
1896 size_t result = GetKind();
1897 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1898 result = (result * 31) + InputAt(i)->GetId();
1899 }
1900 return result;
1901 }
1902
1903 SideEffects GetSideEffects() const { return side_effects_; }
1904
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001905 size_t GetLifetimePosition() const { return lifetime_position_; }
1906 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1907 LiveInterval* GetLiveInterval() const { return live_interval_; }
1908 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1909 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1910
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001911 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1912
1913 // Returns whether the code generation of the instruction will require to have access
1914 // to the current method. Such instructions are:
1915 // (1): Instructions that require an environment, as calling the runtime requires
1916 // to walk the stack and have the current method stored at a specific stack address.
1917 // (2): Object literals like classes and strings, that are loaded from the dex cache
1918 // fields of the current method.
1919 bool NeedsCurrentMethod() const {
1920 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1921 }
1922
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001923 virtual bool NeedsDexCache() const { return false; }
1924
Mark Mendellc4701932015-04-10 13:18:51 -04001925 // Does this instruction have any use in an environment before
1926 // control flow hits 'other'?
1927 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1928
1929 // Remove all references to environment uses of this instruction.
1930 // The caller must ensure that this is safe to do.
1931 void RemoveEnvironmentUsers();
1932
David Brazdil1abb4192015-02-17 18:33:36 +00001933 protected:
1934 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1935 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1936
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001938 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1939
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940 HInstruction* previous_;
1941 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001942 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944 // An instruction gets an id when it is added to the graph.
1945 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001946 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947 int id_;
1948
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001949 // When doing liveness analysis, instructions that have uses get an SSA index.
1950 int ssa_index_;
1951
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001953 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954
1955 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001956 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Nicolas Geoffray39468442014-09-02 15:17:15 +01001958 // The environment associated with this instruction. Not null if the instruction
1959 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001961
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962 // Set by the code generator.
1963 LocationSummary* locations_;
1964
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001965 // Set by the liveness analysis.
1966 LiveInterval* live_interval_;
1967
1968 // Set by the liveness analysis, this is the position in a linear
1969 // order of blocks where this instruction's live interval start.
1970 size_t lifetime_position_;
1971
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972 const SideEffects side_effects_;
1973
Calin Juravleacf735c2015-02-12 15:25:22 +00001974 // TODO: for primitive types this should be marked as invalid.
1975 ReferenceTypeInfo reference_type_info_;
1976
David Brazdil1abb4192015-02-17 18:33:36 +00001977 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001978 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001979 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001980 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001981 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982
1983 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1984};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001985std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987class HInputIterator : public ValueObject {
1988 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001989 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990
1991 bool Done() const { return index_ == instruction_->InputCount(); }
1992 HInstruction* Current() const { return instruction_->InputAt(index_); }
1993 void Advance() { index_++; }
1994
1995 private:
1996 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001997 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998
1999 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2000};
2001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002002class HInstructionIterator : public ValueObject {
2003 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004 explicit HInstructionIterator(const HInstructionList& instructions)
2005 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002006 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007 }
2008
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002009 bool Done() const { return instruction_ == nullptr; }
2010 HInstruction* Current() const { return instruction_; }
2011 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002013 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014 }
2015
2016 private:
2017 HInstruction* instruction_;
2018 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002019
2020 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021};
2022
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002023class HBackwardInstructionIterator : public ValueObject {
2024 public:
2025 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2026 : instruction_(instructions.last_instruction_) {
2027 next_ = Done() ? nullptr : instruction_->GetPrevious();
2028 }
2029
2030 bool Done() const { return instruction_ == nullptr; }
2031 HInstruction* Current() const { return instruction_; }
2032 void Advance() {
2033 instruction_ = next_;
2034 next_ = Done() ? nullptr : instruction_->GetPrevious();
2035 }
2036
2037 private:
2038 HInstruction* instruction_;
2039 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002040
2041 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002042};
2043
Vladimir Markof9f64412015-09-02 14:05:49 +01002044template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045class HTemplateInstruction: public HInstruction {
2046 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 HTemplateInstruction<N>(SideEffects side_effects)
2048 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002049 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002051 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002052
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002054 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2055 DCHECK_LT(i, N);
2056 return inputs_[i];
2057 }
David Brazdil1abb4192015-02-17 18:33:36 +00002058
2059 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002060 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002061 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062 }
2063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002065 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066
2067 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068};
2069
Vladimir Markof9f64412015-09-02 14:05:49 +01002070// HTemplateInstruction specialization for N=0.
2071template<>
2072class HTemplateInstruction<0>: public HInstruction {
2073 public:
2074 explicit HTemplateInstruction(SideEffects side_effects) : HInstruction(side_effects) {}
2075 virtual ~HTemplateInstruction() {}
2076
2077 size_t InputCount() const OVERRIDE { return 0; }
2078
2079 protected:
2080 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2081 LOG(FATAL) << "Unreachable";
2082 UNREACHABLE();
2083 }
2084
2085 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2086 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2087 LOG(FATAL) << "Unreachable";
2088 UNREACHABLE();
2089 }
2090
2091 private:
2092 friend class SsaBuilder;
2093};
2094
Dave Allison20dfc792014-06-16 20:44:29 -07002095template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002096class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002097 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002098 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2099 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002100 virtual ~HExpression() {}
2101
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002102 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002103
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002104 protected:
2105 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002106};
2107
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2109// instruction that branches to the exit block.
2110class HReturnVoid : public HTemplateInstruction<0> {
2111 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002112 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002116 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117
2118 private:
2119 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2120};
2121
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122// Represents dex's RETURN opcodes. A HReturn is a control flow
2123// instruction that branches to the exit block.
2124class HReturn : public HTemplateInstruction<1> {
2125 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002126 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002127 SetRawInputAt(0, value);
2128 }
2129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002130 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002131
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002132 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133
2134 private:
2135 DISALLOW_COPY_AND_ASSIGN(HReturn);
2136};
2137
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002138// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002139// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140// exit block.
2141class HExit : public HTemplateInstruction<0> {
2142 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002143 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002145 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002147 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148
2149 private:
2150 DISALLOW_COPY_AND_ASSIGN(HExit);
2151};
2152
2153// Jumps from one block to another.
2154class HGoto : public HTemplateInstruction<0> {
2155 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002156 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2157
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002158 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002160 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002161 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002162 }
2163
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002164 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165
2166 private:
2167 DISALLOW_COPY_AND_ASSIGN(HGoto);
2168};
2169
Roland Levillain9867bc72015-08-05 10:21:34 +01002170class HConstant : public HExpression<0> {
2171 public:
2172 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2173
2174 bool CanBeMoved() const OVERRIDE { return true; }
2175
2176 virtual bool IsMinusOne() const { return false; }
2177 virtual bool IsZero() const { return false; }
2178 virtual bool IsOne() const { return false; }
2179
2180 DECLARE_INSTRUCTION(Constant);
2181
2182 private:
2183 DISALLOW_COPY_AND_ASSIGN(HConstant);
2184};
2185
2186class HNullConstant : public HConstant {
2187 public:
2188 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2189 return true;
2190 }
2191
2192 size_t ComputeHashCode() const OVERRIDE { return 0; }
2193
2194 DECLARE_INSTRUCTION(NullConstant);
2195
2196 private:
2197 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2198
2199 friend class HGraph;
2200 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2201};
2202
2203// Constants of the type int. Those can be from Dex instructions, or
2204// synthesized (for example with the if-eqz instruction).
2205class HIntConstant : public HConstant {
2206 public:
2207 int32_t GetValue() const { return value_; }
2208
2209 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2210 DCHECK(other->IsIntConstant());
2211 return other->AsIntConstant()->value_ == value_;
2212 }
2213
2214 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2215
2216 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2217 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2218 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2219
2220 DECLARE_INSTRUCTION(IntConstant);
2221
2222 private:
2223 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2224 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2225
2226 const int32_t value_;
2227
2228 friend class HGraph;
2229 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2230 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2231 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2232};
2233
2234class HLongConstant : public HConstant {
2235 public:
2236 int64_t GetValue() const { return value_; }
2237
2238 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2239 DCHECK(other->IsLongConstant());
2240 return other->AsLongConstant()->value_ == value_;
2241 }
2242
2243 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2244
2245 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2246 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2247 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2248
2249 DECLARE_INSTRUCTION(LongConstant);
2250
2251 private:
2252 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2253
2254 const int64_t value_;
2255
2256 friend class HGraph;
2257 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2258};
Dave Allison20dfc792014-06-16 20:44:29 -07002259
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002260// Conditional branch. A block ending with an HIf instruction must have
2261// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002262class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002263 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002264 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002265 SetRawInputAt(0, input);
2266 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002267
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002268 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002270 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +00002271 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002272 }
2273
2274 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +00002275 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002276 }
2277
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002278 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002279
2280 private:
2281 DISALLOW_COPY_AND_ASSIGN(HIf);
2282};
2283
David Brazdilfc6a86a2015-06-26 10:33:45 +00002284
2285// Abstract instruction which marks the beginning and/or end of a try block and
2286// links it to the respective exception handlers. Behaves the same as a Goto in
2287// non-exceptional control flow.
2288// Normal-flow successor is stored at index zero, exception handlers under
2289// higher indices in no particular order.
2290class HTryBoundary : public HTemplateInstruction<0> {
2291 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002292 enum BoundaryKind {
2293 kEntry,
2294 kExit,
2295 };
2296
2297 explicit HTryBoundary(BoundaryKind kind)
2298 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299
2300 bool IsControlFlow() const OVERRIDE { return true; }
2301
2302 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko145acc52015-09-03 13:33:25 +00002303 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002304
2305 // Returns whether `handler` is among its exception handlers (non-zero index
2306 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002307 bool HasExceptionHandler(const HBasicBlock& handler) const {
2308 DCHECK(handler.IsCatchBlock());
Vladimir Marko145acc52015-09-03 13:33:25 +00002309 return GetBlock()->GetSuccessors().Contains(
2310 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002311 }
2312
2313 // If not present already, adds `handler` to its block's list of exception
2314 // handlers.
2315 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002316 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002317 GetBlock()->AddSuccessor(handler);
2318 }
2319 }
2320
David Brazdil56e1acc2015-06-30 15:41:36 +01002321 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322
David Brazdilffee3d32015-07-06 11:48:53 +01002323 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2324
David Brazdilfc6a86a2015-06-26 10:33:45 +00002325 DECLARE_INSTRUCTION(TryBoundary);
2326
2327 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002328 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002329
2330 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2331};
2332
David Brazdilffee3d32015-07-06 11:48:53 +01002333// Iterator over exception handlers of a given HTryBoundary, i.e. over
2334// exceptional successors of its basic block.
2335class HExceptionHandlerIterator : public ValueObject {
2336 public:
2337 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2338 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2339
Vladimir Marko145acc52015-09-03 13:33:25 +00002340 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2341 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002342 size_t CurrentSuccessorIndex() const { return index_; }
2343 void Advance() { ++index_; }
2344
2345 private:
2346 const HBasicBlock& block_;
2347 size_t index_;
2348
2349 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2350};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002351
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002352// Deoptimize to interpreter, upon checking a condition.
2353class HDeoptimize : public HTemplateInstruction<1> {
2354 public:
2355 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2356 : HTemplateInstruction(SideEffects::None()),
2357 dex_pc_(dex_pc) {
2358 SetRawInputAt(0, cond);
2359 }
2360
2361 bool NeedsEnvironment() const OVERRIDE { return true; }
2362 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002363 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002364
2365 DECLARE_INSTRUCTION(Deoptimize);
2366
2367 private:
2368 uint32_t dex_pc_;
2369
2370 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2371};
2372
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002373// Represents the ArtMethod that was passed as a first argument to
2374// the method. It is used by instructions that depend on it, like
2375// instructions that work with the dex cache.
2376class HCurrentMethod : public HExpression<0> {
2377 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002378 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002379
2380 DECLARE_INSTRUCTION(CurrentMethod);
2381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2384};
2385
Roland Levillain88cb1752014-10-20 16:36:47 +01002386class HUnaryOperation : public HExpression<1> {
2387 public:
2388 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2389 : HExpression(result_type, SideEffects::None()) {
2390 SetRawInputAt(0, input);
2391 }
2392
2393 HInstruction* GetInput() const { return InputAt(0); }
2394 Primitive::Type GetResultType() const { return GetType(); }
2395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002396 bool CanBeMoved() const OVERRIDE { return true; }
2397 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002398 UNUSED(other);
2399 return true;
2400 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002401
Roland Levillain9240d6a2014-10-20 16:47:04 +01002402 // Try to statically evaluate `operation` and return a HConstant
2403 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002404 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002405 HConstant* TryStaticEvaluation() const;
2406
2407 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002408 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2409 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002410
Roland Levillain88cb1752014-10-20 16:36:47 +01002411 DECLARE_INSTRUCTION(UnaryOperation);
2412
2413 private:
2414 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2415};
2416
Dave Allison20dfc792014-06-16 20:44:29 -07002417class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002418 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002419 HBinaryOperation(Primitive::Type result_type,
2420 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002421 HInstruction* right,
2422 SideEffects side_effects = SideEffects::None())
2423 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002424 SetRawInputAt(0, left);
2425 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002426 }
2427
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002428 HInstruction* GetLeft() const { return InputAt(0); }
2429 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002430 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002431
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002432 virtual bool IsCommutative() const { return false; }
2433
2434 // Put constant on the right.
2435 // Returns whether order is changed.
2436 bool OrderInputsWithConstantOnTheRight() {
2437 HInstruction* left = InputAt(0);
2438 HInstruction* right = InputAt(1);
2439 if (left->IsConstant() && !right->IsConstant()) {
2440 ReplaceInput(right, 0);
2441 ReplaceInput(left, 1);
2442 return true;
2443 }
2444 return false;
2445 }
2446
2447 // Order inputs by instruction id, but favor constant on the right side.
2448 // This helps GVN for commutative ops.
2449 void OrderInputs() {
2450 DCHECK(IsCommutative());
2451 HInstruction* left = InputAt(0);
2452 HInstruction* right = InputAt(1);
2453 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2454 return;
2455 }
2456 if (OrderInputsWithConstantOnTheRight()) {
2457 return;
2458 }
2459 // Order according to instruction id.
2460 if (left->GetId() > right->GetId()) {
2461 ReplaceInput(right, 0);
2462 ReplaceInput(left, 1);
2463 }
2464 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002465
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002466 bool CanBeMoved() const OVERRIDE { return true; }
2467 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002468 UNUSED(other);
2469 return true;
2470 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002471
Roland Levillain9240d6a2014-10-20 16:47:04 +01002472 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002473 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002474 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002475 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002476
2477 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002478 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2479 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2480 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2481 HLongConstant* y ATTRIBUTE_UNUSED) const {
2482 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2483 return nullptr;
2484 }
2485 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2486 HIntConstant* y ATTRIBUTE_UNUSED) const {
2487 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2488 return nullptr;
2489 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002490
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002491 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002492 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002493 HConstant* GetConstantRight() const;
2494
2495 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002496 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002497 HInstruction* GetLeastConstantLeft() const;
2498
Roland Levillainccc07a92014-09-16 14:48:16 +01002499 DECLARE_INSTRUCTION(BinaryOperation);
2500
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002501 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002502 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2503};
2504
Mark Mendellc4701932015-04-10 13:18:51 -04002505// The comparison bias applies for floating point operations and indicates how NaN
2506// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002507enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002508 kNoBias, // bias is not applicable (i.e. for long operation)
2509 kGtBias, // return 1 for NaN comparisons
2510 kLtBias, // return -1 for NaN comparisons
2511};
2512
Dave Allison20dfc792014-06-16 20:44:29 -07002513class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002515 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002516 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002517 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002518 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002520 bool NeedsMaterialization() const { return needs_materialization_; }
2521 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002522
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002523 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002524 // `instruction`, and disregard moves in between.
2525 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002526
Dave Allison20dfc792014-06-16 20:44:29 -07002527 DECLARE_INSTRUCTION(Condition);
2528
2529 virtual IfCondition GetCondition() const = 0;
2530
Mark Mendellc4701932015-04-10 13:18:51 -04002531 virtual IfCondition GetOppositeCondition() const = 0;
2532
Roland Levillain4fa13f62015-07-06 18:11:54 +01002533 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002534
2535 void SetBias(ComparisonBias bias) { bias_ = bias; }
2536
2537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2538 return bias_ == other->AsCondition()->bias_;
2539 }
2540
Roland Levillain4fa13f62015-07-06 18:11:54 +01002541 bool IsFPConditionTrueIfNaN() const {
2542 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2543 IfCondition if_cond = GetCondition();
2544 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2545 }
2546
2547 bool IsFPConditionFalseIfNaN() const {
2548 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2549 IfCondition if_cond = GetCondition();
2550 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2551 }
2552
Dave Allison20dfc792014-06-16 20:44:29 -07002553 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002554 // For register allocation purposes, returns whether this instruction needs to be
2555 // materialized (that is, not just be in the processor flags).
2556 bool needs_materialization_;
2557
Mark Mendellc4701932015-04-10 13:18:51 -04002558 // Needed if we merge a HCompare into a HCondition.
2559 ComparisonBias bias_;
2560
Dave Allison20dfc792014-06-16 20:44:29 -07002561 DISALLOW_COPY_AND_ASSIGN(HCondition);
2562};
2563
2564// Instruction to check if two inputs are equal to each other.
2565class HEqual : public HCondition {
2566 public:
2567 HEqual(HInstruction* first, HInstruction* second)
2568 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002569
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002570 bool IsCommutative() const OVERRIDE { return true; }
2571
Roland Levillain9867bc72015-08-05 10:21:34 +01002572 template <typename T> bool Compute(T x, T y) const { return x == y; }
2573
2574 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2575 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002576 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002577 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2578 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002579 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002580
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002581 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002583 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002584 return kCondEQ;
2585 }
2586
Mark Mendellc4701932015-04-10 13:18:51 -04002587 IfCondition GetOppositeCondition() const OVERRIDE {
2588 return kCondNE;
2589 }
2590
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002591 private:
2592 DISALLOW_COPY_AND_ASSIGN(HEqual);
2593};
2594
Dave Allison20dfc792014-06-16 20:44:29 -07002595class HNotEqual : public HCondition {
2596 public:
2597 HNotEqual(HInstruction* first, HInstruction* second)
2598 : HCondition(first, second) {}
2599
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002600 bool IsCommutative() const OVERRIDE { return true; }
2601
Roland Levillain9867bc72015-08-05 10:21:34 +01002602 template <typename T> bool Compute(T x, T y) const { return x != y; }
2603
2604 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2605 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002606 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002607 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2608 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002609 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002610
Dave Allison20dfc792014-06-16 20:44:29 -07002611 DECLARE_INSTRUCTION(NotEqual);
2612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002613 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002614 return kCondNE;
2615 }
2616
Mark Mendellc4701932015-04-10 13:18:51 -04002617 IfCondition GetOppositeCondition() const OVERRIDE {
2618 return kCondEQ;
2619 }
2620
Dave Allison20dfc792014-06-16 20:44:29 -07002621 private:
2622 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2623};
2624
2625class HLessThan : public HCondition {
2626 public:
2627 HLessThan(HInstruction* first, HInstruction* second)
2628 : HCondition(first, second) {}
2629
Roland Levillain9867bc72015-08-05 10:21:34 +01002630 template <typename T> bool Compute(T x, T y) const { return x < y; }
2631
2632 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2633 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002634 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002635 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2636 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002637 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002638
Dave Allison20dfc792014-06-16 20:44:29 -07002639 DECLARE_INSTRUCTION(LessThan);
2640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002642 return kCondLT;
2643 }
2644
Mark Mendellc4701932015-04-10 13:18:51 -04002645 IfCondition GetOppositeCondition() const OVERRIDE {
2646 return kCondGE;
2647 }
2648
Dave Allison20dfc792014-06-16 20:44:29 -07002649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2651};
2652
2653class HLessThanOrEqual : public HCondition {
2654 public:
2655 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2656 : HCondition(first, second) {}
2657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2659
2660 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2661 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002662 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2664 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002665 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002666
Dave Allison20dfc792014-06-16 20:44:29 -07002667 DECLARE_INSTRUCTION(LessThanOrEqual);
2668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002669 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002670 return kCondLE;
2671 }
2672
Mark Mendellc4701932015-04-10 13:18:51 -04002673 IfCondition GetOppositeCondition() const OVERRIDE {
2674 return kCondGT;
2675 }
2676
Dave Allison20dfc792014-06-16 20:44:29 -07002677 private:
2678 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2679};
2680
2681class HGreaterThan : public HCondition {
2682 public:
2683 HGreaterThan(HInstruction* first, HInstruction* second)
2684 : HCondition(first, second) {}
2685
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 template <typename T> bool Compute(T x, T y) const { return x > y; }
2687
2688 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2689 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002690 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002691 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2692 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002694
Dave Allison20dfc792014-06-16 20:44:29 -07002695 DECLARE_INSTRUCTION(GreaterThan);
2696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002697 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002698 return kCondGT;
2699 }
2700
Mark Mendellc4701932015-04-10 13:18:51 -04002701 IfCondition GetOppositeCondition() const OVERRIDE {
2702 return kCondLE;
2703 }
2704
Dave Allison20dfc792014-06-16 20:44:29 -07002705 private:
2706 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2707};
2708
2709class HGreaterThanOrEqual : public HCondition {
2710 public:
2711 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2712 : HCondition(first, second) {}
2713
Roland Levillain9867bc72015-08-05 10:21:34 +01002714 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2715
2716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2717 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2720 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002721 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002722
Dave Allison20dfc792014-06-16 20:44:29 -07002723 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002725 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002726 return kCondGE;
2727 }
2728
Mark Mendellc4701932015-04-10 13:18:51 -04002729 IfCondition GetOppositeCondition() const OVERRIDE {
2730 return kCondLT;
2731 }
2732
Dave Allison20dfc792014-06-16 20:44:29 -07002733 private:
2734 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2735};
2736
2737
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002738// Instruction to check how two inputs compare to each other.
2739// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2740class HCompare : public HBinaryOperation {
2741 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002742 HCompare(Primitive::Type type,
2743 HInstruction* first,
2744 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002745 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002747 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2748 bias_(bias),
2749 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002750 DCHECK_EQ(type, first->GetType());
2751 DCHECK_EQ(type, second->GetType());
2752 }
2753
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 template <typename T>
2755 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002756
Roland Levillain9867bc72015-08-05 10:21:34 +01002757 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2758 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2759 }
2760 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2761 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002762 }
2763
Calin Juravleddb7df22014-11-25 20:56:51 +00002764 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2765 return bias_ == other->AsCompare()->bias_;
2766 }
2767
Mark Mendellc4701932015-04-10 13:18:51 -04002768 ComparisonBias GetBias() const { return bias_; }
2769
Roland Levillain4fa13f62015-07-06 18:11:54 +01002770 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002771
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002772 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2773
2774 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2775 // MIPS64 uses a runtime call for FP comparisons.
2776 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2777 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002778
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002779 DECLARE_INSTRUCTION(Compare);
2780
2781 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002782 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002783 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002784
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002785 DISALLOW_COPY_AND_ASSIGN(HCompare);
2786};
2787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002788// A local in the graph. Corresponds to a Dex register.
2789class HLocal : public HTemplateInstruction<0> {
2790 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002791 explicit HLocal(uint16_t reg_number)
2792 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002794 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002795
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002796 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002797
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002798 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002799 // The Dex register number.
2800 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002801
2802 DISALLOW_COPY_AND_ASSIGN(HLocal);
2803};
2804
2805// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002806class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002807 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002808 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002809 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002810 SetRawInputAt(0, local);
2811 }
2812
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002813 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2814
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002815 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002816
2817 private:
2818 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2819};
2820
2821// Store a value in a given local. This instruction has two inputs: the value
2822// and the local.
2823class HStoreLocal : public HTemplateInstruction<2> {
2824 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002825 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002826 SetRawInputAt(0, local);
2827 SetRawInputAt(1, value);
2828 }
2829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002830 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002832 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833
2834 private:
2835 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2836};
2837
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002838class HFloatConstant : public HConstant {
2839 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002840 float GetValue() const { return value_; }
2841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002842 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002843 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002844 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2845 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002846 }
2847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002848 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002849
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002850 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002851 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002852 }
2853 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002854 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002855 }
2856 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002857 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2858 }
2859 bool IsNaN() const {
2860 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002861 }
2862
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002863 DECLARE_INSTRUCTION(FloatConstant);
2864
2865 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002866 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002867 explicit HFloatConstant(int32_t value)
2868 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002869
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002870 const float value_;
2871
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002872 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002873 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002874 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002875 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2876};
2877
2878class HDoubleConstant : public HConstant {
2879 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002880 double GetValue() const { return value_; }
2881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002882 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002883 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002884 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2885 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002886 }
2887
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002888 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002889
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002890 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002891 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002892 }
2893 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002894 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002895 }
2896 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002897 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2898 }
2899 bool IsNaN() const {
2900 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002901 }
2902
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002903 DECLARE_INSTRUCTION(DoubleConstant);
2904
2905 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002906 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002907 explicit HDoubleConstant(int64_t value)
2908 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002909
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 const double value_;
2911
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002912 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002913 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002914 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002915 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2916};
2917
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002918enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002919#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002920#include "intrinsics_list.h"
2921 kNone,
2922 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2923#undef INTRINSICS_LIST
2924#undef OPTIMIZING_INTRINSICS
2925};
2926std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2927
Agi Csaki05f20562015-08-19 14:58:14 -07002928enum IntrinsicNeedsEnvironmentOrCache {
2929 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2930 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002931};
2932
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002933class HInvoke : public HInstruction {
2934 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002935 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002936
2937 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2938 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002939 bool NeedsEnvironment() const OVERRIDE {
2940 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2941 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002942
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002943 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002944 SetRawInputAt(index, argument);
2945 }
2946
Roland Levillain3e3d7332015-04-28 11:00:54 +01002947 // Return the number of arguments. This number can be lower than
2948 // the number of inputs returned by InputCount(), as some invoke
2949 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2950 // inputs at the end of their list of inputs.
2951 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2952
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002953 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002954
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002955 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002956
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002957 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002958 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002959
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002960 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2961
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002962 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002963 return intrinsic_;
2964 }
2965
Agi Csaki05f20562015-08-19 14:58:14 -07002966 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002967 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002968 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002969 }
2970
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002971 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002972 return GetEnvironment()->GetParent() != nullptr;
2973 }
2974
2975 bool CanThrow() const OVERRIDE { return true; }
2976
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002977 DECLARE_INSTRUCTION(Invoke);
2978
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002979 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002980 HInvoke(ArenaAllocator* arena,
2981 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002982 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002983 Primitive::Type return_type,
2984 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002985 uint32_t dex_method_index,
2986 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002987 : HInstruction(
2988 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002989 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002990 inputs_(arena, number_of_arguments),
2991 return_type_(return_type),
2992 dex_pc_(dex_pc),
2993 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002994 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002995 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07002996 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002997 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2998 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002999 }
3000
David Brazdil1abb4192015-02-17 18:33:36 +00003001 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3002 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3003 inputs_.Put(index, input);
3004 }
3005
Roland Levillain3e3d7332015-04-28 11:00:54 +01003006 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003007 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003009 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003010 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003011 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003012 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003013 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003014
3015 private:
3016 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3017};
3018
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003019class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003020 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003021 // Requirements of this method call regarding the class
3022 // initialization (clinit) check of its declaring class.
3023 enum class ClinitCheckRequirement {
3024 kNone, // Class already initialized.
3025 kExplicit, // Static call having explicit clinit check as last input.
3026 kImplicit, // Static call implicitly requiring a clinit check.
3027 };
3028
Vladimir Marko58155012015-08-19 12:49:41 +00003029 // Determines how to load the target ArtMethod*.
3030 enum class MethodLoadKind {
3031 // Use a String init ArtMethod* loaded from Thread entrypoints.
3032 kStringInit,
3033
3034 // Use the method's own ArtMethod* loaded by the register allocator.
3035 kRecursive,
3036
3037 // Use ArtMethod* at a known address, embed the direct address in the code.
3038 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3039 kDirectAddress,
3040
3041 // Use ArtMethod* at an address that will be known at link time, embed the direct
3042 // address in the code. If the image is relocatable, emit .patch_oat entry.
3043 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3044 // the image relocatable or not.
3045 kDirectAddressWithFixup,
3046
3047 // Load from resoved methods array in the dex cache using a PC-relative load.
3048 // Used when we need to use the dex cache, for example for invoke-static that
3049 // may cause class initialization (the entry may point to a resolution method),
3050 // and we know that we can access the dex cache arrays using a PC-relative load.
3051 kDexCachePcRelative,
3052
3053 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3054 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3055 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3056 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3057 kDexCacheViaMethod,
3058 };
3059
3060 // Determines the location of the code pointer.
3061 enum class CodePtrLocation {
3062 // Recursive call, use local PC-relative call instruction.
3063 kCallSelf,
3064
3065 // Use PC-relative call instruction patched at link time.
3066 // Used for calls within an oat file, boot->boot or app->app.
3067 kCallPCRelative,
3068
3069 // Call to a known target address, embed the direct address in code.
3070 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3071 kCallDirect,
3072
3073 // Call to a target address that will be known at link time, embed the direct
3074 // address in code. If the image is relocatable, emit .patch_oat entry.
3075 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3076 // the image relocatable or not.
3077 kCallDirectWithFixup,
3078
3079 // Use code pointer from the ArtMethod*.
3080 // Used when we don't know the target code. This is also the last-resort-kind used when
3081 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3082 kCallArtMethod,
3083 };
3084
3085 struct DispatchInfo {
3086 const MethodLoadKind method_load_kind;
3087 const CodePtrLocation code_ptr_location;
3088 // The method load data holds
3089 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3090 // Note that there are multiple string init methods, each having its own offset.
3091 // - the method address for kDirectAddress
3092 // - the dex cache arrays offset for kDexCachePcRel.
3093 const uint64_t method_load_data;
3094 const uint64_t direct_code_ptr;
3095 };
3096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003097 HInvokeStaticOrDirect(ArenaAllocator* arena,
3098 uint32_t number_of_arguments,
3099 Primitive::Type return_type,
3100 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003101 uint32_t method_index,
3102 MethodReference target_method,
3103 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003104 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003105 InvokeType invoke_type,
3106 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003107 : HInvoke(arena,
3108 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003109 // There is one extra argument for the HCurrentMethod node, and
3110 // potentially one other if the clinit check is explicit, and one other
3111 // if the method is a string factory.
3112 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003113 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003114 return_type,
3115 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003116 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003117 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003118 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003119 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003120 target_method_(target_method),
3121 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003122
Calin Juravle641547a2015-04-21 22:08:51 +01003123 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3124 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003125 // We access the method via the dex cache so we can't do an implicit null check.
3126 // TODO: for intrinsics we can generate implicit null checks.
3127 return false;
3128 }
3129
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003130 bool CanBeNull() const OVERRIDE {
3131 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3132 }
3133
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003134 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003135 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3136 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3137 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003138 bool NeedsDexCache() const OVERRIDE {
3139 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3140 return !IsRecursive() && !IsStringInit();
3141 }
Vladimir Marko58155012015-08-19 12:49:41 +00003142 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003143 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003144 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3145 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3146 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3147 MethodReference GetTargetMethod() const { return target_method_; }
3148
3149 int32_t GetStringInitOffset() const {
3150 DCHECK(IsStringInit());
3151 return dispatch_info_.method_load_data;
3152 }
3153
3154 uint64_t GetMethodAddress() const {
3155 DCHECK(HasMethodAddress());
3156 return dispatch_info_.method_load_data;
3157 }
3158
3159 uint32_t GetDexCacheArrayOffset() const {
3160 DCHECK(HasPcRelDexCache());
3161 return dispatch_info_.method_load_data;
3162 }
3163
3164 uint64_t GetDirectCodePtr() const {
3165 DCHECK(HasDirectCodePtr());
3166 return dispatch_info_.direct_code_ptr;
3167 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003168
Calin Juravle68ad6492015-08-18 17:08:12 +01003169 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3170
Roland Levillain4c0eb422015-04-24 16:43:49 +01003171 // Is this instruction a call to a static method?
3172 bool IsStatic() const {
3173 return GetInvokeType() == kStatic;
3174 }
3175
Roland Levillain3e3d7332015-04-28 11:00:54 +01003176 // Remove the art::HLoadClass instruction set as last input by
3177 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3178 // the initial art::HClinitCheck instruction (only relevant for
3179 // static calls with explicit clinit check).
3180 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003181 DCHECK(IsStaticWithExplicitClinitCheck());
3182 size_t last_input_index = InputCount() - 1;
3183 HInstruction* last_input = InputAt(last_input_index);
3184 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003185 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003186 RemoveAsUserOfInput(last_input_index);
3187 inputs_.DeleteAt(last_input_index);
3188 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3189 DCHECK(IsStaticWithImplicitClinitCheck());
3190 }
3191
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003192 bool IsStringFactoryFor(HFakeString* str) const {
3193 if (!IsStringInit()) return false;
3194 // +1 for the current method.
3195 if (InputCount() == (number_of_arguments_ + 1)) return false;
3196 return InputAt(InputCount() - 1)->AsFakeString() == str;
3197 }
3198
3199 void RemoveFakeStringArgumentAsLastInput() {
3200 DCHECK(IsStringInit());
3201 size_t last_input_index = InputCount() - 1;
3202 HInstruction* last_input = InputAt(last_input_index);
3203 DCHECK(last_input != nullptr);
3204 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3205 RemoveAsUserOfInput(last_input_index);
3206 inputs_.DeleteAt(last_input_index);
3207 }
3208
Roland Levillain4c0eb422015-04-24 16:43:49 +01003209 // Is this a call to a static method whose declaring class has an
3210 // explicit intialization check in the graph?
3211 bool IsStaticWithExplicitClinitCheck() const {
3212 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3213 }
3214
3215 // Is this a call to a static method whose declaring class has an
3216 // implicit intialization check requirement?
3217 bool IsStaticWithImplicitClinitCheck() const {
3218 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3219 }
3220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003221 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003222
Roland Levillain4c0eb422015-04-24 16:43:49 +01003223 protected:
3224 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3225 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3226 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3227 HInstruction* input = input_record.GetInstruction();
3228 // `input` is the last input of a static invoke marked as having
3229 // an explicit clinit check. It must either be:
3230 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3231 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3232 DCHECK(input != nullptr);
3233 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3234 }
3235 return input_record;
3236 }
3237
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003238 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003239 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003240 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003241 // The target method may refer to different dex file or method index than the original
3242 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3243 // in derived class to increase visibility.
3244 MethodReference target_method_;
3245 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003247 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003248};
3249
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003250class HInvokeVirtual : public HInvoke {
3251 public:
3252 HInvokeVirtual(ArenaAllocator* arena,
3253 uint32_t number_of_arguments,
3254 Primitive::Type return_type,
3255 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003256 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003257 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003258 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003259 vtable_index_(vtable_index) {}
3260
Calin Juravle641547a2015-04-21 22:08:51 +01003261 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003263 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003264 }
3265
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003266 uint32_t GetVTableIndex() const { return vtable_index_; }
3267
3268 DECLARE_INSTRUCTION(InvokeVirtual);
3269
3270 private:
3271 const uint32_t vtable_index_;
3272
3273 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3274};
3275
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003276class HInvokeInterface : public HInvoke {
3277 public:
3278 HInvokeInterface(ArenaAllocator* arena,
3279 uint32_t number_of_arguments,
3280 Primitive::Type return_type,
3281 uint32_t dex_pc,
3282 uint32_t dex_method_index,
3283 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003284 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003285 imt_index_(imt_index) {}
3286
Calin Juravle641547a2015-04-21 22:08:51 +01003287 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003289 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003290 }
3291
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003292 uint32_t GetImtIndex() const { return imt_index_; }
3293 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3294
3295 DECLARE_INSTRUCTION(InvokeInterface);
3296
3297 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003298 const uint32_t imt_index_;
3299
3300 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3301};
3302
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003303class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003304 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003305 HNewInstance(HCurrentMethod* current_method,
3306 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003307 uint16_t type_index,
3308 const DexFile& dex_file,
3309 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003310 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003311 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003312 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003313 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003314 entrypoint_(entrypoint) {
3315 SetRawInputAt(0, current_method);
3316 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003317
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003318 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003319 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003320 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003322 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003323 bool NeedsEnvironment() const OVERRIDE { return true; }
3324 // It may throw when called on:
3325 // - interfaces
3326 // - abstract/innaccessible/unknown classes
3327 // TODO: optimize when possible.
3328 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003329
Calin Juravle10e244f2015-01-26 18:54:32 +00003330 bool CanBeNull() const OVERRIDE { return false; }
3331
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003332 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003334 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003335
3336 private:
3337 const uint32_t dex_pc_;
3338 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003339 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003340 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003341
3342 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3343};
3344
Roland Levillain88cb1752014-10-20 16:36:47 +01003345class HNeg : public HUnaryOperation {
3346 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003347 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003348 : HUnaryOperation(result_type, input) {}
3349
Roland Levillain9867bc72015-08-05 10:21:34 +01003350 template <typename T> T Compute(T x) const { return -x; }
3351
3352 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3353 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3354 }
3355 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3356 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3357 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003358
Roland Levillain88cb1752014-10-20 16:36:47 +01003359 DECLARE_INSTRUCTION(Neg);
3360
3361 private:
3362 DISALLOW_COPY_AND_ASSIGN(HNeg);
3363};
3364
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003365class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003366 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003367 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003368 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003369 uint32_t dex_pc,
3370 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003371 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003372 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003373 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003374 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003375 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003376 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003377 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003378 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003379 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003380 }
3381
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003382 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003384 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003385
3386 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003387 bool NeedsEnvironment() const OVERRIDE { return true; }
3388
Mingyao Yang0c365e62015-03-31 15:09:29 -07003389 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3390 bool CanThrow() const OVERRIDE { return true; }
3391
Calin Juravle10e244f2015-01-26 18:54:32 +00003392 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003393
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003394 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3395
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003396 DECLARE_INSTRUCTION(NewArray);
3397
3398 private:
3399 const uint32_t dex_pc_;
3400 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003401 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003402 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003403
3404 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3405};
3406
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003407class HAdd : public HBinaryOperation {
3408 public:
3409 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3410 : HBinaryOperation(result_type, left, right) {}
3411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003412 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003413
Roland Levillain9867bc72015-08-05 10:21:34 +01003414 template <typename T> T Compute(T x, T y) const { return x + y; }
3415
3416 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3417 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003418 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003419 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3420 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003421 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003422
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003423 DECLARE_INSTRUCTION(Add);
3424
3425 private:
3426 DISALLOW_COPY_AND_ASSIGN(HAdd);
3427};
3428
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003429class HSub : public HBinaryOperation {
3430 public:
3431 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3432 : HBinaryOperation(result_type, left, right) {}
3433
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 template <typename T> T Compute(T x, T y) const { return x - y; }
3435
3436 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3437 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003438 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003439 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3440 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003441 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003442
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003443 DECLARE_INSTRUCTION(Sub);
3444
3445 private:
3446 DISALLOW_COPY_AND_ASSIGN(HSub);
3447};
3448
Calin Juravle34bacdf2014-10-07 20:23:36 +01003449class HMul : public HBinaryOperation {
3450 public:
3451 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3452 : HBinaryOperation(result_type, left, right) {}
3453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003454 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003455
Roland Levillain9867bc72015-08-05 10:21:34 +01003456 template <typename T> T Compute(T x, T y) const { return x * y; }
3457
3458 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3459 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3460 }
3461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3462 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3463 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003464
3465 DECLARE_INSTRUCTION(Mul);
3466
3467 private:
3468 DISALLOW_COPY_AND_ASSIGN(HMul);
3469};
3470
Calin Juravle7c4954d2014-10-28 16:57:40 +00003471class HDiv : public HBinaryOperation {
3472 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003473 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003474 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3475 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003476
Roland Levillain9867bc72015-08-05 10:21:34 +01003477 template <typename T>
3478 T Compute(T x, T y) const {
3479 // Our graph structure ensures we never have 0 for `y` during
3480 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003481 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003482 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003483 return (y == -1) ? -x : x / y;
3484 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003485
Roland Levillain9867bc72015-08-05 10:21:34 +01003486 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3487 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3488 }
3489 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3490 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003491 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003493 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003494
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003495 static SideEffects SideEffectsForArchRuntimeCalls() {
3496 // The generated code can use a runtime call.
3497 return SideEffects::CanTriggerGC();
3498 }
3499
Calin Juravle7c4954d2014-10-28 16:57:40 +00003500 DECLARE_INSTRUCTION(Div);
3501
3502 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003503 const uint32_t dex_pc_;
3504
Calin Juravle7c4954d2014-10-28 16:57:40 +00003505 DISALLOW_COPY_AND_ASSIGN(HDiv);
3506};
3507
Calin Juravlebacfec32014-11-14 15:54:36 +00003508class HRem : public HBinaryOperation {
3509 public:
3510 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003511 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3512 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003513
Roland Levillain9867bc72015-08-05 10:21:34 +01003514 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.
Calin Juravlebacfec32014-11-14 15:54:36 +00003518 DCHECK_NE(y, 0);
3519 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3520 return (y == -1) ? 0 : x % y;
3521 }
3522
Roland Levillain9867bc72015-08-05 10:21:34 +01003523 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3524 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3525 }
3526 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3527 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 }
3529
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003530 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003531
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003532 static SideEffects SideEffectsForArchRuntimeCalls() {
3533 return SideEffects::CanTriggerGC();
3534 }
3535
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 DECLARE_INSTRUCTION(Rem);
3537
3538 private:
3539 const uint32_t dex_pc_;
3540
3541 DISALLOW_COPY_AND_ASSIGN(HRem);
3542};
3543
Calin Juravled0d48522014-11-04 16:40:20 +00003544class HDivZeroCheck : public HExpression<1> {
3545 public:
3546 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3547 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3548 SetRawInputAt(0, value);
3549 }
3550
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003551 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3552
Calin Juravled0d48522014-11-04 16:40:20 +00003553 bool CanBeMoved() const OVERRIDE { return true; }
3554
3555 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3556 UNUSED(other);
3557 return true;
3558 }
3559
3560 bool NeedsEnvironment() const OVERRIDE { return true; }
3561 bool CanThrow() const OVERRIDE { return true; }
3562
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003563 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003564
3565 DECLARE_INSTRUCTION(DivZeroCheck);
3566
3567 private:
3568 const uint32_t dex_pc_;
3569
3570 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3571};
3572
Calin Juravle9aec02f2014-11-18 23:06:35 +00003573class HShl : public HBinaryOperation {
3574 public:
3575 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3576 : HBinaryOperation(result_type, left, right) {}
3577
Roland Levillain9867bc72015-08-05 10:21:34 +01003578 template <typename T, typename U, typename V>
3579 T Compute(T x, U y, V max_shift_value) const {
3580 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3581 "V is not the unsigned integer type corresponding to T");
3582 return x << (y & max_shift_value);
3583 }
3584
3585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3586 return GetBlock()->GetGraph()->GetIntConstant(
3587 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3588 }
3589 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3590 // case is handled as `x << static_cast<int>(y)`.
3591 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3592 return GetBlock()->GetGraph()->GetLongConstant(
3593 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3594 }
3595 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3596 return GetBlock()->GetGraph()->GetLongConstant(
3597 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3598 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003599
3600 DECLARE_INSTRUCTION(Shl);
3601
3602 private:
3603 DISALLOW_COPY_AND_ASSIGN(HShl);
3604};
3605
3606class HShr : public HBinaryOperation {
3607 public:
3608 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3609 : HBinaryOperation(result_type, left, right) {}
3610
Roland Levillain9867bc72015-08-05 10:21:34 +01003611 template <typename T, typename U, typename V>
3612 T Compute(T x, U y, V max_shift_value) const {
3613 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3614 "V is not the unsigned integer type corresponding to T");
3615 return x >> (y & max_shift_value);
3616 }
3617
3618 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3619 return GetBlock()->GetGraph()->GetIntConstant(
3620 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3621 }
3622 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3623 // case is handled as `x >> static_cast<int>(y)`.
3624 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3625 return GetBlock()->GetGraph()->GetLongConstant(
3626 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3627 }
3628 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3629 return GetBlock()->GetGraph()->GetLongConstant(
3630 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3631 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003632
3633 DECLARE_INSTRUCTION(Shr);
3634
3635 private:
3636 DISALLOW_COPY_AND_ASSIGN(HShr);
3637};
3638
3639class HUShr : public HBinaryOperation {
3640 public:
3641 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3642 : HBinaryOperation(result_type, left, right) {}
3643
Roland Levillain9867bc72015-08-05 10:21:34 +01003644 template <typename T, typename U, typename V>
3645 T Compute(T x, U y, V max_shift_value) const {
3646 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3647 "V is not the unsigned integer type corresponding to T");
3648 V ux = static_cast<V>(x);
3649 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003650 }
3651
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3653 return GetBlock()->GetGraph()->GetIntConstant(
3654 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3655 }
3656 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3657 // case is handled as `x >>> static_cast<int>(y)`.
3658 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3659 return GetBlock()->GetGraph()->GetLongConstant(
3660 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3661 }
3662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3663 return GetBlock()->GetGraph()->GetLongConstant(
3664 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665 }
3666
3667 DECLARE_INSTRUCTION(UShr);
3668
3669 private:
3670 DISALLOW_COPY_AND_ASSIGN(HUShr);
3671};
3672
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003673class HAnd : public HBinaryOperation {
3674 public:
3675 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3676 : HBinaryOperation(result_type, left, right) {}
3677
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003678 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 template <typename T, typename U>
3681 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3682
3683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3684 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3685 }
3686 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3687 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3688 }
3689 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3690 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3691 }
3692 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3693 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3694 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003695
3696 DECLARE_INSTRUCTION(And);
3697
3698 private:
3699 DISALLOW_COPY_AND_ASSIGN(HAnd);
3700};
3701
3702class HOr : public HBinaryOperation {
3703 public:
3704 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3705 : HBinaryOperation(result_type, left, right) {}
3706
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003707 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003708
Roland Levillain9867bc72015-08-05 10:21:34 +01003709 template <typename T, typename U>
3710 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3711
3712 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3713 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3714 }
3715 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3716 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3717 }
3718 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3719 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3720 }
3721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3722 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3723 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003724
3725 DECLARE_INSTRUCTION(Or);
3726
3727 private:
3728 DISALLOW_COPY_AND_ASSIGN(HOr);
3729};
3730
3731class HXor : public HBinaryOperation {
3732 public:
3733 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3734 : HBinaryOperation(result_type, left, right) {}
3735
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003736 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003737
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 template <typename T, typename U>
3739 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3740
3741 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3742 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3743 }
3744 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3745 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3746 }
3747 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3748 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3749 }
3750 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3751 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3752 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003753
3754 DECLARE_INSTRUCTION(Xor);
3755
3756 private:
3757 DISALLOW_COPY_AND_ASSIGN(HXor);
3758};
3759
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003760// The value of a parameter in this method. Its location depends on
3761// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003762class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003763 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003764 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003765 : HExpression(parameter_type, SideEffects::None()),
3766 index_(index),
3767 is_this_(is_this),
3768 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003769
3770 uint8_t GetIndex() const { return index_; }
3771
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003772 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3773 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003774
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003775 bool IsThis() const { return is_this_; }
3776
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003777 DECLARE_INSTRUCTION(ParameterValue);
3778
3779 private:
3780 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003781 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003782 const uint8_t index_;
3783
Calin Juravle10e244f2015-01-26 18:54:32 +00003784 // Whether or not the parameter value corresponds to 'this' argument.
3785 const bool is_this_;
3786
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003787 bool can_be_null_;
3788
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003789 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3790};
3791
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003792class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003793 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003794 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003795 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003797 bool CanBeMoved() const OVERRIDE { return true; }
3798 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003799 UNUSED(other);
3800 return true;
3801 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003802
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 template <typename T> T Compute(T x) const { return ~x; }
3804
3805 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3806 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3807 }
3808 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3809 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3810 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003811
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003812 DECLARE_INSTRUCTION(Not);
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HNot);
3816};
3817
David Brazdil66d126e2015-04-03 16:02:44 +01003818class HBooleanNot : public HUnaryOperation {
3819 public:
3820 explicit HBooleanNot(HInstruction* input)
3821 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3822
3823 bool CanBeMoved() const OVERRIDE { return true; }
3824 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3825 UNUSED(other);
3826 return true;
3827 }
3828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003830 DCHECK(IsUint<1>(x));
3831 return !x;
3832 }
3833
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3835 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3836 }
3837 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3838 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003839 UNREACHABLE();
3840 }
3841
3842 DECLARE_INSTRUCTION(BooleanNot);
3843
3844 private:
3845 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3846};
3847
Roland Levillaindff1f282014-11-05 14:15:05 +00003848class HTypeConversion : public HExpression<1> {
3849 public:
3850 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003851 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003852 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3853 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003854 SetRawInputAt(0, input);
3855 DCHECK_NE(input->GetType(), result_type);
3856 }
3857
3858 HInstruction* GetInput() const { return InputAt(0); }
3859 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3860 Primitive::Type GetResultType() const { return GetType(); }
3861
Roland Levillain624279f2014-12-04 11:54:28 +00003862 // Required by the x86 and ARM code generators when producing calls
3863 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003864 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003865
Roland Levillaindff1f282014-11-05 14:15:05 +00003866 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003867 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003868
Mark Mendelle82549b2015-05-06 10:55:34 -04003869 // Try to statically evaluate the conversion and return a HConstant
3870 // containing the result. If the input cannot be converted, return nullptr.
3871 HConstant* TryStaticEvaluation() const;
3872
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003873 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3874 Primitive::Type result_type) {
3875 // Some architectures may not require the 'GC' side effects, but at this point
3876 // in the compilation process we do not know what architecture we will
3877 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003878 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3879 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003880 return SideEffects::CanTriggerGC();
3881 }
3882 return SideEffects::None();
3883 }
3884
Roland Levillaindff1f282014-11-05 14:15:05 +00003885 DECLARE_INSTRUCTION(TypeConversion);
3886
3887 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003888 const uint32_t dex_pc_;
3889
Roland Levillaindff1f282014-11-05 14:15:05 +00003890 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3891};
3892
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003893static constexpr uint32_t kNoRegNumber = -1;
3894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003895class HPhi : public HInstruction {
3896 public:
3897 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003898 : HInstruction(SideEffects::None()),
3899 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003900 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003901 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003902 is_live_(false),
3903 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003904 inputs_.SetSize(number_of_inputs);
3905 }
3906
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003907 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3908 static Primitive::Type ToPhiType(Primitive::Type type) {
3909 switch (type) {
3910 case Primitive::kPrimBoolean:
3911 case Primitive::kPrimByte:
3912 case Primitive::kPrimShort:
3913 case Primitive::kPrimChar:
3914 return Primitive::kPrimInt;
3915 default:
3916 return type;
3917 }
3918 }
3919
David Brazdilffee3d32015-07-06 11:48:53 +01003920 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3921
Calin Juravle10e244f2015-01-26 18:54:32 +00003922 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003923
3924 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003925 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003926
Calin Juravle10e244f2015-01-26 18:54:32 +00003927 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003928 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003929
Calin Juravle10e244f2015-01-26 18:54:32 +00003930 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3931 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3932
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003933 uint32_t GetRegNumber() const { return reg_number_; }
3934
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003935 void SetDead() { is_live_ = false; }
3936 void SetLive() { is_live_ = true; }
3937 bool IsDead() const { return !is_live_; }
3938 bool IsLive() const { return is_live_; }
3939
Calin Juravlea4f88312015-04-16 12:57:19 +01003940 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3941 // An equivalent phi is a phi having the same dex register and type.
3942 // It assumes that phis with the same dex register are adjacent.
3943 HPhi* GetNextEquivalentPhiWithSameType() {
3944 HInstruction* next = GetNext();
3945 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3946 if (next->GetType() == GetType()) {
3947 return next->AsPhi();
3948 }
3949 next = next->GetNext();
3950 }
3951 return nullptr;
3952 }
3953
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003954 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003955
David Brazdil1abb4192015-02-17 18:33:36 +00003956 protected:
3957 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3958
3959 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3960 inputs_.Put(index, input);
3961 }
3962
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003963 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003964 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003965 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003966 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003967 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003968 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003969
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003970 DISALLOW_COPY_AND_ASSIGN(HPhi);
3971};
3972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003973class HNullCheck : public HExpression<1> {
3974 public:
3975 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003976 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003977 SetRawInputAt(0, value);
3978 }
3979
Calin Juravle10e244f2015-01-26 18:54:32 +00003980 bool CanBeMoved() const OVERRIDE { return true; }
3981 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003982 UNUSED(other);
3983 return true;
3984 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003985
Calin Juravle10e244f2015-01-26 18:54:32 +00003986 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003987
Calin Juravle10e244f2015-01-26 18:54:32 +00003988 bool CanThrow() const OVERRIDE { return true; }
3989
3990 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003991
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003992 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003993
3994 DECLARE_INSTRUCTION(NullCheck);
3995
3996 private:
3997 const uint32_t dex_pc_;
3998
3999 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4000};
4001
4002class FieldInfo : public ValueObject {
4003 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004004 FieldInfo(MemberOffset field_offset,
4005 Primitive::Type field_type,
4006 bool is_volatile,
4007 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004008 const DexFile& dex_file,
4009 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004010 : field_offset_(field_offset),
4011 field_type_(field_type),
4012 is_volatile_(is_volatile),
4013 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004014 dex_file_(dex_file),
4015 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004016
4017 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004018 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004019 uint32_t GetFieldIndex() const { return index_; }
4020 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004021 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004022 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004023
4024 private:
4025 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004026 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004027 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004028 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004029 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004030 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031};
4032
4033class HInstanceFieldGet : public HExpression<1> {
4034 public:
4035 HInstanceFieldGet(HInstruction* value,
4036 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004037 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004038 bool is_volatile,
4039 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004040 const DexFile& dex_file,
4041 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004042 : HExpression(
4043 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004044 SideEffects::FieldReadOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004045 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046 SetRawInputAt(0, value);
4047 }
4048
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004049 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004050
4051 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4052 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4053 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004054 }
4055
Calin Juravle641547a2015-04-21 22:08:51 +01004056 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4057 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004058 }
4059
4060 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004061 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4062 }
4063
Calin Juravle52c48962014-12-16 17:02:57 +00004064 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004065 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004067 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068
4069 DECLARE_INSTRUCTION(InstanceFieldGet);
4070
4071 private:
4072 const FieldInfo field_info_;
4073
4074 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4075};
4076
4077class HInstanceFieldSet : public HTemplateInstruction<2> {
4078 public:
4079 HInstanceFieldSet(HInstruction* object,
4080 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004083 bool is_volatile,
4084 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004085 const DexFile& dex_file,
4086 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004087 : HTemplateInstruction(
4088 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004089 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004090 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004091 SetRawInputAt(0, object);
4092 SetRawInputAt(1, value);
4093 }
4094
Calin Juravle641547a2015-04-21 22:08:51 +01004095 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4096 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004097 }
4098
Calin Juravle52c48962014-12-16 17:02:57 +00004099 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004101 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004102 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004103 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004104 bool GetValueCanBeNull() const { return value_can_be_null_; }
4105 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004106
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 DECLARE_INSTRUCTION(InstanceFieldSet);
4108
4109 private:
4110 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004111 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004112
4113 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4114};
4115
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004116class HArrayGet : public HExpression<2> {
4117 public:
4118 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004119 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 SetRawInputAt(0, array);
4121 SetRawInputAt(1, index);
4122 }
4123
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124 bool CanBeMoved() const OVERRIDE { return true; }
4125 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004126 UNUSED(other);
4127 return true;
4128 }
Calin Juravle641547a2015-04-21 22:08:51 +01004129 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4130 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004131 // TODO: We can be smarter here.
4132 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4133 // which generates the implicit null check. There are cases when these can be removed
4134 // to produce better code. If we ever add optimizations to do so we should allow an
4135 // implicit check here (as long as the address falls in the first page).
4136 return false;
4137 }
4138
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004139 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004140
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004141 HInstruction* GetArray() const { return InputAt(0); }
4142 HInstruction* GetIndex() const { return InputAt(1); }
4143
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144 DECLARE_INSTRUCTION(ArrayGet);
4145
4146 private:
4147 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4148};
4149
4150class HArraySet : public HTemplateInstruction<3> {
4151 public:
4152 HArraySet(HInstruction* array,
4153 HInstruction* index,
4154 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004155 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004156 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004157 : HTemplateInstruction(
4158 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4159 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004160 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004161 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004162 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4163 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 SetRawInputAt(0, array);
4165 SetRawInputAt(1, index);
4166 SetRawInputAt(2, value);
4167 }
4168
Calin Juravle77520bc2015-01-12 18:45:46 +00004169 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 // We currently always call a runtime method to catch array store
4171 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004172 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004173 }
4174
Mingyao Yang81014cb2015-06-02 03:16:27 -07004175 // Can throw ArrayStoreException.
4176 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4177
Calin Juravle641547a2015-04-21 22:08:51 +01004178 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4179 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 // TODO: Same as for ArrayGet.
4181 return false;
4182 }
4183
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004184 void ClearNeedsTypeCheck() {
4185 needs_type_check_ = false;
4186 }
4187
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004188 void ClearValueCanBeNull() {
4189 value_can_be_null_ = false;
4190 }
4191
4192 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004193 bool NeedsTypeCheck() const { return needs_type_check_; }
4194
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004195 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004196
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004197 HInstruction* GetArray() const { return InputAt(0); }
4198 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004199 HInstruction* GetValue() const { return InputAt(2); }
4200
4201 Primitive::Type GetComponentType() const {
4202 // The Dex format does not type floating point index operations. Since the
4203 // `expected_component_type_` is set during building and can therefore not
4204 // be correct, we also check what is the value type. If it is a floating
4205 // point type, we must use that type.
4206 Primitive::Type value_type = GetValue()->GetType();
4207 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4208 ? value_type
4209 : expected_component_type_;
4210 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004211
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004212 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4213 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4214 }
4215
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216 DECLARE_INSTRUCTION(ArraySet);
4217
4218 private:
4219 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004220 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004222 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223
4224 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4225};
4226
4227class HArrayLength : public HExpression<1> {
4228 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004229 explicit HArrayLength(HInstruction* array)
4230 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4231 // Note that arrays do not change length, so the instruction does not
4232 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 SetRawInputAt(0, array);
4234 }
4235
Calin Juravle77520bc2015-01-12 18:45:46 +00004236 bool CanBeMoved() const OVERRIDE { return true; }
4237 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004238 UNUSED(other);
4239 return true;
4240 }
Calin Juravle641547a2015-04-21 22:08:51 +01004241 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4242 return obj == InputAt(0);
4243 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245 DECLARE_INSTRUCTION(ArrayLength);
4246
4247 private:
4248 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4249};
4250
4251class HBoundsCheck : public HExpression<2> {
4252 public:
4253 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004254 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 DCHECK(index->GetType() == Primitive::kPrimInt);
4256 SetRawInputAt(0, index);
4257 SetRawInputAt(1, length);
4258 }
4259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004260 bool CanBeMoved() const OVERRIDE { return true; }
4261 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004262 UNUSED(other);
4263 return true;
4264 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004266 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004268 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004269
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004270 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271
4272 DECLARE_INSTRUCTION(BoundsCheck);
4273
4274 private:
4275 const uint32_t dex_pc_;
4276
4277 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4278};
4279
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280/**
4281 * Some DEX instructions are folded into multiple HInstructions that need
4282 * to stay live until the last HInstruction. This class
4283 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004284 * HInstruction stays live. `index` represents the stack location index of the
4285 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286 */
4287class HTemporary : public HTemplateInstruction<0> {
4288 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004289 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290
4291 size_t GetIndex() const { return index_; }
4292
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004293 Primitive::Type GetType() const OVERRIDE {
4294 // The previous instruction is the one that will be stored in the temporary location.
4295 DCHECK(GetPrevious() != nullptr);
4296 return GetPrevious()->GetType();
4297 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004298
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 DECLARE_INSTRUCTION(Temporary);
4300
4301 private:
4302 const size_t index_;
4303
4304 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4305};
4306
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004307class HSuspendCheck : public HTemplateInstruction<0> {
4308 public:
4309 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004310 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004311
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004312 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004313 return true;
4314 }
4315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004316 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004317 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4318 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004319
4320 DECLARE_INSTRUCTION(SuspendCheck);
4321
4322 private:
4323 const uint32_t dex_pc_;
4324
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004325 // Only used for code generation, in order to share the same slow path between back edges
4326 // of a same loop.
4327 SlowPathCode* slow_path_;
4328
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004329 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4330};
4331
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004332/**
4333 * Instruction to load a Class object.
4334 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004335class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004336 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004337 HLoadClass(HCurrentMethod* current_method,
4338 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004339 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004340 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004341 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004342 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004344 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004345 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004346 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004347 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004348 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004349 SetRawInputAt(0, current_method);
4350 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004351
4352 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004353
4354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4355 return other->AsLoadClass()->type_index_ == type_index_;
4356 }
4357
4358 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4359
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004360 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004361 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004362 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004363 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004364
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004365 bool NeedsEnvironment() const OVERRIDE {
4366 // Will call runtime and load the class if the class is not loaded yet.
4367 // TODO: finer grain decision.
4368 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004369 }
4370
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004371 bool MustGenerateClinitCheck() const {
4372 return generate_clinit_check_;
4373 }
4374
Calin Juravle0ba218d2015-05-19 18:46:01 +01004375 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4376 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004377 }
4378
4379 bool CanCallRuntime() const {
4380 return MustGenerateClinitCheck() || !is_referrers_class_;
4381 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004382
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004383 bool CanThrow() const OVERRIDE {
4384 // May call runtime and and therefore can throw.
4385 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004386 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004387 }
4388
Calin Juravleacf735c2015-02-12 15:25:22 +00004389 ReferenceTypeInfo GetLoadedClassRTI() {
4390 return loaded_class_rti_;
4391 }
4392
4393 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4394 // Make sure we only set exact types (the loaded class should never be merged).
4395 DCHECK(rti.IsExact());
4396 loaded_class_rti_ = rti;
4397 }
4398
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004399 const DexFile& GetDexFile() { return dex_file_; }
4400
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004401 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4402
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004403 static SideEffects SideEffectsForArchRuntimeCalls() {
4404 return SideEffects::CanTriggerGC();
4405 }
4406
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004407 DECLARE_INSTRUCTION(LoadClass);
4408
4409 private:
4410 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004411 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004412 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004413 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004414 // Whether this instruction must generate the initialization check.
4415 // Used for code generation.
4416 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004417
Calin Juravleacf735c2015-02-12 15:25:22 +00004418 ReferenceTypeInfo loaded_class_rti_;
4419
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004420 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4421};
4422
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004423class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004424 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004425 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004426 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004427 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004428 dex_pc_(dex_pc) {
4429 SetRawInputAt(0, current_method);
4430 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004431
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004432 bool CanBeMoved() const OVERRIDE { return true; }
4433
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004434 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4435 return other->AsLoadString()->string_index_ == string_index_;
4436 }
4437
4438 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4439
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004440 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004441 uint32_t GetStringIndex() const { return string_index_; }
4442
4443 // TODO: Can we deopt or debug when we resolve a string?
4444 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004445 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004446 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004447
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004448 static SideEffects SideEffectsForArchRuntimeCalls() {
4449 return SideEffects::CanTriggerGC();
4450 }
4451
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004452 DECLARE_INSTRUCTION(LoadString);
4453
4454 private:
4455 const uint32_t string_index_;
4456 const uint32_t dex_pc_;
4457
4458 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4459};
4460
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004461/**
4462 * Performs an initialization check on its Class object input.
4463 */
4464class HClinitCheck : public HExpression<1> {
4465 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004466 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004467 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004468 Primitive::kPrimNot,
4469 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470 dex_pc_(dex_pc) {
4471 SetRawInputAt(0, constant);
4472 }
4473
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004474 bool CanBeMoved() const OVERRIDE { return true; }
4475 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4476 UNUSED(other);
4477 return true;
4478 }
4479
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004480 bool NeedsEnvironment() const OVERRIDE {
4481 // May call runtime to initialize the class.
4482 return true;
4483 }
4484
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004485 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004486
4487 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4488
4489 DECLARE_INSTRUCTION(ClinitCheck);
4490
4491 private:
4492 const uint32_t dex_pc_;
4493
4494 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4495};
4496
4497class HStaticFieldGet : public HExpression<1> {
4498 public:
4499 HStaticFieldGet(HInstruction* cls,
4500 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004501 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004502 bool is_volatile,
4503 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004504 const DexFile& dex_file,
4505 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004506 : HExpression(
4507 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004508 SideEffects::FieldReadOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004509 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004510 SetRawInputAt(0, cls);
4511 }
4512
Calin Juravle52c48962014-12-16 17:02:57 +00004513
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004514 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004515
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004516 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004517 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4518 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004519 }
4520
4521 size_t ComputeHashCode() const OVERRIDE {
4522 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4523 }
4524
Calin Juravle52c48962014-12-16 17:02:57 +00004525 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004526 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4527 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004528 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004529
4530 DECLARE_INSTRUCTION(StaticFieldGet);
4531
4532 private:
4533 const FieldInfo field_info_;
4534
4535 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4536};
4537
4538class HStaticFieldSet : public HTemplateInstruction<2> {
4539 public:
4540 HStaticFieldSet(HInstruction* cls,
4541 HInstruction* value,
4542 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004543 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004544 bool is_volatile,
4545 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004546 const DexFile& dex_file,
4547 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004548 : HTemplateInstruction(
4549 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004550 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004551 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004552 SetRawInputAt(0, cls);
4553 SetRawInputAt(1, value);
4554 }
4555
Calin Juravle52c48962014-12-16 17:02:57 +00004556 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4558 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004559 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004560
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004561 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004562 bool GetValueCanBeNull() const { return value_can_be_null_; }
4563 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004564
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004565 DECLARE_INSTRUCTION(StaticFieldSet);
4566
4567 private:
4568 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004569 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004570
4571 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4572};
4573
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004574// Implement the move-exception DEX instruction.
4575class HLoadException : public HExpression<0> {
4576 public:
4577 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4578
David Brazdilbbd733e2015-08-18 17:48:17 +01004579 bool CanBeNull() const OVERRIDE { return false; }
4580
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004581 DECLARE_INSTRUCTION(LoadException);
4582
4583 private:
4584 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4585};
4586
David Brazdilcb1c0552015-08-04 16:22:25 +01004587// Implicit part of move-exception which clears thread-local exception storage.
4588// Must not be removed because the runtime expects the TLS to get cleared.
4589class HClearException : public HTemplateInstruction<0> {
4590 public:
4591 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4592
4593 DECLARE_INSTRUCTION(ClearException);
4594
4595 private:
4596 DISALLOW_COPY_AND_ASSIGN(HClearException);
4597};
4598
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004599class HThrow : public HTemplateInstruction<1> {
4600 public:
4601 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004602 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004603 SetRawInputAt(0, exception);
4604 }
4605
4606 bool IsControlFlow() const OVERRIDE { return true; }
4607
4608 bool NeedsEnvironment() const OVERRIDE { return true; }
4609
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004610 bool CanThrow() const OVERRIDE { return true; }
4611
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004612 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004613
4614 DECLARE_INSTRUCTION(Throw);
4615
4616 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004617 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004618
4619 DISALLOW_COPY_AND_ASSIGN(HThrow);
4620};
4621
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004622class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004623 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004624 HInstanceOf(HInstruction* object,
4625 HLoadClass* constant,
4626 bool class_is_final,
4627 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004628 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004629 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004630 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004631 dex_pc_(dex_pc) {
4632 SetRawInputAt(0, object);
4633 SetRawInputAt(1, constant);
4634 }
4635
4636 bool CanBeMoved() const OVERRIDE { return true; }
4637
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004638 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004639 return true;
4640 }
4641
4642 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004643 return false;
4644 }
4645
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004646 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004647
4648 bool IsClassFinal() const { return class_is_final_; }
4649
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004650 // Used only in code generation.
4651 bool MustDoNullCheck() const { return must_do_null_check_; }
4652 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4653
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004654 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4655 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4656 }
4657
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004658 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004659
4660 private:
4661 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004662 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004663 const uint32_t dex_pc_;
4664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004665 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4666};
4667
Calin Juravleb1498f62015-02-16 13:13:29 +00004668class HBoundType : public HExpression<1> {
4669 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004670 // Constructs an HBoundType with the given upper_bound.
4671 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004672 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004673 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004674 upper_bound_(upper_bound),
4675 upper_can_be_null_(upper_can_be_null),
4676 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004677 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004678 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004679 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004680 }
4681
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004682 // GetUpper* should only be used in reference type propagation.
4683 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4684 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004685
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004686 void SetCanBeNull(bool can_be_null) {
4687 DCHECK(upper_can_be_null_ || !can_be_null);
4688 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004689 }
4690
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004691 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4692
Calin Juravleb1498f62015-02-16 13:13:29 +00004693 DECLARE_INSTRUCTION(BoundType);
4694
4695 private:
4696 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004697 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4698 // It is used to bound the type in cases like:
4699 // if (x instanceof ClassX) {
4700 // // uper_bound_ will be ClassX
4701 // }
4702 const ReferenceTypeInfo upper_bound_;
4703 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4704 // is false then can_be_null_ cannot be true).
4705 const bool upper_can_be_null_;
4706 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004707
4708 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4709};
4710
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004711class HCheckCast : public HTemplateInstruction<2> {
4712 public:
4713 HCheckCast(HInstruction* object,
4714 HLoadClass* constant,
4715 bool class_is_final,
4716 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004717 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004718 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004719 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004720 dex_pc_(dex_pc) {
4721 SetRawInputAt(0, object);
4722 SetRawInputAt(1, constant);
4723 }
4724
4725 bool CanBeMoved() const OVERRIDE { return true; }
4726
4727 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4728 return true;
4729 }
4730
4731 bool NeedsEnvironment() const OVERRIDE {
4732 // Instruction may throw a CheckCastError.
4733 return true;
4734 }
4735
4736 bool CanThrow() const OVERRIDE { return true; }
4737
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004738 bool MustDoNullCheck() const { return must_do_null_check_; }
4739 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4740
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004741 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004742
4743 bool IsClassFinal() const { return class_is_final_; }
4744
4745 DECLARE_INSTRUCTION(CheckCast);
4746
4747 private:
4748 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004749 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004750 const uint32_t dex_pc_;
4751
4752 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004753};
4754
Calin Juravle27df7582015-04-17 19:12:31 +01004755class HMemoryBarrier : public HTemplateInstruction<0> {
4756 public:
4757 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004758 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004759 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004760 barrier_kind_(barrier_kind) {}
4761
4762 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4763
4764 DECLARE_INSTRUCTION(MemoryBarrier);
4765
4766 private:
4767 const MemBarrierKind barrier_kind_;
4768
4769 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4770};
4771
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004772class HMonitorOperation : public HTemplateInstruction<1> {
4773 public:
4774 enum OperationKind {
4775 kEnter,
4776 kExit,
4777 };
4778
4779 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004780 : HTemplateInstruction(
4781 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004782 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004783 SetRawInputAt(0, object);
4784 }
4785
4786 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004787 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4788
4789 bool CanThrow() const OVERRIDE {
4790 // Verifier guarantees that monitor-exit cannot throw.
4791 // This is important because it allows the HGraphBuilder to remove
4792 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4793 return IsEnter();
4794 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004795
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004796 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004797
4798 bool IsEnter() const { return kind_ == kEnter; }
4799
4800 DECLARE_INSTRUCTION(MonitorOperation);
4801
Calin Juravle52c48962014-12-16 17:02:57 +00004802 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004803 const OperationKind kind_;
4804 const uint32_t dex_pc_;
4805
4806 private:
4807 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4808};
4809
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004810/**
4811 * A HInstruction used as a marker for the replacement of new + <init>
4812 * of a String to a call to a StringFactory. Only baseline will see
4813 * the node at code generation, where it will be be treated as null.
4814 * When compiling non-baseline, `HFakeString` instructions are being removed
4815 * in the instruction simplifier.
4816 */
4817class HFakeString : public HTemplateInstruction<0> {
4818 public:
4819 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4820
4821 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4822
4823 DECLARE_INSTRUCTION(FakeString);
4824
4825 private:
4826 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4827};
4828
Vladimir Markof9f64412015-09-02 14:05:49 +01004829class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004830 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004831 MoveOperands(Location source,
4832 Location destination,
4833 Primitive::Type type,
4834 HInstruction* instruction)
4835 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004836
4837 Location GetSource() const { return source_; }
4838 Location GetDestination() const { return destination_; }
4839
4840 void SetSource(Location value) { source_ = value; }
4841 void SetDestination(Location value) { destination_ = value; }
4842
4843 // The parallel move resolver marks moves as "in-progress" by clearing the
4844 // destination (but not the source).
4845 Location MarkPending() {
4846 DCHECK(!IsPending());
4847 Location dest = destination_;
4848 destination_ = Location::NoLocation();
4849 return dest;
4850 }
4851
4852 void ClearPending(Location dest) {
4853 DCHECK(IsPending());
4854 destination_ = dest;
4855 }
4856
4857 bool IsPending() const {
4858 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4859 return destination_.IsInvalid() && !source_.IsInvalid();
4860 }
4861
4862 // True if this blocks a move from the given location.
4863 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004864 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004865 }
4866
4867 // A move is redundant if it's been eliminated, if its source and
4868 // destination are the same, or if its destination is unneeded.
4869 bool IsRedundant() const {
4870 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4871 }
4872
4873 // We clear both operands to indicate move that's been eliminated.
4874 void Eliminate() {
4875 source_ = destination_ = Location::NoLocation();
4876 }
4877
4878 bool IsEliminated() const {
4879 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4880 return source_.IsInvalid();
4881 }
4882
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 Primitive::Type GetType() const { return type_; }
4884
Nicolas Geoffray90218252015-04-15 11:56:51 +01004885 bool Is64BitMove() const {
4886 return Primitive::Is64BitType(type_);
4887 }
4888
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004889 HInstruction* GetInstruction() const { return instruction_; }
4890
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004891 private:
4892 Location source_;
4893 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004894 // The type this move is for.
4895 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004896 // The instruction this move is assocatied with. Null when this move is
4897 // for moving an input in the expected locations of user (including a phi user).
4898 // This is only used in debug mode, to ensure we do not connect interval siblings
4899 // in the same parallel move.
4900 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004901};
4902
4903static constexpr size_t kDefaultNumberOfMoves = 4;
4904
4905class HParallelMove : public HTemplateInstruction<0> {
4906 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004907 explicit HParallelMove(ArenaAllocator* arena)
4908 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004909
Nicolas Geoffray90218252015-04-15 11:56:51 +01004910 void AddMove(Location source,
4911 Location destination,
4912 Primitive::Type type,
4913 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004914 DCHECK(source.IsValid());
4915 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004916 if (kIsDebugBuild) {
4917 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004918 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004919 if (moves_.Get(i).GetInstruction() == instruction) {
4920 // Special case the situation where the move is for the spill slot
4921 // of the instruction.
4922 if ((GetPrevious() == instruction)
4923 || ((GetPrevious() == nullptr)
4924 && instruction->IsPhi()
4925 && instruction->GetBlock() == GetBlock())) {
4926 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4927 << "Doing parallel moves for the same instruction.";
4928 } else {
4929 DCHECK(false) << "Doing parallel moves for the same instruction.";
4930 }
4931 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004932 }
4933 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004934 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004935 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004936 << "Overlapped destination for two moves in a parallel move: "
4937 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4938 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004939 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004940 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004941 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004942 }
4943
4944 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004945 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004946 }
4947
4948 size_t NumMoves() const { return moves_.Size(); }
4949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004950 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004951
4952 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004953 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004954
4955 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4956};
4957
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004958class HGraphVisitor : public ValueObject {
4959 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004960 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4961 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004962
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004963 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004964 virtual void VisitBasicBlock(HBasicBlock* block);
4965
Roland Levillain633021e2014-10-01 14:12:25 +01004966 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004967 void VisitInsertionOrder();
4968
Roland Levillain633021e2014-10-01 14:12:25 +01004969 // Visit the graph following dominator tree reverse post-order.
4970 void VisitReversePostOrder();
4971
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004972 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004973
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004974 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004975#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004976 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4977
4978 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4979
4980#undef DECLARE_VISIT_INSTRUCTION
4981
4982 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004983 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004984
4985 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4986};
4987
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004988class HGraphDelegateVisitor : public HGraphVisitor {
4989 public:
4990 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4991 virtual ~HGraphDelegateVisitor() {}
4992
4993 // Visit functions that delegate to to super class.
4994#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004995 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004996
4997 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4998
4999#undef DECLARE_VISIT_INSTRUCTION
5000
5001 private:
5002 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5003};
5004
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005005class HInsertionOrderIterator : public ValueObject {
5006 public:
5007 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5008
5009 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5010 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5011 void Advance() { ++index_; }
5012
5013 private:
5014 const HGraph& graph_;
5015 size_t index_;
5016
5017 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5018};
5019
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005020class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005021 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005022 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5023 // Check that reverse post order of the graph has been built.
5024 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5025 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005026
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005027 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5028 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005029 void Advance() { ++index_; }
5030
5031 private:
5032 const HGraph& graph_;
5033 size_t index_;
5034
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005035 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005036};
5037
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005038class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005039 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005040 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005041 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5042 // Check that reverse post order of the graph has been built.
5043 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5044 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005045
5046 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005047 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005048 void Advance() { --index_; }
5049
5050 private:
5051 const HGraph& graph_;
5052 size_t index_;
5053
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005054 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005055};
5056
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005057class HLinearPostOrderIterator : public ValueObject {
5058 public:
5059 explicit HLinearPostOrderIterator(const HGraph& graph)
5060 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5061
5062 bool Done() const { return index_ == 0; }
5063
5064 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5065
5066 void Advance() {
5067 --index_;
5068 DCHECK_GE(index_, 0U);
5069 }
5070
5071 private:
5072 const GrowableArray<HBasicBlock*>& order_;
5073 size_t index_;
5074
5075 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5076};
5077
5078class HLinearOrderIterator : public ValueObject {
5079 public:
5080 explicit HLinearOrderIterator(const HGraph& graph)
5081 : order_(graph.GetLinearOrder()), index_(0) {}
5082
5083 bool Done() const { return index_ == order_.Size(); }
5084 HBasicBlock* Current() const { return order_.Get(index_); }
5085 void Advance() { ++index_; }
5086
5087 private:
5088 const GrowableArray<HBasicBlock*>& order_;
5089 size_t index_;
5090
5091 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5092};
5093
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005094// Iterator over the blocks that art part of the loop. Includes blocks part
5095// of an inner loop. The order in which the blocks are iterated is on their
5096// block id.
5097class HBlocksInLoopIterator : public ValueObject {
5098 public:
5099 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5100 : blocks_in_loop_(info.GetBlocks()),
5101 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5102 index_(0) {
5103 if (!blocks_in_loop_.IsBitSet(index_)) {
5104 Advance();
5105 }
5106 }
5107
5108 bool Done() const { return index_ == blocks_.Size(); }
5109 HBasicBlock* Current() const { return blocks_.Get(index_); }
5110 void Advance() {
5111 ++index_;
5112 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5113 if (blocks_in_loop_.IsBitSet(index_)) {
5114 break;
5115 }
5116 }
5117 }
5118
5119 private:
5120 const BitVector& blocks_in_loop_;
5121 const GrowableArray<HBasicBlock*>& blocks_;
5122 size_t index_;
5123
5124 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5125};
5126
Mingyao Yang3584bce2015-05-19 16:01:59 -07005127// Iterator over the blocks that art part of the loop. Includes blocks part
5128// of an inner loop. The order in which the blocks are iterated is reverse
5129// post order.
5130class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5131 public:
5132 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5133 : blocks_in_loop_(info.GetBlocks()),
5134 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5135 index_(0) {
5136 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5137 Advance();
5138 }
5139 }
5140
5141 bool Done() const { return index_ == blocks_.Size(); }
5142 HBasicBlock* Current() const { return blocks_.Get(index_); }
5143 void Advance() {
5144 ++index_;
5145 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5146 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5147 break;
5148 }
5149 }
5150 }
5151
5152 private:
5153 const BitVector& blocks_in_loop_;
5154 const GrowableArray<HBasicBlock*>& blocks_;
5155 size_t index_;
5156
5157 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5158};
5159
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005160inline int64_t Int64FromConstant(HConstant* constant) {
5161 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5162 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5163 : constant->AsLongConstant()->GetValue();
5164}
5165
Vladimir Marko58155012015-08-19 12:49:41 +00005166inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5167 // For the purposes of the compiler, the dex files must actually be the same object
5168 // if we want to safely treat them as the same. This is especially important for JIT
5169 // as custom class loaders can open the same underlying file (or memory) multiple
5170 // times and provide different class resolution but no two class loaders should ever
5171 // use the same DexFile object - doing so is an unsupported hack that can lead to
5172 // all sorts of weird failures.
5173 return &lhs == &rhs;
5174}
5175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005176} // namespace art
5177
5178#endif // ART_COMPILER_OPTIMIZING_NODES_H_