blob: fd5b86ed0ff62656a690b0b24a58f45d5770137d [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
Roland Levillain9867bc72015-08-05 10:21:34 +010020#include <type_traits>
21
David Brazdil8d5b8b22015-03-24 10:51:52 +000022#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080023#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010024#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "handle.h"
27#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010029#include "locations.h"
Vladimir Marko9b688a02015-05-06 14:12:42 +010030#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000034#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035#include "utils/growable_array.h"
36
37namespace art {
38
David Brazdil1abb4192015-02-17 18:33:36 +000039class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010041class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010043class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010044class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000046class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000049class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000050class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000052class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010054class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010055class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000057class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010058class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
61static const int kDefaultNumberOfBlocks = 8;
62static const int kDefaultNumberOfSuccessors = 2;
63static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010064static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010065static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000066static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Calin Juravle9aec02f2014-11-18 23:06:35 +000068static constexpr uint32_t kMaxIntShiftValue = 0x1f;
69static constexpr uint64_t kMaxLongShiftValue = 0x3f;
70
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010071static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
72
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010073static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
74
Dave Allison20dfc792014-06-16 20:44:29 -070075enum IfCondition {
76 kCondEQ,
77 kCondNE,
78 kCondLT,
79 kCondLE,
80 kCondGT,
81 kCondGE,
82};
83
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084class HInstructionList {
85 public:
86 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
87
88 void AddInstruction(HInstruction* instruction);
89 void RemoveInstruction(HInstruction* instruction);
90
David Brazdilc3d743f2015-04-22 13:40:50 +010091 // Insert `instruction` before/after an existing instruction `cursor`.
92 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
93 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
94
Roland Levillain6b469232014-09-25 10:10:38 +010095 // Return true if this list contains `instruction`.
96 bool Contains(HInstruction* instruction) const;
97
Roland Levillainccc07a92014-09-16 14:48:16 +010098 // Return true if `instruction1` is found before `instruction2` in
99 // this instruction list and false otherwise. Abort if none
100 // of these instructions is found.
101 bool FoundBefore(const HInstruction* instruction1,
102 const HInstruction* instruction2) const;
103
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000104 bool IsEmpty() const { return first_instruction_ == nullptr; }
105 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
106
107 // Update the block of all instructions to be `block`.
108 void SetBlockOfInstructions(HBasicBlock* block) const;
109
110 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
111 void Add(const HInstructionList& instruction_list);
112
David Brazdil2d7352b2015-04-20 14:52:42 +0100113 // Return the number of instructions in the list. This is an expensive operation.
114 size_t CountSize() const;
115
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 private:
117 HInstruction* first_instruction_;
118 HInstruction* last_instruction_;
119
120 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000121 friend class HGraph;
122 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100124 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125
126 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
127};
128
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700130class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000131 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 HGraph(ArenaAllocator* arena,
133 const DexFile& dex_file,
134 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100135 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100137 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100138 bool debuggable = false,
139 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100142 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100143 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700144 entry_block_(nullptr),
145 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100146 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100147 number_of_vregs_(0),
148 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000149 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400150 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000151 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100153 dex_file_(dex_file),
154 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100155 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100156 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100157 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000159 cached_null_constant_(nullptr),
160 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000161 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
162 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100163 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
164 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000166 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100168 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169
David Brazdil69ba7b72015-06-23 18:27:30 +0100170 bool IsInSsaForm() const { return in_ssa_form_; }
171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 HBasicBlock* GetEntryBlock() const { return entry_block_; }
173 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100174 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000176 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
177 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000178
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 // Try building the SSA form of this graph, with dominance computation and loop
182 // recognition. Returns whether it was successful in doing all these steps.
183 bool TryBuildingSsa() {
184 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000185 // The SSA builder requires loops to all be natural. Specifically, the dead phi
186 // elimination phase checks the consistency of the graph when doing a post-order
187 // visit for eliminating dead phis: a dead phi can only have loop header phi
188 // users remaining when being visited.
189 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100190 // Precompute per-block try membership before entering the SSA builder,
191 // which needs the information to build catch block phis from values of
192 // locals at throwing instructions inside try blocks.
193 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100195 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000196 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 }
198
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100199 void ComputeDominanceInformation();
200 void ClearDominanceInformation();
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100204 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100205 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000207 // Analyze all natural loops in this graph. Returns false if one
208 // loop is not natural, that is the header does not dominate the
209 // back edge.
210 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211
David Brazdilffee3d32015-07-06 11:48:53 +0100212 // Iterate over blocks to compute try block membership. Needs reverse post
213 // order and loop information.
214 void ComputeTryBlockInformation();
215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000217 // Returns the instruction used to replace the invoke expression or null if the
218 // invoke is for a void method.
219 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220
Mingyao Yang3584bce2015-05-19 16:01:59 -0700221 // Need to add a couple of blocks to test if the loop body is entered and
222 // put deoptimization instructions, etc.
223 void TransformLoopHeaderForBCE(HBasicBlock* header);
224
David Brazdil2d7352b2015-04-20 14:52:42 +0100225 // Removes `block` from the graph.
226 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000227
David Brazdilfc6a86a2015-06-26 10:33:45 +0000228 // Splits the edge between `block` and `successor` while preserving the
229 // indices in the predecessor/successor lists. If there are multiple edges
230 // between the blocks, the lowest indices are used.
231 // Returns the new block which is empty and has the same dex pc as `successor`.
232 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
235 void SimplifyLoop(HBasicBlock* header);
236
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237 int32_t GetNextInstructionId() {
238 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 return current_instruction_id_++;
240 }
241
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 int32_t GetCurrentInstructionId() const {
243 return current_instruction_id_;
244 }
245
246 void SetCurrentInstructionId(int32_t id) {
247 current_instruction_id_ = id;
248 }
249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100250 uint16_t GetMaximumNumberOfOutVRegs() const {
251 return maximum_number_of_out_vregs_;
252 }
253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000254 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
255 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100256 }
257
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100258 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
259 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
260 }
261
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 void UpdateTemporariesVRegSlots(size_t slots) {
263 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100264 }
265
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000266 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100267 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000268 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 }
270
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100271 void SetNumberOfVRegs(uint16_t number_of_vregs) {
272 number_of_vregs_ = number_of_vregs;
273 }
274
275 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100277 return number_of_vregs_;
278 }
279
280 void SetNumberOfInVRegs(uint16_t value) {
281 number_of_in_vregs_ = value;
282 }
283
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100284 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100286 return number_of_vregs_ - number_of_in_vregs_;
287 }
288
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100289 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
290 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100291 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100292
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100293 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
294 return linear_order_;
295 }
296
Mark Mendell1152c922015-04-24 17:06:35 -0400297 bool HasBoundsChecks() const {
298 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800299 }
300
Mark Mendell1152c922015-04-24 17:06:35 -0400301 void SetHasBoundsChecks(bool value) {
302 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800303 }
304
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100305 bool ShouldGenerateConstructorBarrier() const {
306 return should_generate_constructor_barrier_;
307 }
308
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000309 bool IsDebuggable() const { return debuggable_; }
310
David Brazdil8d5b8b22015-03-24 10:51:52 +0000311 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000312 // already, it is created and inserted into the graph. This method is only for
313 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000314 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000315
316 // TODO: This is problematic for the consistency of reference type propagation
317 // because it can be created anytime after the pass and thus it will be left
318 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000319 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000320
David Brazdil8d5b8b22015-03-24 10:51:52 +0000321 HIntConstant* GetIntConstant(int32_t value) {
322 return CreateConstant(value, &cached_int_constants_);
323 }
324 HLongConstant* GetLongConstant(int64_t value) {
325 return CreateConstant(value, &cached_long_constants_);
326 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327 HFloatConstant* GetFloatConstant(float value) {
328 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
329 }
330 HDoubleConstant* GetDoubleConstant(double value) {
331 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
332 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000333
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100334 HCurrentMethod* GetCurrentMethod();
335
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000336 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100337
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 const DexFile& GetDexFile() const {
339 return dex_file_;
340 }
341
342 uint32_t GetMethodIdx() const {
343 return method_idx_;
344 }
345
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100346 InvokeType GetInvokeType() const {
347 return invoke_type_;
348 }
349
Mark Mendellc4701932015-04-10 13:18:51 -0400350 InstructionSet GetInstructionSet() const {
351 return instruction_set_;
352 }
353
David Brazdil2d7352b2015-04-20 14:52:42 +0100354 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 void VisitBlockForDominatorTree(HBasicBlock* block,
356 HBasicBlock* predecessor,
357 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100358 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359 void VisitBlockForBackEdges(HBasicBlock* block,
360 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100361 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000362 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100363 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000364
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000365 template <class InstructionType, typename ValueType>
366 InstructionType* CreateConstant(ValueType value,
367 ArenaSafeMap<ValueType, InstructionType*>* cache) {
368 // Try to find an existing constant of the given value.
369 InstructionType* constant = nullptr;
370 auto cached_constant = cache->find(value);
371 if (cached_constant != cache->end()) {
372 constant = cached_constant->second;
373 }
374
375 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100376 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000377 if (constant == nullptr || constant->GetBlock() == nullptr) {
378 constant = new (arena_) InstructionType(value);
379 cache->Overwrite(value, constant);
380 InsertConstant(constant);
381 }
382 return constant;
383 }
384
David Brazdil8d5b8b22015-03-24 10:51:52 +0000385 void InsertConstant(HConstant* instruction);
386
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 // Cache a float constant into the graph. This method should only be
388 // called by the SsaBuilder when creating "equivalent" instructions.
389 void CacheFloatConstant(HFloatConstant* constant);
390
391 // See CacheFloatConstant comment.
392 void CacheDoubleConstant(HDoubleConstant* constant);
393
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000395
396 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000397 GrowableArray<HBasicBlock*> blocks_;
398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399 // List of blocks to perform a reverse post order tree traversal.
400 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000401
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100402 // List of blocks to perform a linear order tree traversal.
403 GrowableArray<HBasicBlock*> linear_order_;
404
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000405 HBasicBlock* entry_block_;
406 HBasicBlock* exit_block_;
407
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100408 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100409 uint16_t maximum_number_of_out_vregs_;
410
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100411 // The number of virtual registers in this method. Contains the parameters.
412 uint16_t number_of_vregs_;
413
414 // The number of virtual registers used by parameters of this method.
415 uint16_t number_of_in_vregs_;
416
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000417 // Number of vreg size slots that the temporaries use (used in baseline compiler).
418 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100419
Mark Mendell1152c922015-04-24 17:06:35 -0400420 // Has bounds checks. We can totally skip BCE if it's false.
421 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800422
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000423 // Indicates whether the graph should be compiled in a way that
424 // ensures full debuggability. If false, we can apply more
425 // aggressive optimizations that may limit the level of debugging.
426 const bool debuggable_;
427
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000428 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000429 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100431 // The dex file from which the method is from.
432 const DexFile& dex_file_;
433
434 // The method index in the dex file.
435 const uint32_t method_idx_;
436
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100437 // If inlined, this encodes how the callee is being invoked.
438 const InvokeType invoke_type_;
439
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100440 // Whether the graph has been transformed to SSA form. Only used
441 // in debug mode to ensure we are not using properties only valid
442 // for non-SSA form (like the number of temporaries).
443 bool in_ssa_form_;
444
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100445 const bool should_generate_constructor_barrier_;
446
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 const InstructionSet instruction_set_;
448
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000449 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000450 HNullConstant* cached_null_constant_;
451 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000452 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000453 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000454 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000455
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100456 HCurrentMethod* cached_current_method_;
457
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100459 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000460 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 DISALLOW_COPY_AND_ASSIGN(HGraph);
462};
463
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700464class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000465 public:
466 HLoopInformation(HBasicBlock* header, HGraph* graph)
467 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100468 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100470 // Make bit vector growable, as the number of blocks may change.
471 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100472
473 HBasicBlock* GetHeader() const {
474 return header_;
475 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000476
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000477 void SetHeader(HBasicBlock* block) {
478 header_ = block;
479 }
480
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100481 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
482 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
483 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
484
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000485 void AddBackEdge(HBasicBlock* back_edge) {
486 back_edges_.Add(back_edge);
487 }
488
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100489 void RemoveBackEdge(HBasicBlock* back_edge) {
490 back_edges_.Delete(back_edge);
491 }
492
David Brazdil46e2a392015-03-16 17:31:52 +0000493 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100494 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000495 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 }
497 return false;
498 }
499
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000500 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000501 return back_edges_.Size();
502 }
503
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
507 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508 }
509
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100510 // Returns the lifetime position of the back edge that has the
511 // greatest lifetime position.
512 size_t GetLifetimeEnd() const;
513
514 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
515 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
516 if (back_edges_.Get(i) == existing) {
517 back_edges_.Put(i, new_back_edge);
518 return;
519 }
520 }
521 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100522 }
523
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100524 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 // that is the header dominates the back edge.
526 bool Populate();
527
David Brazdila4b8c212015-05-07 09:59:30 +0100528 // Reanalyzes the loop by removing loop info from its blocks and re-running
529 // Populate(). If there are no back edges left, the loop info is completely
530 // removed as well as its SuspendCheck instruction. It must be run on nested
531 // inner loops first.
532 void Update();
533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // Returns whether this loop information contains `block`.
535 // Note that this loop information *must* be populated before entering this function.
536 bool Contains(const HBasicBlock& block) const;
537
538 // Returns whether this loop information is an inner loop of `other`.
539 // Note that `other` *must* be populated before entering this function.
540 bool IsIn(const HLoopInformation& other) const;
541
542 const ArenaBitVector& GetBlocks() const { return blocks_; }
543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000545 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000546
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100548 // Internal recursive implementation of `Populate`.
549 void PopulateRecursive(HBasicBlock* block);
550
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100552 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000553 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100554 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555
556 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
557};
558
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100559static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100560static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100561
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562// A block in a method. Contains the list of instructions represented
563// as a double linked list. Each block knows its predecessors and
564// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100565
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700566class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100568 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
David Brazdilb618ade2015-07-29 10:31:29 +0100571 exceptional_predecessors_(graph->GetArena(), kDefaultNumberOfExceptionalPredecessors),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000572 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573 loop_information_(nullptr),
574 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100575 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100576 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100577 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100578 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000579 lifetime_end_(kNoLifetime),
580 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100582 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
583 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000584 }
585
David Brazdilb618ade2015-07-29 10:31:29 +0100586 const GrowableArray<HInstruction*>& GetExceptionalPredecessors() const {
587 return exceptional_predecessors_;
588 }
589
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100590 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
591 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000592 }
593
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100594 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
595 return dominated_blocks_;
596 }
597
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100598 bool IsEntryBlock() const {
599 return graph_->GetEntryBlock() == this;
600 }
601
602 bool IsExitBlock() const {
603 return graph_->GetExitBlock() == this;
604 }
605
David Brazdil46e2a392015-03-16 17:31:52 +0000606 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000607 bool IsSingleTryBoundary() const;
608
609 // Returns true if this block emits nothing but a jump.
610 bool IsSingleJump() const {
611 HLoopInformation* loop_info = GetLoopInformation();
612 return (IsSingleGoto() || IsSingleTryBoundary())
613 // Back edges generate a suspend check.
614 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
615 }
David Brazdil46e2a392015-03-16 17:31:52 +0000616
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 void AddBackEdge(HBasicBlock* back_edge) {
618 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000619 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100621 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000622 loop_information_->AddBackEdge(back_edge);
623 }
624
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000625 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000626 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000628 int GetBlockId() const { return block_id_; }
629 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000631 HBasicBlock* GetDominator() const { return dominator_; }
632 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100633 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100634 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
636 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
637 if (dominated_blocks_.Get(i) == existing) {
638 dominated_blocks_.Put(i, new_block);
639 return;
640 }
641 }
642 LOG(FATAL) << "Unreachable";
643 UNREACHABLE();
644 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100645 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000646
647 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100648 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649 }
650
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
652 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100653 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100654 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100655 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
656 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657
David Brazdilb618ade2015-07-29 10:31:29 +0100658 void AddExceptionalPredecessor(HInstruction* exceptional_predecessor);
659
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 void AddSuccessor(HBasicBlock* block) {
661 successors_.Add(block);
662 block->predecessors_.Add(this);
663 }
664
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100665 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
666 size_t successor_index = GetSuccessorIndexOf(existing);
667 DCHECK_NE(successor_index, static_cast<size_t>(-1));
668 existing->RemovePredecessor(this);
669 new_block->predecessors_.Add(this);
670 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000671 }
672
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000673 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
674 size_t predecessor_index = GetPredecessorIndexOf(existing);
675 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
676 existing->RemoveSuccessor(this);
677 new_block->successors_.Add(this);
678 predecessors_.Put(predecessor_index, new_block);
679 }
680
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100681 // Insert `this` between `predecessor` and `successor. This method
682 // preserves the indicies, and will update the first edge found between
683 // `predecessor` and `successor`.
684 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
685 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
686 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
687 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
688 DCHECK_NE(successor_index, static_cast<size_t>(-1));
689 successor->predecessors_.Put(predecessor_index, this);
690 predecessor->successors_.Put(successor_index, this);
691 successors_.Add(successor);
692 predecessors_.Add(predecessor);
693 }
694
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100695 void RemovePredecessor(HBasicBlock* block) {
696 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 }
698
David Brazdilb618ade2015-07-29 10:31:29 +0100699 void RemoveExceptionalPredecessor(HInstruction* instruction) {
700 exceptional_predecessors_.Delete(instruction);
701 }
702
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 void RemoveSuccessor(HBasicBlock* block) {
704 successors_.Delete(block);
705 }
706
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 void ClearAllPredecessors() {
708 predecessors_.Reset();
709 }
710
711 void AddPredecessor(HBasicBlock* block) {
712 predecessors_.Add(block);
713 block->successors_.Add(this);
714 }
715
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100716 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100717 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100718 HBasicBlock* temp = predecessors_.Get(0);
719 predecessors_.Put(0, predecessors_.Get(1));
720 predecessors_.Put(1, temp);
721 }
722
David Brazdil769c9e52015-04-27 13:54:09 +0100723 void SwapSuccessors() {
724 DCHECK_EQ(successors_.Size(), 2u);
725 HBasicBlock* temp = successors_.Get(0);
726 successors_.Put(0, successors_.Get(1));
727 successors_.Put(1, temp);
728 }
729
David Brazdilfc6a86a2015-06-26 10:33:45 +0000730 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100731 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
732 if (predecessors_.Get(i) == predecessor) {
733 return i;
734 }
735 }
736 return -1;
737 }
738
David Brazdilb618ade2015-07-29 10:31:29 +0100739 size_t GetExceptionalPredecessorIndexOf(HInstruction* exceptional_predecessor) const {
740 for (size_t i = 0, e = exceptional_predecessors_.Size(); i < e; ++i) {
741 if (exceptional_predecessors_.Get(i) == exceptional_predecessor) {
742 return i;
743 }
744 }
745 return -1;
746 }
747
David Brazdilfc6a86a2015-06-26 10:33:45 +0000748 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100749 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
750 if (successors_.Get(i) == successor) {
751 return i;
752 }
753 }
754 return -1;
755 }
756
David Brazdilfc6a86a2015-06-26 10:33:45 +0000757 HBasicBlock* GetSinglePredecessor() const {
758 DCHECK_EQ(GetPredecessors().Size(), 1u);
759 return GetPredecessors().Get(0);
760 }
761
762 HBasicBlock* GetSingleSuccessor() const {
763 DCHECK_EQ(GetSuccessors().Size(), 1u);
764 return GetSuccessors().Get(0);
765 }
766
767 // Returns whether the first occurrence of `predecessor` in the list of
768 // predecessors is at index `idx`.
769 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
770 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
771 return GetPredecessorIndexOf(predecessor) == idx;
772 }
773
David Brazdilffee3d32015-07-06 11:48:53 +0100774 // Returns the number of non-exceptional successors. SsaChecker ensures that
775 // these are stored at the beginning of the successor list.
776 size_t NumberOfNormalSuccessors() const {
777 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
778 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000779
780 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100781 // created, latter block. Note that this method will add the block to the
782 // graph, create a Goto at the end of the former block and will create an edge
783 // between the blocks. It will not, however, update the reverse post order or
784 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000785 HBasicBlock* SplitBefore(HInstruction* cursor);
786
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000787 // Split the block into two blocks just after `cursor`. Returns the newly
788 // created block. Note that this method just updates raw block information,
789 // like predecessors, successors, dominators, and instruction list. It does not
790 // update the graph, reverse post order, loop information, nor make sure the
791 // blocks are consistent (for example ending with a control flow instruction).
792 HBasicBlock* SplitAfter(HInstruction* cursor);
793
794 // Merge `other` at the end of `this`. Successors and dominated blocks of
795 // `other` are changed to be successors and dominated blocks of `this`. Note
796 // that this method does not update the graph, reverse post order, loop
797 // information, nor make sure the blocks are consistent (for example ending
798 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100799 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000800
801 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
802 // of `this` are moved to `other`.
803 // Note that this method does not update the graph, reverse post order, loop
804 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000805 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000806 void ReplaceWith(HBasicBlock* other);
807
David Brazdil2d7352b2015-04-20 14:52:42 +0100808 // Merge `other` at the end of `this`. This method updates loops, reverse post
809 // order, links to predecessors, successors, dominators and deletes the block
810 // from the graph. The two blocks must be successive, i.e. `this` the only
811 // predecessor of `other` and vice versa.
812 void MergeWith(HBasicBlock* other);
813
814 // Disconnects `this` from all its predecessors, successors and dominator,
815 // removes it from all loops it is included in and eventually from the graph.
816 // The block must not dominate any other block. Predecessors and successors
817 // are safely updated.
818 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000819
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100821 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100822 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100823 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100824 // Replace instruction `initial` with `replacement` within this block.
825 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
826 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100827 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100828 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000829 // RemoveInstruction and RemovePhi delete a given instruction from the respective
830 // instruction list. With 'ensure_safety' set to true, it verifies that the
831 // instruction is not in use and removes it from the use lists of its inputs.
832 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
833 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100834 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835
836 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100837 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838 }
839
Roland Levillain6b879dd2014-09-22 17:13:44 +0100840 bool IsLoopPreHeaderFirstPredecessor() const {
841 DCHECK(IsLoopHeader());
842 DCHECK(!GetPredecessors().IsEmpty());
843 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
844 }
845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846 HLoopInformation* GetLoopInformation() const {
847 return loop_information_;
848 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100851 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 void SetInLoop(HLoopInformation* info) {
854 if (IsLoopHeader()) {
855 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100856 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100857 loop_information_ = info;
858 } else if (loop_information_->Contains(*info->GetHeader())) {
859 // Block is currently part of an outer loop. Make it part of this inner loop.
860 // Note that a non loop header having a loop information means this loop information
861 // has already been populated
862 loop_information_ = info;
863 } else {
864 // Block is part of an inner loop. Do not update the loop information.
865 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
866 // at this point, because this method is being called while populating `info`.
867 }
868 }
869
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000870 // Raw update of the loop information.
871 void SetLoopInformation(HLoopInformation* info) {
872 loop_information_ = info;
873 }
874
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100875 bool IsInLoop() const { return loop_information_ != nullptr; }
876
David Brazdilffee3d32015-07-06 11:48:53 +0100877 HTryBoundary* GetTryEntry() const { return try_entry_; }
878 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
879 bool IsInTry() const { return try_entry_ != nullptr; }
880
881 // Returns the try entry that this block's successors should have. They will
882 // be in the same try, unless the block ends in a try boundary. In that case,
883 // the appropriate try entry will be returned.
884 HTryBoundary* ComputeTryEntryOfSuccessors() const;
885
David Brazdila4b8c212015-05-07 09:59:30 +0100886 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100887 bool Dominates(HBasicBlock* block) const;
888
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100889 size_t GetLifetimeStart() const { return lifetime_start_; }
890 size_t GetLifetimeEnd() const { return lifetime_end_; }
891
892 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
893 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
894
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100895 uint32_t GetDexPc() const { return dex_pc_; }
896
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000897 bool IsCatchBlock() const { return is_catch_block_; }
898 void SetIsCatchBlock() { is_catch_block_ = true; }
899
David Brazdil8d5b8b22015-03-24 10:51:52 +0000900 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000901 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100902 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000903 bool HasSinglePhi() const;
904
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907 GrowableArray<HBasicBlock*> predecessors_;
David Brazdilb618ade2015-07-29 10:31:29 +0100908 GrowableArray<HInstruction*> exceptional_predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 HInstructionList instructions_;
911 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000912 HLoopInformation* loop_information_;
913 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100914 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100916 // The dex program counter of the first instruction of this block.
917 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918 size_t lifetime_start_;
919 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000920 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921
David Brazdilffee3d32015-07-06 11:48:53 +0100922 // If this block is in a try block, `try_entry_` stores one of, possibly
923 // several, TryBoundary instructions entering it.
924 HTryBoundary* try_entry_;
925
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000926 friend class HGraph;
927 friend class HInstruction;
928
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
930};
931
David Brazdilb2bd1c52015-03-25 11:17:37 +0000932// Iterates over the LoopInformation of all loops which contain 'block'
933// from the innermost to the outermost.
934class HLoopInformationOutwardIterator : public ValueObject {
935 public:
936 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
937 : current_(block.GetLoopInformation()) {}
938
939 bool Done() const { return current_ == nullptr; }
940
941 void Advance() {
942 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100943 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000944 }
945
946 HLoopInformation* Current() const {
947 DCHECK(!Done());
948 return current_;
949 }
950
951 private:
952 HLoopInformation* current_;
953
954 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
955};
956
Alexandre Ramesef20f712015-06-09 10:29:30 +0100957#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100958 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000959 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000960 M(ArrayGet, Instruction) \
961 M(ArrayLength, Instruction) \
962 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100963 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000964 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000965 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000966 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +0100967 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100968 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000969 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100970 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100971 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700972 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000973 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000974 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000975 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100976 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000977 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100978 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000979 M(FloatConstant, Constant) \
980 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100981 M(GreaterThan, Condition) \
982 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100983 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000984 M(InstanceFieldGet, Instruction) \
985 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000986 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000988 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000989 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100990 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000991 M(LessThan, Condition) \
992 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000993 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000994 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100995 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000996 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100997 M(Local, Instruction) \
998 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100999 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001000 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001001 M(Mul, BinaryOperation) \
1002 M(Neg, UnaryOperation) \
1003 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001004 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001005 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001006 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001007 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001009 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001010 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001012 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001013 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(Return, Instruction) \
1015 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001016 M(Shl, BinaryOperation) \
1017 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001018 M(StaticFieldGet, Instruction) \
1019 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(StoreLocal, Instruction) \
1021 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001022 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001024 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001025 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001026 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001027 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001028 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Alexandre Ramesef20f712015-06-09 10:29:30 +01001030#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1031
1032#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1033
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001034#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1035
Alexandre Ramesef20f712015-06-09 10:29:30 +01001036#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1037
1038#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1039
1040#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1041 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1042 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1043 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001044 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001045 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1046 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1047
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048#define FOR_EACH_INSTRUCTION(M) \
1049 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1050 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001051 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001052 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001054
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1057#undef FORWARD_DECLARATION
1058
Roland Levillainccc07a92014-09-16 14:48:16 +01001059#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001060 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1061 const char* DebugName() const OVERRIDE { return #type; } \
1062 const H##type* As##type() const OVERRIDE { return this; } \
1063 H##type* As##type() OVERRIDE { return this; } \
1064 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001065 return other->Is##type(); \
1066 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001067 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
David Brazdiled596192015-01-23 10:39:45 +00001069template <typename T> class HUseList;
1070
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001071template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001073 public:
David Brazdiled596192015-01-23 10:39:45 +00001074 HUseListNode* GetPrevious() const { return prev_; }
1075 HUseListNode* GetNext() const { return next_; }
1076 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001077 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001078 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001080 private:
David Brazdiled596192015-01-23 10:39:45 +00001081 HUseListNode(T user, size_t index)
1082 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1083
1084 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001085 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001086 HUseListNode<T>* prev_;
1087 HUseListNode<T>* next_;
1088
1089 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001090
1091 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1092};
1093
David Brazdiled596192015-01-23 10:39:45 +00001094template <typename T>
1095class HUseList : public ValueObject {
1096 public:
1097 HUseList() : first_(nullptr) {}
1098
1099 void Clear() {
1100 first_ = nullptr;
1101 }
1102
1103 // Adds a new entry at the beginning of the use list and returns
1104 // the newly created node.
1105 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001106 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001107 if (IsEmpty()) {
1108 first_ = new_node;
1109 } else {
1110 first_->prev_ = new_node;
1111 new_node->next_ = first_;
1112 first_ = new_node;
1113 }
1114 return new_node;
1115 }
1116
1117 HUseListNode<T>* GetFirst() const {
1118 return first_;
1119 }
1120
1121 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001122 DCHECK(node != nullptr);
1123 DCHECK(Contains(node));
1124
David Brazdiled596192015-01-23 10:39:45 +00001125 if (node->prev_ != nullptr) {
1126 node->prev_->next_ = node->next_;
1127 }
1128 if (node->next_ != nullptr) {
1129 node->next_->prev_ = node->prev_;
1130 }
1131 if (node == first_) {
1132 first_ = node->next_;
1133 }
1134 }
1135
David Brazdil1abb4192015-02-17 18:33:36 +00001136 bool Contains(const HUseListNode<T>* node) const {
1137 if (node == nullptr) {
1138 return false;
1139 }
1140 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1141 if (current == node) {
1142 return true;
1143 }
1144 }
1145 return false;
1146 }
1147
David Brazdiled596192015-01-23 10:39:45 +00001148 bool IsEmpty() const {
1149 return first_ == nullptr;
1150 }
1151
1152 bool HasOnlyOneUse() const {
1153 return first_ != nullptr && first_->next_ == nullptr;
1154 }
1155
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001156 size_t SizeSlow() const {
1157 size_t count = 0;
1158 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1159 ++count;
1160 }
1161 return count;
1162 }
1163
David Brazdiled596192015-01-23 10:39:45 +00001164 private:
1165 HUseListNode<T>* first_;
1166};
1167
1168template<typename T>
1169class HUseIterator : public ValueObject {
1170 public:
1171 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1172
1173 bool Done() const { return current_ == nullptr; }
1174
1175 void Advance() {
1176 DCHECK(!Done());
1177 current_ = current_->GetNext();
1178 }
1179
1180 HUseListNode<T>* Current() const {
1181 DCHECK(!Done());
1182 return current_;
1183 }
1184
1185 private:
1186 HUseListNode<T>* current_;
1187
1188 friend class HValue;
1189};
1190
David Brazdil1abb4192015-02-17 18:33:36 +00001191// This class is used by HEnvironment and HInstruction classes to record the
1192// instructions they use and pointers to the corresponding HUseListNodes kept
1193// by the used instructions.
1194template <typename T>
1195class HUserRecord : public ValueObject {
1196 public:
1197 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1198 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1199
1200 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1201 : instruction_(old_record.instruction_), use_node_(use_node) {
1202 DCHECK(instruction_ != nullptr);
1203 DCHECK(use_node_ != nullptr);
1204 DCHECK(old_record.use_node_ == nullptr);
1205 }
1206
1207 HInstruction* GetInstruction() const { return instruction_; }
1208 HUseListNode<T>* GetUseNode() const { return use_node_; }
1209
1210 private:
1211 // Instruction used by the user.
1212 HInstruction* instruction_;
1213
1214 // Corresponding entry in the use list kept by 'instruction_'.
1215 HUseListNode<T>* use_node_;
1216};
1217
Aart Bik854a02b2015-07-14 16:07:00 -07001218/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001219 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001220 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001221 * For write/read dependences on fields/arrays, the dependence analysis uses
1222 * type disambiguation (e.g. a float field write cannot modify the value of an
1223 * integer field read) and the access type (e.g. a reference array write cannot
1224 * modify the value of a reference field read [although it may modify the
1225 * reference fetch prior to reading the field, which is represented by its own
1226 * write/read dependence]). The analysis makes conservative points-to
1227 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1228 * the same, and any reference read depends on any reference read without
1229 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001230 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001231 * The internal representation uses 38-bit and is described in the table below.
1232 * The first line indicates the side effect, and for field/array accesses the
1233 * second line indicates the type of the access (in the order of the
1234 * Primitive::Type enum).
1235 * The two numbered lines below indicate the bit position in the bitfield (read
1236 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001237 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001238 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1239 * +-------------+---------+---------+--------------+---------+---------+
1240 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1241 * | 3 |333333322|222222221| 1 |111111110|000000000|
1242 * | 7 |654321098|765432109| 8 |765432109|876543210|
1243 *
1244 * Note that, to ease the implementation, 'changes' bits are least significant
1245 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001246 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001247class SideEffects : public ValueObject {
1248 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001249 SideEffects() : flags_(0) {}
1250
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001251 static SideEffects None() {
1252 return SideEffects(0);
1253 }
1254
1255 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001256 return SideEffects(kAllChangeBits | kAllDependOnBits);
1257 }
1258
1259 static SideEffects AllChanges() {
1260 return SideEffects(kAllChangeBits);
1261 }
1262
1263 static SideEffects AllDependencies() {
1264 return SideEffects(kAllDependOnBits);
1265 }
1266
1267 static SideEffects AllExceptGCDependency() {
1268 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1269 }
1270
1271 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001272 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001273 }
1274
Aart Bik34c3ba92015-07-20 14:08:59 -07001275 static SideEffects AllWrites() {
1276 return SideEffects(kAllWrites);
1277 }
1278
1279 static SideEffects AllReads() {
1280 return SideEffects(kAllReads);
1281 }
1282
1283 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1284 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001285 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001286 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001287 }
1288
Aart Bik854a02b2015-07-14 16:07:00 -07001289 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1290 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001291 }
1292
Aart Bik34c3ba92015-07-20 14:08:59 -07001293 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1294 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001295 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001296 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001297 }
1298
1299 static SideEffects ArrayReadOfType(Primitive::Type type) {
1300 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1301 }
1302
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 static SideEffects CanTriggerGC() {
1304 return SideEffects(1ULL << kCanTriggerGCBit);
1305 }
1306
1307 static SideEffects DependsOnGC() {
1308 return SideEffects(1ULL << kDependsOnGCBit);
1309 }
1310
Aart Bik854a02b2015-07-14 16:07:00 -07001311 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001312 SideEffects Union(SideEffects other) const {
1313 return SideEffects(flags_ | other.flags_);
1314 }
1315
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 SideEffects Exclusion(SideEffects other) const {
1317 return SideEffects(flags_ & ~other.flags_);
1318 }
1319
1320 bool Includes(SideEffects other) const {
1321 return (other.flags_ & flags_) == other.flags_;
1322 }
1323
1324 bool HasSideEffects() const {
1325 return (flags_ & kAllChangeBits);
1326 }
1327
1328 bool HasDependencies() const {
1329 return (flags_ & kAllDependOnBits);
1330 }
1331
1332 // Returns true if there are no side effects or dependencies.
1333 bool DoesNothing() const {
1334 return flags_ == 0;
1335 }
1336
Aart Bik854a02b2015-07-14 16:07:00 -07001337 // Returns true if something is written.
1338 bool DoesAnyWrite() const {
1339 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001340 }
1341
Aart Bik854a02b2015-07-14 16:07:00 -07001342 // Returns true if something is read.
1343 bool DoesAnyRead() const {
1344 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001345 }
1346
Aart Bik854a02b2015-07-14 16:07:00 -07001347 // Returns true if potentially everything is written and read
1348 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001349 bool DoesAllReadWrite() const {
1350 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1351 }
1352
Aart Bik854a02b2015-07-14 16:07:00 -07001353 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001354 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001355 }
1356
1357 // Returns true if this may read something written by other.
1358 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1360 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001361 }
1362
1363 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001364 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001365 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001366 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367 for (int s = kLastBit; s >= 0; s--) {
1368 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1369 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1370 // This is a bit for the GC side effect.
1371 if (current_bit_is_set) {
1372 flags += "GC";
1373 }
Aart Bik854a02b2015-07-14 16:07:00 -07001374 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001375 } else {
1376 // This is a bit for the array/field analysis.
1377 // The underscore character stands for the 'can trigger GC' bit.
1378 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1379 if (current_bit_is_set) {
1380 flags += kDebug[s];
1381 }
1382 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1383 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1384 flags += "|";
1385 }
1386 }
Aart Bik854a02b2015-07-14 16:07:00 -07001387 }
1388 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001389 }
1390
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001391 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001392
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 private:
1394 static constexpr int kFieldArrayAnalysisBits = 9;
1395
1396 static constexpr int kFieldWriteOffset = 0;
1397 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1398 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1399 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1400
1401 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1402
1403 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1404 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1405 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1406 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1407
1408 static constexpr int kLastBit = kDependsOnGCBit;
1409 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1410
1411 // Aliases.
1412
1413 static_assert(kChangeBits == kDependOnBits,
1414 "the 'change' bits should match the 'depend on' bits.");
1415
1416 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1417 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1418 static constexpr uint64_t kAllWrites =
1419 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1420 static constexpr uint64_t kAllReads =
1421 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 // Work around the fact that HIR aliases I/F and J/D.
1424 // TODO: remove this interceptor once HIR types are clean
1425 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1426 switch (type) {
1427 case Primitive::kPrimInt:
1428 case Primitive::kPrimFloat:
1429 return TypeFlag(Primitive::kPrimInt, offset) |
1430 TypeFlag(Primitive::kPrimFloat, offset);
1431 case Primitive::kPrimLong:
1432 case Primitive::kPrimDouble:
1433 return TypeFlag(Primitive::kPrimLong, offset) |
1434 TypeFlag(Primitive::kPrimDouble, offset);
1435 default:
1436 return TypeFlag(type, offset);
1437 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001438 }
1439
Aart Bik854a02b2015-07-14 16:07:00 -07001440 // Translates type to bit flag.
1441 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1442 CHECK_NE(type, Primitive::kPrimVoid);
1443 const uint64_t one = 1;
1444 const int shift = type; // 0-based consecutive enum
1445 DCHECK_LE(kFieldWriteOffset, shift);
1446 DCHECK_LT(shift, kArrayWriteOffset);
1447 return one << (type + offset);
1448 }
1449
1450 // Private constructor on direct flags value.
1451 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1452
1453 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001454};
1455
David Brazdiled596192015-01-23 10:39:45 +00001456// A HEnvironment object contains the values of virtual registers at a given location.
1457class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1458 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001459 HEnvironment(ArenaAllocator* arena,
1460 size_t number_of_vregs,
1461 const DexFile& dex_file,
1462 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001463 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001464 InvokeType invoke_type,
1465 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001466 : vregs_(arena, number_of_vregs),
1467 locations_(arena, number_of_vregs),
1468 parent_(nullptr),
1469 dex_file_(dex_file),
1470 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001471 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001472 invoke_type_(invoke_type),
1473 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001474 vregs_.SetSize(number_of_vregs);
1475 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001476 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001477 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001478
1479 locations_.SetSize(number_of_vregs);
1480 for (size_t i = 0; i < number_of_vregs; ++i) {
1481 locations_.Put(i, Location());
1482 }
1483 }
1484
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001485 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001486 : HEnvironment(arena,
1487 to_copy.Size(),
1488 to_copy.GetDexFile(),
1489 to_copy.GetMethodIdx(),
1490 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001491 to_copy.GetInvokeType(),
1492 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001493
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001494 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001495 if (parent_ != nullptr) {
1496 parent_->SetAndCopyParentChain(allocator, parent);
1497 } else {
1498 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1499 parent_->CopyFrom(parent);
1500 if (parent->GetParent() != nullptr) {
1501 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1502 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001503 }
David Brazdiled596192015-01-23 10:39:45 +00001504 }
1505
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001506 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1507 void CopyFrom(HEnvironment* environment);
1508
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001509 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1510 // input to the loop phi instead. This is for inserting instructions that
1511 // require an environment (like HDeoptimization) in the loop pre-header.
1512 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001513
1514 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001515 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001516 }
1517
1518 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001519 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001520 }
1521
David Brazdil1abb4192015-02-17 18:33:36 +00001522 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001523
1524 size_t Size() const { return vregs_.Size(); }
1525
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 HEnvironment* GetParent() const { return parent_; }
1527
1528 void SetLocationAt(size_t index, Location location) {
1529 locations_.Put(index, location);
1530 }
1531
1532 Location GetLocationAt(size_t index) const {
1533 return locations_.Get(index);
1534 }
1535
1536 uint32_t GetDexPc() const {
1537 return dex_pc_;
1538 }
1539
1540 uint32_t GetMethodIdx() const {
1541 return method_idx_;
1542 }
1543
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001544 InvokeType GetInvokeType() const {
1545 return invoke_type_;
1546 }
1547
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001548 const DexFile& GetDexFile() const {
1549 return dex_file_;
1550 }
1551
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 HInstruction* GetHolder() const {
1553 return holder_;
1554 }
1555
David Brazdiled596192015-01-23 10:39:45 +00001556 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001557 // Record instructions' use entries of this environment for constant-time removal.
1558 // It should only be called by HInstruction when a new environment use is added.
1559 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1560 DCHECK(env_use->GetUser() == this);
1561 size_t index = env_use->GetIndex();
1562 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1563 }
David Brazdiled596192015-01-23 10:39:45 +00001564
David Brazdil1abb4192015-02-17 18:33:36 +00001565 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001566 GrowableArray<Location> locations_;
1567 HEnvironment* parent_;
1568 const DexFile& dex_file_;
1569 const uint32_t method_idx_;
1570 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001571 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001572
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001573 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001574 HInstruction* const holder_;
1575
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001576 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001577
1578 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1579};
1580
Calin Juravleacf735c2015-02-12 15:25:22 +00001581class ReferenceTypeInfo : ValueObject {
1582 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001583 typedef Handle<mirror::Class> TypeHandle;
1584
Calin Juravle2e768302015-07-28 14:41:11 +00001585 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1586 // The constructor will check that the type_handle is valid.
1587 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001588 }
1589
Calin Juravle2e768302015-07-28 14:41:11 +00001590 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1591
1592 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1593 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001594 }
1595
Calin Juravle2e768302015-07-28 14:41:11 +00001596 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1597 return IsValidHandle(type_handle_);
1598 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001599 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001600
1601 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1602 DCHECK(IsValid());
1603 return GetTypeHandle()->IsObjectClass();
1604 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001605 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001606 DCHECK(IsValid());
1607 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001608 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001609
1610 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1611
Mathieu Chartier90443472015-07-16 20:32:27 -07001612 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001613 DCHECK(IsValid());
1614 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001615 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1616 }
1617
1618 // Returns true if the type information provide the same amount of details.
1619 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001620 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001621 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001622 if (!IsValid() && !rti.IsValid()) {
1623 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001624 return true;
1625 }
Calin Juravle2e768302015-07-28 14:41:11 +00001626 if (!IsValid() || !rti.IsValid()) {
1627 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001628 return false;
1629 }
Calin Juravle2e768302015-07-28 14:41:11 +00001630 return IsExact() == rti.IsExact()
1631 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001632 }
1633
1634 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001635 ReferenceTypeInfo();
1636 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001637
Calin Juravleacf735c2015-02-12 15:25:22 +00001638 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001639 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001640 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001641 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001642 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001643};
1644
1645std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1646
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001647class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001648 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001649 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001650 : previous_(nullptr),
1651 next_(nullptr),
1652 block_(nullptr),
1653 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001654 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001655 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001656 locations_(nullptr),
1657 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001659 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001660 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661
Dave Allison20dfc792014-06-16 20:44:29 -07001662 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001663
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001664#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001665 enum InstructionKind {
1666 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1667 };
1668#undef DECLARE_KIND
1669
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001670 HInstruction* GetNext() const { return next_; }
1671 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001672
Calin Juravle77520bc2015-01-12 18:45:46 +00001673 HInstruction* GetNextDisregardingMoves() const;
1674 HInstruction* GetPreviousDisregardingMoves() const;
1675
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001676 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001677 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001678 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001679 bool IsInBlock() const { return block_ != nullptr; }
1680 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001681 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001682
Roland Levillain6b879dd2014-09-22 17:13:44 +01001683 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001684 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685
1686 virtual void Accept(HGraphVisitor* visitor) = 0;
1687 virtual const char* DebugName() const = 0;
1688
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001689 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001690 void SetRawInputAt(size_t index, HInstruction* input) {
1691 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1692 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001693
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001694 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 virtual uint32_t GetDexPc() const {
1696 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1697 " does not need an environment";
1698 UNREACHABLE();
1699 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001700 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001701 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001702
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001704 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705
Calin Juravle10e244f2015-01-26 18:54:32 +00001706 // Does not apply for all instructions, but having this at top level greatly
1707 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001708 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001709 virtual bool CanBeNull() const {
1710 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1711 return true;
1712 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001713
Calin Juravle641547a2015-04-21 22:08:51 +01001714 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1715 UNUSED(obj);
1716 return false;
1717 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001718
Calin Juravle2e768302015-07-28 14:41:11 +00001719 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001720
Calin Juravle61d544b2015-02-23 16:46:57 +00001721 ReferenceTypeInfo GetReferenceTypeInfo() const {
1722 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1723 return reference_type_info_;
1724 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001726 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001727 DCHECK(user != nullptr);
1728 HUseListNode<HInstruction*>* use =
1729 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1730 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001731 }
1732
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001733 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001734 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001735 HUseListNode<HEnvironment*>* env_use =
1736 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1737 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001738 }
1739
David Brazdil1abb4192015-02-17 18:33:36 +00001740 void RemoveAsUserOfInput(size_t input) {
1741 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1742 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1743 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001744
David Brazdil1abb4192015-02-17 18:33:36 +00001745 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1746 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001747
David Brazdiled596192015-01-23 10:39:45 +00001748 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1749 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001750 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001751 bool HasOnlyOneNonEnvironmentUse() const {
1752 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1753 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001754
Roland Levillain6c82d402014-10-13 16:10:27 +01001755 // Does this instruction strictly dominate `other_instruction`?
1756 // Returns false if this instruction and `other_instruction` are the same.
1757 // Aborts if this instruction and `other_instruction` are both phis.
1758 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001759
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001760 int GetId() const { return id_; }
1761 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001762
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001763 int GetSsaIndex() const { return ssa_index_; }
1764 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1765 bool HasSsaIndex() const { return ssa_index_ != -1; }
1766
1767 bool HasEnvironment() const { return environment_ != nullptr; }
1768 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001769 // Set the `environment_` field. Raw because this method does not
1770 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001771 void SetRawEnvironment(HEnvironment* environment) {
1772 DCHECK(environment_ == nullptr);
1773 DCHECK_EQ(environment->GetHolder(), this);
1774 environment_ = environment;
1775 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001776
1777 // Set the environment of this instruction, copying it from `environment`. While
1778 // copying, the uses lists are being updated.
1779 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001780 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001781 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001782 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001783 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 if (environment->GetParent() != nullptr) {
1785 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1786 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001787 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001788
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001789 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1790 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001791 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001792 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001793 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001794 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 if (environment->GetParent() != nullptr) {
1796 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1797 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001798 }
1799
Nicolas Geoffray39468442014-09-02 15:17:15 +01001800 // Returns the number of entries in the environment. Typically, that is the
1801 // number of dex registers in a method. It could be more in case of inlining.
1802 size_t EnvironmentSize() const;
1803
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 LocationSummary* GetLocations() const { return locations_; }
1805 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001807 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001808 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001809
Alexandre Rames188d4312015-04-09 18:30:21 +01001810 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1811 // uses of this instruction by `other` are *not* updated.
1812 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1813 ReplaceWith(other);
1814 other->ReplaceInput(this, use_index);
1815 }
1816
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001817 // Move `this` instruction before `cursor`.
1818 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001819
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001820#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001821 bool Is##type() const { return (As##type() != nullptr); } \
1822 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823 virtual H##type* As##type() { return nullptr; }
1824
1825 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1826#undef INSTRUCTION_TYPE_CHECK
1827
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001828 // Returns whether the instruction can be moved within the graph.
1829 virtual bool CanBeMoved() const { return false; }
1830
1831 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001832 virtual bool InstructionTypeEquals(HInstruction* other) const {
1833 UNUSED(other);
1834 return false;
1835 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001836
1837 // Returns whether any data encoded in the two instructions is equal.
1838 // This method does not look at the inputs. Both instructions must be
1839 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001840 virtual bool InstructionDataEquals(HInstruction* other) const {
1841 UNUSED(other);
1842 return false;
1843 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844
1845 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001846 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847 // 2) Their inputs are identical.
1848 bool Equals(HInstruction* other) const;
1849
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001850 virtual InstructionKind GetKind() const = 0;
1851
1852 virtual size_t ComputeHashCode() const {
1853 size_t result = GetKind();
1854 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1855 result = (result * 31) + InputAt(i)->GetId();
1856 }
1857 return result;
1858 }
1859
1860 SideEffects GetSideEffects() const { return side_effects_; }
1861
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001862 size_t GetLifetimePosition() const { return lifetime_position_; }
1863 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1864 LiveInterval* GetLiveInterval() const { return live_interval_; }
1865 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1866 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1867
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001868 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1869
1870 // Returns whether the code generation of the instruction will require to have access
1871 // to the current method. Such instructions are:
1872 // (1): Instructions that require an environment, as calling the runtime requires
1873 // to walk the stack and have the current method stored at a specific stack address.
1874 // (2): Object literals like classes and strings, that are loaded from the dex cache
1875 // fields of the current method.
1876 bool NeedsCurrentMethod() const {
1877 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1878 }
1879
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001880 virtual bool NeedsDexCache() const { return false; }
1881
Mark Mendellc4701932015-04-10 13:18:51 -04001882 // Does this instruction have any use in an environment before
1883 // control flow hits 'other'?
1884 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1885
1886 // Remove all references to environment uses of this instruction.
1887 // The caller must ensure that this is safe to do.
1888 void RemoveEnvironmentUsers();
1889
David Brazdil1abb4192015-02-17 18:33:36 +00001890 protected:
1891 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1892 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001894 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001895 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1896
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897 HInstruction* previous_;
1898 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001899 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901 // An instruction gets an id when it is added to the graph.
1902 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904 int id_;
1905
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001906 // When doing liveness analysis, instructions that have uses get an SSA index.
1907 int ssa_index_;
1908
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001910 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911
1912 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001913 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 // The environment associated with this instruction. Not null if the instruction
1916 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001919 // Set by the code generator.
1920 LocationSummary* locations_;
1921
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001922 // Set by the liveness analysis.
1923 LiveInterval* live_interval_;
1924
1925 // Set by the liveness analysis, this is the position in a linear
1926 // order of blocks where this instruction's live interval start.
1927 size_t lifetime_position_;
1928
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001929 const SideEffects side_effects_;
1930
Calin Juravleacf735c2015-02-12 15:25:22 +00001931 // TODO: for primitive types this should be marked as invalid.
1932 ReferenceTypeInfo reference_type_info_;
1933
David Brazdil1abb4192015-02-17 18:33:36 +00001934 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001935 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001936 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001937 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001938 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001939
1940 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1941};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001942std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944class HInputIterator : public ValueObject {
1945 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001946 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947
1948 bool Done() const { return index_ == instruction_->InputCount(); }
1949 HInstruction* Current() const { return instruction_->InputAt(index_); }
1950 void Advance() { index_++; }
1951
1952 private:
1953 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955
1956 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1957};
1958
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001959class HInstructionIterator : public ValueObject {
1960 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 explicit HInstructionIterator(const HInstructionList& instructions)
1962 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001963 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001964 }
1965
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966 bool Done() const { return instruction_ == nullptr; }
1967 HInstruction* Current() const { return instruction_; }
1968 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001969 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001970 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001971 }
1972
1973 private:
1974 HInstruction* instruction_;
1975 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001976
1977 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001978};
1979
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001980class HBackwardInstructionIterator : public ValueObject {
1981 public:
1982 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1983 : instruction_(instructions.last_instruction_) {
1984 next_ = Done() ? nullptr : instruction_->GetPrevious();
1985 }
1986
1987 bool Done() const { return instruction_ == nullptr; }
1988 HInstruction* Current() const { return instruction_; }
1989 void Advance() {
1990 instruction_ = next_;
1991 next_ = Done() ? nullptr : instruction_->GetPrevious();
1992 }
1993
1994 private:
1995 HInstruction* instruction_;
1996 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001997
1998 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001999};
2000
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002001// An embedded container with N elements of type T. Used (with partial
2002// specialization for N=0) because embedded arrays cannot have size 0.
2003template<typename T, intptr_t N>
2004class EmbeddedArray {
2005 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002006 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002008 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002009
2010 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002011 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 return elements_[i];
2013 }
2014
2015 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002016 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 return elements_[i];
2018 }
2019
2020 const T& At(intptr_t i) const {
2021 return (*this)[i];
2022 }
2023
2024 void SetAt(intptr_t i, const T& val) {
2025 (*this)[i] = val;
2026 }
2027
2028 private:
2029 T elements_[N];
2030};
2031
2032template<typename T>
2033class EmbeddedArray<T, 0> {
2034 public:
2035 intptr_t length() const { return 0; }
2036 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002037 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002038 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002039 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002040 }
2041 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002042 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002044 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045 }
2046};
2047
2048template<intptr_t N>
2049class HTemplateInstruction: public HInstruction {
2050 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051 HTemplateInstruction<N>(SideEffects side_effects)
2052 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002053 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002057 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002058 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2059
2060 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2061 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062 }
2063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002065 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066
2067 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068};
2069
Dave Allison20dfc792014-06-16 20:44:29 -07002070template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002071class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002072 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002073 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2074 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002075 virtual ~HExpression() {}
2076
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002077 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002078
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002079 protected:
2080 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002081};
2082
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2084// instruction that branches to the exit block.
2085class HReturnVoid : public HTemplateInstruction<0> {
2086 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002087 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002088
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002089 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002091 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092
2093 private:
2094 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2095};
2096
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002097// Represents dex's RETURN opcodes. A HReturn is a control flow
2098// instruction that branches to the exit block.
2099class HReturn : public HTemplateInstruction<1> {
2100 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002101 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102 SetRawInputAt(0, value);
2103 }
2104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002105 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002106
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002107 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002108
2109 private:
2110 DISALLOW_COPY_AND_ASSIGN(HReturn);
2111};
2112
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002114// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115// exit block.
2116class HExit : public HTemplateInstruction<0> {
2117 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002122 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123
2124 private:
2125 DISALLOW_COPY_AND_ASSIGN(HExit);
2126};
2127
2128// Jumps from one block to another.
2129class HGoto : public HTemplateInstruction<0> {
2130 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002131 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2132
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002133 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002134
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002135 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002136 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002137 }
2138
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002139 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140
2141 private:
2142 DISALLOW_COPY_AND_ASSIGN(HGoto);
2143};
2144
Roland Levillain9867bc72015-08-05 10:21:34 +01002145class HConstant : public HExpression<0> {
2146 public:
2147 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2148
2149 bool CanBeMoved() const OVERRIDE { return true; }
2150
2151 virtual bool IsMinusOne() const { return false; }
2152 virtual bool IsZero() const { return false; }
2153 virtual bool IsOne() const { return false; }
2154
2155 DECLARE_INSTRUCTION(Constant);
2156
2157 private:
2158 DISALLOW_COPY_AND_ASSIGN(HConstant);
2159};
2160
2161class HNullConstant : public HConstant {
2162 public:
2163 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2164 return true;
2165 }
2166
2167 size_t ComputeHashCode() const OVERRIDE { return 0; }
2168
2169 DECLARE_INSTRUCTION(NullConstant);
2170
2171 private:
2172 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2173
2174 friend class HGraph;
2175 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2176};
2177
2178// Constants of the type int. Those can be from Dex instructions, or
2179// synthesized (for example with the if-eqz instruction).
2180class HIntConstant : public HConstant {
2181 public:
2182 int32_t GetValue() const { return value_; }
2183
2184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2185 DCHECK(other->IsIntConstant());
2186 return other->AsIntConstant()->value_ == value_;
2187 }
2188
2189 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2190
2191 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2192 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2193 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2194
2195 DECLARE_INSTRUCTION(IntConstant);
2196
2197 private:
2198 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2199 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2200
2201 const int32_t value_;
2202
2203 friend class HGraph;
2204 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2205 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2206 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2207};
2208
2209class HLongConstant : public HConstant {
2210 public:
2211 int64_t GetValue() const { return value_; }
2212
2213 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2214 DCHECK(other->IsLongConstant());
2215 return other->AsLongConstant()->value_ == value_;
2216 }
2217
2218 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2219
2220 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2221 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2222 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2223
2224 DECLARE_INSTRUCTION(LongConstant);
2225
2226 private:
2227 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2228
2229 const int64_t value_;
2230
2231 friend class HGraph;
2232 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2233};
Dave Allison20dfc792014-06-16 20:44:29 -07002234
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002235// Conditional branch. A block ending with an HIf instruction must have
2236// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002237class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002238 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002239 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002240 SetRawInputAt(0, input);
2241 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002242
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002243 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002244
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002245 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002246 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247 }
2248
2249 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002250 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002251 }
2252
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002253 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HIf);
2257};
2258
David Brazdilfc6a86a2015-06-26 10:33:45 +00002259
2260// Abstract instruction which marks the beginning and/or end of a try block and
2261// links it to the respective exception handlers. Behaves the same as a Goto in
2262// non-exceptional control flow.
2263// Normal-flow successor is stored at index zero, exception handlers under
2264// higher indices in no particular order.
2265class HTryBoundary : public HTemplateInstruction<0> {
2266 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002267 enum BoundaryKind {
2268 kEntry,
2269 kExit,
2270 };
2271
2272 explicit HTryBoundary(BoundaryKind kind)
2273 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002274
2275 bool IsControlFlow() const OVERRIDE { return true; }
2276
2277 // Returns the block's non-exceptional successor (index zero).
2278 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2279
2280 // Returns whether `handler` is among its exception handlers (non-zero index
2281 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002282 bool HasExceptionHandler(const HBasicBlock& handler) const {
2283 DCHECK(handler.IsCatchBlock());
2284 return GetBlock()->GetSuccessors().Contains(
2285 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002286 }
2287
2288 // If not present already, adds `handler` to its block's list of exception
2289 // handlers.
2290 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002291 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002292 GetBlock()->AddSuccessor(handler);
2293 }
2294 }
2295
David Brazdil56e1acc2015-06-30 15:41:36 +01002296 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002297
David Brazdilffee3d32015-07-06 11:48:53 +01002298 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2299
David Brazdilfc6a86a2015-06-26 10:33:45 +00002300 DECLARE_INSTRUCTION(TryBoundary);
2301
2302 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002303 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002304
2305 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2306};
2307
David Brazdilffee3d32015-07-06 11:48:53 +01002308// Iterator over exception handlers of a given HTryBoundary, i.e. over
2309// exceptional successors of its basic block.
2310class HExceptionHandlerIterator : public ValueObject {
2311 public:
2312 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2313 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2314
2315 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2316 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2317 size_t CurrentSuccessorIndex() const { return index_; }
2318 void Advance() { ++index_; }
2319
2320 private:
2321 const HBasicBlock& block_;
2322 size_t index_;
2323
2324 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2325};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002327// Deoptimize to interpreter, upon checking a condition.
2328class HDeoptimize : public HTemplateInstruction<1> {
2329 public:
2330 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2331 : HTemplateInstruction(SideEffects::None()),
2332 dex_pc_(dex_pc) {
2333 SetRawInputAt(0, cond);
2334 }
2335
2336 bool NeedsEnvironment() const OVERRIDE { return true; }
2337 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002338 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002339
2340 DECLARE_INSTRUCTION(Deoptimize);
2341
2342 private:
2343 uint32_t dex_pc_;
2344
2345 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2346};
2347
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002348// Represents the ArtMethod that was passed as a first argument to
2349// the method. It is used by instructions that depend on it, like
2350// instructions that work with the dex cache.
2351class HCurrentMethod : public HExpression<0> {
2352 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002354
2355 DECLARE_INSTRUCTION(CurrentMethod);
2356
2357 private:
2358 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2359};
2360
Roland Levillain88cb1752014-10-20 16:36:47 +01002361class HUnaryOperation : public HExpression<1> {
2362 public:
2363 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2364 : HExpression(result_type, SideEffects::None()) {
2365 SetRawInputAt(0, input);
2366 }
2367
2368 HInstruction* GetInput() const { return InputAt(0); }
2369 Primitive::Type GetResultType() const { return GetType(); }
2370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002371 bool CanBeMoved() const OVERRIDE { return true; }
2372 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002373 UNUSED(other);
2374 return true;
2375 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002376
Roland Levillain9240d6a2014-10-20 16:47:04 +01002377 // Try to statically evaluate `operation` and return a HConstant
2378 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002379 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002380 HConstant* TryStaticEvaluation() const;
2381
2382 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002383 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2384 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002385
Roland Levillain88cb1752014-10-20 16:36:47 +01002386 DECLARE_INSTRUCTION(UnaryOperation);
2387
2388 private:
2389 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2390};
2391
Dave Allison20dfc792014-06-16 20:44:29 -07002392class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002393 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002394 HBinaryOperation(Primitive::Type result_type,
2395 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002396 HInstruction* right,
2397 SideEffects side_effects = SideEffects::None())
2398 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002399 SetRawInputAt(0, left);
2400 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002401 }
2402
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002403 HInstruction* GetLeft() const { return InputAt(0); }
2404 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002405 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002406
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002407 virtual bool IsCommutative() const { return false; }
2408
2409 // Put constant on the right.
2410 // Returns whether order is changed.
2411 bool OrderInputsWithConstantOnTheRight() {
2412 HInstruction* left = InputAt(0);
2413 HInstruction* right = InputAt(1);
2414 if (left->IsConstant() && !right->IsConstant()) {
2415 ReplaceInput(right, 0);
2416 ReplaceInput(left, 1);
2417 return true;
2418 }
2419 return false;
2420 }
2421
2422 // Order inputs by instruction id, but favor constant on the right side.
2423 // This helps GVN for commutative ops.
2424 void OrderInputs() {
2425 DCHECK(IsCommutative());
2426 HInstruction* left = InputAt(0);
2427 HInstruction* right = InputAt(1);
2428 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2429 return;
2430 }
2431 if (OrderInputsWithConstantOnTheRight()) {
2432 return;
2433 }
2434 // Order according to instruction id.
2435 if (left->GetId() > right->GetId()) {
2436 ReplaceInput(right, 0);
2437 ReplaceInput(left, 1);
2438 }
2439 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002441 bool CanBeMoved() const OVERRIDE { return true; }
2442 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002443 UNUSED(other);
2444 return true;
2445 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002446
Roland Levillain9240d6a2014-10-20 16:47:04 +01002447 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002448 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002449 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002450 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002451
2452 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002453 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2454 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2455 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2456 HLongConstant* y ATTRIBUTE_UNUSED) const {
2457 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2458 return nullptr;
2459 }
2460 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2461 HIntConstant* y ATTRIBUTE_UNUSED) const {
2462 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2463 return nullptr;
2464 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002465
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002466 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002467 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002468 HConstant* GetConstantRight() const;
2469
2470 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002471 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002472 HInstruction* GetLeastConstantLeft() const;
2473
Roland Levillainccc07a92014-09-16 14:48:16 +01002474 DECLARE_INSTRUCTION(BinaryOperation);
2475
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002476 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002477 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2478};
2479
Mark Mendellc4701932015-04-10 13:18:51 -04002480// The comparison bias applies for floating point operations and indicates how NaN
2481// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002482enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002483 kNoBias, // bias is not applicable (i.e. for long operation)
2484 kGtBias, // return 1 for NaN comparisons
2485 kLtBias, // return -1 for NaN comparisons
2486};
2487
Dave Allison20dfc792014-06-16 20:44:29 -07002488class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002489 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002490 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002491 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002492 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002493 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002494
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002495 bool NeedsMaterialization() const { return needs_materialization_; }
2496 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002497
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002498 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002499 // `instruction`, and disregard moves in between.
2500 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002501
Dave Allison20dfc792014-06-16 20:44:29 -07002502 DECLARE_INSTRUCTION(Condition);
2503
2504 virtual IfCondition GetCondition() const = 0;
2505
Mark Mendellc4701932015-04-10 13:18:51 -04002506 virtual IfCondition GetOppositeCondition() const = 0;
2507
Roland Levillain4fa13f62015-07-06 18:11:54 +01002508 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002509
2510 void SetBias(ComparisonBias bias) { bias_ = bias; }
2511
2512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2513 return bias_ == other->AsCondition()->bias_;
2514 }
2515
Roland Levillain4fa13f62015-07-06 18:11:54 +01002516 bool IsFPConditionTrueIfNaN() const {
2517 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2518 IfCondition if_cond = GetCondition();
2519 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2520 }
2521
2522 bool IsFPConditionFalseIfNaN() const {
2523 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2524 IfCondition if_cond = GetCondition();
2525 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2526 }
2527
Dave Allison20dfc792014-06-16 20:44:29 -07002528 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002529 // For register allocation purposes, returns whether this instruction needs to be
2530 // materialized (that is, not just be in the processor flags).
2531 bool needs_materialization_;
2532
Mark Mendellc4701932015-04-10 13:18:51 -04002533 // Needed if we merge a HCompare into a HCondition.
2534 ComparisonBias bias_;
2535
Dave Allison20dfc792014-06-16 20:44:29 -07002536 DISALLOW_COPY_AND_ASSIGN(HCondition);
2537};
2538
2539// Instruction to check if two inputs are equal to each other.
2540class HEqual : public HCondition {
2541 public:
2542 HEqual(HInstruction* first, HInstruction* second)
2543 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002544
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002545 bool IsCommutative() const OVERRIDE { return true; }
2546
Roland Levillain9867bc72015-08-05 10:21:34 +01002547 template <typename T> bool Compute(T x, T y) const { return x == y; }
2548
2549 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2550 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002551 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2553 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002554 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002555
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002556 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002558 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002559 return kCondEQ;
2560 }
2561
Mark Mendellc4701932015-04-10 13:18:51 -04002562 IfCondition GetOppositeCondition() const OVERRIDE {
2563 return kCondNE;
2564 }
2565
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566 private:
2567 DISALLOW_COPY_AND_ASSIGN(HEqual);
2568};
2569
Dave Allison20dfc792014-06-16 20:44:29 -07002570class HNotEqual : public HCondition {
2571 public:
2572 HNotEqual(HInstruction* first, HInstruction* second)
2573 : HCondition(first, second) {}
2574
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002575 bool IsCommutative() const OVERRIDE { return true; }
2576
Roland Levillain9867bc72015-08-05 10:21:34 +01002577 template <typename T> bool Compute(T x, T y) const { return x != y; }
2578
2579 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2580 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002581 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002582 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2583 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002584 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002585
Dave Allison20dfc792014-06-16 20:44:29 -07002586 DECLARE_INSTRUCTION(NotEqual);
2587
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002588 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002589 return kCondNE;
2590 }
2591
Mark Mendellc4701932015-04-10 13:18:51 -04002592 IfCondition GetOppositeCondition() const OVERRIDE {
2593 return kCondEQ;
2594 }
2595
Dave Allison20dfc792014-06-16 20:44:29 -07002596 private:
2597 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2598};
2599
2600class HLessThan : public HCondition {
2601 public:
2602 HLessThan(HInstruction* first, HInstruction* second)
2603 : HCondition(first, second) {}
2604
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 template <typename T> bool Compute(T x, T y) const { return x < y; }
2606
2607 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2608 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002609 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2611 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002612 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002613
Dave Allison20dfc792014-06-16 20:44:29 -07002614 DECLARE_INSTRUCTION(LessThan);
2615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002616 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002617 return kCondLT;
2618 }
2619
Mark Mendellc4701932015-04-10 13:18:51 -04002620 IfCondition GetOppositeCondition() const OVERRIDE {
2621 return kCondGE;
2622 }
2623
Dave Allison20dfc792014-06-16 20:44:29 -07002624 private:
2625 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2626};
2627
2628class HLessThanOrEqual : public HCondition {
2629 public:
2630 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2631 : HCondition(first, second) {}
2632
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2634
2635 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2636 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002637 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002638 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2639 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002640 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002641
Dave Allison20dfc792014-06-16 20:44:29 -07002642 DECLARE_INSTRUCTION(LessThanOrEqual);
2643
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002644 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002645 return kCondLE;
2646 }
2647
Mark Mendellc4701932015-04-10 13:18:51 -04002648 IfCondition GetOppositeCondition() const OVERRIDE {
2649 return kCondGT;
2650 }
2651
Dave Allison20dfc792014-06-16 20:44:29 -07002652 private:
2653 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2654};
2655
2656class HGreaterThan : public HCondition {
2657 public:
2658 HGreaterThan(HInstruction* first, HInstruction* second)
2659 : HCondition(first, second) {}
2660
Roland Levillain9867bc72015-08-05 10:21:34 +01002661 template <typename T> bool Compute(T x, T y) const { return x > y; }
2662
2663 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2664 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002665 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2667 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002669
Dave Allison20dfc792014-06-16 20:44:29 -07002670 DECLARE_INSTRUCTION(GreaterThan);
2671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002672 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002673 return kCondGT;
2674 }
2675
Mark Mendellc4701932015-04-10 13:18:51 -04002676 IfCondition GetOppositeCondition() const OVERRIDE {
2677 return kCondLE;
2678 }
2679
Dave Allison20dfc792014-06-16 20:44:29 -07002680 private:
2681 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2682};
2683
2684class HGreaterThanOrEqual : public HCondition {
2685 public:
2686 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2687 : HCondition(first, second) {}
2688
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2690
2691 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2692 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2695 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Dave Allison20dfc792014-06-16 20:44:29 -07002698 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002701 return kCondGE;
2702 }
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 IfCondition GetOppositeCondition() const OVERRIDE {
2705 return kCondLT;
2706 }
2707
Dave Allison20dfc792014-06-16 20:44:29 -07002708 private:
2709 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2710};
2711
2712
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002713// Instruction to check how two inputs compare to each other.
2714// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2715class HCompare : public HBinaryOperation {
2716 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002717 HCompare(Primitive::Type type,
2718 HInstruction* first,
2719 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002720 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002722 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2723 bias_(bias),
2724 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002725 DCHECK_EQ(type, first->GetType());
2726 DCHECK_EQ(type, second->GetType());
2727 }
2728
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 template <typename T>
2730 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002731
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2733 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2734 }
2735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2736 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002737 }
2738
Calin Juravleddb7df22014-11-25 20:56:51 +00002739 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2740 return bias_ == other->AsCompare()->bias_;
2741 }
2742
Mark Mendellc4701932015-04-10 13:18:51 -04002743 ComparisonBias GetBias() const { return bias_; }
2744
Roland Levillain4fa13f62015-07-06 18:11:54 +01002745 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002746
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002747 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2748
2749 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2750 // MIPS64 uses a runtime call for FP comparisons.
2751 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2752 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002754 DECLARE_INSTRUCTION(Compare);
2755
2756 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002757 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002758 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002759
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002760 DISALLOW_COPY_AND_ASSIGN(HCompare);
2761};
2762
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002763// A local in the graph. Corresponds to a Dex register.
2764class HLocal : public HTemplateInstruction<0> {
2765 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002766 explicit HLocal(uint16_t reg_number)
2767 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002768
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002769 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002770
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002771 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002772
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002774 // The Dex register number.
2775 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002776
2777 DISALLOW_COPY_AND_ASSIGN(HLocal);
2778};
2779
2780// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002781class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002782 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002783 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002784 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002785 SetRawInputAt(0, local);
2786 }
2787
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002788 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2789
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002790 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002791
2792 private:
2793 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2794};
2795
2796// Store a value in a given local. This instruction has two inputs: the value
2797// and the local.
2798class HStoreLocal : public HTemplateInstruction<2> {
2799 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002800 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002801 SetRawInputAt(0, local);
2802 SetRawInputAt(1, value);
2803 }
2804
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002805 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2806
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002807 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002808
2809 private:
2810 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2811};
2812
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002813class HFloatConstant : public HConstant {
2814 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002815 float GetValue() const { return value_; }
2816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002818 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002819 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2820 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002821 }
2822
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002823 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002824
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002825 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002826 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002827 }
2828 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002829 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002830 }
2831 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002832 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2833 }
2834 bool IsNaN() const {
2835 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002836 }
2837
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002838 DECLARE_INSTRUCTION(FloatConstant);
2839
2840 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002841 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002842 explicit HFloatConstant(int32_t value)
2843 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002844
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002845 const float value_;
2846
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002847 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002848 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002849 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002850 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2851};
2852
2853class HDoubleConstant : public HConstant {
2854 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002855 double GetValue() const { return value_; }
2856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002857 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002859 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2860 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 }
2862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002863 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002864
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002865 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002866 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002867 }
2868 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002869 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002870 }
2871 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002872 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2873 }
2874 bool IsNaN() const {
2875 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002876 }
2877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 DECLARE_INSTRUCTION(DoubleConstant);
2879
2880 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002881 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002882 explicit HDoubleConstant(int64_t value)
2883 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002884
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002885 const double value_;
2886
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002887 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002888 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002889 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002890 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2891};
2892
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002893enum class Intrinsics {
agicsaki57b81ec2015-08-11 17:39:37 -07002894#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002895#include "intrinsics_list.h"
2896 kNone,
2897 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2898#undef INTRINSICS_LIST
2899#undef OPTIMIZING_INTRINSICS
2900};
2901std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2902
agicsaki57b81ec2015-08-11 17:39:37 -07002903enum IntrinsicNeedsEnvironment {
2904 kNoEnvironment, // Intrinsic does not require an environment.
2905 kNeedsEnvironment // Intrinsic requires an environment.
2906};
2907
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002908class HInvoke : public HInstruction {
2909 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002910 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002911
2912 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2913 // know their environment.
agicsaki57b81ec2015-08-11 17:39:37 -07002914 bool NeedsEnvironment() const OVERRIDE { return needs_environment_ == kNeedsEnvironment; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002915
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002916 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002917 SetRawInputAt(index, argument);
2918 }
2919
Roland Levillain3e3d7332015-04-28 11:00:54 +01002920 // Return the number of arguments. This number can be lower than
2921 // the number of inputs returned by InputCount(), as some invoke
2922 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2923 // inputs at the end of their list of inputs.
2924 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002926 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002928 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002929
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002930 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002931 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002932
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002933 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2934
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002935 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002936 return intrinsic_;
2937 }
2938
agicsaki57b81ec2015-08-11 17:39:37 -07002939 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironment needs_environment) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002940 intrinsic_ = intrinsic;
agicsaki57b81ec2015-08-11 17:39:37 -07002941 needs_environment_ = needs_environment;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002942 }
2943
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002944 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002945 return GetEnvironment()->GetParent() != nullptr;
2946 }
2947
2948 bool CanThrow() const OVERRIDE { return true; }
2949
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002950 DECLARE_INSTRUCTION(Invoke);
2951
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002952 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002953 HInvoke(ArenaAllocator* arena,
2954 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002955 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002956 Primitive::Type return_type,
2957 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002958 uint32_t dex_method_index,
2959 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002960 : HInstruction(
2961 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002962 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002963 inputs_(arena, number_of_arguments),
2964 return_type_(return_type),
2965 dex_pc_(dex_pc),
2966 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002967 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002968 intrinsic_(Intrinsics::kNone),
2969 needs_environment_(kNeedsEnvironment) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002970 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2971 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002972 }
2973
David Brazdil1abb4192015-02-17 18:33:36 +00002974 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2975 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2976 inputs_.Put(index, input);
2977 }
2978
Roland Levillain3e3d7332015-04-28 11:00:54 +01002979 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002980 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002981 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002982 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002983 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002984 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002985 Intrinsics intrinsic_;
agicsaki57b81ec2015-08-11 17:39:37 -07002986 IntrinsicNeedsEnvironment needs_environment_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002987
2988 private:
2989 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2990};
2991
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002992class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002993 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002994 // Requirements of this method call regarding the class
2995 // initialization (clinit) check of its declaring class.
2996 enum class ClinitCheckRequirement {
2997 kNone, // Class already initialized.
2998 kExplicit, // Static call having explicit clinit check as last input.
2999 kImplicit, // Static call implicitly requiring a clinit check.
3000 };
3001
Vladimir Marko9b688a02015-05-06 14:12:42 +01003002 // Determines how to load the target ArtMethod*.
3003 enum class MethodLoadKind {
3004 // Use a String init ArtMethod* loaded from Thread entrypoints.
3005 kStringInit,
3006
3007 // Use the method's own ArtMethod* loaded by the register allocator.
3008 kRecursive,
3009
3010 // Use ArtMethod* at a known address, embed the direct address in the code.
3011 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3012 kDirectAddress,
3013
3014 // Use ArtMethod* at an address that will be known at link time, embed the direct
3015 // address in the code. If the image is relocatable, emit .patch_oat entry.
3016 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3017 // the image relocatable or not.
3018 kDirectAddressWithFixup,
3019
3020 // Load from resoved methods array in the dex cache using a PC-relative load.
3021 // Used when we need to use the dex cache, for example for invoke-static that
3022 // may cause class initialization (the entry may point to a resolution method),
3023 // and we know that we can access the dex cache arrays using a PC-relative load.
3024 kDexCachePcRelative,
3025
3026 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3027 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3028 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3029 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3030 kDexCacheViaMethod,
3031 };
3032
3033 // Determines the location of the code pointer.
3034 enum class CodePtrLocation {
3035 // Recursive call, use local PC-relative call instruction.
3036 kCallSelf,
3037
3038 // Use PC-relative call instruction patched at link time.
3039 // Used for calls within an oat file, boot->boot or app->app.
3040 kCallPCRelative,
3041
3042 // Call to a known target address, embed the direct address in code.
3043 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3044 kCallDirect,
3045
3046 // Call to a target address that will be known at link time, embed the direct
3047 // address in code. If the image is relocatable, emit .patch_oat entry.
3048 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3049 // the image relocatable or not.
3050 kCallDirectWithFixup,
3051
3052 // Use code pointer from the ArtMethod*.
3053 // Used when we don't know the target code. This is also the last-resort-kind used when
3054 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3055 kCallArtMethod,
3056 };
3057
3058 struct DispatchInfo {
3059 const MethodLoadKind method_load_kind;
3060 const CodePtrLocation code_ptr_location;
3061 // The method load data holds
3062 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3063 // Note that there are multiple string init methods, each having its own offset.
3064 // - the method address for kDirectAddress
3065 // - the dex cache arrays offset for kDexCachePcRel.
3066 const uint64_t method_load_data;
3067 const uint64_t direct_code_ptr;
3068 };
3069
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003070 HInvokeStaticOrDirect(ArenaAllocator* arena,
3071 uint32_t number_of_arguments,
3072 Primitive::Type return_type,
3073 uint32_t dex_pc,
Vladimir Marko9b688a02015-05-06 14:12:42 +01003074 uint32_t method_index,
3075 MethodReference target_method,
3076 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003077 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003078 InvokeType invoke_type,
3079 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003080 : HInvoke(arena,
3081 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003082 // There is one extra argument for the HCurrentMethod node, and
3083 // potentially one other if the clinit check is explicit, and one other
3084 // if the method is a string factory.
3085 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko9b688a02015-05-06 14:12:42 +01003086 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003087 return_type,
3088 dex_pc,
Vladimir Marko9b688a02015-05-06 14:12:42 +01003089 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003090 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003091 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003092 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko9b688a02015-05-06 14:12:42 +01003093 target_method_(target_method),
3094 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003095
Calin Juravle641547a2015-04-21 22:08:51 +01003096 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3097 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003098 // We access the method via the dex cache so we can't do an implicit null check.
3099 // TODO: for intrinsics we can generate implicit null checks.
3100 return false;
3101 }
3102
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003103 bool CanBeNull() const OVERRIDE {
3104 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3105 }
3106
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003107 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko9b688a02015-05-06 14:12:42 +01003108 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3109 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3110 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
3111 bool NeedsDexCache() const OVERRIDE { return !IsRecursive() && !IsStringInit(); }
3112 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003113 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko9b688a02015-05-06 14:12:42 +01003114 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3115 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3116 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3117 MethodReference GetTargetMethod() const { return target_method_; }
3118
3119 int32_t GetStringInitOffset() const {
3120 DCHECK(IsStringInit());
3121 return dispatch_info_.method_load_data;
3122 }
3123
3124 uint64_t GetMethodAddress() const {
3125 DCHECK(HasMethodAddress());
3126 return dispatch_info_.method_load_data;
3127 }
3128
3129 uint32_t GetDexCacheArrayOffset() const {
3130 DCHECK(HasPcRelDexCache());
3131 return dispatch_info_.method_load_data;
3132 }
3133
3134 uint64_t GetDirectCodePtr() const {
3135 DCHECK(HasDirectCodePtr());
3136 return dispatch_info_.direct_code_ptr;
3137 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003138
Roland Levillain4c0eb422015-04-24 16:43:49 +01003139 // Is this instruction a call to a static method?
3140 bool IsStatic() const {
3141 return GetInvokeType() == kStatic;
3142 }
3143
Roland Levillain3e3d7332015-04-28 11:00:54 +01003144 // Remove the art::HLoadClass instruction set as last input by
3145 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3146 // the initial art::HClinitCheck instruction (only relevant for
3147 // static calls with explicit clinit check).
3148 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003149 DCHECK(IsStaticWithExplicitClinitCheck());
3150 size_t last_input_index = InputCount() - 1;
3151 HInstruction* last_input = InputAt(last_input_index);
3152 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003153 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003154 RemoveAsUserOfInput(last_input_index);
3155 inputs_.DeleteAt(last_input_index);
3156 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3157 DCHECK(IsStaticWithImplicitClinitCheck());
3158 }
3159
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003160 bool IsStringFactoryFor(HFakeString* str) const {
3161 if (!IsStringInit()) return false;
3162 // +1 for the current method.
3163 if (InputCount() == (number_of_arguments_ + 1)) return false;
3164 return InputAt(InputCount() - 1)->AsFakeString() == str;
3165 }
3166
3167 void RemoveFakeStringArgumentAsLastInput() {
3168 DCHECK(IsStringInit());
3169 size_t last_input_index = InputCount() - 1;
3170 HInstruction* last_input = InputAt(last_input_index);
3171 DCHECK(last_input != nullptr);
3172 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3173 RemoveAsUserOfInput(last_input_index);
3174 inputs_.DeleteAt(last_input_index);
3175 }
3176
Roland Levillain4c0eb422015-04-24 16:43:49 +01003177 // Is this a call to a static method whose declaring class has an
3178 // explicit intialization check in the graph?
3179 bool IsStaticWithExplicitClinitCheck() const {
3180 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3181 }
3182
3183 // Is this a call to a static method whose declaring class has an
3184 // implicit intialization check requirement?
3185 bool IsStaticWithImplicitClinitCheck() const {
3186 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3187 }
3188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003189 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003190
Roland Levillain4c0eb422015-04-24 16:43:49 +01003191 protected:
3192 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3193 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3194 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3195 HInstruction* input = input_record.GetInstruction();
3196 // `input` is the last input of a static invoke marked as having
3197 // an explicit clinit check. It must either be:
3198 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3199 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3200 DCHECK(input != nullptr);
3201 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3202 }
3203 return input_record;
3204 }
3205
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003206 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003207 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003208 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003209 // The target method may refer to different dex file or method index than the original
3210 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3211 // in derived class to increase visibility.
3212 MethodReference target_method_;
3213 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003215 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003216};
3217
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003218class HInvokeVirtual : public HInvoke {
3219 public:
3220 HInvokeVirtual(ArenaAllocator* arena,
3221 uint32_t number_of_arguments,
3222 Primitive::Type return_type,
3223 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003224 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003225 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003226 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003227 vtable_index_(vtable_index) {}
3228
Calin Juravle641547a2015-04-21 22:08:51 +01003229 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003230 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003231 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003232 }
3233
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003234 uint32_t GetVTableIndex() const { return vtable_index_; }
3235
3236 DECLARE_INSTRUCTION(InvokeVirtual);
3237
3238 private:
3239 const uint32_t vtable_index_;
3240
3241 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3242};
3243
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003244class HInvokeInterface : public HInvoke {
3245 public:
3246 HInvokeInterface(ArenaAllocator* arena,
3247 uint32_t number_of_arguments,
3248 Primitive::Type return_type,
3249 uint32_t dex_pc,
3250 uint32_t dex_method_index,
3251 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003252 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003253 imt_index_(imt_index) {}
3254
Calin Juravle641547a2015-04-21 22:08:51 +01003255 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003256 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003257 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003258 }
3259
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003260 uint32_t GetImtIndex() const { return imt_index_; }
3261 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3262
3263 DECLARE_INSTRUCTION(InvokeInterface);
3264
3265 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003266 const uint32_t imt_index_;
3267
3268 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3269};
3270
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003271class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003272 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003273 HNewInstance(HCurrentMethod* current_method,
3274 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003275 uint16_t type_index,
3276 const DexFile& dex_file,
3277 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003278 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003279 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003280 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003281 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003282 entrypoint_(entrypoint) {
3283 SetRawInputAt(0, current_method);
3284 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003285
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003286 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003287 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003288 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003289
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003290 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003291 bool NeedsEnvironment() const OVERRIDE { return true; }
3292 // It may throw when called on:
3293 // - interfaces
3294 // - abstract/innaccessible/unknown classes
3295 // TODO: optimize when possible.
3296 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003297
Calin Juravle10e244f2015-01-26 18:54:32 +00003298 bool CanBeNull() const OVERRIDE { return false; }
3299
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003300 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3301
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003302 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003303
3304 private:
3305 const uint32_t dex_pc_;
3306 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003307 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003308 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003309
3310 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3311};
3312
Roland Levillain88cb1752014-10-20 16:36:47 +01003313class HNeg : public HUnaryOperation {
3314 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003315 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003316 : HUnaryOperation(result_type, input) {}
3317
Roland Levillain9867bc72015-08-05 10:21:34 +01003318 template <typename T> T Compute(T x) const { return -x; }
3319
3320 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3321 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3322 }
3323 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3324 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3325 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003326
Roland Levillain88cb1752014-10-20 16:36:47 +01003327 DECLARE_INSTRUCTION(Neg);
3328
3329 private:
3330 DISALLOW_COPY_AND_ASSIGN(HNeg);
3331};
3332
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003333class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003334 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003335 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003336 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003337 uint32_t dex_pc,
3338 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003339 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003340 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003341 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003342 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003343 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003344 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003345 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003346 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003347 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003348 }
3349
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003350 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003351 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003352 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003353
3354 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003355 bool NeedsEnvironment() const OVERRIDE { return true; }
3356
Mingyao Yang0c365e62015-03-31 15:09:29 -07003357 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3358 bool CanThrow() const OVERRIDE { return true; }
3359
Calin Juravle10e244f2015-01-26 18:54:32 +00003360 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003361
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003362 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3363
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003364 DECLARE_INSTRUCTION(NewArray);
3365
3366 private:
3367 const uint32_t dex_pc_;
3368 const 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 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003371
3372 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3373};
3374
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003375class HAdd : public HBinaryOperation {
3376 public:
3377 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3378 : HBinaryOperation(result_type, left, right) {}
3379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003380 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003381
Roland Levillain9867bc72015-08-05 10:21:34 +01003382 template <typename T> T Compute(T x, T y) const { return x + y; }
3383
3384 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3385 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003386 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003387 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3388 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003389 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003390
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003391 DECLARE_INSTRUCTION(Add);
3392
3393 private:
3394 DISALLOW_COPY_AND_ASSIGN(HAdd);
3395};
3396
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003397class HSub : public HBinaryOperation {
3398 public:
3399 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3400 : HBinaryOperation(result_type, left, right) {}
3401
Roland Levillain9867bc72015-08-05 10:21:34 +01003402 template <typename T> T Compute(T x, T y) const { return x - y; }
3403
3404 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3405 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003406 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3408 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003409 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003410
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003411 DECLARE_INSTRUCTION(Sub);
3412
3413 private:
3414 DISALLOW_COPY_AND_ASSIGN(HSub);
3415};
3416
Calin Juravle34bacdf2014-10-07 20:23:36 +01003417class HMul : public HBinaryOperation {
3418 public:
3419 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3420 : HBinaryOperation(result_type, left, right) {}
3421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003422 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003423
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 template <typename T> T Compute(T x, T y) const { return x * y; }
3425
3426 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3427 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3428 }
3429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3430 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3431 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003432
3433 DECLARE_INSTRUCTION(Mul);
3434
3435 private:
3436 DISALLOW_COPY_AND_ASSIGN(HMul);
3437};
3438
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439class HDiv : public HBinaryOperation {
3440 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003441 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003442 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3443 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444
Roland Levillain9867bc72015-08-05 10:21:34 +01003445 template <typename T>
3446 T Compute(T x, T y) const {
3447 // Our graph structure ensures we never have 0 for `y` during
3448 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003449 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003450 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003451 return (y == -1) ? -x : x / y;
3452 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003453
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3455 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3456 }
3457 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3458 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003459 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003460
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003461 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003462
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003463 static SideEffects SideEffectsForArchRuntimeCalls() {
3464 // The generated code can use a runtime call.
3465 return SideEffects::CanTriggerGC();
3466 }
3467
Calin Juravle7c4954d2014-10-28 16:57:40 +00003468 DECLARE_INSTRUCTION(Div);
3469
3470 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003471 const uint32_t dex_pc_;
3472
Calin Juravle7c4954d2014-10-28 16:57:40 +00003473 DISALLOW_COPY_AND_ASSIGN(HDiv);
3474};
3475
Calin Juravlebacfec32014-11-14 15:54:36 +00003476class HRem : public HBinaryOperation {
3477 public:
3478 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003479 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3480 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003481
Roland Levillain9867bc72015-08-05 10:21:34 +01003482 template <typename T>
3483 T Compute(T x, T y) const {
3484 // Our graph structure ensures we never have 0 for `y` during
3485 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 DCHECK_NE(y, 0);
3487 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3488 return (y == -1) ? 0 : x % y;
3489 }
3490
Roland Levillain9867bc72015-08-05 10:21:34 +01003491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3492 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3493 }
3494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3495 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 }
3497
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003498 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003500 static SideEffects SideEffectsForArchRuntimeCalls() {
3501 return SideEffects::CanTriggerGC();
3502 }
3503
Calin Juravlebacfec32014-11-14 15:54:36 +00003504 DECLARE_INSTRUCTION(Rem);
3505
3506 private:
3507 const uint32_t dex_pc_;
3508
3509 DISALLOW_COPY_AND_ASSIGN(HRem);
3510};
3511
Calin Juravled0d48522014-11-04 16:40:20 +00003512class HDivZeroCheck : public HExpression<1> {
3513 public:
3514 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3515 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3516 SetRawInputAt(0, value);
3517 }
3518
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003519 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3520
Calin Juravled0d48522014-11-04 16:40:20 +00003521 bool CanBeMoved() const OVERRIDE { return true; }
3522
3523 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3524 UNUSED(other);
3525 return true;
3526 }
3527
3528 bool NeedsEnvironment() const OVERRIDE { return true; }
3529 bool CanThrow() const OVERRIDE { return true; }
3530
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003531 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003532
3533 DECLARE_INSTRUCTION(DivZeroCheck);
3534
3535 private:
3536 const uint32_t dex_pc_;
3537
3538 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3539};
3540
Calin Juravle9aec02f2014-11-18 23:06:35 +00003541class HShl : public HBinaryOperation {
3542 public:
3543 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3544 : HBinaryOperation(result_type, left, right) {}
3545
Roland Levillain9867bc72015-08-05 10:21:34 +01003546 template <typename T, typename U, typename V>
3547 T Compute(T x, U y, V max_shift_value) const {
3548 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3549 "V is not the unsigned integer type corresponding to T");
3550 return x << (y & max_shift_value);
3551 }
3552
3553 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3554 return GetBlock()->GetGraph()->GetIntConstant(
3555 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3556 }
3557 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3558 // case is handled as `x << static_cast<int>(y)`.
3559 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3560 return GetBlock()->GetGraph()->GetLongConstant(
3561 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3562 }
3563 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3564 return GetBlock()->GetGraph()->GetLongConstant(
3565 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3566 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003567
3568 DECLARE_INSTRUCTION(Shl);
3569
3570 private:
3571 DISALLOW_COPY_AND_ASSIGN(HShl);
3572};
3573
3574class HShr : public HBinaryOperation {
3575 public:
3576 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3577 : HBinaryOperation(result_type, left, right) {}
3578
Roland Levillain9867bc72015-08-05 10:21:34 +01003579 template <typename T, typename U, typename V>
3580 T Compute(T x, U y, V max_shift_value) const {
3581 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3582 "V is not the unsigned integer type corresponding to T");
3583 return x >> (y & max_shift_value);
3584 }
3585
3586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3587 return GetBlock()->GetGraph()->GetIntConstant(
3588 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3589 }
3590 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3591 // case is handled as `x >> static_cast<int>(y)`.
3592 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3593 return GetBlock()->GetGraph()->GetLongConstant(
3594 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3595 }
3596 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3597 return GetBlock()->GetGraph()->GetLongConstant(
3598 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3599 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003600
3601 DECLARE_INSTRUCTION(Shr);
3602
3603 private:
3604 DISALLOW_COPY_AND_ASSIGN(HShr);
3605};
3606
3607class HUShr : public HBinaryOperation {
3608 public:
3609 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3610 : HBinaryOperation(result_type, left, right) {}
3611
Roland Levillain9867bc72015-08-05 10:21:34 +01003612 template <typename T, typename U, typename V>
3613 T Compute(T x, U y, V max_shift_value) const {
3614 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3615 "V is not the unsigned integer type corresponding to T");
3616 V ux = static_cast<V>(x);
3617 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003618 }
3619
Roland Levillain9867bc72015-08-05 10:21:34 +01003620 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3621 return GetBlock()->GetGraph()->GetIntConstant(
3622 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3623 }
3624 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3625 // case is handled as `x >>> static_cast<int>(y)`.
3626 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3627 return GetBlock()->GetGraph()->GetLongConstant(
3628 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3629 }
3630 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3631 return GetBlock()->GetGraph()->GetLongConstant(
3632 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633 }
3634
3635 DECLARE_INSTRUCTION(UShr);
3636
3637 private:
3638 DISALLOW_COPY_AND_ASSIGN(HUShr);
3639};
3640
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003641class HAnd : public HBinaryOperation {
3642 public:
3643 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3644 : HBinaryOperation(result_type, left, right) {}
3645
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003646 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003647
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 template <typename T, typename U>
3649 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3650
3651 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3652 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3653 }
3654 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3655 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3656 }
3657 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3658 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3659 }
3660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3661 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3662 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003663
3664 DECLARE_INSTRUCTION(And);
3665
3666 private:
3667 DISALLOW_COPY_AND_ASSIGN(HAnd);
3668};
3669
3670class HOr : public HBinaryOperation {
3671 public:
3672 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3673 : HBinaryOperation(result_type, left, right) {}
3674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003676
Roland Levillain9867bc72015-08-05 10:21:34 +01003677 template <typename T, typename U>
3678 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3679
3680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3681 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3682 }
3683 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3684 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3685 }
3686 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3687 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3688 }
3689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3690 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3691 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003692
3693 DECLARE_INSTRUCTION(Or);
3694
3695 private:
3696 DISALLOW_COPY_AND_ASSIGN(HOr);
3697};
3698
3699class HXor : public HBinaryOperation {
3700 public:
3701 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3702 : HBinaryOperation(result_type, left, right) {}
3703
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003704 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003705
Roland Levillain9867bc72015-08-05 10:21:34 +01003706 template <typename T, typename U>
3707 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3708
3709 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3710 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3711 }
3712 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3713 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3714 }
3715 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3716 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3717 }
3718 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3719 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3720 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003721
3722 DECLARE_INSTRUCTION(Xor);
3723
3724 private:
3725 DISALLOW_COPY_AND_ASSIGN(HXor);
3726};
3727
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003728// The value of a parameter in this method. Its location depends on
3729// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003730class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003731 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003732 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3733 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003734
3735 uint8_t GetIndex() const { return index_; }
3736
Calin Juravle10e244f2015-01-26 18:54:32 +00003737 bool CanBeNull() const OVERRIDE { return !is_this_; }
3738
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003739 bool IsThis() const { return is_this_; }
3740
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003741 DECLARE_INSTRUCTION(ParameterValue);
3742
3743 private:
3744 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003745 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003746 const uint8_t index_;
3747
Calin Juravle10e244f2015-01-26 18:54:32 +00003748 // Whether or not the parameter value corresponds to 'this' argument.
3749 const bool is_this_;
3750
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003751 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3752};
3753
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003754class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003755 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003756 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003757 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003759 bool CanBeMoved() const OVERRIDE { return true; }
3760 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003761 UNUSED(other);
3762 return true;
3763 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003764
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 template <typename T> T Compute(T x) const { return ~x; }
3766
3767 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3768 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3769 }
3770 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3771 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3772 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003773
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003774 DECLARE_INSTRUCTION(Not);
3775
3776 private:
3777 DISALLOW_COPY_AND_ASSIGN(HNot);
3778};
3779
David Brazdil66d126e2015-04-03 16:02:44 +01003780class HBooleanNot : public HUnaryOperation {
3781 public:
3782 explicit HBooleanNot(HInstruction* input)
3783 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3784
3785 bool CanBeMoved() const OVERRIDE { return true; }
3786 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3787 UNUSED(other);
3788 return true;
3789 }
3790
Roland Levillain9867bc72015-08-05 10:21:34 +01003791 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003792 DCHECK(IsUint<1>(x));
3793 return !x;
3794 }
3795
Roland Levillain9867bc72015-08-05 10:21:34 +01003796 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3797 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3798 }
3799 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3800 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003801 UNREACHABLE();
3802 }
3803
3804 DECLARE_INSTRUCTION(BooleanNot);
3805
3806 private:
3807 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3808};
3809
Roland Levillaindff1f282014-11-05 14:15:05 +00003810class HTypeConversion : public HExpression<1> {
3811 public:
3812 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003813 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003814 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3815 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003816 SetRawInputAt(0, input);
3817 DCHECK_NE(input->GetType(), result_type);
3818 }
3819
3820 HInstruction* GetInput() const { return InputAt(0); }
3821 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3822 Primitive::Type GetResultType() const { return GetType(); }
3823
Roland Levillain624279f2014-12-04 11:54:28 +00003824 // Required by the x86 and ARM code generators when producing calls
3825 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003826 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003827
Roland Levillaindff1f282014-11-05 14:15:05 +00003828 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003829 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003830
Mark Mendelle82549b2015-05-06 10:55:34 -04003831 // Try to statically evaluate the conversion and return a HConstant
3832 // containing the result. If the input cannot be converted, return nullptr.
3833 HConstant* TryStaticEvaluation() const;
3834
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003835 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3836 Primitive::Type result_type) {
3837 // Some architectures may not require the 'GC' side effects, but at this point
3838 // in the compilation process we do not know what architecture we will
3839 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003840 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3841 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003842 return SideEffects::CanTriggerGC();
3843 }
3844 return SideEffects::None();
3845 }
3846
Roland Levillaindff1f282014-11-05 14:15:05 +00003847 DECLARE_INSTRUCTION(TypeConversion);
3848
3849 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003850 const uint32_t dex_pc_;
3851
Roland Levillaindff1f282014-11-05 14:15:05 +00003852 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3853};
3854
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003855static constexpr uint32_t kNoRegNumber = -1;
3856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003857class HPhi : public HInstruction {
3858 public:
3859 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003860 : HInstruction(SideEffects::None()),
3861 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003862 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003863 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003864 is_live_(false),
3865 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003866 inputs_.SetSize(number_of_inputs);
3867 }
3868
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003869 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3870 static Primitive::Type ToPhiType(Primitive::Type type) {
3871 switch (type) {
3872 case Primitive::kPrimBoolean:
3873 case Primitive::kPrimByte:
3874 case Primitive::kPrimShort:
3875 case Primitive::kPrimChar:
3876 return Primitive::kPrimInt;
3877 default:
3878 return type;
3879 }
3880 }
3881
David Brazdilffee3d32015-07-06 11:48:53 +01003882 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3883
Calin Juravle10e244f2015-01-26 18:54:32 +00003884 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003885
3886 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003887 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003888
Calin Juravle10e244f2015-01-26 18:54:32 +00003889 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003890 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003891
Calin Juravle10e244f2015-01-26 18:54:32 +00003892 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3893 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003895 uint32_t GetRegNumber() const { return reg_number_; }
3896
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003897 void SetDead() { is_live_ = false; }
3898 void SetLive() { is_live_ = true; }
3899 bool IsDead() const { return !is_live_; }
3900 bool IsLive() const { return is_live_; }
3901
Calin Juravlea4f88312015-04-16 12:57:19 +01003902 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3903 // An equivalent phi is a phi having the same dex register and type.
3904 // It assumes that phis with the same dex register are adjacent.
3905 HPhi* GetNextEquivalentPhiWithSameType() {
3906 HInstruction* next = GetNext();
3907 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3908 if (next->GetType() == GetType()) {
3909 return next->AsPhi();
3910 }
3911 next = next->GetNext();
3912 }
3913 return nullptr;
3914 }
3915
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003916 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003917
David Brazdil1abb4192015-02-17 18:33:36 +00003918 protected:
3919 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3920
3921 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3922 inputs_.Put(index, input);
3923 }
3924
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003925 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003926 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003927 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003928 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003929 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003930 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003931
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003932 DISALLOW_COPY_AND_ASSIGN(HPhi);
3933};
3934
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003935class HNullCheck : public HExpression<1> {
3936 public:
3937 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003938 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 SetRawInputAt(0, value);
3940 }
3941
Calin Juravle10e244f2015-01-26 18:54:32 +00003942 bool CanBeMoved() const OVERRIDE { return true; }
3943 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003944 UNUSED(other);
3945 return true;
3946 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003947
Calin Juravle10e244f2015-01-26 18:54:32 +00003948 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003949
Calin Juravle10e244f2015-01-26 18:54:32 +00003950 bool CanThrow() const OVERRIDE { return true; }
3951
3952 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003953
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003954 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003955
3956 DECLARE_INSTRUCTION(NullCheck);
3957
3958 private:
3959 const uint32_t dex_pc_;
3960
3961 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3962};
3963
3964class FieldInfo : public ValueObject {
3965 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003966 FieldInfo(MemberOffset field_offset,
3967 Primitive::Type field_type,
3968 bool is_volatile,
3969 uint32_t index,
3970 const DexFile& dex_file)
3971 : field_offset_(field_offset),
3972 field_type_(field_type),
3973 is_volatile_(is_volatile),
3974 index_(index),
3975 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976
3977 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003978 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003979 uint32_t GetFieldIndex() const { return index_; }
3980 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003981 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003982
3983 private:
3984 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003985 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003986 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003987 uint32_t index_;
3988 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989};
3990
3991class HInstanceFieldGet : public HExpression<1> {
3992 public:
3993 HInstanceFieldGet(HInstruction* value,
3994 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003995 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003996 bool is_volatile,
3997 uint32_t field_idx,
3998 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003999 : HExpression(
4000 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004001 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004002 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004003 SetRawInputAt(0, value);
4004 }
4005
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004006 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004007
4008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4009 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4010 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004011 }
4012
Calin Juravle641547a2015-04-21 22:08:51 +01004013 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4014 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004015 }
4016
4017 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004018 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4019 }
4020
Calin Juravle52c48962014-12-16 17:02:57 +00004021 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004022 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004023 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004024 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004025
4026 DECLARE_INSTRUCTION(InstanceFieldGet);
4027
4028 private:
4029 const FieldInfo field_info_;
4030
4031 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4032};
4033
4034class HInstanceFieldSet : public HTemplateInstruction<2> {
4035 public:
4036 HInstanceFieldSet(HInstruction* object,
4037 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004038 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004039 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004040 bool is_volatile,
4041 uint32_t field_idx,
4042 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004043 : HTemplateInstruction(
4044 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004045 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004046 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004047 SetRawInputAt(0, object);
4048 SetRawInputAt(1, value);
4049 }
4050
Calin Juravle641547a2015-04-21 22:08:51 +01004051 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4052 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004053 }
4054
Calin Juravle52c48962014-12-16 17:02:57 +00004055 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004056 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004057 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004058 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004059 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004060 bool GetValueCanBeNull() const { return value_can_be_null_; }
4061 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004062
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063 DECLARE_INSTRUCTION(InstanceFieldSet);
4064
4065 private:
4066 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004067 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068
4069 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4070};
4071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072class HArrayGet : public HExpression<2> {
4073 public:
4074 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004075 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 SetRawInputAt(0, array);
4077 SetRawInputAt(1, index);
4078 }
4079
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004080 bool CanBeMoved() const OVERRIDE { return true; }
4081 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004082 UNUSED(other);
4083 return true;
4084 }
Calin Juravle641547a2015-04-21 22:08:51 +01004085 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4086 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004087 // TODO: We can be smarter here.
4088 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4089 // which generates the implicit null check. There are cases when these can be removed
4090 // to produce better code. If we ever add optimizations to do so we should allow an
4091 // implicit check here (as long as the address falls in the first page).
4092 return false;
4093 }
4094
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004095 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004096
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004097 HInstruction* GetArray() const { return InputAt(0); }
4098 HInstruction* GetIndex() const { return InputAt(1); }
4099
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 DECLARE_INSTRUCTION(ArrayGet);
4101
4102 private:
4103 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4104};
4105
4106class HArraySet : public HTemplateInstruction<3> {
4107 public:
4108 HArraySet(HInstruction* array,
4109 HInstruction* index,
4110 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004111 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004112 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004113 : HTemplateInstruction(
4114 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4115 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004116 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004117 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004118 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4119 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 SetRawInputAt(0, array);
4121 SetRawInputAt(1, index);
4122 SetRawInputAt(2, value);
4123 }
4124
Calin Juravle77520bc2015-01-12 18:45:46 +00004125 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004126 // We currently always call a runtime method to catch array store
4127 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004128 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 }
4130
Mingyao Yang81014cb2015-06-02 03:16:27 -07004131 // Can throw ArrayStoreException.
4132 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4133
Calin Juravle641547a2015-04-21 22:08:51 +01004134 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4135 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004136 // TODO: Same as for ArrayGet.
4137 return false;
4138 }
4139
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004140 void ClearNeedsTypeCheck() {
4141 needs_type_check_ = false;
4142 }
4143
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004144 void ClearValueCanBeNull() {
4145 value_can_be_null_ = false;
4146 }
4147
4148 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004149 bool NeedsTypeCheck() const { return needs_type_check_; }
4150
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004151 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004152
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004153 HInstruction* GetArray() const { return InputAt(0); }
4154 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004155 HInstruction* GetValue() const { return InputAt(2); }
4156
4157 Primitive::Type GetComponentType() const {
4158 // The Dex format does not type floating point index operations. Since the
4159 // `expected_component_type_` is set during building and can therefore not
4160 // be correct, we also check what is the value type. If it is a floating
4161 // point type, we must use that type.
4162 Primitive::Type value_type = GetValue()->GetType();
4163 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4164 ? value_type
4165 : expected_component_type_;
4166 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004167
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004168 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4169 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4170 }
4171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 DECLARE_INSTRUCTION(ArraySet);
4173
4174 private:
4175 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004176 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004177 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004178 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179
4180 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4181};
4182
4183class HArrayLength : public HExpression<1> {
4184 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004185 explicit HArrayLength(HInstruction* array)
4186 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4187 // Note that arrays do not change length, so the instruction does not
4188 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004189 SetRawInputAt(0, array);
4190 }
4191
Calin Juravle77520bc2015-01-12 18:45:46 +00004192 bool CanBeMoved() const OVERRIDE { return true; }
4193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004194 UNUSED(other);
4195 return true;
4196 }
Calin Juravle641547a2015-04-21 22:08:51 +01004197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4198 return obj == InputAt(0);
4199 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004200
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004201 DECLARE_INSTRUCTION(ArrayLength);
4202
4203 private:
4204 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4205};
4206
4207class HBoundsCheck : public HExpression<2> {
4208 public:
4209 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004210 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004211 DCHECK(index->GetType() == Primitive::kPrimInt);
4212 SetRawInputAt(0, index);
4213 SetRawInputAt(1, length);
4214 }
4215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004216 bool CanBeMoved() const OVERRIDE { return true; }
4217 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004218 UNUSED(other);
4219 return true;
4220 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004221
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004222 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004224 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004225
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004226 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227
4228 DECLARE_INSTRUCTION(BoundsCheck);
4229
4230 private:
4231 const uint32_t dex_pc_;
4232
4233 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4234};
4235
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236/**
4237 * Some DEX instructions are folded into multiple HInstructions that need
4238 * to stay live until the last HInstruction. This class
4239 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004240 * HInstruction stays live. `index` represents the stack location index of the
4241 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 */
4243class HTemporary : public HTemplateInstruction<0> {
4244 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004245 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246
4247 size_t GetIndex() const { return index_; }
4248
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004249 Primitive::Type GetType() const OVERRIDE {
4250 // The previous instruction is the one that will be stored in the temporary location.
4251 DCHECK(GetPrevious() != nullptr);
4252 return GetPrevious()->GetType();
4253 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004254
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255 DECLARE_INSTRUCTION(Temporary);
4256
4257 private:
4258 const size_t index_;
4259
4260 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4261};
4262
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004263class HSuspendCheck : public HTemplateInstruction<0> {
4264 public:
4265 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004266 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004268 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004269 return true;
4270 }
4271
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004272 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004273 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4274 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004275
4276 DECLARE_INSTRUCTION(SuspendCheck);
4277
4278 private:
4279 const uint32_t dex_pc_;
4280
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004281 // Only used for code generation, in order to share the same slow path between back edges
4282 // of a same loop.
4283 SlowPathCode* slow_path_;
4284
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004285 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4286};
4287
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004288/**
4289 * Instruction to load a Class object.
4290 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004291class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004292 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004293 HLoadClass(HCurrentMethod* current_method,
4294 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004295 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004296 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004297 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004298 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004299 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004300 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004301 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004302 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004303 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004304 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004305 SetRawInputAt(0, current_method);
4306 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004307
4308 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004309
4310 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4311 return other->AsLoadClass()->type_index_ == type_index_;
4312 }
4313
4314 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004316 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004317 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004318 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004319 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004320
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004321 bool NeedsEnvironment() const OVERRIDE {
4322 // Will call runtime and load the class if the class is not loaded yet.
4323 // TODO: finer grain decision.
4324 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004325 }
4326
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004327 bool MustGenerateClinitCheck() const {
4328 return generate_clinit_check_;
4329 }
4330
Calin Juravle0ba218d2015-05-19 18:46:01 +01004331 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4332 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004333 }
4334
4335 bool CanCallRuntime() const {
4336 return MustGenerateClinitCheck() || !is_referrers_class_;
4337 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004338
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004339 bool CanThrow() const OVERRIDE {
4340 // May call runtime and and therefore can throw.
4341 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004342 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004343 }
4344
Calin Juravleacf735c2015-02-12 15:25:22 +00004345 ReferenceTypeInfo GetLoadedClassRTI() {
4346 return loaded_class_rti_;
4347 }
4348
4349 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4350 // Make sure we only set exact types (the loaded class should never be merged).
4351 DCHECK(rti.IsExact());
4352 loaded_class_rti_ = rti;
4353 }
4354
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004355 const DexFile& GetDexFile() { return dex_file_; }
4356
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004357 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004359 static SideEffects SideEffectsForArchRuntimeCalls() {
4360 return SideEffects::CanTriggerGC();
4361 }
4362
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004363 DECLARE_INSTRUCTION(LoadClass);
4364
4365 private:
4366 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004367 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004368 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004369 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004370 // Whether this instruction must generate the initialization check.
4371 // Used for code generation.
4372 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004373
Calin Juravleacf735c2015-02-12 15:25:22 +00004374 ReferenceTypeInfo loaded_class_rti_;
4375
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004376 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4377};
4378
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004379class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004380 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004381 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004382 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004383 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004384 dex_pc_(dex_pc) {
4385 SetRawInputAt(0, current_method);
4386 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004387
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004388 bool CanBeMoved() const OVERRIDE { return true; }
4389
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004390 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4391 return other->AsLoadString()->string_index_ == string_index_;
4392 }
4393
4394 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4395
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004396 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004397 uint32_t GetStringIndex() const { return string_index_; }
4398
4399 // TODO: Can we deopt or debug when we resolve a string?
4400 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004401 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004402
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004403 static SideEffects SideEffectsForArchRuntimeCalls() {
4404 return SideEffects::CanTriggerGC();
4405 }
4406
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004407 DECLARE_INSTRUCTION(LoadString);
4408
4409 private:
4410 const uint32_t string_index_;
4411 const uint32_t dex_pc_;
4412
4413 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4414};
4415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004416/**
4417 * Performs an initialization check on its Class object input.
4418 */
4419class HClinitCheck : public HExpression<1> {
4420 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004421 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004422 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004423 Primitive::kPrimNot,
4424 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004425 dex_pc_(dex_pc) {
4426 SetRawInputAt(0, constant);
4427 }
4428
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004429 bool CanBeMoved() const OVERRIDE { return true; }
4430 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4431 UNUSED(other);
4432 return true;
4433 }
4434
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004435 bool NeedsEnvironment() const OVERRIDE {
4436 // May call runtime to initialize the class.
4437 return true;
4438 }
4439
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004440 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004441
4442 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4443
4444 DECLARE_INSTRUCTION(ClinitCheck);
4445
4446 private:
4447 const uint32_t dex_pc_;
4448
4449 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4450};
4451
4452class HStaticFieldGet : public HExpression<1> {
4453 public:
4454 HStaticFieldGet(HInstruction* cls,
4455 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004456 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004457 bool is_volatile,
4458 uint32_t field_idx,
4459 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004460 : HExpression(
4461 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004462 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004463 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004464 SetRawInputAt(0, cls);
4465 }
4466
Calin Juravle52c48962014-12-16 17:02:57 +00004467
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004468 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004469
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004471 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4472 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004473 }
4474
4475 size_t ComputeHashCode() const OVERRIDE {
4476 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4477 }
4478
Calin Juravle52c48962014-12-16 17:02:57 +00004479 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004480 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4481 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004482 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483
4484 DECLARE_INSTRUCTION(StaticFieldGet);
4485
4486 private:
4487 const FieldInfo field_info_;
4488
4489 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4490};
4491
4492class HStaticFieldSet : public HTemplateInstruction<2> {
4493 public:
4494 HStaticFieldSet(HInstruction* cls,
4495 HInstruction* value,
4496 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004497 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004498 bool is_volatile,
4499 uint32_t field_idx,
4500 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004501 : HTemplateInstruction(
4502 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004503 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004504 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004505 SetRawInputAt(0, cls);
4506 SetRawInputAt(1, value);
4507 }
4508
Calin Juravle52c48962014-12-16 17:02:57 +00004509 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004510 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4511 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004512 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004513
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004514 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004515 bool GetValueCanBeNull() const { return value_can_be_null_; }
4516 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004517
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004518 DECLARE_INSTRUCTION(StaticFieldSet);
4519
4520 private:
4521 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004522 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004523
4524 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4525};
4526
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004527// Implement the move-exception DEX instruction.
4528class HLoadException : public HExpression<0> {
4529 public:
4530 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4531
4532 DECLARE_INSTRUCTION(LoadException);
4533
4534 private:
4535 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4536};
4537
David Brazdilcb1c0552015-08-04 16:22:25 +01004538// Implicit part of move-exception which clears thread-local exception storage.
4539// Must not be removed because the runtime expects the TLS to get cleared.
4540class HClearException : public HTemplateInstruction<0> {
4541 public:
4542 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4543
4544 DECLARE_INSTRUCTION(ClearException);
4545
4546 private:
4547 DISALLOW_COPY_AND_ASSIGN(HClearException);
4548};
4549
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004550class HThrow : public HTemplateInstruction<1> {
4551 public:
4552 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004553 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004554 SetRawInputAt(0, exception);
4555 }
4556
4557 bool IsControlFlow() const OVERRIDE { return true; }
4558
4559 bool NeedsEnvironment() const OVERRIDE { return true; }
4560
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004561 bool CanThrow() const OVERRIDE { return true; }
4562
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004563 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004564
4565 DECLARE_INSTRUCTION(Throw);
4566
4567 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004568 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004569
4570 DISALLOW_COPY_AND_ASSIGN(HThrow);
4571};
4572
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004573class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004574 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004575 HInstanceOf(HInstruction* object,
4576 HLoadClass* constant,
4577 bool class_is_final,
4578 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004579 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004580 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004581 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004582 dex_pc_(dex_pc) {
4583 SetRawInputAt(0, object);
4584 SetRawInputAt(1, constant);
4585 }
4586
4587 bool CanBeMoved() const OVERRIDE { return true; }
4588
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004589 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004590 return true;
4591 }
4592
4593 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004594 return false;
4595 }
4596
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004597 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004598
4599 bool IsClassFinal() const { return class_is_final_; }
4600
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004601 // Used only in code generation.
4602 bool MustDoNullCheck() const { return must_do_null_check_; }
4603 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4604
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004605 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4606 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4607 }
4608
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004609 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004610
4611 private:
4612 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004613 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004614 const uint32_t dex_pc_;
4615
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004616 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4617};
4618
Calin Juravleb1498f62015-02-16 13:13:29 +00004619class HBoundType : public HExpression<1> {
4620 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004621 // Constructs an HBoundType with the given upper_bound.
4622 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004623 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004624 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004625 upper_bound_(upper_bound),
4626 upper_can_be_null_(upper_can_be_null),
4627 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004628 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004629 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004630 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004631 }
4632
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004633 // GetUpper* should only be used in reference type propagation.
4634 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4635 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004636
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004637 void SetCanBeNull(bool can_be_null) {
4638 DCHECK(upper_can_be_null_ || !can_be_null);
4639 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004640 }
4641
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004642 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4643
Calin Juravleb1498f62015-02-16 13:13:29 +00004644 DECLARE_INSTRUCTION(BoundType);
4645
4646 private:
4647 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004648 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4649 // It is used to bound the type in cases like:
4650 // if (x instanceof ClassX) {
4651 // // uper_bound_ will be ClassX
4652 // }
4653 const ReferenceTypeInfo upper_bound_;
4654 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4655 // is false then can_be_null_ cannot be true).
4656 const bool upper_can_be_null_;
4657 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004658
4659 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4660};
4661
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004662class HCheckCast : public HTemplateInstruction<2> {
4663 public:
4664 HCheckCast(HInstruction* object,
4665 HLoadClass* constant,
4666 bool class_is_final,
4667 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004668 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004669 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004670 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004671 dex_pc_(dex_pc) {
4672 SetRawInputAt(0, object);
4673 SetRawInputAt(1, constant);
4674 }
4675
4676 bool CanBeMoved() const OVERRIDE { return true; }
4677
4678 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4679 return true;
4680 }
4681
4682 bool NeedsEnvironment() const OVERRIDE {
4683 // Instruction may throw a CheckCastError.
4684 return true;
4685 }
4686
4687 bool CanThrow() const OVERRIDE { return true; }
4688
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004689 bool MustDoNullCheck() const { return must_do_null_check_; }
4690 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4691
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004692 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004693
4694 bool IsClassFinal() const { return class_is_final_; }
4695
4696 DECLARE_INSTRUCTION(CheckCast);
4697
4698 private:
4699 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004700 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004701 const uint32_t dex_pc_;
4702
4703 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004704};
4705
Calin Juravle27df7582015-04-17 19:12:31 +01004706class HMemoryBarrier : public HTemplateInstruction<0> {
4707 public:
4708 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004709 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004710 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004711 barrier_kind_(barrier_kind) {}
4712
4713 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4714
4715 DECLARE_INSTRUCTION(MemoryBarrier);
4716
4717 private:
4718 const MemBarrierKind barrier_kind_;
4719
4720 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4721};
4722
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004723class HMonitorOperation : public HTemplateInstruction<1> {
4724 public:
4725 enum OperationKind {
4726 kEnter,
4727 kExit,
4728 };
4729
4730 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004731 : HTemplateInstruction(
4732 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004733 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004734 SetRawInputAt(0, object);
4735 }
4736
4737 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004738 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4739
4740 bool CanThrow() const OVERRIDE {
4741 // Verifier guarantees that monitor-exit cannot throw.
4742 // This is important because it allows the HGraphBuilder to remove
4743 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4744 return IsEnter();
4745 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004747 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004748
4749 bool IsEnter() const { return kind_ == kEnter; }
4750
4751 DECLARE_INSTRUCTION(MonitorOperation);
4752
Calin Juravle52c48962014-12-16 17:02:57 +00004753 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004754 const OperationKind kind_;
4755 const uint32_t dex_pc_;
4756
4757 private:
4758 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4759};
4760
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004761/**
4762 * A HInstruction used as a marker for the replacement of new + <init>
4763 * of a String to a call to a StringFactory. Only baseline will see
4764 * the node at code generation, where it will be be treated as null.
4765 * When compiling non-baseline, `HFakeString` instructions are being removed
4766 * in the instruction simplifier.
4767 */
4768class HFakeString : public HTemplateInstruction<0> {
4769 public:
4770 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4771
4772 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4773
4774 DECLARE_INSTRUCTION(FakeString);
4775
4776 private:
4777 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4778};
4779
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004780class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004781 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004782 MoveOperands(Location source,
4783 Location destination,
4784 Primitive::Type type,
4785 HInstruction* instruction)
4786 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004787
4788 Location GetSource() const { return source_; }
4789 Location GetDestination() const { return destination_; }
4790
4791 void SetSource(Location value) { source_ = value; }
4792 void SetDestination(Location value) { destination_ = value; }
4793
4794 // The parallel move resolver marks moves as "in-progress" by clearing the
4795 // destination (but not the source).
4796 Location MarkPending() {
4797 DCHECK(!IsPending());
4798 Location dest = destination_;
4799 destination_ = Location::NoLocation();
4800 return dest;
4801 }
4802
4803 void ClearPending(Location dest) {
4804 DCHECK(IsPending());
4805 destination_ = dest;
4806 }
4807
4808 bool IsPending() const {
4809 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4810 return destination_.IsInvalid() && !source_.IsInvalid();
4811 }
4812
4813 // True if this blocks a move from the given location.
4814 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004815 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004816 }
4817
4818 // A move is redundant if it's been eliminated, if its source and
4819 // destination are the same, or if its destination is unneeded.
4820 bool IsRedundant() const {
4821 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4822 }
4823
4824 // We clear both operands to indicate move that's been eliminated.
4825 void Eliminate() {
4826 source_ = destination_ = Location::NoLocation();
4827 }
4828
4829 bool IsEliminated() const {
4830 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4831 return source_.IsInvalid();
4832 }
4833
Alexey Frunze4dda3372015-06-01 18:31:49 -07004834 Primitive::Type GetType() const { return type_; }
4835
Nicolas Geoffray90218252015-04-15 11:56:51 +01004836 bool Is64BitMove() const {
4837 return Primitive::Is64BitType(type_);
4838 }
4839
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004840 HInstruction* GetInstruction() const { return instruction_; }
4841
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004842 private:
4843 Location source_;
4844 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004845 // The type this move is for.
4846 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004847 // The instruction this move is assocatied with. Null when this move is
4848 // for moving an input in the expected locations of user (including a phi user).
4849 // This is only used in debug mode, to ensure we do not connect interval siblings
4850 // in the same parallel move.
4851 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004852};
4853
4854static constexpr size_t kDefaultNumberOfMoves = 4;
4855
4856class HParallelMove : public HTemplateInstruction<0> {
4857 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004858 explicit HParallelMove(ArenaAllocator* arena)
4859 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004860
Nicolas Geoffray90218252015-04-15 11:56:51 +01004861 void AddMove(Location source,
4862 Location destination,
4863 Primitive::Type type,
4864 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004865 DCHECK(source.IsValid());
4866 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004867 if (kIsDebugBuild) {
4868 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004869 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004870 if (moves_.Get(i).GetInstruction() == instruction) {
4871 // Special case the situation where the move is for the spill slot
4872 // of the instruction.
4873 if ((GetPrevious() == instruction)
4874 || ((GetPrevious() == nullptr)
4875 && instruction->IsPhi()
4876 && instruction->GetBlock() == GetBlock())) {
4877 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4878 << "Doing parallel moves for the same instruction.";
4879 } else {
4880 DCHECK(false) << "Doing parallel moves for the same instruction.";
4881 }
4882 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004883 }
4884 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004885 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004886 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004887 << "Overlapped destination for two moves in a parallel move: "
4888 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4889 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004890 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004891 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004892 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004893 }
4894
4895 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004896 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004897 }
4898
4899 size_t NumMoves() const { return moves_.Size(); }
4900
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004901 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004902
4903 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004904 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004905
4906 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4907};
4908
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004909class HGraphVisitor : public ValueObject {
4910 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004911 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4912 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004913
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004914 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004915 virtual void VisitBasicBlock(HBasicBlock* block);
4916
Roland Levillain633021e2014-10-01 14:12:25 +01004917 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004918 void VisitInsertionOrder();
4919
Roland Levillain633021e2014-10-01 14:12:25 +01004920 // Visit the graph following dominator tree reverse post-order.
4921 void VisitReversePostOrder();
4922
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004923 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004924
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004925 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004926#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004927 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4928
4929 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4930
4931#undef DECLARE_VISIT_INSTRUCTION
4932
4933 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004934 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004935
4936 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4937};
4938
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004939class HGraphDelegateVisitor : public HGraphVisitor {
4940 public:
4941 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4942 virtual ~HGraphDelegateVisitor() {}
4943
4944 // Visit functions that delegate to to super class.
4945#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004946 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004947
4948 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4949
4950#undef DECLARE_VISIT_INSTRUCTION
4951
4952 private:
4953 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4954};
4955
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004956class HInsertionOrderIterator : public ValueObject {
4957 public:
4958 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4959
4960 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4961 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4962 void Advance() { ++index_; }
4963
4964 private:
4965 const HGraph& graph_;
4966 size_t index_;
4967
4968 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4969};
4970
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004971class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004972 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004973 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4974 // Check that reverse post order of the graph has been built.
4975 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4976 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004977
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004978 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4979 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004980 void Advance() { ++index_; }
4981
4982 private:
4983 const HGraph& graph_;
4984 size_t index_;
4985
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004986 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004987};
4988
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004989class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004990 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004991 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004992 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4993 // Check that reverse post order of the graph has been built.
4994 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4995 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004996
4997 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004998 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004999 void Advance() { --index_; }
5000
5001 private:
5002 const HGraph& graph_;
5003 size_t index_;
5004
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005005 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005006};
5007
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005008class HLinearPostOrderIterator : public ValueObject {
5009 public:
5010 explicit HLinearPostOrderIterator(const HGraph& graph)
5011 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5012
5013 bool Done() const { return index_ == 0; }
5014
5015 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5016
5017 void Advance() {
5018 --index_;
5019 DCHECK_GE(index_, 0U);
5020 }
5021
5022 private:
5023 const GrowableArray<HBasicBlock*>& order_;
5024 size_t index_;
5025
5026 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5027};
5028
5029class HLinearOrderIterator : public ValueObject {
5030 public:
5031 explicit HLinearOrderIterator(const HGraph& graph)
5032 : order_(graph.GetLinearOrder()), index_(0) {}
5033
5034 bool Done() const { return index_ == order_.Size(); }
5035 HBasicBlock* Current() const { return order_.Get(index_); }
5036 void Advance() { ++index_; }
5037
5038 private:
5039 const GrowableArray<HBasicBlock*>& order_;
5040 size_t index_;
5041
5042 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5043};
5044
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005045// Iterator over the blocks that art part of the loop. Includes blocks part
5046// of an inner loop. The order in which the blocks are iterated is on their
5047// block id.
5048class HBlocksInLoopIterator : public ValueObject {
5049 public:
5050 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5051 : blocks_in_loop_(info.GetBlocks()),
5052 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5053 index_(0) {
5054 if (!blocks_in_loop_.IsBitSet(index_)) {
5055 Advance();
5056 }
5057 }
5058
5059 bool Done() const { return index_ == blocks_.Size(); }
5060 HBasicBlock* Current() const { return blocks_.Get(index_); }
5061 void Advance() {
5062 ++index_;
5063 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5064 if (blocks_in_loop_.IsBitSet(index_)) {
5065 break;
5066 }
5067 }
5068 }
5069
5070 private:
5071 const BitVector& blocks_in_loop_;
5072 const GrowableArray<HBasicBlock*>& blocks_;
5073 size_t index_;
5074
5075 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5076};
5077
Mingyao Yang3584bce2015-05-19 16:01:59 -07005078// Iterator over the blocks that art part of the loop. Includes blocks part
5079// of an inner loop. The order in which the blocks are iterated is reverse
5080// post order.
5081class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5082 public:
5083 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5084 : blocks_in_loop_(info.GetBlocks()),
5085 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5086 index_(0) {
5087 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5088 Advance();
5089 }
5090 }
5091
5092 bool Done() const { return index_ == blocks_.Size(); }
5093 HBasicBlock* Current() const { return blocks_.Get(index_); }
5094 void Advance() {
5095 ++index_;
5096 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5097 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5098 break;
5099 }
5100 }
5101 }
5102
5103 private:
5104 const BitVector& blocks_in_loop_;
5105 const GrowableArray<HBasicBlock*>& blocks_;
5106 size_t index_;
5107
5108 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5109};
5110
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005111inline int64_t Int64FromConstant(HConstant* constant) {
5112 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5113 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5114 : constant->AsLongConstant()->GetValue();
5115}
5116
Vladimir Marko9b688a02015-05-06 14:12:42 +01005117inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5118 // For the purposes of the compiler, the dex files must actually be the same object
5119 // if we want to safely treat them as the same. This is especially important for JIT
5120 // as custom class loaders can open the same underlying file (or memory) multiple
5121 // times and provide different class resolution but no two class loaders should ever
5122 // use the same DexFile object - doing so is an unsupported hack that can lead to
5123 // all sorts of weird failures.
5124 return &lhs == &rhs;
5125}
5126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005127} // namespace art
5128
5129#endif // ART_COMPILER_OPTIMIZING_NODES_H_