blob: ee82fda6c814015f15f5f0450efc3372fff7417e [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
62static const int kDefaultNumberOfBlocks = 8;
63static const int kDefaultNumberOfSuccessors = 2;
64static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010065static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010066static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000067static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068
Calin Juravle9aec02f2014-11-18 23:06:35 +000069static constexpr uint32_t kMaxIntShiftValue = 0x1f;
70static constexpr uint64_t kMaxLongShiftValue = 0x3f;
71
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010072static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
73
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010074static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
75
Dave Allison20dfc792014-06-16 20:44:29 -070076enum IfCondition {
77 kCondEQ,
78 kCondNE,
79 kCondLT,
80 kCondLE,
81 kCondGT,
82 kCondGE,
83};
84
Vladimir Markof9f64412015-09-02 14:05:49 +010085class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010086 public:
87 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
88
89 void AddInstruction(HInstruction* instruction);
90 void RemoveInstruction(HInstruction* instruction);
91
David Brazdilc3d743f2015-04-22 13:40:50 +010092 // Insert `instruction` before/after an existing instruction `cursor`.
93 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
94 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
95
Roland Levillain6b469232014-09-25 10:10:38 +010096 // Return true if this list contains `instruction`.
97 bool Contains(HInstruction* instruction) const;
98
Roland Levillainccc07a92014-09-16 14:48:16 +010099 // Return true if `instruction1` is found before `instruction2` in
100 // this instruction list and false otherwise. Abort if none
101 // of these instructions is found.
102 bool FoundBefore(const HInstruction* instruction1,
103 const HInstruction* instruction2) const;
104
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000105 bool IsEmpty() const { return first_instruction_ == nullptr; }
106 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
107
108 // Update the block of all instructions to be `block`.
109 void SetBlockOfInstructions(HBasicBlock* block) const;
110
111 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
112 void Add(const HInstructionList& instruction_list);
113
David Brazdil2d7352b2015-04-20 14:52:42 +0100114 // Return the number of instructions in the list. This is an expensive operation.
115 size_t CountSize() const;
116
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100117 private:
118 HInstruction* first_instruction_;
119 HInstruction* last_instruction_;
120
121 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000122 friend class HGraph;
123 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100125 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100126
127 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
128};
129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100131class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000132 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100133 HGraph(ArenaAllocator* arena,
134 const DexFile& dex_file,
135 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100136 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100138 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139 bool debuggable = false,
140 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000141 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100143 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100144 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700145 entry_block_(nullptr),
146 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100147 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100148 number_of_vregs_(0),
149 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000150 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400151 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000152 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000153 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 dex_file_(dex_file),
155 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100156 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100157 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100158 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000160 cached_null_constant_(nullptr),
161 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000162 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
163 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100164 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
165 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100168 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100169 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170
David Brazdil69ba7b72015-06-23 18:27:30 +0100171 bool IsInSsaForm() const { return in_ssa_form_; }
172
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000173 HBasicBlock* GetEntryBlock() const { return entry_block_; }
174 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100175 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000176
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000177 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
178 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000179
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000180 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 // Try building the SSA form of this graph, with dominance computation and loop
183 // recognition. Returns whether it was successful in doing all these steps.
184 bool TryBuildingSsa() {
185 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000186 // The SSA builder requires loops to all be natural. Specifically, the dead phi
187 // elimination phase checks the consistency of the graph when doing a post-order
188 // visit for eliminating dead phis: a dead phi can only have loop header phi
189 // users remaining when being visited.
190 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100191 // Precompute per-block try membership before entering the SSA builder,
192 // which needs the information to build catch block phis from values of
193 // locals at throwing instructions inside try blocks.
194 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000195 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100196 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000197 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000198 }
199
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100200 void ComputeDominanceInformation();
201 void ClearDominanceInformation();
202
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100205 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100206 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000207
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000208 // Analyze all natural loops in this graph. Returns false if one
209 // loop is not natural, that is the header does not dominate the
210 // back edge.
211 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
David Brazdilffee3d32015-07-06 11:48:53 +0100213 // Iterate over blocks to compute try block membership. Needs reverse post
214 // order and loop information.
215 void ComputeTryBlockInformation();
216
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000218 // Returns the instruction used to replace the invoke expression or null if the
219 // invoke is for a void method.
220 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221
Mingyao Yang3584bce2015-05-19 16:01:59 -0700222 // Need to add a couple of blocks to test if the loop body is entered and
223 // put deoptimization instructions, etc.
224 void TransformLoopHeaderForBCE(HBasicBlock* header);
225
David Brazdil2d7352b2015-04-20 14:52:42 +0100226 // Removes `block` from the graph.
227 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000228
David Brazdilfc6a86a2015-06-26 10:33:45 +0000229 // Splits the edge between `block` and `successor` while preserving the
230 // indices in the predecessor/successor lists. If there are multiple edges
231 // between the blocks, the lowest indices are used.
232 // Returns the new block which is empty and has the same dex pc as `successor`.
233 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
234
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
236 void SimplifyLoop(HBasicBlock* header);
237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 int32_t GetNextInstructionId() {
239 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000240 return current_instruction_id_++;
241 }
242
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000243 int32_t GetCurrentInstructionId() const {
244 return current_instruction_id_;
245 }
246
247 void SetCurrentInstructionId(int32_t id) {
248 current_instruction_id_ = id;
249 }
250
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100251 uint16_t GetMaximumNumberOfOutVRegs() const {
252 return maximum_number_of_out_vregs_;
253 }
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
256 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100257 }
258
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100259 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
260 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
261 }
262
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000263 void UpdateTemporariesVRegSlots(size_t slots) {
264 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100265 }
266
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000267 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000269 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100270 }
271
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100272 void SetNumberOfVRegs(uint16_t number_of_vregs) {
273 number_of_vregs_ = number_of_vregs;
274 }
275
276 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100278 return number_of_vregs_;
279 }
280
281 void SetNumberOfInVRegs(uint16_t value) {
282 number_of_in_vregs_ = value;
283 }
284
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100285 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100286 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100287 return number_of_vregs_ - number_of_in_vregs_;
288 }
289
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100290 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
291 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100292 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100294 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
295 return linear_order_;
296 }
297
Mark Mendell1152c922015-04-24 17:06:35 -0400298 bool HasBoundsChecks() const {
299 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800300 }
301
Mark Mendell1152c922015-04-24 17:06:35 -0400302 void SetHasBoundsChecks(bool value) {
303 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800304 }
305
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100306 bool ShouldGenerateConstructorBarrier() const {
307 return should_generate_constructor_barrier_;
308 }
309
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000310 bool IsDebuggable() const { return debuggable_; }
311
David Brazdil8d5b8b22015-03-24 10:51:52 +0000312 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000313 // already, it is created and inserted into the graph. This method is only for
314 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000315 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000316
317 // TODO: This is problematic for the consistency of reference type propagation
318 // because it can be created anytime after the pass and thus it will be left
319 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000320 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000321
David Brazdil8d5b8b22015-03-24 10:51:52 +0000322 HIntConstant* GetIntConstant(int32_t value) {
323 return CreateConstant(value, &cached_int_constants_);
324 }
325 HLongConstant* GetLongConstant(int64_t value) {
326 return CreateConstant(value, &cached_long_constants_);
327 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000328 HFloatConstant* GetFloatConstant(float value) {
329 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
330 }
331 HDoubleConstant* GetDoubleConstant(double value) {
332 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
333 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000334
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100335 HCurrentMethod* GetCurrentMethod();
336
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000337 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100338
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100339 const DexFile& GetDexFile() const {
340 return dex_file_;
341 }
342
343 uint32_t GetMethodIdx() const {
344 return method_idx_;
345 }
346
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100347 InvokeType GetInvokeType() const {
348 return invoke_type_;
349 }
350
Mark Mendellc4701932015-04-10 13:18:51 -0400351 InstructionSet GetInstructionSet() const {
352 return instruction_set_;
353 }
354
David Brazdilbbd733e2015-08-18 17:48:17 +0100355 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
356 bool HasTryCatch() const;
357
David Brazdil2d7352b2015-04-20 14:52:42 +0100358 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359 void VisitBlockForDominatorTree(HBasicBlock* block,
360 HBasicBlock* predecessor,
361 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100362 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363 void VisitBlockForBackEdges(HBasicBlock* block,
364 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100365 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000366 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100367 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000368
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000369 template <class InstructionType, typename ValueType>
370 InstructionType* CreateConstant(ValueType value,
371 ArenaSafeMap<ValueType, InstructionType*>* cache) {
372 // Try to find an existing constant of the given value.
373 InstructionType* constant = nullptr;
374 auto cached_constant = cache->find(value);
375 if (cached_constant != cache->end()) {
376 constant = cached_constant->second;
377 }
378
379 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100380 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000381 if (constant == nullptr || constant->GetBlock() == nullptr) {
382 constant = new (arena_) InstructionType(value);
383 cache->Overwrite(value, constant);
384 InsertConstant(constant);
385 }
386 return constant;
387 }
388
David Brazdil8d5b8b22015-03-24 10:51:52 +0000389 void InsertConstant(HConstant* instruction);
390
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 // Cache a float constant into the graph. This method should only be
392 // called by the SsaBuilder when creating "equivalent" instructions.
393 void CacheFloatConstant(HFloatConstant* constant);
394
395 // See CacheFloatConstant comment.
396 void CacheDoubleConstant(HDoubleConstant* constant);
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000399
400 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 GrowableArray<HBasicBlock*> blocks_;
402
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100403 // List of blocks to perform a reverse post order tree traversal.
404 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000405
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100406 // List of blocks to perform a linear order tree traversal.
407 GrowableArray<HBasicBlock*> linear_order_;
408
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000409 HBasicBlock* entry_block_;
410 HBasicBlock* exit_block_;
411
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100412 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100413 uint16_t maximum_number_of_out_vregs_;
414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100415 // The number of virtual registers in this method. Contains the parameters.
416 uint16_t number_of_vregs_;
417
418 // The number of virtual registers used by parameters of this method.
419 uint16_t number_of_in_vregs_;
420
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000421 // Number of vreg size slots that the temporaries use (used in baseline compiler).
422 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100423
Mark Mendell1152c922015-04-24 17:06:35 -0400424 // Has bounds checks. We can totally skip BCE if it's false.
425 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800426
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000427 // Indicates whether the graph should be compiled in a way that
428 // ensures full debuggability. If false, we can apply more
429 // aggressive optimizations that may limit the level of debugging.
430 const bool debuggable_;
431
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000432 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000434
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100435 // The dex file from which the method is from.
436 const DexFile& dex_file_;
437
438 // The method index in the dex file.
439 const uint32_t method_idx_;
440
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100441 // If inlined, this encodes how the callee is being invoked.
442 const InvokeType invoke_type_;
443
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 // Whether the graph has been transformed to SSA form. Only used
445 // in debug mode to ensure we are not using properties only valid
446 // for non-SSA form (like the number of temporaries).
447 bool in_ssa_form_;
448
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100449 const bool should_generate_constructor_barrier_;
450
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 const InstructionSet instruction_set_;
452
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000453 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000454 HNullConstant* cached_null_constant_;
455 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000456 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000457 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000459
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100460 HCurrentMethod* cached_current_method_;
461
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100463 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000464 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465 DISALLOW_COPY_AND_ASSIGN(HGraph);
466};
467
Vladimir Markof9f64412015-09-02 14:05:49 +0100468class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000469 public:
470 HLoopInformation(HBasicBlock* header, HGraph* graph)
471 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100472 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100474 // Make bit vector growable, as the number of blocks may change.
475 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100476
477 HBasicBlock* GetHeader() const {
478 return header_;
479 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000481 void SetHeader(HBasicBlock* block) {
482 header_ = block;
483 }
484
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100485 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
486 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
487 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
488
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 void AddBackEdge(HBasicBlock* back_edge) {
490 back_edges_.Add(back_edge);
491 }
492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 void RemoveBackEdge(HBasicBlock* back_edge) {
494 back_edges_.Delete(back_edge);
495 }
496
David Brazdil46e2a392015-03-16 17:31:52 +0000497 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000499 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500 }
501 return false;
502 }
503
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000504 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505 return back_edges_.Size();
506 }
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
511 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 }
513
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100514 // Returns the lifetime position of the back edge that has the
515 // greatest lifetime position.
516 size_t GetLifetimeEnd() const;
517
518 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
519 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
520 if (back_edges_.Get(i) == existing) {
521 back_edges_.Put(i, new_back_edge);
522 return;
523 }
524 }
525 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 // that is the header dominates the back edge.
530 bool Populate();
531
David Brazdila4b8c212015-05-07 09:59:30 +0100532 // Reanalyzes the loop by removing loop info from its blocks and re-running
533 // Populate(). If there are no back edges left, the loop info is completely
534 // removed as well as its SuspendCheck instruction. It must be run on nested
535 // inner loops first.
536 void Update();
537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // Returns whether this loop information contains `block`.
539 // Note that this loop information *must* be populated before entering this function.
540 bool Contains(const HBasicBlock& block) const;
541
542 // Returns whether this loop information is an inner loop of `other`.
543 // Note that `other` *must* be populated before entering this function.
544 bool IsIn(const HLoopInformation& other) const;
545
546 const ArenaBitVector& GetBlocks() const { return blocks_; }
547
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000548 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000549 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000550
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100552 // Internal recursive implementation of `Populate`.
553 void PopulateRecursive(HBasicBlock* block);
554
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100556 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559
560 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
561};
562
David Brazdilec16f792015-08-19 15:04:01 +0100563// Stores try/catch information for basic blocks.
564// Note that HGraph is constructed so that catch blocks cannot simultaneously
565// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100566class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100567 public:
568 // Try block information constructor.
569 explicit TryCatchInformation(const HTryBoundary& try_entry)
570 : try_entry_(&try_entry),
571 catch_dex_file_(nullptr),
572 catch_type_index_(DexFile::kDexNoIndex16) {
573 DCHECK(try_entry_ != nullptr);
574 }
575
576 // Catch block information constructor.
577 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
578 : try_entry_(nullptr),
579 catch_dex_file_(&dex_file),
580 catch_type_index_(catch_type_index) {}
581
582 bool IsTryBlock() const { return try_entry_ != nullptr; }
583
584 const HTryBoundary& GetTryEntry() const {
585 DCHECK(IsTryBlock());
586 return *try_entry_;
587 }
588
589 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
590
591 bool IsCatchAllTypeIndex() const {
592 DCHECK(IsCatchBlock());
593 return catch_type_index_ == DexFile::kDexNoIndex16;
594 }
595
596 uint16_t GetCatchTypeIndex() const {
597 DCHECK(IsCatchBlock());
598 return catch_type_index_;
599 }
600
601 const DexFile& GetCatchDexFile() const {
602 DCHECK(IsCatchBlock());
603 return *catch_dex_file_;
604 }
605
606 private:
607 // One of possibly several TryBoundary instructions entering the block's try.
608 // Only set for try blocks.
609 const HTryBoundary* try_entry_;
610
611 // Exception type information. Only set for catch blocks.
612 const DexFile* catch_dex_file_;
613 const uint16_t catch_type_index_;
614};
615
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100616static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100617static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100618
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000619// A block in a method. Contains the list of instructions represented
620// as a double linked list. Each block knows its predecessors and
621// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100622
Vladimir Markof9f64412015-09-02 14:05:49 +0100623class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100625 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000627 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
628 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629 loop_information_(nullptr),
630 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100631 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100633 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100634 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000635 lifetime_end_(kNoLifetime),
David Brazdilec16f792015-08-19 15:04:01 +0100636 try_catch_information_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100638 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
639 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640 }
641
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100642 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
643 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 }
645
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100646 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
647 return dominated_blocks_;
648 }
649
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100650 bool IsEntryBlock() const {
651 return graph_->GetEntryBlock() == this;
652 }
653
654 bool IsExitBlock() const {
655 return graph_->GetExitBlock() == this;
656 }
657
David Brazdil46e2a392015-03-16 17:31:52 +0000658 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000659 bool IsSingleTryBoundary() const;
660
661 // Returns true if this block emits nothing but a jump.
662 bool IsSingleJump() const {
663 HLoopInformation* loop_info = GetLoopInformation();
664 return (IsSingleGoto() || IsSingleTryBoundary())
665 // Back edges generate a suspend check.
666 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
667 }
David Brazdil46e2a392015-03-16 17:31:52 +0000668
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000669 void AddBackEdge(HBasicBlock* back_edge) {
670 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000672 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100673 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000674 loop_information_->AddBackEdge(back_edge);
675 }
676
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000677 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000678 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000679
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000680 int GetBlockId() const { return block_id_; }
681 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000682
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000683 HBasicBlock* GetDominator() const { return dominator_; }
684 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100685 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100686 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000687 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
688 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
689 if (dominated_blocks_.Get(i) == existing) {
690 dominated_blocks_.Put(i, new_block);
691 return;
692 }
693 }
694 LOG(FATAL) << "Unreachable";
695 UNREACHABLE();
696 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100697 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698
699 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100700 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000701 }
702
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
704 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100705 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100706 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100707 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
708 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
710 void AddSuccessor(HBasicBlock* block) {
711 successors_.Add(block);
712 block->predecessors_.Add(this);
713 }
714
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100715 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
716 size_t successor_index = GetSuccessorIndexOf(existing);
717 DCHECK_NE(successor_index, static_cast<size_t>(-1));
718 existing->RemovePredecessor(this);
719 new_block->predecessors_.Add(this);
720 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721 }
722
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
724 size_t predecessor_index = GetPredecessorIndexOf(existing);
725 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
726 existing->RemoveSuccessor(this);
727 new_block->successors_.Add(this);
728 predecessors_.Put(predecessor_index, new_block);
729 }
730
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100731 // Insert `this` between `predecessor` and `successor. This method
732 // preserves the indicies, and will update the first edge found between
733 // `predecessor` and `successor`.
734 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
735 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
736 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
737 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
738 DCHECK_NE(successor_index, static_cast<size_t>(-1));
739 successor->predecessors_.Put(predecessor_index, this);
740 predecessor->successors_.Put(successor_index, this);
741 successors_.Add(successor);
742 predecessors_.Add(predecessor);
743 }
744
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100745 void RemovePredecessor(HBasicBlock* block) {
746 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 }
748
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 void RemoveSuccessor(HBasicBlock* block) {
750 successors_.Delete(block);
751 }
752
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 void ClearAllPredecessors() {
754 predecessors_.Reset();
755 }
756
757 void AddPredecessor(HBasicBlock* block) {
758 predecessors_.Add(block);
759 block->successors_.Add(this);
760 }
761
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100762 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100763 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100764 HBasicBlock* temp = predecessors_.Get(0);
765 predecessors_.Put(0, predecessors_.Get(1));
766 predecessors_.Put(1, temp);
767 }
768
David Brazdil769c9e52015-04-27 13:54:09 +0100769 void SwapSuccessors() {
770 DCHECK_EQ(successors_.Size(), 2u);
771 HBasicBlock* temp = successors_.Get(0);
772 successors_.Put(0, successors_.Get(1));
773 successors_.Put(1, temp);
774 }
775
David Brazdilfc6a86a2015-06-26 10:33:45 +0000776 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
778 if (predecessors_.Get(i) == predecessor) {
779 return i;
780 }
781 }
782 return -1;
783 }
784
David Brazdilfc6a86a2015-06-26 10:33:45 +0000785 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100786 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
787 if (successors_.Get(i) == successor) {
788 return i;
789 }
790 }
791 return -1;
792 }
793
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 HBasicBlock* GetSinglePredecessor() const {
795 DCHECK_EQ(GetPredecessors().Size(), 1u);
796 return GetPredecessors().Get(0);
797 }
798
799 HBasicBlock* GetSingleSuccessor() const {
800 DCHECK_EQ(GetSuccessors().Size(), 1u);
801 return GetSuccessors().Get(0);
802 }
803
804 // Returns whether the first occurrence of `predecessor` in the list of
805 // predecessors is at index `idx`.
806 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
807 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
808 return GetPredecessorIndexOf(predecessor) == idx;
809 }
810
David Brazdilffee3d32015-07-06 11:48:53 +0100811 // Returns the number of non-exceptional successors. SsaChecker ensures that
812 // these are stored at the beginning of the successor list.
813 size_t NumberOfNormalSuccessors() const {
814 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
815 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000816
817 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100818 // created, latter block. Note that this method will add the block to the
819 // graph, create a Goto at the end of the former block and will create an edge
820 // between the blocks. It will not, however, update the reverse post order or
821 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000822 HBasicBlock* SplitBefore(HInstruction* cursor);
823
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000824 // Split the block into two blocks just after `cursor`. Returns the newly
825 // created block. Note that this method just updates raw block information,
826 // like predecessors, successors, dominators, and instruction list. It does not
827 // update the graph, reverse post order, loop information, nor make sure the
828 // blocks are consistent (for example ending with a control flow instruction).
829 HBasicBlock* SplitAfter(HInstruction* cursor);
830
831 // Merge `other` at the end of `this`. Successors and dominated blocks of
832 // `other` are changed to be successors and dominated blocks of `this`. Note
833 // that this method does not update the graph, reverse post order, loop
834 // information, nor make sure the blocks are consistent (for example ending
835 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100836 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000837
838 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
839 // of `this` are moved to `other`.
840 // Note that this method does not update the graph, reverse post order, loop
841 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000842 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000843 void ReplaceWith(HBasicBlock* other);
844
David Brazdil2d7352b2015-04-20 14:52:42 +0100845 // Merge `other` at the end of `this`. This method updates loops, reverse post
846 // order, links to predecessors, successors, dominators and deletes the block
847 // from the graph. The two blocks must be successive, i.e. `this` the only
848 // predecessor of `other` and vice versa.
849 void MergeWith(HBasicBlock* other);
850
851 // Disconnects `this` from all its predecessors, successors and dominator,
852 // removes it from all loops it is included in and eventually from the graph.
853 // The block must not dominate any other block. Predecessors and successors
854 // are safely updated.
855 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000856
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100858 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100859 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100860 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100861 // Replace instruction `initial` with `replacement` within this block.
862 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
863 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100865 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000866 // RemoveInstruction and RemovePhi delete a given instruction from the respective
867 // instruction list. With 'ensure_safety' set to true, it verifies that the
868 // instruction is not in use and removes it from the use lists of its inputs.
869 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
870 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100871 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100872
873 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100874 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875 }
876
Roland Levillain6b879dd2014-09-22 17:13:44 +0100877 bool IsLoopPreHeaderFirstPredecessor() const {
878 DCHECK(IsLoopHeader());
879 DCHECK(!GetPredecessors().IsEmpty());
880 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
881 }
882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 HLoopInformation* GetLoopInformation() const {
884 return loop_information_;
885 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000887 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100888 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000889 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 void SetInLoop(HLoopInformation* info) {
891 if (IsLoopHeader()) {
892 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100893 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 loop_information_ = info;
895 } else if (loop_information_->Contains(*info->GetHeader())) {
896 // Block is currently part of an outer loop. Make it part of this inner loop.
897 // Note that a non loop header having a loop information means this loop information
898 // has already been populated
899 loop_information_ = info;
900 } else {
901 // Block is part of an inner loop. Do not update the loop information.
902 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
903 // at this point, because this method is being called while populating `info`.
904 }
905 }
906
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000907 // Raw update of the loop information.
908 void SetLoopInformation(HLoopInformation* info) {
909 loop_information_ = info;
910 }
911
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100912 bool IsInLoop() const { return loop_information_ != nullptr; }
913
David Brazdilec16f792015-08-19 15:04:01 +0100914 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
915
916 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
917 try_catch_information_ = try_catch_information;
918 }
919
920 bool IsTryBlock() const {
921 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
922 }
923
924 bool IsCatchBlock() const {
925 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
926 }
David Brazdilffee3d32015-07-06 11:48:53 +0100927
928 // Returns the try entry that this block's successors should have. They will
929 // be in the same try, unless the block ends in a try boundary. In that case,
930 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100931 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100932
David Brazdila4b8c212015-05-07 09:59:30 +0100933 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 bool Dominates(HBasicBlock* block) const;
935
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100936 size_t GetLifetimeStart() const { return lifetime_start_; }
937 size_t GetLifetimeEnd() const { return lifetime_end_; }
938
939 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
940 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
941
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100942 uint32_t GetDexPc() const { return dex_pc_; }
943
David Brazdil8d5b8b22015-03-24 10:51:52 +0000944 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000945 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100946 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000947 bool HasSinglePhi() const;
948
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000950 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 GrowableArray<HBasicBlock*> predecessors_;
952 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100953 HInstructionList instructions_;
954 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000955 HLoopInformation* loop_information_;
956 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100957 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100959 // The dex program counter of the first instruction of this block.
960 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100961 size_t lifetime_start_;
962 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100963 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100964
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000965 friend class HGraph;
966 friend class HInstruction;
967
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
969};
970
David Brazdilb2bd1c52015-03-25 11:17:37 +0000971// Iterates over the LoopInformation of all loops which contain 'block'
972// from the innermost to the outermost.
973class HLoopInformationOutwardIterator : public ValueObject {
974 public:
975 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
976 : current_(block.GetLoopInformation()) {}
977
978 bool Done() const { return current_ == nullptr; }
979
980 void Advance() {
981 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100982 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000983 }
984
985 HLoopInformation* Current() const {
986 DCHECK(!Done());
987 return current_;
988 }
989
990 private:
991 HLoopInformation* current_;
992
993 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
994};
995
Alexandre Ramesef20f712015-06-09 10:29:30 +0100996#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100997 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000998 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000999 M(ArrayGet, Instruction) \
1000 M(ArrayLength, Instruction) \
1001 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001002 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001004 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001005 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001006 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001007 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001009 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001010 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001011 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001013 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001014 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001016 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001017 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(FloatConstant, Constant) \
1019 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(GreaterThan, Condition) \
1021 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001022 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(InstanceFieldGet, Instruction) \
1024 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001025 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001027 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001028 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(LessThan, Condition) \
1031 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001032 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001033 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001035 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(Local, Instruction) \
1037 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001038 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001039 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(Mul, BinaryOperation) \
1041 M(Neg, UnaryOperation) \
1042 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001044 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001046 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001048 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001052 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(Return, Instruction) \
1054 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001055 M(Shl, BinaryOperation) \
1056 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001057 M(StaticFieldGet, Instruction) \
1058 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(StoreLocal, Instruction) \
1060 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001063 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001064 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001065 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001067 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
Alexandre Ramesef20f712015-06-09 10:29:30 +01001069#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1070
1071#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1072
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001073#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1074
Alexandre Ramesef20f712015-06-09 10:29:30 +01001075#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1076
1077#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1078
1079#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1080 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1081 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1082 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001083 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1085 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1086
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001087#define FOR_EACH_INSTRUCTION(M) \
1088 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1089 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001090 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001091 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001092 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001093
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001094#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1096#undef FORWARD_DECLARATION
1097
Roland Levillainccc07a92014-09-16 14:48:16 +01001098#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001099 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1100 const char* DebugName() const OVERRIDE { return #type; } \
1101 const H##type* As##type() const OVERRIDE { return this; } \
1102 H##type* As##type() OVERRIDE { return this; } \
1103 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001104 return other->Is##type(); \
1105 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001106 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107
David Brazdiled596192015-01-23 10:39:45 +00001108template <typename T> class HUseList;
1109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001110template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001111class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001112 public:
David Brazdiled596192015-01-23 10:39:45 +00001113 HUseListNode* GetPrevious() const { return prev_; }
1114 HUseListNode* GetNext() const { return next_; }
1115 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001116 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001117 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001119 private:
David Brazdiled596192015-01-23 10:39:45 +00001120 HUseListNode(T user, size_t index)
1121 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1122
1123 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001124 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001125 HUseListNode<T>* prev_;
1126 HUseListNode<T>* next_;
1127
1128 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129
1130 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1131};
1132
David Brazdiled596192015-01-23 10:39:45 +00001133template <typename T>
1134class HUseList : public ValueObject {
1135 public:
1136 HUseList() : first_(nullptr) {}
1137
1138 void Clear() {
1139 first_ = nullptr;
1140 }
1141
1142 // Adds a new entry at the beginning of the use list and returns
1143 // the newly created node.
1144 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001145 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001146 if (IsEmpty()) {
1147 first_ = new_node;
1148 } else {
1149 first_->prev_ = new_node;
1150 new_node->next_ = first_;
1151 first_ = new_node;
1152 }
1153 return new_node;
1154 }
1155
1156 HUseListNode<T>* GetFirst() const {
1157 return first_;
1158 }
1159
1160 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001161 DCHECK(node != nullptr);
1162 DCHECK(Contains(node));
1163
David Brazdiled596192015-01-23 10:39:45 +00001164 if (node->prev_ != nullptr) {
1165 node->prev_->next_ = node->next_;
1166 }
1167 if (node->next_ != nullptr) {
1168 node->next_->prev_ = node->prev_;
1169 }
1170 if (node == first_) {
1171 first_ = node->next_;
1172 }
1173 }
1174
David Brazdil1abb4192015-02-17 18:33:36 +00001175 bool Contains(const HUseListNode<T>* node) const {
1176 if (node == nullptr) {
1177 return false;
1178 }
1179 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1180 if (current == node) {
1181 return true;
1182 }
1183 }
1184 return false;
1185 }
1186
David Brazdiled596192015-01-23 10:39:45 +00001187 bool IsEmpty() const {
1188 return first_ == nullptr;
1189 }
1190
1191 bool HasOnlyOneUse() const {
1192 return first_ != nullptr && first_->next_ == nullptr;
1193 }
1194
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001195 size_t SizeSlow() const {
1196 size_t count = 0;
1197 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1198 ++count;
1199 }
1200 return count;
1201 }
1202
David Brazdiled596192015-01-23 10:39:45 +00001203 private:
1204 HUseListNode<T>* first_;
1205};
1206
1207template<typename T>
1208class HUseIterator : public ValueObject {
1209 public:
1210 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1211
1212 bool Done() const { return current_ == nullptr; }
1213
1214 void Advance() {
1215 DCHECK(!Done());
1216 current_ = current_->GetNext();
1217 }
1218
1219 HUseListNode<T>* Current() const {
1220 DCHECK(!Done());
1221 return current_;
1222 }
1223
1224 private:
1225 HUseListNode<T>* current_;
1226
1227 friend class HValue;
1228};
1229
David Brazdil1abb4192015-02-17 18:33:36 +00001230// This class is used by HEnvironment and HInstruction classes to record the
1231// instructions they use and pointers to the corresponding HUseListNodes kept
1232// by the used instructions.
1233template <typename T>
1234class HUserRecord : public ValueObject {
1235 public:
1236 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1237 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1238
1239 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1240 : instruction_(old_record.instruction_), use_node_(use_node) {
1241 DCHECK(instruction_ != nullptr);
1242 DCHECK(use_node_ != nullptr);
1243 DCHECK(old_record.use_node_ == nullptr);
1244 }
1245
1246 HInstruction* GetInstruction() const { return instruction_; }
1247 HUseListNode<T>* GetUseNode() const { return use_node_; }
1248
1249 private:
1250 // Instruction used by the user.
1251 HInstruction* instruction_;
1252
1253 // Corresponding entry in the use list kept by 'instruction_'.
1254 HUseListNode<T>* use_node_;
1255};
1256
Aart Bik854a02b2015-07-14 16:07:00 -07001257/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001258 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001259 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001260 * For write/read dependences on fields/arrays, the dependence analysis uses
1261 * type disambiguation (e.g. a float field write cannot modify the value of an
1262 * integer field read) and the access type (e.g. a reference array write cannot
1263 * modify the value of a reference field read [although it may modify the
1264 * reference fetch prior to reading the field, which is represented by its own
1265 * write/read dependence]). The analysis makes conservative points-to
1266 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1267 * the same, and any reference read depends on any reference read without
1268 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001269 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001270 * The internal representation uses 38-bit and is described in the table below.
1271 * The first line indicates the side effect, and for field/array accesses the
1272 * second line indicates the type of the access (in the order of the
1273 * Primitive::Type enum).
1274 * The two numbered lines below indicate the bit position in the bitfield (read
1275 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001276 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001277 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1278 * +-------------+---------+---------+--------------+---------+---------+
1279 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1280 * | 3 |333333322|222222221| 1 |111111110|000000000|
1281 * | 7 |654321098|765432109| 8 |765432109|876543210|
1282 *
1283 * Note that, to ease the implementation, 'changes' bits are least significant
1284 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001285 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001286class SideEffects : public ValueObject {
1287 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001288 SideEffects() : flags_(0) {}
1289
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001290 static SideEffects None() {
1291 return SideEffects(0);
1292 }
1293
1294 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001295 return SideEffects(kAllChangeBits | kAllDependOnBits);
1296 }
1297
1298 static SideEffects AllChanges() {
1299 return SideEffects(kAllChangeBits);
1300 }
1301
1302 static SideEffects AllDependencies() {
1303 return SideEffects(kAllDependOnBits);
1304 }
1305
1306 static SideEffects AllExceptGCDependency() {
1307 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1308 }
1309
1310 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001311 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001312 }
1313
Aart Bik34c3ba92015-07-20 14:08:59 -07001314 static SideEffects AllWrites() {
1315 return SideEffects(kAllWrites);
1316 }
1317
1318 static SideEffects AllReads() {
1319 return SideEffects(kAllReads);
1320 }
1321
1322 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1323 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001324 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001325 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326 }
1327
Aart Bik854a02b2015-07-14 16:07:00 -07001328 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1329 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 }
1331
Aart Bik34c3ba92015-07-20 14:08:59 -07001332 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1333 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001334 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001335 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001336 }
1337
1338 static SideEffects ArrayReadOfType(Primitive::Type type) {
1339 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1340 }
1341
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001342 static SideEffects CanTriggerGC() {
1343 return SideEffects(1ULL << kCanTriggerGCBit);
1344 }
1345
1346 static SideEffects DependsOnGC() {
1347 return SideEffects(1ULL << kDependsOnGCBit);
1348 }
1349
Aart Bik854a02b2015-07-14 16:07:00 -07001350 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001351 SideEffects Union(SideEffects other) const {
1352 return SideEffects(flags_ | other.flags_);
1353 }
1354
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001355 SideEffects Exclusion(SideEffects other) const {
1356 return SideEffects(flags_ & ~other.flags_);
1357 }
1358
1359 bool Includes(SideEffects other) const {
1360 return (other.flags_ & flags_) == other.flags_;
1361 }
1362
1363 bool HasSideEffects() const {
1364 return (flags_ & kAllChangeBits);
1365 }
1366
1367 bool HasDependencies() const {
1368 return (flags_ & kAllDependOnBits);
1369 }
1370
1371 // Returns true if there are no side effects or dependencies.
1372 bool DoesNothing() const {
1373 return flags_ == 0;
1374 }
1375
Aart Bik854a02b2015-07-14 16:07:00 -07001376 // Returns true if something is written.
1377 bool DoesAnyWrite() const {
1378 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001379 }
1380
Aart Bik854a02b2015-07-14 16:07:00 -07001381 // Returns true if something is read.
1382 bool DoesAnyRead() const {
1383 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001384 }
1385
Aart Bik854a02b2015-07-14 16:07:00 -07001386 // Returns true if potentially everything is written and read
1387 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 bool DoesAllReadWrite() const {
1389 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001394 }
1395
1396 // Returns true if this may read something written by other.
1397 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001398 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1399 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001400 }
1401
1402 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001403 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001404 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001405 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 for (int s = kLastBit; s >= 0; s--) {
1407 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1408 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1409 // This is a bit for the GC side effect.
1410 if (current_bit_is_set) {
1411 flags += "GC";
1412 }
Aart Bik854a02b2015-07-14 16:07:00 -07001413 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 } else {
1415 // This is a bit for the array/field analysis.
1416 // The underscore character stands for the 'can trigger GC' bit.
1417 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1418 if (current_bit_is_set) {
1419 flags += kDebug[s];
1420 }
1421 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1422 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1423 flags += "|";
1424 }
1425 }
Aart Bik854a02b2015-07-14 16:07:00 -07001426 }
1427 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001428 }
1429
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001431
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 private:
1433 static constexpr int kFieldArrayAnalysisBits = 9;
1434
1435 static constexpr int kFieldWriteOffset = 0;
1436 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1437 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1438 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1439
1440 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1441
1442 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1443 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1444 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1445 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1446
1447 static constexpr int kLastBit = kDependsOnGCBit;
1448 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1449
1450 // Aliases.
1451
1452 static_assert(kChangeBits == kDependOnBits,
1453 "the 'change' bits should match the 'depend on' bits.");
1454
1455 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1456 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1457 static constexpr uint64_t kAllWrites =
1458 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1459 static constexpr uint64_t kAllReads =
1460 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001461
Aart Bik854a02b2015-07-14 16:07:00 -07001462 // Work around the fact that HIR aliases I/F and J/D.
1463 // TODO: remove this interceptor once HIR types are clean
1464 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1465 switch (type) {
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimFloat:
1468 return TypeFlag(Primitive::kPrimInt, offset) |
1469 TypeFlag(Primitive::kPrimFloat, offset);
1470 case Primitive::kPrimLong:
1471 case Primitive::kPrimDouble:
1472 return TypeFlag(Primitive::kPrimLong, offset) |
1473 TypeFlag(Primitive::kPrimDouble, offset);
1474 default:
1475 return TypeFlag(type, offset);
1476 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001477 }
1478
Aart Bik854a02b2015-07-14 16:07:00 -07001479 // Translates type to bit flag.
1480 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1481 CHECK_NE(type, Primitive::kPrimVoid);
1482 const uint64_t one = 1;
1483 const int shift = type; // 0-based consecutive enum
1484 DCHECK_LE(kFieldWriteOffset, shift);
1485 DCHECK_LT(shift, kArrayWriteOffset);
1486 return one << (type + offset);
1487 }
1488
1489 // Private constructor on direct flags value.
1490 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1491
1492 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493};
1494
David Brazdiled596192015-01-23 10:39:45 +00001495// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001496class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001497 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001498 HEnvironment(ArenaAllocator* arena,
1499 size_t number_of_vregs,
1500 const DexFile& dex_file,
1501 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001502 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001503 InvokeType invoke_type,
1504 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001505 : vregs_(arena, number_of_vregs),
1506 locations_(arena, number_of_vregs),
1507 parent_(nullptr),
1508 dex_file_(dex_file),
1509 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001510 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001511 invoke_type_(invoke_type),
1512 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001513 vregs_.SetSize(number_of_vregs);
1514 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001515 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001516 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001517
1518 locations_.SetSize(number_of_vregs);
1519 for (size_t i = 0; i < number_of_vregs; ++i) {
1520 locations_.Put(i, Location());
1521 }
1522 }
1523
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001524 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001525 : HEnvironment(arena,
1526 to_copy.Size(),
1527 to_copy.GetDexFile(),
1528 to_copy.GetMethodIdx(),
1529 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001530 to_copy.GetInvokeType(),
1531 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001532
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001533 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 if (parent_ != nullptr) {
1535 parent_->SetAndCopyParentChain(allocator, parent);
1536 } else {
1537 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1538 parent_->CopyFrom(parent);
1539 if (parent->GetParent() != nullptr) {
1540 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1541 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001542 }
David Brazdiled596192015-01-23 10:39:45 +00001543 }
1544
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001545 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1546 void CopyFrom(HEnvironment* environment);
1547
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001548 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1549 // input to the loop phi instead. This is for inserting instructions that
1550 // require an environment (like HDeoptimization) in the loop pre-header.
1551 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001552
1553 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001554 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001555 }
1556
1557 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001558 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001559 }
1560
David Brazdil1abb4192015-02-17 18:33:36 +00001561 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001562
1563 size_t Size() const { return vregs_.Size(); }
1564
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001565 HEnvironment* GetParent() const { return parent_; }
1566
1567 void SetLocationAt(size_t index, Location location) {
1568 locations_.Put(index, location);
1569 }
1570
1571 Location GetLocationAt(size_t index) const {
1572 return locations_.Get(index);
1573 }
1574
1575 uint32_t GetDexPc() const {
1576 return dex_pc_;
1577 }
1578
1579 uint32_t GetMethodIdx() const {
1580 return method_idx_;
1581 }
1582
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001583 InvokeType GetInvokeType() const {
1584 return invoke_type_;
1585 }
1586
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 const DexFile& GetDexFile() const {
1588 return dex_file_;
1589 }
1590
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001591 HInstruction* GetHolder() const {
1592 return holder_;
1593 }
1594
David Brazdiled596192015-01-23 10:39:45 +00001595 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001596 // Record instructions' use entries of this environment for constant-time removal.
1597 // It should only be called by HInstruction when a new environment use is added.
1598 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1599 DCHECK(env_use->GetUser() == this);
1600 size_t index = env_use->GetIndex();
1601 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1602 }
David Brazdiled596192015-01-23 10:39:45 +00001603
David Brazdil1abb4192015-02-17 18:33:36 +00001604 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001605 GrowableArray<Location> locations_;
1606 HEnvironment* parent_;
1607 const DexFile& dex_file_;
1608 const uint32_t method_idx_;
1609 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001610 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001611
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001612 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001613 HInstruction* const holder_;
1614
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001615 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001616
1617 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1618};
1619
Calin Juravleacf735c2015-02-12 15:25:22 +00001620class ReferenceTypeInfo : ValueObject {
1621 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001622 typedef Handle<mirror::Class> TypeHandle;
1623
Calin Juravle2e768302015-07-28 14:41:11 +00001624 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1625 // The constructor will check that the type_handle is valid.
1626 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001627 }
1628
Calin Juravle2e768302015-07-28 14:41:11 +00001629 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1630
1631 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1632 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001633 }
1634
Calin Juravle2e768302015-07-28 14:41:11 +00001635 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1636 return IsValidHandle(type_handle_);
1637 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001638 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001639
1640 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1641 DCHECK(IsValid());
1642 return GetTypeHandle()->IsObjectClass();
1643 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001644 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001645 DCHECK(IsValid());
1646 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001647 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001648
1649 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1650
Mathieu Chartier90443472015-07-16 20:32:27 -07001651 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001652 DCHECK(IsValid());
1653 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001654 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1655 }
1656
1657 // Returns true if the type information provide the same amount of details.
1658 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001659 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001660 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001661 if (!IsValid() && !rti.IsValid()) {
1662 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001663 return true;
1664 }
Calin Juravle2e768302015-07-28 14:41:11 +00001665 if (!IsValid() || !rti.IsValid()) {
1666 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001667 return false;
1668 }
Calin Juravle2e768302015-07-28 14:41:11 +00001669 return IsExact() == rti.IsExact()
1670 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001671 }
1672
1673 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001674 ReferenceTypeInfo();
1675 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001676
Calin Juravleacf735c2015-02-12 15:25:22 +00001677 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001678 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001679 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001680 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001681 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001682};
1683
1684std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1685
Vladimir Markof9f64412015-09-02 14:05:49 +01001686class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001687 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001689 : previous_(nullptr),
1690 next_(nullptr),
1691 block_(nullptr),
1692 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001693 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001694 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001695 locations_(nullptr),
1696 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001698 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001699 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700
Dave Allison20dfc792014-06-16 20:44:29 -07001701 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001702
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001703#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001704 enum InstructionKind {
1705 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1706 };
1707#undef DECLARE_KIND
1708
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001709 HInstruction* GetNext() const { return next_; }
1710 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
Calin Juravle77520bc2015-01-12 18:45:46 +00001712 HInstruction* GetNextDisregardingMoves() const;
1713 HInstruction* GetPreviousDisregardingMoves() const;
1714
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001715 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001716 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001717 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001718 bool IsInBlock() const { return block_ != nullptr; }
1719 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001720 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001721
Roland Levillain6b879dd2014-09-22 17:13:44 +01001722 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001723 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724
1725 virtual void Accept(HGraphVisitor* visitor) = 0;
1726 virtual const char* DebugName() const = 0;
1727
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001728 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001729 void SetRawInputAt(size_t index, HInstruction* input) {
1730 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1731 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001732
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001733 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 virtual uint32_t GetDexPc() const {
1735 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1736 " does not need an environment";
1737 UNREACHABLE();
1738 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001739 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001740
Roland Levillaine161a2a2014-10-03 12:45:18 +01001741 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001742 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001743
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001744 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001745 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746
Calin Juravle10e244f2015-01-26 18:54:32 +00001747 // Does not apply for all instructions, but having this at top level greatly
1748 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001749 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001750 virtual bool CanBeNull() const {
1751 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1752 return true;
1753 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001754
Calin Juravle641547a2015-04-21 22:08:51 +01001755 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1756 UNUSED(obj);
1757 return false;
1758 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001759
Calin Juravle2e768302015-07-28 14:41:11 +00001760 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001761
Calin Juravle61d544b2015-02-23 16:46:57 +00001762 ReferenceTypeInfo GetReferenceTypeInfo() const {
1763 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1764 return reference_type_info_;
1765 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001766
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001767 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001768 DCHECK(user != nullptr);
1769 HUseListNode<HInstruction*>* use =
1770 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1771 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772 }
1773
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001775 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001776 HUseListNode<HEnvironment*>* env_use =
1777 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1778 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001779 }
1780
David Brazdil1abb4192015-02-17 18:33:36 +00001781 void RemoveAsUserOfInput(size_t input) {
1782 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1783 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1784 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785
David Brazdil1abb4192015-02-17 18:33:36 +00001786 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1787 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001788
David Brazdiled596192015-01-23 10:39:45 +00001789 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1790 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001791 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001792 bool HasOnlyOneNonEnvironmentUse() const {
1793 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1794 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
Roland Levillain6c82d402014-10-13 16:10:27 +01001796 // Does this instruction strictly dominate `other_instruction`?
1797 // Returns false if this instruction and `other_instruction` are the same.
1798 // Aborts if this instruction and `other_instruction` are both phis.
1799 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001800
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001801 int GetId() const { return id_; }
1802 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001804 int GetSsaIndex() const { return ssa_index_; }
1805 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1806 bool HasSsaIndex() const { return ssa_index_ != -1; }
1807
1808 bool HasEnvironment() const { return environment_ != nullptr; }
1809 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001810 // Set the `environment_` field. Raw because this method does not
1811 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001812 void SetRawEnvironment(HEnvironment* environment) {
1813 DCHECK(environment_ == nullptr);
1814 DCHECK_EQ(environment->GetHolder(), this);
1815 environment_ = environment;
1816 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001817
1818 // Set the environment of this instruction, copying it from `environment`. While
1819 // copying, the uses lists are being updated.
1820 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001821 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001822 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001824 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001825 if (environment->GetParent() != nullptr) {
1826 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1827 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001828 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001830 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1831 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001832 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001833 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001835 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001836 if (environment->GetParent() != nullptr) {
1837 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1838 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001839 }
1840
Nicolas Geoffray39468442014-09-02 15:17:15 +01001841 // Returns the number of entries in the environment. Typically, that is the
1842 // number of dex registers in a method. It could be more in case of inlining.
1843 size_t EnvironmentSize() const;
1844
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001845 LocationSummary* GetLocations() const { return locations_; }
1846 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001849 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850
Alexandre Rames188d4312015-04-09 18:30:21 +01001851 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1852 // uses of this instruction by `other` are *not* updated.
1853 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1854 ReplaceWith(other);
1855 other->ReplaceInput(this, use_index);
1856 }
1857
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001858 // Move `this` instruction before `cursor`.
1859 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001860
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001861#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001862 bool Is##type() const { return (As##type() != nullptr); } \
1863 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864 virtual H##type* As##type() { return nullptr; }
1865
1866 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1867#undef INSTRUCTION_TYPE_CHECK
1868
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001869 // Returns whether the instruction can be moved within the graph.
1870 virtual bool CanBeMoved() const { return false; }
1871
1872 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001873 virtual bool InstructionTypeEquals(HInstruction* other) const {
1874 UNUSED(other);
1875 return false;
1876 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001877
1878 // Returns whether any data encoded in the two instructions is equal.
1879 // This method does not look at the inputs. Both instructions must be
1880 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001881 virtual bool InstructionDataEquals(HInstruction* other) const {
1882 UNUSED(other);
1883 return false;
1884 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885
1886 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001887 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888 // 2) Their inputs are identical.
1889 bool Equals(HInstruction* other) const;
1890
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001891 virtual InstructionKind GetKind() const = 0;
1892
1893 virtual size_t ComputeHashCode() const {
1894 size_t result = GetKind();
1895 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1896 result = (result * 31) + InputAt(i)->GetId();
1897 }
1898 return result;
1899 }
1900
1901 SideEffects GetSideEffects() const { return side_effects_; }
1902
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001903 size_t GetLifetimePosition() const { return lifetime_position_; }
1904 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1905 LiveInterval* GetLiveInterval() const { return live_interval_; }
1906 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1907 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1908
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001909 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1910
1911 // Returns whether the code generation of the instruction will require to have access
1912 // to the current method. Such instructions are:
1913 // (1): Instructions that require an environment, as calling the runtime requires
1914 // to walk the stack and have the current method stored at a specific stack address.
1915 // (2): Object literals like classes and strings, that are loaded from the dex cache
1916 // fields of the current method.
1917 bool NeedsCurrentMethod() const {
1918 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1919 }
1920
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001921 virtual bool NeedsDexCache() const { return false; }
1922
Mark Mendellc4701932015-04-10 13:18:51 -04001923 // Does this instruction have any use in an environment before
1924 // control flow hits 'other'?
1925 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1926
1927 // Remove all references to environment uses of this instruction.
1928 // The caller must ensure that this is safe to do.
1929 void RemoveEnvironmentUsers();
1930
David Brazdil1abb4192015-02-17 18:33:36 +00001931 protected:
1932 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1933 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1934
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001935 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001936 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1937
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938 HInstruction* previous_;
1939 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001940 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001942 // An instruction gets an id when it is added to the graph.
1943 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001944 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001945 int id_;
1946
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001947 // When doing liveness analysis, instructions that have uses get an SSA index.
1948 int ssa_index_;
1949
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001951 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952
1953 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001954 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 // The environment associated with this instruction. Not null if the instruction
1957 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001958 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960 // Set by the code generator.
1961 LocationSummary* locations_;
1962
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001963 // Set by the liveness analysis.
1964 LiveInterval* live_interval_;
1965
1966 // Set by the liveness analysis, this is the position in a linear
1967 // order of blocks where this instruction's live interval start.
1968 size_t lifetime_position_;
1969
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970 const SideEffects side_effects_;
1971
Calin Juravleacf735c2015-02-12 15:25:22 +00001972 // TODO: for primitive types this should be marked as invalid.
1973 ReferenceTypeInfo reference_type_info_;
1974
David Brazdil1abb4192015-02-17 18:33:36 +00001975 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001976 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001977 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001978 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001979 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001980
1981 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1982};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001983std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001985class HInputIterator : public ValueObject {
1986 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001987 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001988
1989 bool Done() const { return index_ == instruction_->InputCount(); }
1990 HInstruction* Current() const { return instruction_->InputAt(index_); }
1991 void Advance() { index_++; }
1992
1993 private:
1994 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
1997 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1998};
1999
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002000class HInstructionIterator : public ValueObject {
2001 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002 explicit HInstructionIterator(const HInstructionList& instructions)
2003 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002004 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002005 }
2006
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007 bool Done() const { return instruction_ == nullptr; }
2008 HInstruction* Current() const { return instruction_; }
2009 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002010 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002011 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 }
2013
2014 private:
2015 HInstruction* instruction_;
2016 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002017
2018 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019};
2020
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002021class HBackwardInstructionIterator : public ValueObject {
2022 public:
2023 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2024 : instruction_(instructions.last_instruction_) {
2025 next_ = Done() ? nullptr : instruction_->GetPrevious();
2026 }
2027
2028 bool Done() const { return instruction_ == nullptr; }
2029 HInstruction* Current() const { return instruction_; }
2030 void Advance() {
2031 instruction_ = next_;
2032 next_ = Done() ? nullptr : instruction_->GetPrevious();
2033 }
2034
2035 private:
2036 HInstruction* instruction_;
2037 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002038
2039 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002040};
2041
Vladimir Markof9f64412015-09-02 14:05:49 +01002042template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043class HTemplateInstruction: public HInstruction {
2044 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045 HTemplateInstruction<N>(SideEffects side_effects)
2046 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002047 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002049 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002051 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002052 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2053 DCHECK_LT(i, N);
2054 return inputs_[i];
2055 }
David Brazdil1abb4192015-02-17 18:33:36 +00002056
2057 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002058 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002059 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002060 }
2061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002063 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064
2065 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002066};
2067
Vladimir Markof9f64412015-09-02 14:05:49 +01002068// HTemplateInstruction specialization for N=0.
2069template<>
2070class HTemplateInstruction<0>: public HInstruction {
2071 public:
2072 explicit HTemplateInstruction(SideEffects side_effects) : HInstruction(side_effects) {}
2073 virtual ~HTemplateInstruction() {}
2074
2075 size_t InputCount() const OVERRIDE { return 0; }
2076
2077 protected:
2078 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2079 LOG(FATAL) << "Unreachable";
2080 UNREACHABLE();
2081 }
2082
2083 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2084 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2085 LOG(FATAL) << "Unreachable";
2086 UNREACHABLE();
2087 }
2088
2089 private:
2090 friend class SsaBuilder;
2091};
2092
Dave Allison20dfc792014-06-16 20:44:29 -07002093template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002094class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002095 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002096 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2097 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002098 virtual ~HExpression() {}
2099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002100 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002101
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002102 protected:
2103 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002104};
2105
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2107// instruction that branches to the exit block.
2108class HReturnVoid : public HTemplateInstruction<0> {
2109 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002111
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002112 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002114 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115
2116 private:
2117 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2118};
2119
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002120// Represents dex's RETURN opcodes. A HReturn is a control flow
2121// instruction that branches to the exit block.
2122class HReturn : public HTemplateInstruction<1> {
2123 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002124 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002125 SetRawInputAt(0, value);
2126 }
2127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002129
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002130 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131
2132 private:
2133 DISALLOW_COPY_AND_ASSIGN(HReturn);
2134};
2135
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002136// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002137// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002138// exit block.
2139class HExit : public HTemplateInstruction<0> {
2140 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002141 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002143 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002145 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146
2147 private:
2148 DISALLOW_COPY_AND_ASSIGN(HExit);
2149};
2150
2151// Jumps from one block to another.
2152class HGoto : public HTemplateInstruction<0> {
2153 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002154 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2155
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002156 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002158 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002159 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002160 }
2161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002162 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163
2164 private:
2165 DISALLOW_COPY_AND_ASSIGN(HGoto);
2166};
2167
Roland Levillain9867bc72015-08-05 10:21:34 +01002168class HConstant : public HExpression<0> {
2169 public:
2170 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2171
2172 bool CanBeMoved() const OVERRIDE { return true; }
2173
2174 virtual bool IsMinusOne() const { return false; }
2175 virtual bool IsZero() const { return false; }
2176 virtual bool IsOne() const { return false; }
2177
2178 DECLARE_INSTRUCTION(Constant);
2179
2180 private:
2181 DISALLOW_COPY_AND_ASSIGN(HConstant);
2182};
2183
2184class HNullConstant : public HConstant {
2185 public:
2186 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2187 return true;
2188 }
2189
2190 size_t ComputeHashCode() const OVERRIDE { return 0; }
2191
2192 DECLARE_INSTRUCTION(NullConstant);
2193
2194 private:
2195 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2196
2197 friend class HGraph;
2198 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2199};
2200
2201// Constants of the type int. Those can be from Dex instructions, or
2202// synthesized (for example with the if-eqz instruction).
2203class HIntConstant : public HConstant {
2204 public:
2205 int32_t GetValue() const { return value_; }
2206
2207 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2208 DCHECK(other->IsIntConstant());
2209 return other->AsIntConstant()->value_ == value_;
2210 }
2211
2212 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2213
2214 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2215 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2216 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2217
2218 DECLARE_INSTRUCTION(IntConstant);
2219
2220 private:
2221 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2222 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2223
2224 const int32_t value_;
2225
2226 friend class HGraph;
2227 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2228 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2229 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2230};
2231
2232class HLongConstant : public HConstant {
2233 public:
2234 int64_t GetValue() const { return value_; }
2235
2236 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2237 DCHECK(other->IsLongConstant());
2238 return other->AsLongConstant()->value_ == value_;
2239 }
2240
2241 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2242
2243 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2244 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2245 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2246
2247 DECLARE_INSTRUCTION(LongConstant);
2248
2249 private:
2250 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2251
2252 const int64_t value_;
2253
2254 friend class HGraph;
2255 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2256};
Dave Allison20dfc792014-06-16 20:44:29 -07002257
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002258// Conditional branch. A block ending with an HIf instruction must have
2259// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002261 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002263 SetRawInputAt(0, input);
2264 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002265
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002266 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002268 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002269 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002270 }
2271
2272 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002273 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002274 }
2275
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002276 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002277
2278 private:
2279 DISALLOW_COPY_AND_ASSIGN(HIf);
2280};
2281
David Brazdilfc6a86a2015-06-26 10:33:45 +00002282
2283// Abstract instruction which marks the beginning and/or end of a try block and
2284// links it to the respective exception handlers. Behaves the same as a Goto in
2285// non-exceptional control flow.
2286// Normal-flow successor is stored at index zero, exception handlers under
2287// higher indices in no particular order.
2288class HTryBoundary : public HTemplateInstruction<0> {
2289 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002290 enum BoundaryKind {
2291 kEntry,
2292 kExit,
2293 };
2294
2295 explicit HTryBoundary(BoundaryKind kind)
2296 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002297
2298 bool IsControlFlow() const OVERRIDE { return true; }
2299
2300 // Returns the block's non-exceptional successor (index zero).
2301 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2302
2303 // Returns whether `handler` is among its exception handlers (non-zero index
2304 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002305 bool HasExceptionHandler(const HBasicBlock& handler) const {
2306 DCHECK(handler.IsCatchBlock());
2307 return GetBlock()->GetSuccessors().Contains(
2308 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002309 }
2310
2311 // If not present already, adds `handler` to its block's list of exception
2312 // handlers.
2313 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002314 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002315 GetBlock()->AddSuccessor(handler);
2316 }
2317 }
2318
David Brazdil56e1acc2015-06-30 15:41:36 +01002319 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002320
David Brazdilffee3d32015-07-06 11:48:53 +01002321 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2322
David Brazdilfc6a86a2015-06-26 10:33:45 +00002323 DECLARE_INSTRUCTION(TryBoundary);
2324
2325 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002326 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002327
2328 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2329};
2330
David Brazdilffee3d32015-07-06 11:48:53 +01002331// Iterator over exception handlers of a given HTryBoundary, i.e. over
2332// exceptional successors of its basic block.
2333class HExceptionHandlerIterator : public ValueObject {
2334 public:
2335 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2336 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2337
2338 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2339 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2340 size_t CurrentSuccessorIndex() const { return index_; }
2341 void Advance() { ++index_; }
2342
2343 private:
2344 const HBasicBlock& block_;
2345 size_t index_;
2346
2347 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2348};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002349
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002350// Deoptimize to interpreter, upon checking a condition.
2351class HDeoptimize : public HTemplateInstruction<1> {
2352 public:
2353 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2354 : HTemplateInstruction(SideEffects::None()),
2355 dex_pc_(dex_pc) {
2356 SetRawInputAt(0, cond);
2357 }
2358
2359 bool NeedsEnvironment() const OVERRIDE { return true; }
2360 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002361 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002362
2363 DECLARE_INSTRUCTION(Deoptimize);
2364
2365 private:
2366 uint32_t dex_pc_;
2367
2368 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2369};
2370
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002371// Represents the ArtMethod that was passed as a first argument to
2372// the method. It is used by instructions that depend on it, like
2373// instructions that work with the dex cache.
2374class HCurrentMethod : public HExpression<0> {
2375 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002376 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002377
2378 DECLARE_INSTRUCTION(CurrentMethod);
2379
2380 private:
2381 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2382};
2383
Roland Levillain88cb1752014-10-20 16:36:47 +01002384class HUnaryOperation : public HExpression<1> {
2385 public:
2386 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2387 : HExpression(result_type, SideEffects::None()) {
2388 SetRawInputAt(0, input);
2389 }
2390
2391 HInstruction* GetInput() const { return InputAt(0); }
2392 Primitive::Type GetResultType() const { return GetType(); }
2393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 bool CanBeMoved() const OVERRIDE { return true; }
2395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002396 UNUSED(other);
2397 return true;
2398 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002399
Roland Levillain9240d6a2014-10-20 16:47:04 +01002400 // Try to statically evaluate `operation` and return a HConstant
2401 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002402 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002403 HConstant* TryStaticEvaluation() const;
2404
2405 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002406 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2407 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002408
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 DECLARE_INSTRUCTION(UnaryOperation);
2410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2413};
2414
Dave Allison20dfc792014-06-16 20:44:29 -07002415class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002417 HBinaryOperation(Primitive::Type result_type,
2418 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002419 HInstruction* right,
2420 SideEffects side_effects = SideEffects::None())
2421 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002422 SetRawInputAt(0, left);
2423 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002424 }
2425
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002426 HInstruction* GetLeft() const { return InputAt(0); }
2427 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002428 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002429
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002430 virtual bool IsCommutative() const { return false; }
2431
2432 // Put constant on the right.
2433 // Returns whether order is changed.
2434 bool OrderInputsWithConstantOnTheRight() {
2435 HInstruction* left = InputAt(0);
2436 HInstruction* right = InputAt(1);
2437 if (left->IsConstant() && !right->IsConstant()) {
2438 ReplaceInput(right, 0);
2439 ReplaceInput(left, 1);
2440 return true;
2441 }
2442 return false;
2443 }
2444
2445 // Order inputs by instruction id, but favor constant on the right side.
2446 // This helps GVN for commutative ops.
2447 void OrderInputs() {
2448 DCHECK(IsCommutative());
2449 HInstruction* left = InputAt(0);
2450 HInstruction* right = InputAt(1);
2451 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2452 return;
2453 }
2454 if (OrderInputsWithConstantOnTheRight()) {
2455 return;
2456 }
2457 // Order according to instruction id.
2458 if (left->GetId() > right->GetId()) {
2459 ReplaceInput(right, 0);
2460 ReplaceInput(left, 1);
2461 }
2462 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002464 bool CanBeMoved() const OVERRIDE { return true; }
2465 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002466 UNUSED(other);
2467 return true;
2468 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002469
Roland Levillain9240d6a2014-10-20 16:47:04 +01002470 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002471 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002472 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002473 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002474
2475 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002476 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2477 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2478 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2479 HLongConstant* y ATTRIBUTE_UNUSED) const {
2480 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2481 return nullptr;
2482 }
2483 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2484 HIntConstant* y ATTRIBUTE_UNUSED) const {
2485 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2486 return nullptr;
2487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002488
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002489 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002490 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002491 HConstant* GetConstantRight() const;
2492
2493 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002494 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002495 HInstruction* GetLeastConstantLeft() const;
2496
Roland Levillainccc07a92014-09-16 14:48:16 +01002497 DECLARE_INSTRUCTION(BinaryOperation);
2498
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002499 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002500 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2501};
2502
Mark Mendellc4701932015-04-10 13:18:51 -04002503// The comparison bias applies for floating point operations and indicates how NaN
2504// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002505enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002506 kNoBias, // bias is not applicable (i.e. for long operation)
2507 kGtBias, // return 1 for NaN comparisons
2508 kLtBias, // return -1 for NaN comparisons
2509};
2510
Dave Allison20dfc792014-06-16 20:44:29 -07002511class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002513 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002514 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002515 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002516 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002518 bool NeedsMaterialization() const { return needs_materialization_; }
2519 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002521 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002522 // `instruction`, and disregard moves in between.
2523 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002524
Dave Allison20dfc792014-06-16 20:44:29 -07002525 DECLARE_INSTRUCTION(Condition);
2526
2527 virtual IfCondition GetCondition() const = 0;
2528
Mark Mendellc4701932015-04-10 13:18:51 -04002529 virtual IfCondition GetOppositeCondition() const = 0;
2530
Roland Levillain4fa13f62015-07-06 18:11:54 +01002531 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002532
2533 void SetBias(ComparisonBias bias) { bias_ = bias; }
2534
2535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2536 return bias_ == other->AsCondition()->bias_;
2537 }
2538
Roland Levillain4fa13f62015-07-06 18:11:54 +01002539 bool IsFPConditionTrueIfNaN() const {
2540 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2541 IfCondition if_cond = GetCondition();
2542 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2543 }
2544
2545 bool IsFPConditionFalseIfNaN() const {
2546 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2547 IfCondition if_cond = GetCondition();
2548 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2549 }
2550
Dave Allison20dfc792014-06-16 20:44:29 -07002551 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002552 // For register allocation purposes, returns whether this instruction needs to be
2553 // materialized (that is, not just be in the processor flags).
2554 bool needs_materialization_;
2555
Mark Mendellc4701932015-04-10 13:18:51 -04002556 // Needed if we merge a HCompare into a HCondition.
2557 ComparisonBias bias_;
2558
Dave Allison20dfc792014-06-16 20:44:29 -07002559 DISALLOW_COPY_AND_ASSIGN(HCondition);
2560};
2561
2562// Instruction to check if two inputs are equal to each other.
2563class HEqual : public HCondition {
2564 public:
2565 HEqual(HInstruction* first, HInstruction* second)
2566 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002567
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002568 bool IsCommutative() const OVERRIDE { return true; }
2569
Roland Levillain9867bc72015-08-05 10:21:34 +01002570 template <typename T> bool Compute(T x, T y) const { return x == y; }
2571
2572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2573 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2576 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002577 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002579 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002581 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002582 return kCondEQ;
2583 }
2584
Mark Mendellc4701932015-04-10 13:18:51 -04002585 IfCondition GetOppositeCondition() const OVERRIDE {
2586 return kCondNE;
2587 }
2588
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002589 private:
2590 DISALLOW_COPY_AND_ASSIGN(HEqual);
2591};
2592
Dave Allison20dfc792014-06-16 20:44:29 -07002593class HNotEqual : public HCondition {
2594 public:
2595 HNotEqual(HInstruction* first, HInstruction* second)
2596 : HCondition(first, second) {}
2597
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002598 bool IsCommutative() const OVERRIDE { return true; }
2599
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 template <typename T> bool Compute(T x, T y) const { return x != y; }
2601
2602 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2603 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002604 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2606 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002607 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002608
Dave Allison20dfc792014-06-16 20:44:29 -07002609 DECLARE_INSTRUCTION(NotEqual);
2610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002612 return kCondNE;
2613 }
2614
Mark Mendellc4701932015-04-10 13:18:51 -04002615 IfCondition GetOppositeCondition() const OVERRIDE {
2616 return kCondEQ;
2617 }
2618
Dave Allison20dfc792014-06-16 20:44:29 -07002619 private:
2620 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2621};
2622
2623class HLessThan : public HCondition {
2624 public:
2625 HLessThan(HInstruction* first, HInstruction* second)
2626 : HCondition(first, second) {}
2627
Roland Levillain9867bc72015-08-05 10:21:34 +01002628 template <typename T> bool Compute(T x, T y) const { return x < y; }
2629
2630 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2631 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2634 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002635 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002636
Dave Allison20dfc792014-06-16 20:44:29 -07002637 DECLARE_INSTRUCTION(LessThan);
2638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002639 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002640 return kCondLT;
2641 }
2642
Mark Mendellc4701932015-04-10 13:18:51 -04002643 IfCondition GetOppositeCondition() const OVERRIDE {
2644 return kCondGE;
2645 }
2646
Dave Allison20dfc792014-06-16 20:44:29 -07002647 private:
2648 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2649};
2650
2651class HLessThanOrEqual : public HCondition {
2652 public:
2653 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2654 : HCondition(first, second) {}
2655
Roland Levillain9867bc72015-08-05 10:21:34 +01002656 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2657
2658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2659 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002660 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002661 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2662 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
Dave Allison20dfc792014-06-16 20:44:29 -07002665 DECLARE_INSTRUCTION(LessThanOrEqual);
2666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002668 return kCondLE;
2669 }
2670
Mark Mendellc4701932015-04-10 13:18:51 -04002671 IfCondition GetOppositeCondition() const OVERRIDE {
2672 return kCondGT;
2673 }
2674
Dave Allison20dfc792014-06-16 20:44:29 -07002675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2677};
2678
2679class HGreaterThan : public HCondition {
2680 public:
2681 HGreaterThan(HInstruction* first, HInstruction* second)
2682 : HCondition(first, second) {}
2683
Roland Levillain9867bc72015-08-05 10:21:34 +01002684 template <typename T> bool Compute(T x, T y) const { return x > y; }
2685
2686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2687 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002688 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2690 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002691 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002692
Dave Allison20dfc792014-06-16 20:44:29 -07002693 DECLARE_INSTRUCTION(GreaterThan);
2694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002696 return kCondGT;
2697 }
2698
Mark Mendellc4701932015-04-10 13:18:51 -04002699 IfCondition GetOppositeCondition() const OVERRIDE {
2700 return kCondLE;
2701 }
2702
Dave Allison20dfc792014-06-16 20:44:29 -07002703 private:
2704 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2705};
2706
2707class HGreaterThanOrEqual : public HCondition {
2708 public:
2709 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2710 : HCondition(first, second) {}
2711
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2713
2714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2715 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2718 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002719 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002720
Dave Allison20dfc792014-06-16 20:44:29 -07002721 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002723 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002724 return kCondGE;
2725 }
2726
Mark Mendellc4701932015-04-10 13:18:51 -04002727 IfCondition GetOppositeCondition() const OVERRIDE {
2728 return kCondLT;
2729 }
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 private:
2732 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2733};
2734
2735
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002736// Instruction to check how two inputs compare to each other.
2737// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2738class HCompare : public HBinaryOperation {
2739 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002740 HCompare(Primitive::Type type,
2741 HInstruction* first,
2742 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002743 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002744 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002745 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2746 bias_(bias),
2747 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002748 DCHECK_EQ(type, first->GetType());
2749 DCHECK_EQ(type, second->GetType());
2750 }
2751
Roland Levillain9867bc72015-08-05 10:21:34 +01002752 template <typename T>
2753 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002754
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2756 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2757 }
2758 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2759 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002760 }
2761
Calin Juravleddb7df22014-11-25 20:56:51 +00002762 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2763 return bias_ == other->AsCompare()->bias_;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 ComparisonBias GetBias() const { return bias_; }
2767
Roland Levillain4fa13f62015-07-06 18:11:54 +01002768 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002769
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002770 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2771
2772 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2773 // MIPS64 uses a runtime call for FP comparisons.
2774 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002776
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002777 DECLARE_INSTRUCTION(Compare);
2778
2779 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002780 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002782
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002783 DISALLOW_COPY_AND_ASSIGN(HCompare);
2784};
2785
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002786// A local in the graph. Corresponds to a Dex register.
2787class HLocal : public HTemplateInstruction<0> {
2788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002789 explicit HLocal(uint16_t reg_number)
2790 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002791
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002792 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002793
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002794 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002795
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002796 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002797 // The Dex register number.
2798 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002799
2800 DISALLOW_COPY_AND_ASSIGN(HLocal);
2801};
2802
2803// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002804class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002805 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002806 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002807 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002808 SetRawInputAt(0, local);
2809 }
2810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002811 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002813 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002814
2815 private:
2816 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2817};
2818
2819// Store a value in a given local. This instruction has two inputs: the value
2820// and the local.
2821class HStoreLocal : public HTemplateInstruction<2> {
2822 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002823 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002824 SetRawInputAt(0, local);
2825 SetRawInputAt(1, value);
2826 }
2827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002830 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002831
2832 private:
2833 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2834};
2835
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002836class HFloatConstant : public HConstant {
2837 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002838 float GetValue() const { return value_; }
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002842 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2843 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002844 }
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002847
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002848 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002849 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002850 }
2851 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002852 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002853 }
2854 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002855 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2856 }
2857 bool IsNaN() const {
2858 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002859 }
2860
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 DECLARE_INSTRUCTION(FloatConstant);
2862
2863 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002864 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002865 explicit HFloatConstant(int32_t value)
2866 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 const float value_;
2869
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002870 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002871 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002872 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2874};
2875
2876class HDoubleConstant : public HConstant {
2877 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 double GetValue() const { return value_; }
2879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002880 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002882 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2883 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002884 }
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002887
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002888 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002889 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002890 }
2891 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002892 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002893 }
2894 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002895 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2896 }
2897 bool IsNaN() const {
2898 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002899 }
2900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 DECLARE_INSTRUCTION(DoubleConstant);
2902
2903 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002904 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002905 explicit HDoubleConstant(int64_t value)
2906 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002907
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002908 const double value_;
2909
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002910 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002911 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002912 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002913 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2914};
2915
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002916enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002917#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002918#include "intrinsics_list.h"
2919 kNone,
2920 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2921#undef INTRINSICS_LIST
2922#undef OPTIMIZING_INTRINSICS
2923};
2924std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2925
Agi Csaki05f20562015-08-19 14:58:14 -07002926enum IntrinsicNeedsEnvironmentOrCache {
2927 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2928 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002929};
2930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002931class HInvoke : public HInstruction {
2932 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002933 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002934
2935 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2936 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002937 bool NeedsEnvironment() const OVERRIDE {
2938 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2939 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002940
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002941 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002942 SetRawInputAt(index, argument);
2943 }
2944
Roland Levillain3e3d7332015-04-28 11:00:54 +01002945 // Return the number of arguments. This number can be lower than
2946 // the number of inputs returned by InputCount(), as some invoke
2947 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2948 // inputs at the end of their list of inputs.
2949 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002952
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002953 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002954
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002955 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002956 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002957
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002958 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2959
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002960 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961 return intrinsic_;
2962 }
2963
Agi Csaki05f20562015-08-19 14:58:14 -07002964 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002965 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002966 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002967 }
2968
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002969 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002970 return GetEnvironment()->GetParent() != nullptr;
2971 }
2972
2973 bool CanThrow() const OVERRIDE { return true; }
2974
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002975 DECLARE_INSTRUCTION(Invoke);
2976
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002977 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002978 HInvoke(ArenaAllocator* arena,
2979 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002980 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002981 Primitive::Type return_type,
2982 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002983 uint32_t dex_method_index,
2984 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002985 : HInstruction(
2986 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002987 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002988 inputs_(arena, number_of_arguments),
2989 return_type_(return_type),
2990 dex_pc_(dex_pc),
2991 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002992 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002993 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07002994 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002995 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2996 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 }
2998
David Brazdil1abb4192015-02-17 18:33:36 +00002999 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3000 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3001 inputs_.Put(index, input);
3002 }
3003
Roland Levillain3e3d7332015-04-28 11:00:54 +01003004 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003005 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003006 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003007 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003009 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003010 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003011 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003012
3013 private:
3014 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3015};
3016
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003017class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003018 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003019 // Requirements of this method call regarding the class
3020 // initialization (clinit) check of its declaring class.
3021 enum class ClinitCheckRequirement {
3022 kNone, // Class already initialized.
3023 kExplicit, // Static call having explicit clinit check as last input.
3024 kImplicit, // Static call implicitly requiring a clinit check.
3025 };
3026
Vladimir Marko58155012015-08-19 12:49:41 +00003027 // Determines how to load the target ArtMethod*.
3028 enum class MethodLoadKind {
3029 // Use a String init ArtMethod* loaded from Thread entrypoints.
3030 kStringInit,
3031
3032 // Use the method's own ArtMethod* loaded by the register allocator.
3033 kRecursive,
3034
3035 // Use ArtMethod* at a known address, embed the direct address in the code.
3036 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3037 kDirectAddress,
3038
3039 // Use ArtMethod* at an address that will be known at link time, embed the direct
3040 // address in the code. If the image is relocatable, emit .patch_oat entry.
3041 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3042 // the image relocatable or not.
3043 kDirectAddressWithFixup,
3044
3045 // Load from resoved methods array in the dex cache using a PC-relative load.
3046 // Used when we need to use the dex cache, for example for invoke-static that
3047 // may cause class initialization (the entry may point to a resolution method),
3048 // and we know that we can access the dex cache arrays using a PC-relative load.
3049 kDexCachePcRelative,
3050
3051 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3052 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3053 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3054 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3055 kDexCacheViaMethod,
3056 };
3057
3058 // Determines the location of the code pointer.
3059 enum class CodePtrLocation {
3060 // Recursive call, use local PC-relative call instruction.
3061 kCallSelf,
3062
3063 // Use PC-relative call instruction patched at link time.
3064 // Used for calls within an oat file, boot->boot or app->app.
3065 kCallPCRelative,
3066
3067 // Call to a known target address, embed the direct address in code.
3068 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3069 kCallDirect,
3070
3071 // Call to a target address that will be known at link time, embed the direct
3072 // address in code. If the image is relocatable, emit .patch_oat entry.
3073 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3074 // the image relocatable or not.
3075 kCallDirectWithFixup,
3076
3077 // Use code pointer from the ArtMethod*.
3078 // Used when we don't know the target code. This is also the last-resort-kind used when
3079 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3080 kCallArtMethod,
3081 };
3082
3083 struct DispatchInfo {
3084 const MethodLoadKind method_load_kind;
3085 const CodePtrLocation code_ptr_location;
3086 // The method load data holds
3087 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3088 // Note that there are multiple string init methods, each having its own offset.
3089 // - the method address for kDirectAddress
3090 // - the dex cache arrays offset for kDexCachePcRel.
3091 const uint64_t method_load_data;
3092 const uint64_t direct_code_ptr;
3093 };
3094
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003095 HInvokeStaticOrDirect(ArenaAllocator* arena,
3096 uint32_t number_of_arguments,
3097 Primitive::Type return_type,
3098 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003099 uint32_t method_index,
3100 MethodReference target_method,
3101 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003102 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003103 InvokeType invoke_type,
3104 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003105 : HInvoke(arena,
3106 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003107 // There is one extra argument for the HCurrentMethod node, and
3108 // potentially one other if the clinit check is explicit, and one other
3109 // if the method is a string factory.
3110 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003111 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003112 return_type,
3113 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003114 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003115 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003116 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003117 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003118 target_method_(target_method),
3119 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003120
Calin Juravle641547a2015-04-21 22:08:51 +01003121 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3122 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003123 // We access the method via the dex cache so we can't do an implicit null check.
3124 // TODO: for intrinsics we can generate implicit null checks.
3125 return false;
3126 }
3127
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003128 bool CanBeNull() const OVERRIDE {
3129 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3130 }
3131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003132 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003133 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3134 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3135 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003136 bool NeedsDexCache() const OVERRIDE {
3137 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3138 return !IsRecursive() && !IsStringInit();
3139 }
Vladimir Marko58155012015-08-19 12:49:41 +00003140 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003141 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003142 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3143 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3144 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3145 MethodReference GetTargetMethod() const { return target_method_; }
3146
3147 int32_t GetStringInitOffset() const {
3148 DCHECK(IsStringInit());
3149 return dispatch_info_.method_load_data;
3150 }
3151
3152 uint64_t GetMethodAddress() const {
3153 DCHECK(HasMethodAddress());
3154 return dispatch_info_.method_load_data;
3155 }
3156
3157 uint32_t GetDexCacheArrayOffset() const {
3158 DCHECK(HasPcRelDexCache());
3159 return dispatch_info_.method_load_data;
3160 }
3161
3162 uint64_t GetDirectCodePtr() const {
3163 DCHECK(HasDirectCodePtr());
3164 return dispatch_info_.direct_code_ptr;
3165 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003166
Calin Juravle68ad6492015-08-18 17:08:12 +01003167 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3168
Roland Levillain4c0eb422015-04-24 16:43:49 +01003169 // Is this instruction a call to a static method?
3170 bool IsStatic() const {
3171 return GetInvokeType() == kStatic;
3172 }
3173
Roland Levillain3e3d7332015-04-28 11:00:54 +01003174 // Remove the art::HLoadClass instruction set as last input by
3175 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3176 // the initial art::HClinitCheck instruction (only relevant for
3177 // static calls with explicit clinit check).
3178 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003179 DCHECK(IsStaticWithExplicitClinitCheck());
3180 size_t last_input_index = InputCount() - 1;
3181 HInstruction* last_input = InputAt(last_input_index);
3182 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003183 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003184 RemoveAsUserOfInput(last_input_index);
3185 inputs_.DeleteAt(last_input_index);
3186 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3187 DCHECK(IsStaticWithImplicitClinitCheck());
3188 }
3189
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003190 bool IsStringFactoryFor(HFakeString* str) const {
3191 if (!IsStringInit()) return false;
3192 // +1 for the current method.
3193 if (InputCount() == (number_of_arguments_ + 1)) return false;
3194 return InputAt(InputCount() - 1)->AsFakeString() == str;
3195 }
3196
3197 void RemoveFakeStringArgumentAsLastInput() {
3198 DCHECK(IsStringInit());
3199 size_t last_input_index = InputCount() - 1;
3200 HInstruction* last_input = InputAt(last_input_index);
3201 DCHECK(last_input != nullptr);
3202 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3203 RemoveAsUserOfInput(last_input_index);
3204 inputs_.DeleteAt(last_input_index);
3205 }
3206
Roland Levillain4c0eb422015-04-24 16:43:49 +01003207 // Is this a call to a static method whose declaring class has an
3208 // explicit intialization check in the graph?
3209 bool IsStaticWithExplicitClinitCheck() const {
3210 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3211 }
3212
3213 // Is this a call to a static method whose declaring class has an
3214 // implicit intialization check requirement?
3215 bool IsStaticWithImplicitClinitCheck() const {
3216 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3217 }
3218
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003219 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003220
Roland Levillain4c0eb422015-04-24 16:43:49 +01003221 protected:
3222 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3223 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3224 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3225 HInstruction* input = input_record.GetInstruction();
3226 // `input` is the last input of a static invoke marked as having
3227 // an explicit clinit check. It must either be:
3228 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3229 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3230 DCHECK(input != nullptr);
3231 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3232 }
3233 return input_record;
3234 }
3235
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003236 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003237 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003238 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003239 // The target method may refer to different dex file or method index than the original
3240 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3241 // in derived class to increase visibility.
3242 MethodReference target_method_;
3243 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003245 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003246};
3247
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003248class HInvokeVirtual : public HInvoke {
3249 public:
3250 HInvokeVirtual(ArenaAllocator* arena,
3251 uint32_t number_of_arguments,
3252 Primitive::Type return_type,
3253 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003255 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003257 vtable_index_(vtable_index) {}
3258
Calin Juravle641547a2015-04-21 22:08:51 +01003259 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003260 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003261 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 }
3263
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003264 uint32_t GetVTableIndex() const { return vtable_index_; }
3265
3266 DECLARE_INSTRUCTION(InvokeVirtual);
3267
3268 private:
3269 const uint32_t vtable_index_;
3270
3271 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3272};
3273
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003274class HInvokeInterface : public HInvoke {
3275 public:
3276 HInvokeInterface(ArenaAllocator* arena,
3277 uint32_t number_of_arguments,
3278 Primitive::Type return_type,
3279 uint32_t dex_pc,
3280 uint32_t dex_method_index,
3281 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003282 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003283 imt_index_(imt_index) {}
3284
Calin Juravle641547a2015-04-21 22:08:51 +01003285 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003286 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003287 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 }
3289
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003290 uint32_t GetImtIndex() const { return imt_index_; }
3291 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3292
3293 DECLARE_INSTRUCTION(InvokeInterface);
3294
3295 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003296 const uint32_t imt_index_;
3297
3298 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3299};
3300
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003301class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003302 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003303 HNewInstance(HCurrentMethod* current_method,
3304 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003305 uint16_t type_index,
3306 const DexFile& dex_file,
3307 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003308 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003309 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003310 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003311 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003312 entrypoint_(entrypoint) {
3313 SetRawInputAt(0, current_method);
3314 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003316 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003317 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003318 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003319
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003320 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003321 bool NeedsEnvironment() const OVERRIDE { return true; }
3322 // It may throw when called on:
3323 // - interfaces
3324 // - abstract/innaccessible/unknown classes
3325 // TODO: optimize when possible.
3326 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003327
Calin Juravle10e244f2015-01-26 18:54:32 +00003328 bool CanBeNull() const OVERRIDE { return false; }
3329
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003330 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003332 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003333
3334 private:
3335 const uint32_t dex_pc_;
3336 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003337 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003338 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003339
3340 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3341};
3342
Roland Levillain88cb1752014-10-20 16:36:47 +01003343class HNeg : public HUnaryOperation {
3344 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003345 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003346 : HUnaryOperation(result_type, input) {}
3347
Roland Levillain9867bc72015-08-05 10:21:34 +01003348 template <typename T> T Compute(T x) const { return -x; }
3349
3350 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3351 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3352 }
3353 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3354 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3355 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003356
Roland Levillain88cb1752014-10-20 16:36:47 +01003357 DECLARE_INSTRUCTION(Neg);
3358
3359 private:
3360 DISALLOW_COPY_AND_ASSIGN(HNeg);
3361};
3362
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003363class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003364 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003365 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003366 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003367 uint32_t dex_pc,
3368 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003369 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003370 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003371 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003372 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003373 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003374 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003375 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003376 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003377 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003378 }
3379
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003380 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003381 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003382 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383
3384 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003385 bool NeedsEnvironment() const OVERRIDE { return true; }
3386
Mingyao Yang0c365e62015-03-31 15:09:29 -07003387 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3388 bool CanThrow() const OVERRIDE { return true; }
3389
Calin Juravle10e244f2015-01-26 18:54:32 +00003390 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003391
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003392 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3393
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003394 DECLARE_INSTRUCTION(NewArray);
3395
3396 private:
3397 const uint32_t dex_pc_;
3398 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003399 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003400 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003401
3402 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3403};
3404
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003405class HAdd : public HBinaryOperation {
3406 public:
3407 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3408 : HBinaryOperation(result_type, left, right) {}
3409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003410 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003411
Roland Levillain9867bc72015-08-05 10:21:34 +01003412 template <typename T> T Compute(T x, T y) const { return x + y; }
3413
3414 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3415 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003416 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3418 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003419 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003420
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003421 DECLARE_INSTRUCTION(Add);
3422
3423 private:
3424 DISALLOW_COPY_AND_ASSIGN(HAdd);
3425};
3426
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003427class HSub : public HBinaryOperation {
3428 public:
3429 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3430 : HBinaryOperation(result_type, left, right) {}
3431
Roland Levillain9867bc72015-08-05 10:21:34 +01003432 template <typename T> T Compute(T x, T y) const { return x - y; }
3433
3434 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3435 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003436 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3438 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003439 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003440
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003441 DECLARE_INSTRUCTION(Sub);
3442
3443 private:
3444 DISALLOW_COPY_AND_ASSIGN(HSub);
3445};
3446
Calin Juravle34bacdf2014-10-07 20:23:36 +01003447class HMul : public HBinaryOperation {
3448 public:
3449 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3450 : HBinaryOperation(result_type, left, right) {}
3451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003452 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003453
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 template <typename T> T Compute(T x, T y) const { return x * y; }
3455
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3457 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3458 }
3459 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3460 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3461 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003462
3463 DECLARE_INSTRUCTION(Mul);
3464
3465 private:
3466 DISALLOW_COPY_AND_ASSIGN(HMul);
3467};
3468
Calin Juravle7c4954d2014-10-28 16:57:40 +00003469class HDiv : public HBinaryOperation {
3470 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003471 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003472 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3473 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003474
Roland Levillain9867bc72015-08-05 10:21:34 +01003475 template <typename T>
3476 T Compute(T x, T y) const {
3477 // Our graph structure ensures we never have 0 for `y` during
3478 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003479 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003480 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003481 return (y == -1) ? -x : x / y;
3482 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003483
Roland Levillain9867bc72015-08-05 10:21:34 +01003484 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3485 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3486 }
3487 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3488 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003489 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003490
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003491 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003492
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003493 static SideEffects SideEffectsForArchRuntimeCalls() {
3494 // The generated code can use a runtime call.
3495 return SideEffects::CanTriggerGC();
3496 }
3497
Calin Juravle7c4954d2014-10-28 16:57:40 +00003498 DECLARE_INSTRUCTION(Div);
3499
3500 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003501 const uint32_t dex_pc_;
3502
Calin Juravle7c4954d2014-10-28 16:57:40 +00003503 DISALLOW_COPY_AND_ASSIGN(HDiv);
3504};
3505
Calin Juravlebacfec32014-11-14 15:54:36 +00003506class HRem : public HBinaryOperation {
3507 public:
3508 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003509 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3510 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003511
Roland Levillain9867bc72015-08-05 10:21:34 +01003512 template <typename T>
3513 T Compute(T x, T y) const {
3514 // Our graph structure ensures we never have 0 for `y` during
3515 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003516 DCHECK_NE(y, 0);
3517 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3518 return (y == -1) ? 0 : x % y;
3519 }
3520
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3522 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3523 }
3524 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3525 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003526 }
3527
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003528 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003530 static SideEffects SideEffectsForArchRuntimeCalls() {
3531 return SideEffects::CanTriggerGC();
3532 }
3533
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 DECLARE_INSTRUCTION(Rem);
3535
3536 private:
3537 const uint32_t dex_pc_;
3538
3539 DISALLOW_COPY_AND_ASSIGN(HRem);
3540};
3541
Calin Juravled0d48522014-11-04 16:40:20 +00003542class HDivZeroCheck : public HExpression<1> {
3543 public:
3544 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3545 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3546 SetRawInputAt(0, value);
3547 }
3548
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003549 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3550
Calin Juravled0d48522014-11-04 16:40:20 +00003551 bool CanBeMoved() const OVERRIDE { return true; }
3552
3553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3554 UNUSED(other);
3555 return true;
3556 }
3557
3558 bool NeedsEnvironment() const OVERRIDE { return true; }
3559 bool CanThrow() const OVERRIDE { return true; }
3560
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003561 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003562
3563 DECLARE_INSTRUCTION(DivZeroCheck);
3564
3565 private:
3566 const uint32_t dex_pc_;
3567
3568 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3569};
3570
Calin Juravle9aec02f2014-11-18 23:06:35 +00003571class HShl : public HBinaryOperation {
3572 public:
3573 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3574 : HBinaryOperation(result_type, left, right) {}
3575
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 template <typename T, typename U, typename V>
3577 T Compute(T x, U y, V max_shift_value) const {
3578 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3579 "V is not the unsigned integer type corresponding to T");
3580 return x << (y & max_shift_value);
3581 }
3582
3583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3584 return GetBlock()->GetGraph()->GetIntConstant(
3585 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3586 }
3587 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3588 // case is handled as `x << static_cast<int>(y)`.
3589 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3590 return GetBlock()->GetGraph()->GetLongConstant(
3591 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3592 }
3593 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3594 return GetBlock()->GetGraph()->GetLongConstant(
3595 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3596 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003597
3598 DECLARE_INSTRUCTION(Shl);
3599
3600 private:
3601 DISALLOW_COPY_AND_ASSIGN(HShl);
3602};
3603
3604class HShr : public HBinaryOperation {
3605 public:
3606 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3607 : HBinaryOperation(result_type, left, right) {}
3608
Roland Levillain9867bc72015-08-05 10:21:34 +01003609 template <typename T, typename U, typename V>
3610 T Compute(T x, U y, V max_shift_value) const {
3611 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3612 "V is not the unsigned integer type corresponding to T");
3613 return x >> (y & max_shift_value);
3614 }
3615
3616 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3617 return GetBlock()->GetGraph()->GetIntConstant(
3618 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3619 }
3620 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3621 // case is handled as `x >> static_cast<int>(y)`.
3622 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3623 return GetBlock()->GetGraph()->GetLongConstant(
3624 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3625 }
3626 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3627 return GetBlock()->GetGraph()->GetLongConstant(
3628 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3629 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003630
3631 DECLARE_INSTRUCTION(Shr);
3632
3633 private:
3634 DISALLOW_COPY_AND_ASSIGN(HShr);
3635};
3636
3637class HUShr : public HBinaryOperation {
3638 public:
3639 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3640 : HBinaryOperation(result_type, left, right) {}
3641
Roland Levillain9867bc72015-08-05 10:21:34 +01003642 template <typename T, typename U, typename V>
3643 T Compute(T x, U y, V max_shift_value) const {
3644 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3645 "V is not the unsigned integer type corresponding to T");
3646 V ux = static_cast<V>(x);
3647 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648 }
3649
Roland Levillain9867bc72015-08-05 10:21:34 +01003650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3651 return GetBlock()->GetGraph()->GetIntConstant(
3652 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3653 }
3654 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3655 // case is handled as `x >>> static_cast<int>(y)`.
3656 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3657 return GetBlock()->GetGraph()->GetLongConstant(
3658 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3659 }
3660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3661 return GetBlock()->GetGraph()->GetLongConstant(
3662 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 }
3664
3665 DECLARE_INSTRUCTION(UShr);
3666
3667 private:
3668 DISALLOW_COPY_AND_ASSIGN(HUShr);
3669};
3670
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671class HAnd : public HBinaryOperation {
3672 public:
3673 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3674 : HBinaryOperation(result_type, left, right) {}
3675
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003676 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003677
Roland Levillain9867bc72015-08-05 10:21:34 +01003678 template <typename T, typename U>
3679 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3680
3681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3682 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3683 }
3684 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3685 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3686 }
3687 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3688 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3689 }
3690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3691 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3692 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003693
3694 DECLARE_INSTRUCTION(And);
3695
3696 private:
3697 DISALLOW_COPY_AND_ASSIGN(HAnd);
3698};
3699
3700class HOr : public HBinaryOperation {
3701 public:
3702 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3703 : HBinaryOperation(result_type, left, right) {}
3704
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003705 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003706
Roland Levillain9867bc72015-08-05 10:21:34 +01003707 template <typename T, typename U>
3708 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3709
3710 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3711 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3712 }
3713 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3714 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3715 }
3716 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3717 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3718 }
3719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3720 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3721 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003722
3723 DECLARE_INSTRUCTION(Or);
3724
3725 private:
3726 DISALLOW_COPY_AND_ASSIGN(HOr);
3727};
3728
3729class HXor : public HBinaryOperation {
3730 public:
3731 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3732 : HBinaryOperation(result_type, left, right) {}
3733
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003734 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003735
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 template <typename T, typename U>
3737 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3738
3739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3740 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3741 }
3742 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3743 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3744 }
3745 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3746 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3747 }
3748 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3749 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3750 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003751
3752 DECLARE_INSTRUCTION(Xor);
3753
3754 private:
3755 DISALLOW_COPY_AND_ASSIGN(HXor);
3756};
3757
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003758// The value of a parameter in this method. Its location depends on
3759// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003760class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003761 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003762 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003763 : HExpression(parameter_type, SideEffects::None()),
3764 index_(index),
3765 is_this_(is_this),
3766 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003767
3768 uint8_t GetIndex() const { return index_; }
3769
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003770 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3771 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003772
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003773 bool IsThis() const { return is_this_; }
3774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003775 DECLARE_INSTRUCTION(ParameterValue);
3776
3777 private:
3778 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003779 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003780 const uint8_t index_;
3781
Calin Juravle10e244f2015-01-26 18:54:32 +00003782 // Whether or not the parameter value corresponds to 'this' argument.
3783 const bool is_this_;
3784
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003785 bool can_be_null_;
3786
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003787 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3788};
3789
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003790class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003791 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003792 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003793 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003795 bool CanBeMoved() const OVERRIDE { return true; }
3796 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003797 UNUSED(other);
3798 return true;
3799 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003800
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 template <typename T> T Compute(T x) const { return ~x; }
3802
3803 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3804 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3805 }
3806 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3807 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3808 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003809
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003810 DECLARE_INSTRUCTION(Not);
3811
3812 private:
3813 DISALLOW_COPY_AND_ASSIGN(HNot);
3814};
3815
David Brazdil66d126e2015-04-03 16:02:44 +01003816class HBooleanNot : public HUnaryOperation {
3817 public:
3818 explicit HBooleanNot(HInstruction* input)
3819 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3820
3821 bool CanBeMoved() const OVERRIDE { return true; }
3822 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3823 UNUSED(other);
3824 return true;
3825 }
3826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003828 DCHECK(IsUint<1>(x));
3829 return !x;
3830 }
3831
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3833 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3834 }
3835 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3836 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003837 UNREACHABLE();
3838 }
3839
3840 DECLARE_INSTRUCTION(BooleanNot);
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3844};
3845
Roland Levillaindff1f282014-11-05 14:15:05 +00003846class HTypeConversion : public HExpression<1> {
3847 public:
3848 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003849 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003850 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3851 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003852 SetRawInputAt(0, input);
3853 DCHECK_NE(input->GetType(), result_type);
3854 }
3855
3856 HInstruction* GetInput() const { return InputAt(0); }
3857 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3858 Primitive::Type GetResultType() const { return GetType(); }
3859
Roland Levillain624279f2014-12-04 11:54:28 +00003860 // Required by the x86 and ARM code generators when producing calls
3861 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003862 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003863
Roland Levillaindff1f282014-11-05 14:15:05 +00003864 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003865 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003866
Mark Mendelle82549b2015-05-06 10:55:34 -04003867 // Try to statically evaluate the conversion and return a HConstant
3868 // containing the result. If the input cannot be converted, return nullptr.
3869 HConstant* TryStaticEvaluation() const;
3870
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003871 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3872 Primitive::Type result_type) {
3873 // Some architectures may not require the 'GC' side effects, but at this point
3874 // in the compilation process we do not know what architecture we will
3875 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003876 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3877 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003878 return SideEffects::CanTriggerGC();
3879 }
3880 return SideEffects::None();
3881 }
3882
Roland Levillaindff1f282014-11-05 14:15:05 +00003883 DECLARE_INSTRUCTION(TypeConversion);
3884
3885 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003886 const uint32_t dex_pc_;
3887
Roland Levillaindff1f282014-11-05 14:15:05 +00003888 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3889};
3890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003891static constexpr uint32_t kNoRegNumber = -1;
3892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003893class HPhi : public HInstruction {
3894 public:
3895 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003896 : HInstruction(SideEffects::None()),
3897 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003898 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003899 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003900 is_live_(false),
3901 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003902 inputs_.SetSize(number_of_inputs);
3903 }
3904
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003905 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3906 static Primitive::Type ToPhiType(Primitive::Type type) {
3907 switch (type) {
3908 case Primitive::kPrimBoolean:
3909 case Primitive::kPrimByte:
3910 case Primitive::kPrimShort:
3911 case Primitive::kPrimChar:
3912 return Primitive::kPrimInt;
3913 default:
3914 return type;
3915 }
3916 }
3917
David Brazdilffee3d32015-07-06 11:48:53 +01003918 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3919
Calin Juravle10e244f2015-01-26 18:54:32 +00003920 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003921
3922 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003923 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003924
Calin Juravle10e244f2015-01-26 18:54:32 +00003925 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003926 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003927
Calin Juravle10e244f2015-01-26 18:54:32 +00003928 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3929 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3930
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003931 uint32_t GetRegNumber() const { return reg_number_; }
3932
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003933 void SetDead() { is_live_ = false; }
3934 void SetLive() { is_live_ = true; }
3935 bool IsDead() const { return !is_live_; }
3936 bool IsLive() const { return is_live_; }
3937
Calin Juravlea4f88312015-04-16 12:57:19 +01003938 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3939 // An equivalent phi is a phi having the same dex register and type.
3940 // It assumes that phis with the same dex register are adjacent.
3941 HPhi* GetNextEquivalentPhiWithSameType() {
3942 HInstruction* next = GetNext();
3943 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3944 if (next->GetType() == GetType()) {
3945 return next->AsPhi();
3946 }
3947 next = next->GetNext();
3948 }
3949 return nullptr;
3950 }
3951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003952 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003953
David Brazdil1abb4192015-02-17 18:33:36 +00003954 protected:
3955 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3956
3957 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3958 inputs_.Put(index, input);
3959 }
3960
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003961 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003962 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003963 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003964 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003965 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003966 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003967
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003968 DISALLOW_COPY_AND_ASSIGN(HPhi);
3969};
3970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971class HNullCheck : public HExpression<1> {
3972 public:
3973 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003974 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003975 SetRawInputAt(0, value);
3976 }
3977
Calin Juravle10e244f2015-01-26 18:54:32 +00003978 bool CanBeMoved() const OVERRIDE { return true; }
3979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003980 UNUSED(other);
3981 return true;
3982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003983
Calin Juravle10e244f2015-01-26 18:54:32 +00003984 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003985
Calin Juravle10e244f2015-01-26 18:54:32 +00003986 bool CanThrow() const OVERRIDE { return true; }
3987
3988 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003989
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003990 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003991
3992 DECLARE_INSTRUCTION(NullCheck);
3993
3994 private:
3995 const uint32_t dex_pc_;
3996
3997 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3998};
3999
4000class FieldInfo : public ValueObject {
4001 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004002 FieldInfo(MemberOffset field_offset,
4003 Primitive::Type field_type,
4004 bool is_volatile,
4005 uint32_t index,
4006 const DexFile& dex_file)
4007 : field_offset_(field_offset),
4008 field_type_(field_type),
4009 is_volatile_(is_volatile),
4010 index_(index),
4011 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004012
4013 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004014 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004015 uint32_t GetFieldIndex() const { return index_; }
4016 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004017 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004018
4019 private:
4020 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004021 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004022 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004023 uint32_t index_;
4024 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004025};
4026
4027class HInstanceFieldGet : public HExpression<1> {
4028 public:
4029 HInstanceFieldGet(HInstruction* value,
4030 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004031 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004032 bool is_volatile,
4033 uint32_t field_idx,
4034 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004035 : HExpression(
4036 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004037 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004038 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004039 SetRawInputAt(0, value);
4040 }
4041
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004042 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004043
4044 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4045 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4046 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004047 }
4048
Calin Juravle641547a2015-04-21 22:08:51 +01004049 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4050 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004051 }
4052
4053 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004054 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4055 }
4056
Calin Juravle52c48962014-12-16 17:02:57 +00004057 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004058 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004059 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004060 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004061
4062 DECLARE_INSTRUCTION(InstanceFieldGet);
4063
4064 private:
4065 const FieldInfo field_info_;
4066
4067 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4068};
4069
4070class HInstanceFieldSet : public HTemplateInstruction<2> {
4071 public:
4072 HInstanceFieldSet(HInstruction* object,
4073 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004074 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004075 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004076 bool is_volatile,
4077 uint32_t field_idx,
4078 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004079 : HTemplateInstruction(
4080 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004081 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004082 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004083 SetRawInputAt(0, object);
4084 SetRawInputAt(1, value);
4085 }
4086
Calin Juravle641547a2015-04-21 22:08:51 +01004087 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4088 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004089 }
4090
Calin Juravle52c48962014-12-16 17:02:57 +00004091 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004092 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004093 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004094 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004095 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004096 bool GetValueCanBeNull() const { return value_can_be_null_; }
4097 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004098
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099 DECLARE_INSTRUCTION(InstanceFieldSet);
4100
4101 private:
4102 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004103 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004104
4105 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4106};
4107
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108class HArrayGet : public HExpression<2> {
4109 public:
4110 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004111 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004112 SetRawInputAt(0, array);
4113 SetRawInputAt(1, index);
4114 }
4115
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004116 bool CanBeMoved() const OVERRIDE { return true; }
4117 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004118 UNUSED(other);
4119 return true;
4120 }
Calin Juravle641547a2015-04-21 22:08:51 +01004121 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4122 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 // TODO: We can be smarter here.
4124 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4125 // which generates the implicit null check. There are cases when these can be removed
4126 // to produce better code. If we ever add optimizations to do so we should allow an
4127 // implicit check here (as long as the address falls in the first page).
4128 return false;
4129 }
4130
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004131 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004133 HInstruction* GetArray() const { return InputAt(0); }
4134 HInstruction* GetIndex() const { return InputAt(1); }
4135
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 DECLARE_INSTRUCTION(ArrayGet);
4137
4138 private:
4139 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4140};
4141
4142class HArraySet : public HTemplateInstruction<3> {
4143 public:
4144 HArraySet(HInstruction* array,
4145 HInstruction* index,
4146 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004147 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004148 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004149 : HTemplateInstruction(
4150 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4151 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004152 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004153 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004154 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4155 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156 SetRawInputAt(0, array);
4157 SetRawInputAt(1, index);
4158 SetRawInputAt(2, value);
4159 }
4160
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 // We currently always call a runtime method to catch array store
4163 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004164 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165 }
4166
Mingyao Yang81014cb2015-06-02 03:16:27 -07004167 // Can throw ArrayStoreException.
4168 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4169
Calin Juravle641547a2015-04-21 22:08:51 +01004170 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4171 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004172 // TODO: Same as for ArrayGet.
4173 return false;
4174 }
4175
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004176 void ClearNeedsTypeCheck() {
4177 needs_type_check_ = false;
4178 }
4179
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 void ClearValueCanBeNull() {
4181 value_can_be_null_ = false;
4182 }
4183
4184 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004185 bool NeedsTypeCheck() const { return needs_type_check_; }
4186
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004187 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004188
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004189 HInstruction* GetArray() const { return InputAt(0); }
4190 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004191 HInstruction* GetValue() const { return InputAt(2); }
4192
4193 Primitive::Type GetComponentType() const {
4194 // The Dex format does not type floating point index operations. Since the
4195 // `expected_component_type_` is set during building and can therefore not
4196 // be correct, we also check what is the value type. If it is a floating
4197 // point type, we must use that type.
4198 Primitive::Type value_type = GetValue()->GetType();
4199 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4200 ? value_type
4201 : expected_component_type_;
4202 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004203
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004204 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4205 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4206 }
4207
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004208 DECLARE_INSTRUCTION(ArraySet);
4209
4210 private:
4211 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004212 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004214 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004215
4216 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4217};
4218
4219class HArrayLength : public HExpression<1> {
4220 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004221 explicit HArrayLength(HInstruction* array)
4222 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4223 // Note that arrays do not change length, so the instruction does not
4224 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225 SetRawInputAt(0, array);
4226 }
4227
Calin Juravle77520bc2015-01-12 18:45:46 +00004228 bool CanBeMoved() const OVERRIDE { return true; }
4229 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004230 UNUSED(other);
4231 return true;
4232 }
Calin Juravle641547a2015-04-21 22:08:51 +01004233 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4234 return obj == InputAt(0);
4235 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 DECLARE_INSTRUCTION(ArrayLength);
4238
4239 private:
4240 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4241};
4242
4243class HBoundsCheck : public HExpression<2> {
4244 public:
4245 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004246 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004247 DCHECK(index->GetType() == Primitive::kPrimInt);
4248 SetRawInputAt(0, index);
4249 SetRawInputAt(1, length);
4250 }
4251
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004252 bool CanBeMoved() const OVERRIDE { return true; }
4253 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004254 UNUSED(other);
4255 return true;
4256 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004258 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004260 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004261
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004262 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263
4264 DECLARE_INSTRUCTION(BoundsCheck);
4265
4266 private:
4267 const uint32_t dex_pc_;
4268
4269 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4270};
4271
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272/**
4273 * Some DEX instructions are folded into multiple HInstructions that need
4274 * to stay live until the last HInstruction. This class
4275 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004276 * HInstruction stays live. `index` represents the stack location index of the
4277 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278 */
4279class HTemporary : public HTemplateInstruction<0> {
4280 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004281 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282
4283 size_t GetIndex() const { return index_; }
4284
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004285 Primitive::Type GetType() const OVERRIDE {
4286 // The previous instruction is the one that will be stored in the temporary location.
4287 DCHECK(GetPrevious() != nullptr);
4288 return GetPrevious()->GetType();
4289 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004290
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291 DECLARE_INSTRUCTION(Temporary);
4292
4293 private:
4294 const size_t index_;
4295
4296 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4297};
4298
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004299class HSuspendCheck : public HTemplateInstruction<0> {
4300 public:
4301 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004302 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004304 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004305 return true;
4306 }
4307
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004308 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004309 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4310 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004311
4312 DECLARE_INSTRUCTION(SuspendCheck);
4313
4314 private:
4315 const uint32_t dex_pc_;
4316
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004317 // Only used for code generation, in order to share the same slow path between back edges
4318 // of a same loop.
4319 SlowPathCode* slow_path_;
4320
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004321 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4322};
4323
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004324/**
4325 * Instruction to load a Class object.
4326 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004327class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004328 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004329 HLoadClass(HCurrentMethod* current_method,
4330 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004331 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004332 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004334 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004335 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004336 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004337 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004338 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004339 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004340 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004341 SetRawInputAt(0, current_method);
4342 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004343
4344 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004345
4346 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4347 return other->AsLoadClass()->type_index_ == type_index_;
4348 }
4349
4350 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4351
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004352 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004353 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004354 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004355 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004356
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004357 bool NeedsEnvironment() const OVERRIDE {
4358 // Will call runtime and load the class if the class is not loaded yet.
4359 // TODO: finer grain decision.
4360 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004361 }
4362
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004363 bool MustGenerateClinitCheck() const {
4364 return generate_clinit_check_;
4365 }
4366
Calin Juravle0ba218d2015-05-19 18:46:01 +01004367 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4368 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004369 }
4370
4371 bool CanCallRuntime() const {
4372 return MustGenerateClinitCheck() || !is_referrers_class_;
4373 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004374
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004375 bool CanThrow() const OVERRIDE {
4376 // May call runtime and and therefore can throw.
4377 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004378 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004379 }
4380
Calin Juravleacf735c2015-02-12 15:25:22 +00004381 ReferenceTypeInfo GetLoadedClassRTI() {
4382 return loaded_class_rti_;
4383 }
4384
4385 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4386 // Make sure we only set exact types (the loaded class should never be merged).
4387 DCHECK(rti.IsExact());
4388 loaded_class_rti_ = rti;
4389 }
4390
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004391 const DexFile& GetDexFile() { return dex_file_; }
4392
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004393 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4394
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004395 static SideEffects SideEffectsForArchRuntimeCalls() {
4396 return SideEffects::CanTriggerGC();
4397 }
4398
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004399 DECLARE_INSTRUCTION(LoadClass);
4400
4401 private:
4402 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004403 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004404 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004405 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004406 // Whether this instruction must generate the initialization check.
4407 // Used for code generation.
4408 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004409
Calin Juravleacf735c2015-02-12 15:25:22 +00004410 ReferenceTypeInfo loaded_class_rti_;
4411
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004412 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4413};
4414
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004415class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004416 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004417 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004418 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004419 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004420 dex_pc_(dex_pc) {
4421 SetRawInputAt(0, current_method);
4422 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004423
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004424 bool CanBeMoved() const OVERRIDE { return true; }
4425
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004426 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4427 return other->AsLoadString()->string_index_ == string_index_;
4428 }
4429
4430 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4431
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004432 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004433 uint32_t GetStringIndex() const { return string_index_; }
4434
4435 // TODO: Can we deopt or debug when we resolve a string?
4436 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004437 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004438 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004439
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004440 static SideEffects SideEffectsForArchRuntimeCalls() {
4441 return SideEffects::CanTriggerGC();
4442 }
4443
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004444 DECLARE_INSTRUCTION(LoadString);
4445
4446 private:
4447 const uint32_t string_index_;
4448 const uint32_t dex_pc_;
4449
4450 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4451};
4452
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004453/**
4454 * Performs an initialization check on its Class object input.
4455 */
4456class HClinitCheck : public HExpression<1> {
4457 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004458 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004459 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004460 Primitive::kPrimNot,
4461 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004462 dex_pc_(dex_pc) {
4463 SetRawInputAt(0, constant);
4464 }
4465
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004466 bool CanBeMoved() const OVERRIDE { return true; }
4467 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4468 UNUSED(other);
4469 return true;
4470 }
4471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004472 bool NeedsEnvironment() const OVERRIDE {
4473 // May call runtime to initialize the class.
4474 return true;
4475 }
4476
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004477 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478
4479 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4480
4481 DECLARE_INSTRUCTION(ClinitCheck);
4482
4483 private:
4484 const uint32_t dex_pc_;
4485
4486 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4487};
4488
4489class HStaticFieldGet : public HExpression<1> {
4490 public:
4491 HStaticFieldGet(HInstruction* cls,
4492 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004493 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004494 bool is_volatile,
4495 uint32_t field_idx,
4496 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004497 : HExpression(
4498 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004499 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004500 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004501 SetRawInputAt(0, cls);
4502 }
4503
Calin Juravle52c48962014-12-16 17:02:57 +00004504
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004505 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004506
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004507 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004508 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4509 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004510 }
4511
4512 size_t ComputeHashCode() const OVERRIDE {
4513 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4514 }
4515
Calin Juravle52c48962014-12-16 17:02:57 +00004516 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4518 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004519 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004520
4521 DECLARE_INSTRUCTION(StaticFieldGet);
4522
4523 private:
4524 const FieldInfo field_info_;
4525
4526 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4527};
4528
4529class HStaticFieldSet : public HTemplateInstruction<2> {
4530 public:
4531 HStaticFieldSet(HInstruction* cls,
4532 HInstruction* value,
4533 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004534 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004535 bool is_volatile,
4536 uint32_t field_idx,
4537 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004538 : HTemplateInstruction(
4539 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004540 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004541 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004542 SetRawInputAt(0, cls);
4543 SetRawInputAt(1, value);
4544 }
4545
Calin Juravle52c48962014-12-16 17:02:57 +00004546 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004547 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4548 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004549 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004550
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004551 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004552 bool GetValueCanBeNull() const { return value_can_be_null_; }
4553 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004554
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 DECLARE_INSTRUCTION(StaticFieldSet);
4556
4557 private:
4558 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004559 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004560
4561 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4562};
4563
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004564// Implement the move-exception DEX instruction.
4565class HLoadException : public HExpression<0> {
4566 public:
4567 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4568
David Brazdilbbd733e2015-08-18 17:48:17 +01004569 bool CanBeNull() const OVERRIDE { return false; }
4570
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004571 DECLARE_INSTRUCTION(LoadException);
4572
4573 private:
4574 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4575};
4576
David Brazdilcb1c0552015-08-04 16:22:25 +01004577// Implicit part of move-exception which clears thread-local exception storage.
4578// Must not be removed because the runtime expects the TLS to get cleared.
4579class HClearException : public HTemplateInstruction<0> {
4580 public:
4581 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4582
4583 DECLARE_INSTRUCTION(ClearException);
4584
4585 private:
4586 DISALLOW_COPY_AND_ASSIGN(HClearException);
4587};
4588
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004589class HThrow : public HTemplateInstruction<1> {
4590 public:
4591 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004592 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004593 SetRawInputAt(0, exception);
4594 }
4595
4596 bool IsControlFlow() const OVERRIDE { return true; }
4597
4598 bool NeedsEnvironment() const OVERRIDE { return true; }
4599
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004600 bool CanThrow() const OVERRIDE { return true; }
4601
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004602 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004603
4604 DECLARE_INSTRUCTION(Throw);
4605
4606 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004607 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004608
4609 DISALLOW_COPY_AND_ASSIGN(HThrow);
4610};
4611
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004612class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004613 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004614 HInstanceOf(HInstruction* object,
4615 HLoadClass* constant,
4616 bool class_is_final,
4617 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004618 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004619 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004620 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004621 dex_pc_(dex_pc) {
4622 SetRawInputAt(0, object);
4623 SetRawInputAt(1, constant);
4624 }
4625
4626 bool CanBeMoved() const OVERRIDE { return true; }
4627
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004628 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004629 return true;
4630 }
4631
4632 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004633 return false;
4634 }
4635
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004636 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004637
4638 bool IsClassFinal() const { return class_is_final_; }
4639
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004640 // Used only in code generation.
4641 bool MustDoNullCheck() const { return must_do_null_check_; }
4642 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4643
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004644 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4645 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4646 }
4647
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004648 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004649
4650 private:
4651 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004652 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004653 const uint32_t dex_pc_;
4654
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004655 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4656};
4657
Calin Juravleb1498f62015-02-16 13:13:29 +00004658class HBoundType : public HExpression<1> {
4659 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004660 // Constructs an HBoundType with the given upper_bound.
4661 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004662 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004663 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004664 upper_bound_(upper_bound),
4665 upper_can_be_null_(upper_can_be_null),
4666 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004667 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004668 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004669 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004670 }
4671
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004672 // GetUpper* should only be used in reference type propagation.
4673 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4674 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004675
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004676 void SetCanBeNull(bool can_be_null) {
4677 DCHECK(upper_can_be_null_ || !can_be_null);
4678 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004679 }
4680
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004681 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4682
Calin Juravleb1498f62015-02-16 13:13:29 +00004683 DECLARE_INSTRUCTION(BoundType);
4684
4685 private:
4686 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004687 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4688 // It is used to bound the type in cases like:
4689 // if (x instanceof ClassX) {
4690 // // uper_bound_ will be ClassX
4691 // }
4692 const ReferenceTypeInfo upper_bound_;
4693 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4694 // is false then can_be_null_ cannot be true).
4695 const bool upper_can_be_null_;
4696 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004697
4698 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4699};
4700
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004701class HCheckCast : public HTemplateInstruction<2> {
4702 public:
4703 HCheckCast(HInstruction* object,
4704 HLoadClass* constant,
4705 bool class_is_final,
4706 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004707 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004708 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004709 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004710 dex_pc_(dex_pc) {
4711 SetRawInputAt(0, object);
4712 SetRawInputAt(1, constant);
4713 }
4714
4715 bool CanBeMoved() const OVERRIDE { return true; }
4716
4717 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4718 return true;
4719 }
4720
4721 bool NeedsEnvironment() const OVERRIDE {
4722 // Instruction may throw a CheckCastError.
4723 return true;
4724 }
4725
4726 bool CanThrow() const OVERRIDE { return true; }
4727
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004728 bool MustDoNullCheck() const { return must_do_null_check_; }
4729 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4730
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004731 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004732
4733 bool IsClassFinal() const { return class_is_final_; }
4734
4735 DECLARE_INSTRUCTION(CheckCast);
4736
4737 private:
4738 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004739 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004740 const uint32_t dex_pc_;
4741
4742 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004743};
4744
Calin Juravle27df7582015-04-17 19:12:31 +01004745class HMemoryBarrier : public HTemplateInstruction<0> {
4746 public:
4747 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004748 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004749 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004750 barrier_kind_(barrier_kind) {}
4751
4752 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4753
4754 DECLARE_INSTRUCTION(MemoryBarrier);
4755
4756 private:
4757 const MemBarrierKind barrier_kind_;
4758
4759 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4760};
4761
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004762class HMonitorOperation : public HTemplateInstruction<1> {
4763 public:
4764 enum OperationKind {
4765 kEnter,
4766 kExit,
4767 };
4768
4769 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004770 : HTemplateInstruction(
4771 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004772 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004773 SetRawInputAt(0, object);
4774 }
4775
4776 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004777 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4778
4779 bool CanThrow() const OVERRIDE {
4780 // Verifier guarantees that monitor-exit cannot throw.
4781 // This is important because it allows the HGraphBuilder to remove
4782 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4783 return IsEnter();
4784 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004785
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004786 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004787
4788 bool IsEnter() const { return kind_ == kEnter; }
4789
4790 DECLARE_INSTRUCTION(MonitorOperation);
4791
Calin Juravle52c48962014-12-16 17:02:57 +00004792 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004793 const OperationKind kind_;
4794 const uint32_t dex_pc_;
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4798};
4799
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004800/**
4801 * A HInstruction used as a marker for the replacement of new + <init>
4802 * of a String to a call to a StringFactory. Only baseline will see
4803 * the node at code generation, where it will be be treated as null.
4804 * When compiling non-baseline, `HFakeString` instructions are being removed
4805 * in the instruction simplifier.
4806 */
4807class HFakeString : public HTemplateInstruction<0> {
4808 public:
4809 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4810
4811 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4812
4813 DECLARE_INSTRUCTION(FakeString);
4814
4815 private:
4816 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4817};
4818
Vladimir Markof9f64412015-09-02 14:05:49 +01004819class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004820 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004821 MoveOperands(Location source,
4822 Location destination,
4823 Primitive::Type type,
4824 HInstruction* instruction)
4825 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004826
4827 Location GetSource() const { return source_; }
4828 Location GetDestination() const { return destination_; }
4829
4830 void SetSource(Location value) { source_ = value; }
4831 void SetDestination(Location value) { destination_ = value; }
4832
4833 // The parallel move resolver marks moves as "in-progress" by clearing the
4834 // destination (but not the source).
4835 Location MarkPending() {
4836 DCHECK(!IsPending());
4837 Location dest = destination_;
4838 destination_ = Location::NoLocation();
4839 return dest;
4840 }
4841
4842 void ClearPending(Location dest) {
4843 DCHECK(IsPending());
4844 destination_ = dest;
4845 }
4846
4847 bool IsPending() const {
4848 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4849 return destination_.IsInvalid() && !source_.IsInvalid();
4850 }
4851
4852 // True if this blocks a move from the given location.
4853 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004854 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004855 }
4856
4857 // A move is redundant if it's been eliminated, if its source and
4858 // destination are the same, or if its destination is unneeded.
4859 bool IsRedundant() const {
4860 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4861 }
4862
4863 // We clear both operands to indicate move that's been eliminated.
4864 void Eliminate() {
4865 source_ = destination_ = Location::NoLocation();
4866 }
4867
4868 bool IsEliminated() const {
4869 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4870 return source_.IsInvalid();
4871 }
4872
Alexey Frunze4dda3372015-06-01 18:31:49 -07004873 Primitive::Type GetType() const { return type_; }
4874
Nicolas Geoffray90218252015-04-15 11:56:51 +01004875 bool Is64BitMove() const {
4876 return Primitive::Is64BitType(type_);
4877 }
4878
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004879 HInstruction* GetInstruction() const { return instruction_; }
4880
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004881 private:
4882 Location source_;
4883 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004884 // The type this move is for.
4885 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004886 // The instruction this move is assocatied with. Null when this move is
4887 // for moving an input in the expected locations of user (including a phi user).
4888 // This is only used in debug mode, to ensure we do not connect interval siblings
4889 // in the same parallel move.
4890 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004891};
4892
4893static constexpr size_t kDefaultNumberOfMoves = 4;
4894
4895class HParallelMove : public HTemplateInstruction<0> {
4896 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004897 explicit HParallelMove(ArenaAllocator* arena)
4898 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004899
Nicolas Geoffray90218252015-04-15 11:56:51 +01004900 void AddMove(Location source,
4901 Location destination,
4902 Primitive::Type type,
4903 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004904 DCHECK(source.IsValid());
4905 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004906 if (kIsDebugBuild) {
4907 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004908 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004909 if (moves_.Get(i).GetInstruction() == instruction) {
4910 // Special case the situation where the move is for the spill slot
4911 // of the instruction.
4912 if ((GetPrevious() == instruction)
4913 || ((GetPrevious() == nullptr)
4914 && instruction->IsPhi()
4915 && instruction->GetBlock() == GetBlock())) {
4916 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4917 << "Doing parallel moves for the same instruction.";
4918 } else {
4919 DCHECK(false) << "Doing parallel moves for the same instruction.";
4920 }
4921 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004922 }
4923 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004924 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004925 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004926 << "Overlapped destination for two moves in a parallel move: "
4927 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4928 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004929 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004930 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004931 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004932 }
4933
4934 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004935 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004936 }
4937
4938 size_t NumMoves() const { return moves_.Size(); }
4939
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004940 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004941
4942 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004943 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004944
4945 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4946};
4947
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004948class HGraphVisitor : public ValueObject {
4949 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004950 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4951 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004952
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004953 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004954 virtual void VisitBasicBlock(HBasicBlock* block);
4955
Roland Levillain633021e2014-10-01 14:12:25 +01004956 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004957 void VisitInsertionOrder();
4958
Roland Levillain633021e2014-10-01 14:12:25 +01004959 // Visit the graph following dominator tree reverse post-order.
4960 void VisitReversePostOrder();
4961
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004962 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004963
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004964 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004965#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004966 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4967
4968 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4969
4970#undef DECLARE_VISIT_INSTRUCTION
4971
4972 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004973 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004974
4975 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4976};
4977
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004978class HGraphDelegateVisitor : public HGraphVisitor {
4979 public:
4980 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4981 virtual ~HGraphDelegateVisitor() {}
4982
4983 // Visit functions that delegate to to super class.
4984#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004985 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004986
4987 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4988
4989#undef DECLARE_VISIT_INSTRUCTION
4990
4991 private:
4992 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4993};
4994
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004995class HInsertionOrderIterator : public ValueObject {
4996 public:
4997 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4998
4999 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5000 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5001 void Advance() { ++index_; }
5002
5003 private:
5004 const HGraph& graph_;
5005 size_t index_;
5006
5007 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5008};
5009
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005010class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005011 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005012 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5013 // Check that reverse post order of the graph has been built.
5014 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5015 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005016
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005017 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5018 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005019 void Advance() { ++index_; }
5020
5021 private:
5022 const HGraph& graph_;
5023 size_t index_;
5024
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005025 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005026};
5027
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005028class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005029 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005030 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005031 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5032 // Check that reverse post order of the graph has been built.
5033 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5034 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005035
5036 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005037 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005038 void Advance() { --index_; }
5039
5040 private:
5041 const HGraph& graph_;
5042 size_t index_;
5043
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005044 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005045};
5046
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005047class HLinearPostOrderIterator : public ValueObject {
5048 public:
5049 explicit HLinearPostOrderIterator(const HGraph& graph)
5050 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5051
5052 bool Done() const { return index_ == 0; }
5053
5054 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5055
5056 void Advance() {
5057 --index_;
5058 DCHECK_GE(index_, 0U);
5059 }
5060
5061 private:
5062 const GrowableArray<HBasicBlock*>& order_;
5063 size_t index_;
5064
5065 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5066};
5067
5068class HLinearOrderIterator : public ValueObject {
5069 public:
5070 explicit HLinearOrderIterator(const HGraph& graph)
5071 : order_(graph.GetLinearOrder()), index_(0) {}
5072
5073 bool Done() const { return index_ == order_.Size(); }
5074 HBasicBlock* Current() const { return order_.Get(index_); }
5075 void Advance() { ++index_; }
5076
5077 private:
5078 const GrowableArray<HBasicBlock*>& order_;
5079 size_t index_;
5080
5081 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5082};
5083
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005084// Iterator over the blocks that art part of the loop. Includes blocks part
5085// of an inner loop. The order in which the blocks are iterated is on their
5086// block id.
5087class HBlocksInLoopIterator : public ValueObject {
5088 public:
5089 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5090 : blocks_in_loop_(info.GetBlocks()),
5091 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5092 index_(0) {
5093 if (!blocks_in_loop_.IsBitSet(index_)) {
5094 Advance();
5095 }
5096 }
5097
5098 bool Done() const { return index_ == blocks_.Size(); }
5099 HBasicBlock* Current() const { return blocks_.Get(index_); }
5100 void Advance() {
5101 ++index_;
5102 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5103 if (blocks_in_loop_.IsBitSet(index_)) {
5104 break;
5105 }
5106 }
5107 }
5108
5109 private:
5110 const BitVector& blocks_in_loop_;
5111 const GrowableArray<HBasicBlock*>& blocks_;
5112 size_t index_;
5113
5114 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5115};
5116
Mingyao Yang3584bce2015-05-19 16:01:59 -07005117// Iterator over the blocks that art part of the loop. Includes blocks part
5118// of an inner loop. The order in which the blocks are iterated is reverse
5119// post order.
5120class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5121 public:
5122 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5123 : blocks_in_loop_(info.GetBlocks()),
5124 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5125 index_(0) {
5126 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5127 Advance();
5128 }
5129 }
5130
5131 bool Done() const { return index_ == blocks_.Size(); }
5132 HBasicBlock* Current() const { return blocks_.Get(index_); }
5133 void Advance() {
5134 ++index_;
5135 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5136 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5137 break;
5138 }
5139 }
5140 }
5141
5142 private:
5143 const BitVector& blocks_in_loop_;
5144 const GrowableArray<HBasicBlock*>& blocks_;
5145 size_t index_;
5146
5147 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5148};
5149
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005150inline int64_t Int64FromConstant(HConstant* constant) {
5151 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5152 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5153 : constant->AsLongConstant()->GetValue();
5154}
5155
Vladimir Marko58155012015-08-19 12:49:41 +00005156inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5157 // For the purposes of the compiler, the dex files must actually be the same object
5158 // if we want to safely treat them as the same. This is especially important for JIT
5159 // as custom class loaders can open the same underlying file (or memory) multiple
5160 // times and provide different class resolution but no two class loaders should ever
5161 // use the same DexFile object - doing so is an unsupported hack that can lead to
5162 // all sorts of weird failures.
5163 return &lhs == &rhs;
5164}
5165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005166} // namespace art
5167
5168#endif // ART_COMPILER_OPTIMIZING_NODES_H_