blob: 2374c9cf202955c6388eaf00b944e2af3381f59d [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 Marko58155012015-08-19 12:49:41 +000030#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),
571 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572 loop_information_(nullptr),
573 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100574 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100575 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100576 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100577 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000578 lifetime_end_(kNoLifetime),
579 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100581 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
582 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000583 }
584
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100585 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
586 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 }
588
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100589 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
590 return dominated_blocks_;
591 }
592
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100593 bool IsEntryBlock() const {
594 return graph_->GetEntryBlock() == this;
595 }
596
597 bool IsExitBlock() const {
598 return graph_->GetExitBlock() == this;
599 }
600
David Brazdil46e2a392015-03-16 17:31:52 +0000601 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000602 bool IsSingleTryBoundary() const;
603
604 // Returns true if this block emits nothing but a jump.
605 bool IsSingleJump() const {
606 HLoopInformation* loop_info = GetLoopInformation();
607 return (IsSingleGoto() || IsSingleTryBoundary())
608 // Back edges generate a suspend check.
609 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
610 }
David Brazdil46e2a392015-03-16 17:31:52 +0000611
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000612 void AddBackEdge(HBasicBlock* back_edge) {
613 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000614 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100616 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 loop_information_->AddBackEdge(back_edge);
618 }
619
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000620 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000621 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000623 int GetBlockId() const { return block_id_; }
624 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000626 HBasicBlock* GetDominator() const { return dominator_; }
627 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100628 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100629 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000630 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
631 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
632 if (dominated_blocks_.Get(i) == existing) {
633 dominated_blocks_.Put(i, new_block);
634 return;
635 }
636 }
637 LOG(FATAL) << "Unreachable";
638 UNREACHABLE();
639 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100640 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641
642 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100643 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644 }
645
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
647 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100648 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100649 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100650 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
651 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
653 void AddSuccessor(HBasicBlock* block) {
654 successors_.Add(block);
655 block->predecessors_.Add(this);
656 }
657
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100658 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
659 size_t successor_index = GetSuccessorIndexOf(existing);
660 DCHECK_NE(successor_index, static_cast<size_t>(-1));
661 existing->RemovePredecessor(this);
662 new_block->predecessors_.Add(this);
663 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000664 }
665
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000666 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
667 size_t predecessor_index = GetPredecessorIndexOf(existing);
668 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
669 existing->RemoveSuccessor(this);
670 new_block->successors_.Add(this);
671 predecessors_.Put(predecessor_index, new_block);
672 }
673
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100674 // Insert `this` between `predecessor` and `successor. This method
675 // preserves the indicies, and will update the first edge found between
676 // `predecessor` and `successor`.
677 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
678 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
679 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
680 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
681 DCHECK_NE(successor_index, static_cast<size_t>(-1));
682 successor->predecessors_.Put(predecessor_index, this);
683 predecessor->successors_.Put(successor_index, this);
684 successors_.Add(successor);
685 predecessors_.Add(predecessor);
686 }
687
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100688 void RemovePredecessor(HBasicBlock* block) {
689 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 }
691
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000692 void RemoveSuccessor(HBasicBlock* block) {
693 successors_.Delete(block);
694 }
695
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 void ClearAllPredecessors() {
697 predecessors_.Reset();
698 }
699
700 void AddPredecessor(HBasicBlock* block) {
701 predecessors_.Add(block);
702 block->successors_.Add(this);
703 }
704
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100705 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100706 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100707 HBasicBlock* temp = predecessors_.Get(0);
708 predecessors_.Put(0, predecessors_.Get(1));
709 predecessors_.Put(1, temp);
710 }
711
David Brazdil769c9e52015-04-27 13:54:09 +0100712 void SwapSuccessors() {
713 DCHECK_EQ(successors_.Size(), 2u);
714 HBasicBlock* temp = successors_.Get(0);
715 successors_.Put(0, successors_.Get(1));
716 successors_.Put(1, temp);
717 }
718
David Brazdilfc6a86a2015-06-26 10:33:45 +0000719 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100720 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
721 if (predecessors_.Get(i) == predecessor) {
722 return i;
723 }
724 }
725 return -1;
726 }
727
David Brazdilfc6a86a2015-06-26 10:33:45 +0000728 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100729 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
730 if (successors_.Get(i) == successor) {
731 return i;
732 }
733 }
734 return -1;
735 }
736
David Brazdilfc6a86a2015-06-26 10:33:45 +0000737 HBasicBlock* GetSinglePredecessor() const {
738 DCHECK_EQ(GetPredecessors().Size(), 1u);
739 return GetPredecessors().Get(0);
740 }
741
742 HBasicBlock* GetSingleSuccessor() const {
743 DCHECK_EQ(GetSuccessors().Size(), 1u);
744 return GetSuccessors().Get(0);
745 }
746
747 // Returns whether the first occurrence of `predecessor` in the list of
748 // predecessors is at index `idx`.
749 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
750 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
751 return GetPredecessorIndexOf(predecessor) == idx;
752 }
753
David Brazdilffee3d32015-07-06 11:48:53 +0100754 // Returns the number of non-exceptional successors. SsaChecker ensures that
755 // these are stored at the beginning of the successor list.
756 size_t NumberOfNormalSuccessors() const {
757 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
758 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000759
760 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100761 // created, latter block. Note that this method will add the block to the
762 // graph, create a Goto at the end of the former block and will create an edge
763 // between the blocks. It will not, however, update the reverse post order or
764 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000765 HBasicBlock* SplitBefore(HInstruction* cursor);
766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 // Split the block into two blocks just after `cursor`. Returns the newly
768 // created block. Note that this method just updates raw block information,
769 // like predecessors, successors, dominators, and instruction list. It does not
770 // update the graph, reverse post order, loop information, nor make sure the
771 // blocks are consistent (for example ending with a control flow instruction).
772 HBasicBlock* SplitAfter(HInstruction* cursor);
773
774 // Merge `other` at the end of `this`. Successors and dominated blocks of
775 // `other` are changed to be successors and dominated blocks of `this`. Note
776 // that this method does not update the graph, reverse post order, loop
777 // information, nor make sure the blocks are consistent (for example ending
778 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100779 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000780
781 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
782 // of `this` are moved to `other`.
783 // Note that this method does not update the graph, reverse post order, loop
784 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000785 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000786 void ReplaceWith(HBasicBlock* other);
787
David Brazdil2d7352b2015-04-20 14:52:42 +0100788 // Merge `other` at the end of `this`. This method updates loops, reverse post
789 // order, links to predecessors, successors, dominators and deletes the block
790 // from the graph. The two blocks must be successive, i.e. `this` the only
791 // predecessor of `other` and vice versa.
792 void MergeWith(HBasicBlock* other);
793
794 // Disconnects `this` from all its predecessors, successors and dominator,
795 // removes it from all loops it is included in and eventually from the graph.
796 // The block must not dominate any other block. Predecessors and successors
797 // are safely updated.
798 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000799
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100801 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100802 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100803 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100804 // Replace instruction `initial` with `replacement` within this block.
805 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
806 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100808 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000809 // RemoveInstruction and RemovePhi delete a given instruction from the respective
810 // instruction list. With 'ensure_safety' set to true, it verifies that the
811 // instruction is not in use and removes it from the use lists of its inputs.
812 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
813 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100814 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
816 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100817 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Roland Levillain6b879dd2014-09-22 17:13:44 +0100820 bool IsLoopPreHeaderFirstPredecessor() const {
821 DCHECK(IsLoopHeader());
822 DCHECK(!GetPredecessors().IsEmpty());
823 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
824 }
825
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100826 HLoopInformation* GetLoopInformation() const {
827 return loop_information_;
828 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000830 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 void SetInLoop(HLoopInformation* info) {
834 if (IsLoopHeader()) {
835 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100836 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100837 loop_information_ = info;
838 } else if (loop_information_->Contains(*info->GetHeader())) {
839 // Block is currently part of an outer loop. Make it part of this inner loop.
840 // Note that a non loop header having a loop information means this loop information
841 // has already been populated
842 loop_information_ = info;
843 } else {
844 // Block is part of an inner loop. Do not update the loop information.
845 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
846 // at this point, because this method is being called while populating `info`.
847 }
848 }
849
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 // Raw update of the loop information.
851 void SetLoopInformation(HLoopInformation* info) {
852 loop_information_ = info;
853 }
854
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100855 bool IsInLoop() const { return loop_information_ != nullptr; }
856
David Brazdilffee3d32015-07-06 11:48:53 +0100857 HTryBoundary* GetTryEntry() const { return try_entry_; }
858 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
859 bool IsInTry() const { return try_entry_ != nullptr; }
860
861 // Returns the try entry that this block's successors should have. They will
862 // be in the same try, unless the block ends in a try boundary. In that case,
863 // the appropriate try entry will be returned.
864 HTryBoundary* ComputeTryEntryOfSuccessors() const;
865
David Brazdila4b8c212015-05-07 09:59:30 +0100866 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100867 bool Dominates(HBasicBlock* block) const;
868
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100869 size_t GetLifetimeStart() const { return lifetime_start_; }
870 size_t GetLifetimeEnd() const { return lifetime_end_; }
871
872 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
873 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
874
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100875 uint32_t GetDexPc() const { return dex_pc_; }
876
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000877 bool IsCatchBlock() const { return is_catch_block_; }
878 void SetIsCatchBlock() { is_catch_block_ = true; }
879
David Brazdil8d5b8b22015-03-24 10:51:52 +0000880 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000881 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100882 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000883 bool HasSinglePhi() const;
884
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000886 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887 GrowableArray<HBasicBlock*> predecessors_;
888 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 HInstructionList instructions_;
890 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000891 HLoopInformation* loop_information_;
892 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100893 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100895 // The dex program counter of the first instruction of this block.
896 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100897 size_t lifetime_start_;
898 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000899 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
David Brazdilffee3d32015-07-06 11:48:53 +0100901 // If this block is in a try block, `try_entry_` stores one of, possibly
902 // several, TryBoundary instructions entering it.
903 HTryBoundary* try_entry_;
904
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000905 friend class HGraph;
906 friend class HInstruction;
907
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
909};
910
David Brazdilb2bd1c52015-03-25 11:17:37 +0000911// Iterates over the LoopInformation of all loops which contain 'block'
912// from the innermost to the outermost.
913class HLoopInformationOutwardIterator : public ValueObject {
914 public:
915 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
916 : current_(block.GetLoopInformation()) {}
917
918 bool Done() const { return current_ == nullptr; }
919
920 void Advance() {
921 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100922 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000923 }
924
925 HLoopInformation* Current() const {
926 DCHECK(!Done());
927 return current_;
928 }
929
930 private:
931 HLoopInformation* current_;
932
933 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
934};
935
Alexandre Ramesef20f712015-06-09 10:29:30 +0100936#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000938 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000939 M(ArrayGet, Instruction) \
940 M(ArrayLength, Instruction) \
941 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100942 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000943 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000944 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000945 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +0100946 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100947 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000948 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100949 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100950 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700951 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000952 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000953 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000954 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100955 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000956 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100957 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000958 M(FloatConstant, Constant) \
959 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100960 M(GreaterThan, Condition) \
961 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000963 M(InstanceFieldGet, Instruction) \
964 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000965 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100966 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000967 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000968 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100969 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000970 M(LessThan, Condition) \
971 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000972 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000973 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100974 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000975 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100976 M(Local, Instruction) \
977 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100978 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000979 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000980 M(Mul, BinaryOperation) \
981 M(Neg, UnaryOperation) \
982 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100983 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100984 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000985 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000986 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000987 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000988 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000990 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100991 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000992 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100993 M(Return, Instruction) \
994 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000995 M(Shl, BinaryOperation) \
996 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100997 M(StaticFieldGet, Instruction) \
998 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100999 M(StoreLocal, Instruction) \
1000 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001003 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001005 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001006 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001007 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008
Alexandre Ramesef20f712015-06-09 10:29:30 +01001009#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1010
1011#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1012
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001013#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1014
Alexandre Ramesef20f712015-06-09 10:29:30 +01001015#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1016
1017#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1018
1019#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1020 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1021 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1022 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001023 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001024 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1025 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1026
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027#define FOR_EACH_INSTRUCTION(M) \
1028 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1029 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001030 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001031 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001033
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1036#undef FORWARD_DECLARATION
1037
Roland Levillainccc07a92014-09-16 14:48:16 +01001038#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001039 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1040 const char* DebugName() const OVERRIDE { return #type; } \
1041 const H##type* As##type() const OVERRIDE { return this; } \
1042 H##type* As##type() OVERRIDE { return this; } \
1043 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001044 return other->Is##type(); \
1045 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001046 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001047
David Brazdiled596192015-01-23 10:39:45 +00001048template <typename T> class HUseList;
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001051class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052 public:
David Brazdiled596192015-01-23 10:39:45 +00001053 HUseListNode* GetPrevious() const { return prev_; }
1054 HUseListNode* GetNext() const { return next_; }
1055 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001056 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001057 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001059 private:
David Brazdiled596192015-01-23 10:39:45 +00001060 HUseListNode(T user, size_t index)
1061 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1062
1063 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001064 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001065 HUseListNode<T>* prev_;
1066 HUseListNode<T>* next_;
1067
1068 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001069
1070 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1071};
1072
David Brazdiled596192015-01-23 10:39:45 +00001073template <typename T>
1074class HUseList : public ValueObject {
1075 public:
1076 HUseList() : first_(nullptr) {}
1077
1078 void Clear() {
1079 first_ = nullptr;
1080 }
1081
1082 // Adds a new entry at the beginning of the use list and returns
1083 // the newly created node.
1084 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001085 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001086 if (IsEmpty()) {
1087 first_ = new_node;
1088 } else {
1089 first_->prev_ = new_node;
1090 new_node->next_ = first_;
1091 first_ = new_node;
1092 }
1093 return new_node;
1094 }
1095
1096 HUseListNode<T>* GetFirst() const {
1097 return first_;
1098 }
1099
1100 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001101 DCHECK(node != nullptr);
1102 DCHECK(Contains(node));
1103
David Brazdiled596192015-01-23 10:39:45 +00001104 if (node->prev_ != nullptr) {
1105 node->prev_->next_ = node->next_;
1106 }
1107 if (node->next_ != nullptr) {
1108 node->next_->prev_ = node->prev_;
1109 }
1110 if (node == first_) {
1111 first_ = node->next_;
1112 }
1113 }
1114
David Brazdil1abb4192015-02-17 18:33:36 +00001115 bool Contains(const HUseListNode<T>* node) const {
1116 if (node == nullptr) {
1117 return false;
1118 }
1119 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1120 if (current == node) {
1121 return true;
1122 }
1123 }
1124 return false;
1125 }
1126
David Brazdiled596192015-01-23 10:39:45 +00001127 bool IsEmpty() const {
1128 return first_ == nullptr;
1129 }
1130
1131 bool HasOnlyOneUse() const {
1132 return first_ != nullptr && first_->next_ == nullptr;
1133 }
1134
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001135 size_t SizeSlow() const {
1136 size_t count = 0;
1137 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1138 ++count;
1139 }
1140 return count;
1141 }
1142
David Brazdiled596192015-01-23 10:39:45 +00001143 private:
1144 HUseListNode<T>* first_;
1145};
1146
1147template<typename T>
1148class HUseIterator : public ValueObject {
1149 public:
1150 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1151
1152 bool Done() const { return current_ == nullptr; }
1153
1154 void Advance() {
1155 DCHECK(!Done());
1156 current_ = current_->GetNext();
1157 }
1158
1159 HUseListNode<T>* Current() const {
1160 DCHECK(!Done());
1161 return current_;
1162 }
1163
1164 private:
1165 HUseListNode<T>* current_;
1166
1167 friend class HValue;
1168};
1169
David Brazdil1abb4192015-02-17 18:33:36 +00001170// This class is used by HEnvironment and HInstruction classes to record the
1171// instructions they use and pointers to the corresponding HUseListNodes kept
1172// by the used instructions.
1173template <typename T>
1174class HUserRecord : public ValueObject {
1175 public:
1176 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1177 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1178
1179 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1180 : instruction_(old_record.instruction_), use_node_(use_node) {
1181 DCHECK(instruction_ != nullptr);
1182 DCHECK(use_node_ != nullptr);
1183 DCHECK(old_record.use_node_ == nullptr);
1184 }
1185
1186 HInstruction* GetInstruction() const { return instruction_; }
1187 HUseListNode<T>* GetUseNode() const { return use_node_; }
1188
1189 private:
1190 // Instruction used by the user.
1191 HInstruction* instruction_;
1192
1193 // Corresponding entry in the use list kept by 'instruction_'.
1194 HUseListNode<T>* use_node_;
1195};
1196
Aart Bik854a02b2015-07-14 16:07:00 -07001197/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001198 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001199 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001200 * For write/read dependences on fields/arrays, the dependence analysis uses
1201 * type disambiguation (e.g. a float field write cannot modify the value of an
1202 * integer field read) and the access type (e.g. a reference array write cannot
1203 * modify the value of a reference field read [although it may modify the
1204 * reference fetch prior to reading the field, which is represented by its own
1205 * write/read dependence]). The analysis makes conservative points-to
1206 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1207 * the same, and any reference read depends on any reference read without
1208 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001209 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001210 * The internal representation uses 38-bit and is described in the table below.
1211 * The first line indicates the side effect, and for field/array accesses the
1212 * second line indicates the type of the access (in the order of the
1213 * Primitive::Type enum).
1214 * The two numbered lines below indicate the bit position in the bitfield (read
1215 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001216 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001217 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1218 * +-------------+---------+---------+--------------+---------+---------+
1219 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1220 * | 3 |333333322|222222221| 1 |111111110|000000000|
1221 * | 7 |654321098|765432109| 8 |765432109|876543210|
1222 *
1223 * Note that, to ease the implementation, 'changes' bits are least significant
1224 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001225 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001226class SideEffects : public ValueObject {
1227 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001228 SideEffects() : flags_(0) {}
1229
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001230 static SideEffects None() {
1231 return SideEffects(0);
1232 }
1233
1234 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001235 return SideEffects(kAllChangeBits | kAllDependOnBits);
1236 }
1237
1238 static SideEffects AllChanges() {
1239 return SideEffects(kAllChangeBits);
1240 }
1241
1242 static SideEffects AllDependencies() {
1243 return SideEffects(kAllDependOnBits);
1244 }
1245
1246 static SideEffects AllExceptGCDependency() {
1247 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1248 }
1249
1250 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001251 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001252 }
1253
Aart Bik34c3ba92015-07-20 14:08:59 -07001254 static SideEffects AllWrites() {
1255 return SideEffects(kAllWrites);
1256 }
1257
1258 static SideEffects AllReads() {
1259 return SideEffects(kAllReads);
1260 }
1261
1262 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1263 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001264 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001265 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001266 }
1267
Aart Bik854a02b2015-07-14 16:07:00 -07001268 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1269 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001270 }
1271
Aart Bik34c3ba92015-07-20 14:08:59 -07001272 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1273 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001275 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001276 }
1277
1278 static SideEffects ArrayReadOfType(Primitive::Type type) {
1279 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1280 }
1281
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001282 static SideEffects CanTriggerGC() {
1283 return SideEffects(1ULL << kCanTriggerGCBit);
1284 }
1285
1286 static SideEffects DependsOnGC() {
1287 return SideEffects(1ULL << kDependsOnGCBit);
1288 }
1289
Aart Bik854a02b2015-07-14 16:07:00 -07001290 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001291 SideEffects Union(SideEffects other) const {
1292 return SideEffects(flags_ | other.flags_);
1293 }
1294
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001295 SideEffects Exclusion(SideEffects other) const {
1296 return SideEffects(flags_ & ~other.flags_);
1297 }
1298
1299 bool Includes(SideEffects other) const {
1300 return (other.flags_ & flags_) == other.flags_;
1301 }
1302
1303 bool HasSideEffects() const {
1304 return (flags_ & kAllChangeBits);
1305 }
1306
1307 bool HasDependencies() const {
1308 return (flags_ & kAllDependOnBits);
1309 }
1310
1311 // Returns true if there are no side effects or dependencies.
1312 bool DoesNothing() const {
1313 return flags_ == 0;
1314 }
1315
Aart Bik854a02b2015-07-14 16:07:00 -07001316 // Returns true if something is written.
1317 bool DoesAnyWrite() const {
1318 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001319 }
1320
Aart Bik854a02b2015-07-14 16:07:00 -07001321 // Returns true if something is read.
1322 bool DoesAnyRead() const {
1323 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001324 }
1325
Aart Bik854a02b2015-07-14 16:07:00 -07001326 // Returns true if potentially everything is written and read
1327 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 bool DoesAllReadWrite() const {
1329 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1330 }
1331
Aart Bik854a02b2015-07-14 16:07:00 -07001332 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001333 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001334 }
1335
1336 // Returns true if this may read something written by other.
1337 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1339 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001340 }
1341
1342 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001343 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001344 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001345 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001346 for (int s = kLastBit; s >= 0; s--) {
1347 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1348 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1349 // This is a bit for the GC side effect.
1350 if (current_bit_is_set) {
1351 flags += "GC";
1352 }
Aart Bik854a02b2015-07-14 16:07:00 -07001353 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001354 } else {
1355 // This is a bit for the array/field analysis.
1356 // The underscore character stands for the 'can trigger GC' bit.
1357 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1358 if (current_bit_is_set) {
1359 flags += kDebug[s];
1360 }
1361 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1362 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1363 flags += "|";
1364 }
1365 }
Aart Bik854a02b2015-07-14 16:07:00 -07001366 }
1367 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001368 }
1369
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001370 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001372 private:
1373 static constexpr int kFieldArrayAnalysisBits = 9;
1374
1375 static constexpr int kFieldWriteOffset = 0;
1376 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1377 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1378 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1379
1380 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1381
1382 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1383 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1384 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1385 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1386
1387 static constexpr int kLastBit = kDependsOnGCBit;
1388 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1389
1390 // Aliases.
1391
1392 static_assert(kChangeBits == kDependOnBits,
1393 "the 'change' bits should match the 'depend on' bits.");
1394
1395 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1396 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1397 static constexpr uint64_t kAllWrites =
1398 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1399 static constexpr uint64_t kAllReads =
1400 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001401
Aart Bik854a02b2015-07-14 16:07:00 -07001402 // Work around the fact that HIR aliases I/F and J/D.
1403 // TODO: remove this interceptor once HIR types are clean
1404 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1405 switch (type) {
1406 case Primitive::kPrimInt:
1407 case Primitive::kPrimFloat:
1408 return TypeFlag(Primitive::kPrimInt, offset) |
1409 TypeFlag(Primitive::kPrimFloat, offset);
1410 case Primitive::kPrimLong:
1411 case Primitive::kPrimDouble:
1412 return TypeFlag(Primitive::kPrimLong, offset) |
1413 TypeFlag(Primitive::kPrimDouble, offset);
1414 default:
1415 return TypeFlag(type, offset);
1416 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001417 }
1418
Aart Bik854a02b2015-07-14 16:07:00 -07001419 // Translates type to bit flag.
1420 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1421 CHECK_NE(type, Primitive::kPrimVoid);
1422 const uint64_t one = 1;
1423 const int shift = type; // 0-based consecutive enum
1424 DCHECK_LE(kFieldWriteOffset, shift);
1425 DCHECK_LT(shift, kArrayWriteOffset);
1426 return one << (type + offset);
1427 }
1428
1429 // Private constructor on direct flags value.
1430 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1431
1432 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433};
1434
David Brazdiled596192015-01-23 10:39:45 +00001435// A HEnvironment object contains the values of virtual registers at a given location.
1436class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1437 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001438 HEnvironment(ArenaAllocator* arena,
1439 size_t number_of_vregs,
1440 const DexFile& dex_file,
1441 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001442 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001443 InvokeType invoke_type,
1444 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001445 : vregs_(arena, number_of_vregs),
1446 locations_(arena, number_of_vregs),
1447 parent_(nullptr),
1448 dex_file_(dex_file),
1449 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001450 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001451 invoke_type_(invoke_type),
1452 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001453 vregs_.SetSize(number_of_vregs);
1454 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001455 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001456 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001457
1458 locations_.SetSize(number_of_vregs);
1459 for (size_t i = 0; i < number_of_vregs; ++i) {
1460 locations_.Put(i, Location());
1461 }
1462 }
1463
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001464 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001465 : HEnvironment(arena,
1466 to_copy.Size(),
1467 to_copy.GetDexFile(),
1468 to_copy.GetMethodIdx(),
1469 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001470 to_copy.GetInvokeType(),
1471 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001472
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001473 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001474 if (parent_ != nullptr) {
1475 parent_->SetAndCopyParentChain(allocator, parent);
1476 } else {
1477 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1478 parent_->CopyFrom(parent);
1479 if (parent->GetParent() != nullptr) {
1480 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1481 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001482 }
David Brazdiled596192015-01-23 10:39:45 +00001483 }
1484
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001485 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1486 void CopyFrom(HEnvironment* environment);
1487
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001488 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1489 // input to the loop phi instead. This is for inserting instructions that
1490 // require an environment (like HDeoptimization) in the loop pre-header.
1491 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001492
1493 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001494 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001495 }
1496
1497 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001498 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001499 }
1500
David Brazdil1abb4192015-02-17 18:33:36 +00001501 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001502
1503 size_t Size() const { return vregs_.Size(); }
1504
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001505 HEnvironment* GetParent() const { return parent_; }
1506
1507 void SetLocationAt(size_t index, Location location) {
1508 locations_.Put(index, location);
1509 }
1510
1511 Location GetLocationAt(size_t index) const {
1512 return locations_.Get(index);
1513 }
1514
1515 uint32_t GetDexPc() const {
1516 return dex_pc_;
1517 }
1518
1519 uint32_t GetMethodIdx() const {
1520 return method_idx_;
1521 }
1522
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001523 InvokeType GetInvokeType() const {
1524 return invoke_type_;
1525 }
1526
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001527 const DexFile& GetDexFile() const {
1528 return dex_file_;
1529 }
1530
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001531 HInstruction* GetHolder() const {
1532 return holder_;
1533 }
1534
David Brazdiled596192015-01-23 10:39:45 +00001535 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001536 // Record instructions' use entries of this environment for constant-time removal.
1537 // It should only be called by HInstruction when a new environment use is added.
1538 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1539 DCHECK(env_use->GetUser() == this);
1540 size_t index = env_use->GetIndex();
1541 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1542 }
David Brazdiled596192015-01-23 10:39:45 +00001543
David Brazdil1abb4192015-02-17 18:33:36 +00001544 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001545 GrowableArray<Location> locations_;
1546 HEnvironment* parent_;
1547 const DexFile& dex_file_;
1548 const uint32_t method_idx_;
1549 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001550 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001551
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001552 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001553 HInstruction* const holder_;
1554
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001555 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001556
1557 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1558};
1559
Calin Juravleacf735c2015-02-12 15:25:22 +00001560class ReferenceTypeInfo : ValueObject {
1561 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001562 typedef Handle<mirror::Class> TypeHandle;
1563
Calin Juravle2e768302015-07-28 14:41:11 +00001564 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1565 // The constructor will check that the type_handle is valid.
1566 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001567 }
1568
Calin Juravle2e768302015-07-28 14:41:11 +00001569 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1570
1571 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1572 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001573 }
1574
Calin Juravle2e768302015-07-28 14:41:11 +00001575 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1576 return IsValidHandle(type_handle_);
1577 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001578 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001579
1580 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1581 DCHECK(IsValid());
1582 return GetTypeHandle()->IsObjectClass();
1583 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001584 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001585 DCHECK(IsValid());
1586 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001587 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001588
1589 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1590
Mathieu Chartier90443472015-07-16 20:32:27 -07001591 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001592 DCHECK(IsValid());
1593 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001594 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1595 }
1596
1597 // Returns true if the type information provide the same amount of details.
1598 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001599 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001600 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001601 if (!IsValid() && !rti.IsValid()) {
1602 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001603 return true;
1604 }
Calin Juravle2e768302015-07-28 14:41:11 +00001605 if (!IsValid() || !rti.IsValid()) {
1606 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001607 return false;
1608 }
Calin Juravle2e768302015-07-28 14:41:11 +00001609 return IsExact() == rti.IsExact()
1610 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001611 }
1612
1613 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001614 ReferenceTypeInfo();
1615 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001616
Calin Juravleacf735c2015-02-12 15:25:22 +00001617 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001618 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001619 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001620 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001621 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001622};
1623
1624std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1625
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001626class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001627 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001629 : previous_(nullptr),
1630 next_(nullptr),
1631 block_(nullptr),
1632 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001633 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001634 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001635 locations_(nullptr),
1636 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001637 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001638 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001639 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640
Dave Allison20dfc792014-06-16 20:44:29 -07001641 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001642
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001643#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001644 enum InstructionKind {
1645 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1646 };
1647#undef DECLARE_KIND
1648
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001649 HInstruction* GetNext() const { return next_; }
1650 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001651
Calin Juravle77520bc2015-01-12 18:45:46 +00001652 HInstruction* GetNextDisregardingMoves() const;
1653 HInstruction* GetPreviousDisregardingMoves() const;
1654
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001655 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001656 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001657 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001658 bool IsInBlock() const { return block_ != nullptr; }
1659 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001660 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001661
Roland Levillain6b879dd2014-09-22 17:13:44 +01001662 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001663 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001664
1665 virtual void Accept(HGraphVisitor* visitor) = 0;
1666 virtual const char* DebugName() const = 0;
1667
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001668 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001669 void SetRawInputAt(size_t index, HInstruction* input) {
1670 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1671 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001673 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001674 virtual uint32_t GetDexPc() const {
1675 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1676 " does not need an environment";
1677 UNREACHABLE();
1678 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001679 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001680 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001681
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001683 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001684
Calin Juravle10e244f2015-01-26 18:54:32 +00001685 // Does not apply for all instructions, but having this at top level greatly
1686 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001687 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001688 virtual bool CanBeNull() const {
1689 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1690 return true;
1691 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001692
Calin Juravle641547a2015-04-21 22:08:51 +01001693 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1694 UNUSED(obj);
1695 return false;
1696 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001697
Calin Juravle2e768302015-07-28 14:41:11 +00001698 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001699
Calin Juravle61d544b2015-02-23 16:46:57 +00001700 ReferenceTypeInfo GetReferenceTypeInfo() const {
1701 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1702 return reference_type_info_;
1703 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001704
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001706 DCHECK(user != nullptr);
1707 HUseListNode<HInstruction*>* use =
1708 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1709 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001710 }
1711
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001712 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001713 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001714 HUseListNode<HEnvironment*>* env_use =
1715 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1716 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001717 }
1718
David Brazdil1abb4192015-02-17 18:33:36 +00001719 void RemoveAsUserOfInput(size_t input) {
1720 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1721 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1722 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001723
David Brazdil1abb4192015-02-17 18:33:36 +00001724 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1725 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001726
David Brazdiled596192015-01-23 10:39:45 +00001727 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1728 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001729 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001730 bool HasOnlyOneNonEnvironmentUse() const {
1731 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1732 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001733
Roland Levillain6c82d402014-10-13 16:10:27 +01001734 // Does this instruction strictly dominate `other_instruction`?
1735 // Returns false if this instruction and `other_instruction` are the same.
1736 // Aborts if this instruction and `other_instruction` are both phis.
1737 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001738
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001739 int GetId() const { return id_; }
1740 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001742 int GetSsaIndex() const { return ssa_index_; }
1743 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1744 bool HasSsaIndex() const { return ssa_index_ != -1; }
1745
1746 bool HasEnvironment() const { return environment_ != nullptr; }
1747 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001748 // Set the `environment_` field. Raw because this method does not
1749 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 void SetRawEnvironment(HEnvironment* environment) {
1751 DCHECK(environment_ == nullptr);
1752 DCHECK_EQ(environment->GetHolder(), this);
1753 environment_ = environment;
1754 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001755
1756 // Set the environment of this instruction, copying it from `environment`. While
1757 // copying, the uses lists are being updated.
1758 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001759 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001760 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001761 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001762 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001763 if (environment->GetParent() != nullptr) {
1764 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1765 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001766 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001767
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001768 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1769 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001770 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001771 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001772 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001773 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 if (environment->GetParent() != nullptr) {
1775 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1776 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001777 }
1778
Nicolas Geoffray39468442014-09-02 15:17:15 +01001779 // Returns the number of entries in the environment. Typically, that is the
1780 // number of dex registers in a method. It could be more in case of inlining.
1781 size_t EnvironmentSize() const;
1782
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 LocationSummary* GetLocations() const { return locations_; }
1784 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001787 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001788
Alexandre Rames188d4312015-04-09 18:30:21 +01001789 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1790 // uses of this instruction by `other` are *not* updated.
1791 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1792 ReplaceWith(other);
1793 other->ReplaceInput(this, use_index);
1794 }
1795
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001796 // Move `this` instruction before `cursor`.
1797 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001798
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001799#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001800 bool Is##type() const { return (As##type() != nullptr); } \
1801 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 virtual H##type* As##type() { return nullptr; }
1803
1804 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1805#undef INSTRUCTION_TYPE_CHECK
1806
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001807 // Returns whether the instruction can be moved within the graph.
1808 virtual bool CanBeMoved() const { return false; }
1809
1810 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001811 virtual bool InstructionTypeEquals(HInstruction* other) const {
1812 UNUSED(other);
1813 return false;
1814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001815
1816 // Returns whether any data encoded in the two instructions is equal.
1817 // This method does not look at the inputs. Both instructions must be
1818 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001819 virtual bool InstructionDataEquals(HInstruction* other) const {
1820 UNUSED(other);
1821 return false;
1822 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
1824 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001825 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001826 // 2) Their inputs are identical.
1827 bool Equals(HInstruction* other) const;
1828
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001829 virtual InstructionKind GetKind() const = 0;
1830
1831 virtual size_t ComputeHashCode() const {
1832 size_t result = GetKind();
1833 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1834 result = (result * 31) + InputAt(i)->GetId();
1835 }
1836 return result;
1837 }
1838
1839 SideEffects GetSideEffects() const { return side_effects_; }
1840
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001841 size_t GetLifetimePosition() const { return lifetime_position_; }
1842 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1843 LiveInterval* GetLiveInterval() const { return live_interval_; }
1844 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1845 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1846
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001847 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1848
1849 // Returns whether the code generation of the instruction will require to have access
1850 // to the current method. Such instructions are:
1851 // (1): Instructions that require an environment, as calling the runtime requires
1852 // to walk the stack and have the current method stored at a specific stack address.
1853 // (2): Object literals like classes and strings, that are loaded from the dex cache
1854 // fields of the current method.
1855 bool NeedsCurrentMethod() const {
1856 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1857 }
1858
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001859 virtual bool NeedsDexCache() const { return false; }
1860
Mark Mendellc4701932015-04-10 13:18:51 -04001861 // Does this instruction have any use in an environment before
1862 // control flow hits 'other'?
1863 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1864
1865 // Remove all references to environment uses of this instruction.
1866 // The caller must ensure that this is safe to do.
1867 void RemoveEnvironmentUsers();
1868
David Brazdil1abb4192015-02-17 18:33:36 +00001869 protected:
1870 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1871 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1872
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001873 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001874 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1875
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001876 HInstruction* previous_;
1877 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001878 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001879
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880 // An instruction gets an id when it is added to the graph.
1881 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001882 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001883 int id_;
1884
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001885 // When doing liveness analysis, instructions that have uses get an SSA index.
1886 int ssa_index_;
1887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001888 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001889 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890
1891 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001892 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893
Nicolas Geoffray39468442014-09-02 15:17:15 +01001894 // The environment associated with this instruction. Not null if the instruction
1895 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001898 // Set by the code generator.
1899 LocationSummary* locations_;
1900
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001901 // Set by the liveness analysis.
1902 LiveInterval* live_interval_;
1903
1904 // Set by the liveness analysis, this is the position in a linear
1905 // order of blocks where this instruction's live interval start.
1906 size_t lifetime_position_;
1907
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001908 const SideEffects side_effects_;
1909
Calin Juravleacf735c2015-02-12 15:25:22 +00001910 // TODO: for primitive types this should be marked as invalid.
1911 ReferenceTypeInfo reference_type_info_;
1912
David Brazdil1abb4192015-02-17 18:33:36 +00001913 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001914 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001915 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001916 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001918
1919 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1920};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001921std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001922
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923class HInputIterator : public ValueObject {
1924 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001925 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001926
1927 bool Done() const { return index_ == instruction_->InputCount(); }
1928 HInstruction* Current() const { return instruction_->InputAt(index_); }
1929 void Advance() { index_++; }
1930
1931 private:
1932 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001933 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934
1935 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1936};
1937
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938class HInstructionIterator : public ValueObject {
1939 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940 explicit HInstructionIterator(const HInstructionList& instructions)
1941 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001942 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943 }
1944
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001945 bool Done() const { return instruction_ == nullptr; }
1946 HInstruction* Current() const { return instruction_; }
1947 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001949 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001950 }
1951
1952 private:
1953 HInstruction* instruction_;
1954 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001955
1956 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957};
1958
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001959class HBackwardInstructionIterator : public ValueObject {
1960 public:
1961 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1962 : instruction_(instructions.last_instruction_) {
1963 next_ = Done() ? nullptr : instruction_->GetPrevious();
1964 }
1965
1966 bool Done() const { return instruction_ == nullptr; }
1967 HInstruction* Current() const { return instruction_; }
1968 void Advance() {
1969 instruction_ = next_;
1970 next_ = Done() ? nullptr : instruction_->GetPrevious();
1971 }
1972
1973 private:
1974 HInstruction* instruction_;
1975 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001976
1977 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001978};
1979
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001980// An embedded container with N elements of type T. Used (with partial
1981// specialization for N=0) because embedded arrays cannot have size 0.
1982template<typename T, intptr_t N>
1983class EmbeddedArray {
1984 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001985 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001987 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
1989 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991 return elements_[i];
1992 }
1993
1994 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001995 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996 return elements_[i];
1997 }
1998
1999 const T& At(intptr_t i) const {
2000 return (*this)[i];
2001 }
2002
2003 void SetAt(intptr_t i, const T& val) {
2004 (*this)[i] = val;
2005 }
2006
2007 private:
2008 T elements_[N];
2009};
2010
2011template<typename T>
2012class EmbeddedArray<T, 0> {
2013 public:
2014 intptr_t length() const { return 0; }
2015 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002016 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002018 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 }
2020 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002021 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002023 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 }
2025};
2026
2027template<intptr_t N>
2028class HTemplateInstruction: public HInstruction {
2029 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030 HTemplateInstruction<N>(SideEffects side_effects)
2031 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002032 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002034 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002035
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002037 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2038
2039 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2040 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041 }
2042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002044 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002045
2046 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047};
2048
Dave Allison20dfc792014-06-16 20:44:29 -07002049template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002050class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002051 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2053 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002054 virtual ~HExpression() {}
2055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002056 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002057
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 protected:
2059 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002060};
2061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2063// instruction that branches to the exit block.
2064class HReturnVoid : public HTemplateInstruction<0> {
2065 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002067
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002068 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002070 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2074};
2075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002076// Represents dex's RETURN opcodes. A HReturn is a control flow
2077// instruction that branches to the exit block.
2078class HReturn : public HTemplateInstruction<1> {
2079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002081 SetRawInputAt(0, value);
2082 }
2083
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002084 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002085
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002086 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002087
2088 private:
2089 DISALLOW_COPY_AND_ASSIGN(HReturn);
2090};
2091
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002093// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094// exit block.
2095class HExit : public HTemplateInstruction<0> {
2096 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002097 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002101 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102
2103 private:
2104 DISALLOW_COPY_AND_ASSIGN(HExit);
2105};
2106
2107// Jumps from one block to another.
2108class HGoto : public HTemplateInstruction<0> {
2109 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2111
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002112 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002114 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002115 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002116 }
2117
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002118 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
2120 private:
2121 DISALLOW_COPY_AND_ASSIGN(HGoto);
2122};
2123
Roland Levillain9867bc72015-08-05 10:21:34 +01002124class HConstant : public HExpression<0> {
2125 public:
2126 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2127
2128 bool CanBeMoved() const OVERRIDE { return true; }
2129
2130 virtual bool IsMinusOne() const { return false; }
2131 virtual bool IsZero() const { return false; }
2132 virtual bool IsOne() const { return false; }
2133
2134 DECLARE_INSTRUCTION(Constant);
2135
2136 private:
2137 DISALLOW_COPY_AND_ASSIGN(HConstant);
2138};
2139
2140class HNullConstant : public HConstant {
2141 public:
2142 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2143 return true;
2144 }
2145
2146 size_t ComputeHashCode() const OVERRIDE { return 0; }
2147
2148 DECLARE_INSTRUCTION(NullConstant);
2149
2150 private:
2151 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2152
2153 friend class HGraph;
2154 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2155};
2156
2157// Constants of the type int. Those can be from Dex instructions, or
2158// synthesized (for example with the if-eqz instruction).
2159class HIntConstant : public HConstant {
2160 public:
2161 int32_t GetValue() const { return value_; }
2162
2163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2164 DCHECK(other->IsIntConstant());
2165 return other->AsIntConstant()->value_ == value_;
2166 }
2167
2168 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2169
2170 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2171 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2172 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2173
2174 DECLARE_INSTRUCTION(IntConstant);
2175
2176 private:
2177 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2178 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2179
2180 const int32_t value_;
2181
2182 friend class HGraph;
2183 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2184 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2185 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2186};
2187
2188class HLongConstant : public HConstant {
2189 public:
2190 int64_t GetValue() const { return value_; }
2191
2192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2193 DCHECK(other->IsLongConstant());
2194 return other->AsLongConstant()->value_ == value_;
2195 }
2196
2197 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2198
2199 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2200 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2201 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2202
2203 DECLARE_INSTRUCTION(LongConstant);
2204
2205 private:
2206 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2207
2208 const int64_t value_;
2209
2210 friend class HGraph;
2211 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2212};
Dave Allison20dfc792014-06-16 20:44:29 -07002213
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002214// Conditional branch. A block ending with an HIf instruction must have
2215// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002217 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002219 SetRawInputAt(0, input);
2220 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002221
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002222 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002223
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002224 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002225 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002226 }
2227
2228 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002229 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002230 }
2231
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002232 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002233
2234 private:
2235 DISALLOW_COPY_AND_ASSIGN(HIf);
2236};
2237
David Brazdilfc6a86a2015-06-26 10:33:45 +00002238
2239// Abstract instruction which marks the beginning and/or end of a try block and
2240// links it to the respective exception handlers. Behaves the same as a Goto in
2241// non-exceptional control flow.
2242// Normal-flow successor is stored at index zero, exception handlers under
2243// higher indices in no particular order.
2244class HTryBoundary : public HTemplateInstruction<0> {
2245 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002246 enum BoundaryKind {
2247 kEntry,
2248 kExit,
2249 };
2250
2251 explicit HTryBoundary(BoundaryKind kind)
2252 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002253
2254 bool IsControlFlow() const OVERRIDE { return true; }
2255
2256 // Returns the block's non-exceptional successor (index zero).
2257 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2258
2259 // Returns whether `handler` is among its exception handlers (non-zero index
2260 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002261 bool HasExceptionHandler(const HBasicBlock& handler) const {
2262 DCHECK(handler.IsCatchBlock());
2263 return GetBlock()->GetSuccessors().Contains(
2264 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002265 }
2266
2267 // If not present already, adds `handler` to its block's list of exception
2268 // handlers.
2269 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002270 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002271 GetBlock()->AddSuccessor(handler);
2272 }
2273 }
2274
David Brazdil56e1acc2015-06-30 15:41:36 +01002275 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002276
David Brazdilffee3d32015-07-06 11:48:53 +01002277 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2278
David Brazdilfc6a86a2015-06-26 10:33:45 +00002279 DECLARE_INSTRUCTION(TryBoundary);
2280
2281 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002282 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002283
2284 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2285};
2286
David Brazdilffee3d32015-07-06 11:48:53 +01002287// Iterator over exception handlers of a given HTryBoundary, i.e. over
2288// exceptional successors of its basic block.
2289class HExceptionHandlerIterator : public ValueObject {
2290 public:
2291 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2292 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2293
2294 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2295 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2296 size_t CurrentSuccessorIndex() const { return index_; }
2297 void Advance() { ++index_; }
2298
2299 private:
2300 const HBasicBlock& block_;
2301 size_t index_;
2302
2303 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2304};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002305
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002306// Deoptimize to interpreter, upon checking a condition.
2307class HDeoptimize : public HTemplateInstruction<1> {
2308 public:
2309 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2310 : HTemplateInstruction(SideEffects::None()),
2311 dex_pc_(dex_pc) {
2312 SetRawInputAt(0, cond);
2313 }
2314
2315 bool NeedsEnvironment() const OVERRIDE { return true; }
2316 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002317 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002318
2319 DECLARE_INSTRUCTION(Deoptimize);
2320
2321 private:
2322 uint32_t dex_pc_;
2323
2324 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2325};
2326
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002327// Represents the ArtMethod that was passed as a first argument to
2328// the method. It is used by instructions that depend on it, like
2329// instructions that work with the dex cache.
2330class HCurrentMethod : public HExpression<0> {
2331 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002332 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002333
2334 DECLARE_INSTRUCTION(CurrentMethod);
2335
2336 private:
2337 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2338};
2339
Roland Levillain88cb1752014-10-20 16:36:47 +01002340class HUnaryOperation : public HExpression<1> {
2341 public:
2342 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2343 : HExpression(result_type, SideEffects::None()) {
2344 SetRawInputAt(0, input);
2345 }
2346
2347 HInstruction* GetInput() const { return InputAt(0); }
2348 Primitive::Type GetResultType() const { return GetType(); }
2349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002350 bool CanBeMoved() const OVERRIDE { return true; }
2351 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002352 UNUSED(other);
2353 return true;
2354 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002355
Roland Levillain9240d6a2014-10-20 16:47:04 +01002356 // Try to statically evaluate `operation` and return a HConstant
2357 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002358 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002359 HConstant* TryStaticEvaluation() const;
2360
2361 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002362 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2363 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002364
Roland Levillain88cb1752014-10-20 16:36:47 +01002365 DECLARE_INSTRUCTION(UnaryOperation);
2366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2369};
2370
Dave Allison20dfc792014-06-16 20:44:29 -07002371class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002372 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002373 HBinaryOperation(Primitive::Type result_type,
2374 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002375 HInstruction* right,
2376 SideEffects side_effects = SideEffects::None())
2377 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002378 SetRawInputAt(0, left);
2379 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002380 }
2381
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002382 HInstruction* GetLeft() const { return InputAt(0); }
2383 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002384 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002385
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002386 virtual bool IsCommutative() const { return false; }
2387
2388 // Put constant on the right.
2389 // Returns whether order is changed.
2390 bool OrderInputsWithConstantOnTheRight() {
2391 HInstruction* left = InputAt(0);
2392 HInstruction* right = InputAt(1);
2393 if (left->IsConstant() && !right->IsConstant()) {
2394 ReplaceInput(right, 0);
2395 ReplaceInput(left, 1);
2396 return true;
2397 }
2398 return false;
2399 }
2400
2401 // Order inputs by instruction id, but favor constant on the right side.
2402 // This helps GVN for commutative ops.
2403 void OrderInputs() {
2404 DCHECK(IsCommutative());
2405 HInstruction* left = InputAt(0);
2406 HInstruction* right = InputAt(1);
2407 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2408 return;
2409 }
2410 if (OrderInputsWithConstantOnTheRight()) {
2411 return;
2412 }
2413 // Order according to instruction id.
2414 if (left->GetId() > right->GetId()) {
2415 ReplaceInput(right, 0);
2416 ReplaceInput(left, 1);
2417 }
2418 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 bool CanBeMoved() const OVERRIDE { return true; }
2421 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002422 UNUSED(other);
2423 return true;
2424 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002425
Roland Levillain9240d6a2014-10-20 16:47:04 +01002426 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002427 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002428 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002429 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002430
2431 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002432 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2433 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2434 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2435 HLongConstant* y ATTRIBUTE_UNUSED) const {
2436 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2437 return nullptr;
2438 }
2439 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2440 HIntConstant* y ATTRIBUTE_UNUSED) const {
2441 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2442 return nullptr;
2443 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002444
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002445 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002446 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002447 HConstant* GetConstantRight() const;
2448
2449 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002450 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002451 HInstruction* GetLeastConstantLeft() const;
2452
Roland Levillainccc07a92014-09-16 14:48:16 +01002453 DECLARE_INSTRUCTION(BinaryOperation);
2454
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002455 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002456 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2457};
2458
Mark Mendellc4701932015-04-10 13:18:51 -04002459// The comparison bias applies for floating point operations and indicates how NaN
2460// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002461enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002462 kNoBias, // bias is not applicable (i.e. for long operation)
2463 kGtBias, // return 1 for NaN comparisons
2464 kLtBias, // return -1 for NaN comparisons
2465};
2466
Dave Allison20dfc792014-06-16 20:44:29 -07002467class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002468 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002469 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002470 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002471 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002472 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002473
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002474 bool NeedsMaterialization() const { return needs_materialization_; }
2475 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002476
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002477 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002478 // `instruction`, and disregard moves in between.
2479 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002480
Dave Allison20dfc792014-06-16 20:44:29 -07002481 DECLARE_INSTRUCTION(Condition);
2482
2483 virtual IfCondition GetCondition() const = 0;
2484
Mark Mendellc4701932015-04-10 13:18:51 -04002485 virtual IfCondition GetOppositeCondition() const = 0;
2486
Roland Levillain4fa13f62015-07-06 18:11:54 +01002487 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002488
2489 void SetBias(ComparisonBias bias) { bias_ = bias; }
2490
2491 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2492 return bias_ == other->AsCondition()->bias_;
2493 }
2494
Roland Levillain4fa13f62015-07-06 18:11:54 +01002495 bool IsFPConditionTrueIfNaN() const {
2496 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2497 IfCondition if_cond = GetCondition();
2498 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2499 }
2500
2501 bool IsFPConditionFalseIfNaN() const {
2502 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2503 IfCondition if_cond = GetCondition();
2504 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2505 }
2506
Dave Allison20dfc792014-06-16 20:44:29 -07002507 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002508 // For register allocation purposes, returns whether this instruction needs to be
2509 // materialized (that is, not just be in the processor flags).
2510 bool needs_materialization_;
2511
Mark Mendellc4701932015-04-10 13:18:51 -04002512 // Needed if we merge a HCompare into a HCondition.
2513 ComparisonBias bias_;
2514
Dave Allison20dfc792014-06-16 20:44:29 -07002515 DISALLOW_COPY_AND_ASSIGN(HCondition);
2516};
2517
2518// Instruction to check if two inputs are equal to each other.
2519class HEqual : public HCondition {
2520 public:
2521 HEqual(HInstruction* first, HInstruction* second)
2522 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002523
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002524 bool IsCommutative() const OVERRIDE { return true; }
2525
Roland Levillain9867bc72015-08-05 10:21:34 +01002526 template <typename T> bool Compute(T x, T y) const { return x == y; }
2527
2528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2529 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002530 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2532 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002533 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002535 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002537 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002538 return kCondEQ;
2539 }
2540
Mark Mendellc4701932015-04-10 13:18:51 -04002541 IfCondition GetOppositeCondition() const OVERRIDE {
2542 return kCondNE;
2543 }
2544
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545 private:
2546 DISALLOW_COPY_AND_ASSIGN(HEqual);
2547};
2548
Dave Allison20dfc792014-06-16 20:44:29 -07002549class HNotEqual : public HCondition {
2550 public:
2551 HNotEqual(HInstruction* first, HInstruction* second)
2552 : HCondition(first, second) {}
2553
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002554 bool IsCommutative() const OVERRIDE { return true; }
2555
Roland Levillain9867bc72015-08-05 10:21:34 +01002556 template <typename T> bool Compute(T x, T y) const { return x != y; }
2557
2558 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2559 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002560 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2562 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002563 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002564
Dave Allison20dfc792014-06-16 20:44:29 -07002565 DECLARE_INSTRUCTION(NotEqual);
2566
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002567 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002568 return kCondNE;
2569 }
2570
Mark Mendellc4701932015-04-10 13:18:51 -04002571 IfCondition GetOppositeCondition() const OVERRIDE {
2572 return kCondEQ;
2573 }
2574
Dave Allison20dfc792014-06-16 20:44:29 -07002575 private:
2576 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2577};
2578
2579class HLessThan : public HCondition {
2580 public:
2581 HLessThan(HInstruction* first, HInstruction* second)
2582 : HCondition(first, second) {}
2583
Roland Levillain9867bc72015-08-05 10:21:34 +01002584 template <typename T> bool Compute(T x, T y) const { return x < y; }
2585
2586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2587 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2590 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002591 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002592
Dave Allison20dfc792014-06-16 20:44:29 -07002593 DECLARE_INSTRUCTION(LessThan);
2594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002595 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002596 return kCondLT;
2597 }
2598
Mark Mendellc4701932015-04-10 13:18:51 -04002599 IfCondition GetOppositeCondition() const OVERRIDE {
2600 return kCondGE;
2601 }
2602
Dave Allison20dfc792014-06-16 20:44:29 -07002603 private:
2604 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2605};
2606
2607class HLessThanOrEqual : public HCondition {
2608 public:
2609 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2610 : HCondition(first, second) {}
2611
Roland Levillain9867bc72015-08-05 10:21:34 +01002612 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2613
2614 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2615 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002616 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2618 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002619 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002620
Dave Allison20dfc792014-06-16 20:44:29 -07002621 DECLARE_INSTRUCTION(LessThanOrEqual);
2622
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002623 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002624 return kCondLE;
2625 }
2626
Mark Mendellc4701932015-04-10 13:18:51 -04002627 IfCondition GetOppositeCondition() const OVERRIDE {
2628 return kCondGT;
2629 }
2630
Dave Allison20dfc792014-06-16 20:44:29 -07002631 private:
2632 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2633};
2634
2635class HGreaterThan : public HCondition {
2636 public:
2637 HGreaterThan(HInstruction* first, HInstruction* second)
2638 : HCondition(first, second) {}
2639
Roland Levillain9867bc72015-08-05 10:21:34 +01002640 template <typename T> bool Compute(T x, T y) const { return x > y; }
2641
2642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2643 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002644 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002645 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2646 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002647 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002648
Dave Allison20dfc792014-06-16 20:44:29 -07002649 DECLARE_INSTRUCTION(GreaterThan);
2650
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002651 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002652 return kCondGT;
2653 }
2654
Mark Mendellc4701932015-04-10 13:18:51 -04002655 IfCondition GetOppositeCondition() const OVERRIDE {
2656 return kCondLE;
2657 }
2658
Dave Allison20dfc792014-06-16 20:44:29 -07002659 private:
2660 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2661};
2662
2663class HGreaterThanOrEqual : public HCondition {
2664 public:
2665 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2666 : HCondition(first, second) {}
2667
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2669
2670 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2671 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002672 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002673 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2674 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002675 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002676
Dave Allison20dfc792014-06-16 20:44:29 -07002677 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2678
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002679 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002680 return kCondGE;
2681 }
2682
Mark Mendellc4701932015-04-10 13:18:51 -04002683 IfCondition GetOppositeCondition() const OVERRIDE {
2684 return kCondLT;
2685 }
2686
Dave Allison20dfc792014-06-16 20:44:29 -07002687 private:
2688 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2689};
2690
2691
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002692// Instruction to check how two inputs compare to each other.
2693// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2694class HCompare : public HBinaryOperation {
2695 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002696 HCompare(Primitive::Type type,
2697 HInstruction* first,
2698 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002699 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002700 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002701 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2702 bias_(bias),
2703 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002704 DCHECK_EQ(type, first->GetType());
2705 DCHECK_EQ(type, second->GetType());
2706 }
2707
Roland Levillain9867bc72015-08-05 10:21:34 +01002708 template <typename T>
2709 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002710
Roland Levillain9867bc72015-08-05 10:21:34 +01002711 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2712 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2713 }
2714 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2715 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002716 }
2717
Calin Juravleddb7df22014-11-25 20:56:51 +00002718 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2719 return bias_ == other->AsCompare()->bias_;
2720 }
2721
Mark Mendellc4701932015-04-10 13:18:51 -04002722 ComparisonBias GetBias() const { return bias_; }
2723
Roland Levillain4fa13f62015-07-06 18:11:54 +01002724 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002725
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002726 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2727
2728 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2729 // MIPS64 uses a runtime call for FP comparisons.
2730 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2731 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002732
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002733 DECLARE_INSTRUCTION(Compare);
2734
2735 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002736 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002737 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002738
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002739 DISALLOW_COPY_AND_ASSIGN(HCompare);
2740};
2741
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002742// A local in the graph. Corresponds to a Dex register.
2743class HLocal : public HTemplateInstruction<0> {
2744 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002745 explicit HLocal(uint16_t reg_number)
2746 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002748 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002749
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002750 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002753 // The Dex register number.
2754 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002755
2756 DISALLOW_COPY_AND_ASSIGN(HLocal);
2757};
2758
2759// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002760class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002761 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002762 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002763 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002764 SetRawInputAt(0, local);
2765 }
2766
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002767 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2768
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002769 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002770
2771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2773};
2774
2775// Store a value in a given local. This instruction has two inputs: the value
2776// and the local.
2777class HStoreLocal : public HTemplateInstruction<2> {
2778 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002779 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002780 SetRawInputAt(0, local);
2781 SetRawInputAt(1, value);
2782 }
2783
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002784 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002786 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002787
2788 private:
2789 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2790};
2791
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002792class HFloatConstant : public HConstant {
2793 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002794 float GetValue() const { return value_; }
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002797 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002798 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2799 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002800 }
2801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002802 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002803
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002804 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002805 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002806 }
2807 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002808 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002809 }
2810 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002811 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2812 }
2813 bool IsNaN() const {
2814 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002815 }
2816
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002817 DECLARE_INSTRUCTION(FloatConstant);
2818
2819 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002820 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002821 explicit HFloatConstant(int32_t value)
2822 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002823
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002824 const float value_;
2825
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002826 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002827 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002828 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002829 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2830};
2831
2832class HDoubleConstant : public HConstant {
2833 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002834 double GetValue() const { return value_; }
2835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002836 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002838 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2839 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002840 }
2841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002842 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002843
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002844 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002845 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002846 }
2847 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002848 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002849 }
2850 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002851 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2852 }
2853 bool IsNaN() const {
2854 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002855 }
2856
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002857 DECLARE_INSTRUCTION(DoubleConstant);
2858
2859 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002860 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002861 explicit HDoubleConstant(int64_t value)
2862 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002863
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002864 const double value_;
2865
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002866 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002867 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002868 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002869 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2870};
2871
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002872enum class Intrinsics {
agicsaki57b81ec2015-08-11 17:39:37 -07002873#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002874#include "intrinsics_list.h"
2875 kNone,
2876 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2877#undef INTRINSICS_LIST
2878#undef OPTIMIZING_INTRINSICS
2879};
2880std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2881
agicsaki57b81ec2015-08-11 17:39:37 -07002882enum IntrinsicNeedsEnvironment {
2883 kNoEnvironment, // Intrinsic does not require an environment.
2884 kNeedsEnvironment // Intrinsic requires an environment.
2885};
2886
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002887class HInvoke : public HInstruction {
2888 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002889 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002890
2891 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2892 // know their environment.
agicsaki57b81ec2015-08-11 17:39:37 -07002893 bool NeedsEnvironment() const OVERRIDE { return needs_environment_ == kNeedsEnvironment; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002894
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002895 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002896 SetRawInputAt(index, argument);
2897 }
2898
Roland Levillain3e3d7332015-04-28 11:00:54 +01002899 // Return the number of arguments. This number can be lower than
2900 // the number of inputs returned by InputCount(), as some invoke
2901 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2902 // inputs at the end of their list of inputs.
2903 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002905 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002906
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002907 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002908
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002909 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002910 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002911
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002912 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2913
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002914 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002915 return intrinsic_;
2916 }
2917
agicsaki57b81ec2015-08-11 17:39:37 -07002918 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironment needs_environment) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002919 intrinsic_ = intrinsic;
agicsaki57b81ec2015-08-11 17:39:37 -07002920 needs_environment_ = needs_environment;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002921 }
2922
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002923 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002924 return GetEnvironment()->GetParent() != nullptr;
2925 }
2926
2927 bool CanThrow() const OVERRIDE { return true; }
2928
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002929 DECLARE_INSTRUCTION(Invoke);
2930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002931 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002932 HInvoke(ArenaAllocator* arena,
2933 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002934 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002935 Primitive::Type return_type,
2936 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002937 uint32_t dex_method_index,
2938 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002939 : HInstruction(
2940 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002941 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002942 inputs_(arena, number_of_arguments),
2943 return_type_(return_type),
2944 dex_pc_(dex_pc),
2945 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002946 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002947 intrinsic_(Intrinsics::kNone),
2948 needs_environment_(kNeedsEnvironment) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002949 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2950 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002951 }
2952
David Brazdil1abb4192015-02-17 18:33:36 +00002953 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2954 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2955 inputs_.Put(index, input);
2956 }
2957
Roland Levillain3e3d7332015-04-28 11:00:54 +01002958 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002959 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002960 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002961 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002962 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002963 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002964 Intrinsics intrinsic_;
agicsaki57b81ec2015-08-11 17:39:37 -07002965 IntrinsicNeedsEnvironment needs_environment_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002966
2967 private:
2968 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2969};
2970
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002971class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002972 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002973 // Requirements of this method call regarding the class
2974 // initialization (clinit) check of its declaring class.
2975 enum class ClinitCheckRequirement {
2976 kNone, // Class already initialized.
2977 kExplicit, // Static call having explicit clinit check as last input.
2978 kImplicit, // Static call implicitly requiring a clinit check.
2979 };
2980
Vladimir Marko58155012015-08-19 12:49:41 +00002981 // Determines how to load the target ArtMethod*.
2982 enum class MethodLoadKind {
2983 // Use a String init ArtMethod* loaded from Thread entrypoints.
2984 kStringInit,
2985
2986 // Use the method's own ArtMethod* loaded by the register allocator.
2987 kRecursive,
2988
2989 // Use ArtMethod* at a known address, embed the direct address in the code.
2990 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
2991 kDirectAddress,
2992
2993 // Use ArtMethod* at an address that will be known at link time, embed the direct
2994 // address in the code. If the image is relocatable, emit .patch_oat entry.
2995 // Used for app->boot calls with relocatable image and boot->boot calls, whether
2996 // the image relocatable or not.
2997 kDirectAddressWithFixup,
2998
2999 // Load from resoved methods array in the dex cache using a PC-relative load.
3000 // Used when we need to use the dex cache, for example for invoke-static that
3001 // may cause class initialization (the entry may point to a resolution method),
3002 // and we know that we can access the dex cache arrays using a PC-relative load.
3003 kDexCachePcRelative,
3004
3005 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3006 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3007 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3008 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3009 kDexCacheViaMethod,
3010 };
3011
3012 // Determines the location of the code pointer.
3013 enum class CodePtrLocation {
3014 // Recursive call, use local PC-relative call instruction.
3015 kCallSelf,
3016
3017 // Use PC-relative call instruction patched at link time.
3018 // Used for calls within an oat file, boot->boot or app->app.
3019 kCallPCRelative,
3020
3021 // Call to a known target address, embed the direct address in code.
3022 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3023 kCallDirect,
3024
3025 // Call to a target address that will be known at link time, embed the direct
3026 // address in code. If the image is relocatable, emit .patch_oat entry.
3027 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3028 // the image relocatable or not.
3029 kCallDirectWithFixup,
3030
3031 // Use code pointer from the ArtMethod*.
3032 // Used when we don't know the target code. This is also the last-resort-kind used when
3033 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3034 kCallArtMethod,
3035 };
3036
3037 struct DispatchInfo {
3038 const MethodLoadKind method_load_kind;
3039 const CodePtrLocation code_ptr_location;
3040 // The method load data holds
3041 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3042 // Note that there are multiple string init methods, each having its own offset.
3043 // - the method address for kDirectAddress
3044 // - the dex cache arrays offset for kDexCachePcRel.
3045 const uint64_t method_load_data;
3046 const uint64_t direct_code_ptr;
3047 };
3048
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003049 HInvokeStaticOrDirect(ArenaAllocator* arena,
3050 uint32_t number_of_arguments,
3051 Primitive::Type return_type,
3052 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003053 uint32_t method_index,
3054 MethodReference target_method,
3055 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003056 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003057 InvokeType invoke_type,
3058 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003059 : HInvoke(arena,
3060 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003061 // There is one extra argument for the HCurrentMethod node, and
3062 // potentially one other if the clinit check is explicit, and one other
3063 // if the method is a string factory.
3064 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003065 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003066 return_type,
3067 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003068 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003069 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003070 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003071 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003072 target_method_(target_method),
3073 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003074
Calin Juravle641547a2015-04-21 22:08:51 +01003075 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3076 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003077 // We access the method via the dex cache so we can't do an implicit null check.
3078 // TODO: for intrinsics we can generate implicit null checks.
3079 return false;
3080 }
3081
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003082 bool CanBeNull() const OVERRIDE {
3083 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3084 }
3085
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003086 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003087 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3088 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3089 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
3090 bool NeedsDexCache() const OVERRIDE { return !IsRecursive() && !IsStringInit(); }
3091 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003092 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003093 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3094 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3095 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3096 MethodReference GetTargetMethod() const { return target_method_; }
3097
3098 int32_t GetStringInitOffset() const {
3099 DCHECK(IsStringInit());
3100 return dispatch_info_.method_load_data;
3101 }
3102
3103 uint64_t GetMethodAddress() const {
3104 DCHECK(HasMethodAddress());
3105 return dispatch_info_.method_load_data;
3106 }
3107
3108 uint32_t GetDexCacheArrayOffset() const {
3109 DCHECK(HasPcRelDexCache());
3110 return dispatch_info_.method_load_data;
3111 }
3112
3113 uint64_t GetDirectCodePtr() const {
3114 DCHECK(HasDirectCodePtr());
3115 return dispatch_info_.direct_code_ptr;
3116 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003117
Roland Levillain4c0eb422015-04-24 16:43:49 +01003118 // Is this instruction a call to a static method?
3119 bool IsStatic() const {
3120 return GetInvokeType() == kStatic;
3121 }
3122
Roland Levillain3e3d7332015-04-28 11:00:54 +01003123 // Remove the art::HLoadClass instruction set as last input by
3124 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3125 // the initial art::HClinitCheck instruction (only relevant for
3126 // static calls with explicit clinit check).
3127 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003128 DCHECK(IsStaticWithExplicitClinitCheck());
3129 size_t last_input_index = InputCount() - 1;
3130 HInstruction* last_input = InputAt(last_input_index);
3131 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003132 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003133 RemoveAsUserOfInput(last_input_index);
3134 inputs_.DeleteAt(last_input_index);
3135 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3136 DCHECK(IsStaticWithImplicitClinitCheck());
3137 }
3138
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003139 bool IsStringFactoryFor(HFakeString* str) const {
3140 if (!IsStringInit()) return false;
3141 // +1 for the current method.
3142 if (InputCount() == (number_of_arguments_ + 1)) return false;
3143 return InputAt(InputCount() - 1)->AsFakeString() == str;
3144 }
3145
3146 void RemoveFakeStringArgumentAsLastInput() {
3147 DCHECK(IsStringInit());
3148 size_t last_input_index = InputCount() - 1;
3149 HInstruction* last_input = InputAt(last_input_index);
3150 DCHECK(last_input != nullptr);
3151 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3152 RemoveAsUserOfInput(last_input_index);
3153 inputs_.DeleteAt(last_input_index);
3154 }
3155
Roland Levillain4c0eb422015-04-24 16:43:49 +01003156 // Is this a call to a static method whose declaring class has an
3157 // explicit intialization check in the graph?
3158 bool IsStaticWithExplicitClinitCheck() const {
3159 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3160 }
3161
3162 // Is this a call to a static method whose declaring class has an
3163 // implicit intialization check requirement?
3164 bool IsStaticWithImplicitClinitCheck() const {
3165 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3166 }
3167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003168 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003169
Roland Levillain4c0eb422015-04-24 16:43:49 +01003170 protected:
3171 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3172 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3173 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3174 HInstruction* input = input_record.GetInstruction();
3175 // `input` is the last input of a static invoke marked as having
3176 // an explicit clinit check. It must either be:
3177 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3178 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3179 DCHECK(input != nullptr);
3180 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3181 }
3182 return input_record;
3183 }
3184
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003185 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003186 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003187 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003188 // The target method may refer to different dex file or method index than the original
3189 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3190 // in derived class to increase visibility.
3191 MethodReference target_method_;
3192 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003194 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003195};
3196
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003197class HInvokeVirtual : public HInvoke {
3198 public:
3199 HInvokeVirtual(ArenaAllocator* arena,
3200 uint32_t number_of_arguments,
3201 Primitive::Type return_type,
3202 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003203 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003204 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003205 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003206 vtable_index_(vtable_index) {}
3207
Calin Juravle641547a2015-04-21 22:08:51 +01003208 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003209 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003210 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003211 }
3212
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003213 uint32_t GetVTableIndex() const { return vtable_index_; }
3214
3215 DECLARE_INSTRUCTION(InvokeVirtual);
3216
3217 private:
3218 const uint32_t vtable_index_;
3219
3220 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3221};
3222
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003223class HInvokeInterface : public HInvoke {
3224 public:
3225 HInvokeInterface(ArenaAllocator* arena,
3226 uint32_t number_of_arguments,
3227 Primitive::Type return_type,
3228 uint32_t dex_pc,
3229 uint32_t dex_method_index,
3230 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003231 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003232 imt_index_(imt_index) {}
3233
Calin Juravle641547a2015-04-21 22:08:51 +01003234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003235 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003236 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003237 }
3238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003239 uint32_t GetImtIndex() const { return imt_index_; }
3240 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3241
3242 DECLARE_INSTRUCTION(InvokeInterface);
3243
3244 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003245 const uint32_t imt_index_;
3246
3247 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3248};
3249
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003250class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003251 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003252 HNewInstance(HCurrentMethod* current_method,
3253 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003254 uint16_t type_index,
3255 const DexFile& dex_file,
3256 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003257 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003258 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003259 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003260 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003261 entrypoint_(entrypoint) {
3262 SetRawInputAt(0, current_method);
3263 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003264
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003265 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003266 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003267 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003268
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003269 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003270 bool NeedsEnvironment() const OVERRIDE { return true; }
3271 // It may throw when called on:
3272 // - interfaces
3273 // - abstract/innaccessible/unknown classes
3274 // TODO: optimize when possible.
3275 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003276
Calin Juravle10e244f2015-01-26 18:54:32 +00003277 bool CanBeNull() const OVERRIDE { return false; }
3278
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003279 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3280
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003281 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003282
3283 private:
3284 const uint32_t dex_pc_;
3285 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003286 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003287 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003288
3289 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3290};
3291
Roland Levillain88cb1752014-10-20 16:36:47 +01003292class HNeg : public HUnaryOperation {
3293 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003294 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003295 : HUnaryOperation(result_type, input) {}
3296
Roland Levillain9867bc72015-08-05 10:21:34 +01003297 template <typename T> T Compute(T x) const { return -x; }
3298
3299 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3300 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3301 }
3302 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3303 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3304 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003305
Roland Levillain88cb1752014-10-20 16:36:47 +01003306 DECLARE_INSTRUCTION(Neg);
3307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HNeg);
3310};
3311
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003312class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003313 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003314 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003315 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003316 uint32_t dex_pc,
3317 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003318 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003319 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003320 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003321 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003322 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003323 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003324 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003325 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003326 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003327 }
3328
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003329 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003330 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003331 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003332
3333 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003334 bool NeedsEnvironment() const OVERRIDE { return true; }
3335
Mingyao Yang0c365e62015-03-31 15:09:29 -07003336 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3337 bool CanThrow() const OVERRIDE { return true; }
3338
Calin Juravle10e244f2015-01-26 18:54:32 +00003339 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003340
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003341 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3342
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003343 DECLARE_INSTRUCTION(NewArray);
3344
3345 private:
3346 const uint32_t dex_pc_;
3347 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003348 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003349 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003350
3351 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3352};
3353
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003354class HAdd : public HBinaryOperation {
3355 public:
3356 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3357 : HBinaryOperation(result_type, left, right) {}
3358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003359 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003360
Roland Levillain9867bc72015-08-05 10:21:34 +01003361 template <typename T> T Compute(T x, T y) const { return x + y; }
3362
3363 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3364 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003365 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3367 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003368 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003369
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003370 DECLARE_INSTRUCTION(Add);
3371
3372 private:
3373 DISALLOW_COPY_AND_ASSIGN(HAdd);
3374};
3375
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003376class HSub : public HBinaryOperation {
3377 public:
3378 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3379 : HBinaryOperation(result_type, left, right) {}
3380
Roland Levillain9867bc72015-08-05 10:21:34 +01003381 template <typename T> T Compute(T x, T y) const { return x - y; }
3382
3383 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3384 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003385 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003386 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3387 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003388 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003389
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003390 DECLARE_INSTRUCTION(Sub);
3391
3392 private:
3393 DISALLOW_COPY_AND_ASSIGN(HSub);
3394};
3395
Calin Juravle34bacdf2014-10-07 20:23:36 +01003396class HMul : public HBinaryOperation {
3397 public:
3398 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3399 : HBinaryOperation(result_type, left, right) {}
3400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003401 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003402
Roland Levillain9867bc72015-08-05 10:21:34 +01003403 template <typename T> T Compute(T x, T y) const { return x * y; }
3404
3405 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3406 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3407 }
3408 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3409 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3410 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003411
3412 DECLARE_INSTRUCTION(Mul);
3413
3414 private:
3415 DISALLOW_COPY_AND_ASSIGN(HMul);
3416};
3417
Calin Juravle7c4954d2014-10-28 16:57:40 +00003418class HDiv : public HBinaryOperation {
3419 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003420 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003421 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3422 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003423
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 template <typename T>
3425 T Compute(T x, T y) const {
3426 // Our graph structure ensures we never have 0 for `y` during
3427 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003428 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003429 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003430 return (y == -1) ? -x : x / y;
3431 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003432
Roland Levillain9867bc72015-08-05 10:21:34 +01003433 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3434 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3435 }
3436 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3437 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003438 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003440 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003441
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003442 static SideEffects SideEffectsForArchRuntimeCalls() {
3443 // The generated code can use a runtime call.
3444 return SideEffects::CanTriggerGC();
3445 }
3446
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 DECLARE_INSTRUCTION(Div);
3448
3449 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003450 const uint32_t dex_pc_;
3451
Calin Juravle7c4954d2014-10-28 16:57:40 +00003452 DISALLOW_COPY_AND_ASSIGN(HDiv);
3453};
3454
Calin Juravlebacfec32014-11-14 15:54:36 +00003455class HRem : public HBinaryOperation {
3456 public:
3457 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003458 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3459 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003460
Roland Levillain9867bc72015-08-05 10:21:34 +01003461 template <typename T>
3462 T Compute(T x, T y) const {
3463 // Our graph structure ensures we never have 0 for `y` during
3464 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003465 DCHECK_NE(y, 0);
3466 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3467 return (y == -1) ? 0 : x % y;
3468 }
3469
Roland Levillain9867bc72015-08-05 10:21:34 +01003470 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3471 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3472 }
3473 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3474 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003475 }
3476
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003477 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003479 static SideEffects SideEffectsForArchRuntimeCalls() {
3480 return SideEffects::CanTriggerGC();
3481 }
3482
Calin Juravlebacfec32014-11-14 15:54:36 +00003483 DECLARE_INSTRUCTION(Rem);
3484
3485 private:
3486 const uint32_t dex_pc_;
3487
3488 DISALLOW_COPY_AND_ASSIGN(HRem);
3489};
3490
Calin Juravled0d48522014-11-04 16:40:20 +00003491class HDivZeroCheck : public HExpression<1> {
3492 public:
3493 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3494 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3495 SetRawInputAt(0, value);
3496 }
3497
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003498 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3499
Calin Juravled0d48522014-11-04 16:40:20 +00003500 bool CanBeMoved() const OVERRIDE { return true; }
3501
3502 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3503 UNUSED(other);
3504 return true;
3505 }
3506
3507 bool NeedsEnvironment() const OVERRIDE { return true; }
3508 bool CanThrow() const OVERRIDE { return true; }
3509
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003510 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003511
3512 DECLARE_INSTRUCTION(DivZeroCheck);
3513
3514 private:
3515 const uint32_t dex_pc_;
3516
3517 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3518};
3519
Calin Juravle9aec02f2014-11-18 23:06:35 +00003520class HShl : public HBinaryOperation {
3521 public:
3522 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3523 : HBinaryOperation(result_type, left, right) {}
3524
Roland Levillain9867bc72015-08-05 10:21:34 +01003525 template <typename T, typename U, typename V>
3526 T Compute(T x, U y, V max_shift_value) const {
3527 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3528 "V is not the unsigned integer type corresponding to T");
3529 return x << (y & max_shift_value);
3530 }
3531
3532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3533 return GetBlock()->GetGraph()->GetIntConstant(
3534 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3535 }
3536 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3537 // case is handled as `x << static_cast<int>(y)`.
3538 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3539 return GetBlock()->GetGraph()->GetLongConstant(
3540 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3541 }
3542 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3543 return GetBlock()->GetGraph()->GetLongConstant(
3544 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3545 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003546
3547 DECLARE_INSTRUCTION(Shl);
3548
3549 private:
3550 DISALLOW_COPY_AND_ASSIGN(HShl);
3551};
3552
3553class HShr : public HBinaryOperation {
3554 public:
3555 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3556 : HBinaryOperation(result_type, left, right) {}
3557
Roland Levillain9867bc72015-08-05 10:21:34 +01003558 template <typename T, typename U, typename V>
3559 T Compute(T x, U y, V max_shift_value) const {
3560 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3561 "V is not the unsigned integer type corresponding to T");
3562 return x >> (y & max_shift_value);
3563 }
3564
3565 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3566 return GetBlock()->GetGraph()->GetIntConstant(
3567 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3568 }
3569 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3570 // case is handled as `x >> static_cast<int>(y)`.
3571 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3572 return GetBlock()->GetGraph()->GetLongConstant(
3573 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3574 }
3575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3576 return GetBlock()->GetGraph()->GetLongConstant(
3577 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3578 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003579
3580 DECLARE_INSTRUCTION(Shr);
3581
3582 private:
3583 DISALLOW_COPY_AND_ASSIGN(HShr);
3584};
3585
3586class HUShr : public HBinaryOperation {
3587 public:
3588 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3589 : HBinaryOperation(result_type, left, right) {}
3590
Roland Levillain9867bc72015-08-05 10:21:34 +01003591 template <typename T, typename U, typename V>
3592 T Compute(T x, U y, V max_shift_value) const {
3593 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3594 "V is not the unsigned integer type corresponding to T");
3595 V ux = static_cast<V>(x);
3596 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003597 }
3598
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3600 return GetBlock()->GetGraph()->GetIntConstant(
3601 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3602 }
3603 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3604 // case is handled as `x >>> static_cast<int>(y)`.
3605 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3606 return GetBlock()->GetGraph()->GetLongConstant(
3607 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3608 }
3609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3610 return GetBlock()->GetGraph()->GetLongConstant(
3611 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003612 }
3613
3614 DECLARE_INSTRUCTION(UShr);
3615
3616 private:
3617 DISALLOW_COPY_AND_ASSIGN(HUShr);
3618};
3619
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003620class HAnd : public HBinaryOperation {
3621 public:
3622 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3623 : HBinaryOperation(result_type, left, right) {}
3624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003625 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003626
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 template <typename T, typename U>
3628 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3629
3630 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3631 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3632 }
3633 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3634 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3635 }
3636 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3637 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3638 }
3639 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3640 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3641 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003642
3643 DECLARE_INSTRUCTION(And);
3644
3645 private:
3646 DISALLOW_COPY_AND_ASSIGN(HAnd);
3647};
3648
3649class HOr : public HBinaryOperation {
3650 public:
3651 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3652 : HBinaryOperation(result_type, left, right) {}
3653
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003654 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003655
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 template <typename T, typename U>
3657 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3658
3659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3660 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3661 }
3662 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3663 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3664 }
3665 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3666 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3667 }
3668 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3669 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3670 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671
3672 DECLARE_INSTRUCTION(Or);
3673
3674 private:
3675 DISALLOW_COPY_AND_ASSIGN(HOr);
3676};
3677
3678class HXor : public HBinaryOperation {
3679 public:
3680 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3681 : HBinaryOperation(result_type, left, right) {}
3682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003683 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003684
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 template <typename T, typename U>
3686 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3687
3688 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3689 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3690 }
3691 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3692 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3693 }
3694 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3695 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3696 }
3697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3698 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3699 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003700
3701 DECLARE_INSTRUCTION(Xor);
3702
3703 private:
3704 DISALLOW_COPY_AND_ASSIGN(HXor);
3705};
3706
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003707// The value of a parameter in this method. Its location depends on
3708// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003709class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003710 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003711 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3712 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003713
3714 uint8_t GetIndex() const { return index_; }
3715
Calin Juravle10e244f2015-01-26 18:54:32 +00003716 bool CanBeNull() const OVERRIDE { return !is_this_; }
3717
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003718 bool IsThis() const { return is_this_; }
3719
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003720 DECLARE_INSTRUCTION(ParameterValue);
3721
3722 private:
3723 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003724 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003725 const uint8_t index_;
3726
Calin Juravle10e244f2015-01-26 18:54:32 +00003727 // Whether or not the parameter value corresponds to 'this' argument.
3728 const bool is_this_;
3729
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003730 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3731};
3732
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003733class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003734 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003735 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003736 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003737
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003738 bool CanBeMoved() const OVERRIDE { return true; }
3739 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003740 UNUSED(other);
3741 return true;
3742 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003743
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 template <typename T> T Compute(T x) const { return ~x; }
3745
3746 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3747 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3748 }
3749 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3750 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3751 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003752
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003753 DECLARE_INSTRUCTION(Not);
3754
3755 private:
3756 DISALLOW_COPY_AND_ASSIGN(HNot);
3757};
3758
David Brazdil66d126e2015-04-03 16:02:44 +01003759class HBooleanNot : public HUnaryOperation {
3760 public:
3761 explicit HBooleanNot(HInstruction* input)
3762 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3763
3764 bool CanBeMoved() const OVERRIDE { return true; }
3765 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3766 UNUSED(other);
3767 return true;
3768 }
3769
Roland Levillain9867bc72015-08-05 10:21:34 +01003770 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003771 DCHECK(IsUint<1>(x));
3772 return !x;
3773 }
3774
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3776 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3777 }
3778 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3779 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003780 UNREACHABLE();
3781 }
3782
3783 DECLARE_INSTRUCTION(BooleanNot);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3787};
3788
Roland Levillaindff1f282014-11-05 14:15:05 +00003789class HTypeConversion : public HExpression<1> {
3790 public:
3791 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003792 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003793 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3794 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003795 SetRawInputAt(0, input);
3796 DCHECK_NE(input->GetType(), result_type);
3797 }
3798
3799 HInstruction* GetInput() const { return InputAt(0); }
3800 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3801 Primitive::Type GetResultType() const { return GetType(); }
3802
Roland Levillain624279f2014-12-04 11:54:28 +00003803 // Required by the x86 and ARM code generators when producing calls
3804 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003805 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003806
Roland Levillaindff1f282014-11-05 14:15:05 +00003807 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003808 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003809
Mark Mendelle82549b2015-05-06 10:55:34 -04003810 // Try to statically evaluate the conversion and return a HConstant
3811 // containing the result. If the input cannot be converted, return nullptr.
3812 HConstant* TryStaticEvaluation() const;
3813
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003814 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3815 Primitive::Type result_type) {
3816 // Some architectures may not require the 'GC' side effects, but at this point
3817 // in the compilation process we do not know what architecture we will
3818 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003819 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3820 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003821 return SideEffects::CanTriggerGC();
3822 }
3823 return SideEffects::None();
3824 }
3825
Roland Levillaindff1f282014-11-05 14:15:05 +00003826 DECLARE_INSTRUCTION(TypeConversion);
3827
3828 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003829 const uint32_t dex_pc_;
3830
Roland Levillaindff1f282014-11-05 14:15:05 +00003831 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3832};
3833
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003834static constexpr uint32_t kNoRegNumber = -1;
3835
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003836class HPhi : public HInstruction {
3837 public:
3838 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003839 : HInstruction(SideEffects::None()),
3840 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003841 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003842 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003843 is_live_(false),
3844 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003845 inputs_.SetSize(number_of_inputs);
3846 }
3847
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003848 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3849 static Primitive::Type ToPhiType(Primitive::Type type) {
3850 switch (type) {
3851 case Primitive::kPrimBoolean:
3852 case Primitive::kPrimByte:
3853 case Primitive::kPrimShort:
3854 case Primitive::kPrimChar:
3855 return Primitive::kPrimInt;
3856 default:
3857 return type;
3858 }
3859 }
3860
David Brazdilffee3d32015-07-06 11:48:53 +01003861 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3862
Calin Juravle10e244f2015-01-26 18:54:32 +00003863 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003864
3865 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003866 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003867
Calin Juravle10e244f2015-01-26 18:54:32 +00003868 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003869 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003870
Calin Juravle10e244f2015-01-26 18:54:32 +00003871 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3872 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003874 uint32_t GetRegNumber() const { return reg_number_; }
3875
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003876 void SetDead() { is_live_ = false; }
3877 void SetLive() { is_live_ = true; }
3878 bool IsDead() const { return !is_live_; }
3879 bool IsLive() const { return is_live_; }
3880
Calin Juravlea4f88312015-04-16 12:57:19 +01003881 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3882 // An equivalent phi is a phi having the same dex register and type.
3883 // It assumes that phis with the same dex register are adjacent.
3884 HPhi* GetNextEquivalentPhiWithSameType() {
3885 HInstruction* next = GetNext();
3886 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3887 if (next->GetType() == GetType()) {
3888 return next->AsPhi();
3889 }
3890 next = next->GetNext();
3891 }
3892 return nullptr;
3893 }
3894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003895 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003896
David Brazdil1abb4192015-02-17 18:33:36 +00003897 protected:
3898 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3899
3900 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3901 inputs_.Put(index, input);
3902 }
3903
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003904 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003905 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003906 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003907 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003908 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003909 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003911 DISALLOW_COPY_AND_ASSIGN(HPhi);
3912};
3913
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003914class HNullCheck : public HExpression<1> {
3915 public:
3916 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003917 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918 SetRawInputAt(0, value);
3919 }
3920
Calin Juravle10e244f2015-01-26 18:54:32 +00003921 bool CanBeMoved() const OVERRIDE { return true; }
3922 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003923 UNUSED(other);
3924 return true;
3925 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003926
Calin Juravle10e244f2015-01-26 18:54:32 +00003927 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928
Calin Juravle10e244f2015-01-26 18:54:32 +00003929 bool CanThrow() const OVERRIDE { return true; }
3930
3931 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003932
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003933 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934
3935 DECLARE_INSTRUCTION(NullCheck);
3936
3937 private:
3938 const uint32_t dex_pc_;
3939
3940 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3941};
3942
3943class FieldInfo : public ValueObject {
3944 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003945 FieldInfo(MemberOffset field_offset,
3946 Primitive::Type field_type,
3947 bool is_volatile,
3948 uint32_t index,
3949 const DexFile& dex_file)
3950 : field_offset_(field_offset),
3951 field_type_(field_type),
3952 is_volatile_(is_volatile),
3953 index_(index),
3954 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003955
3956 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003957 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003958 uint32_t GetFieldIndex() const { return index_; }
3959 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003960 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003961
3962 private:
3963 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003964 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003965 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003966 uint32_t index_;
3967 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003968};
3969
3970class HInstanceFieldGet : public HExpression<1> {
3971 public:
3972 HInstanceFieldGet(HInstruction* value,
3973 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003974 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003975 bool is_volatile,
3976 uint32_t field_idx,
3977 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003978 : HExpression(
3979 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003980 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003981 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003982 SetRawInputAt(0, value);
3983 }
3984
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003985 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003986
3987 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3988 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3989 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003990 }
3991
Calin Juravle641547a2015-04-21 22:08:51 +01003992 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3993 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003994 }
3995
3996 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003997 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3998 }
3999
Calin Juravle52c48962014-12-16 17:02:57 +00004000 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004001 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004002 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004003 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004004
4005 DECLARE_INSTRUCTION(InstanceFieldGet);
4006
4007 private:
4008 const FieldInfo field_info_;
4009
4010 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4011};
4012
4013class HInstanceFieldSet : public HTemplateInstruction<2> {
4014 public:
4015 HInstanceFieldSet(HInstruction* object,
4016 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004017 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004018 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004019 bool is_volatile,
4020 uint32_t field_idx,
4021 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004022 : HTemplateInstruction(
4023 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004024 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004025 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004026 SetRawInputAt(0, object);
4027 SetRawInputAt(1, value);
4028 }
4029
Calin Juravle641547a2015-04-21 22:08:51 +01004030 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4031 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004032 }
4033
Calin Juravle52c48962014-12-16 17:02:57 +00004034 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004035 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004036 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004037 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004038 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004039 bool GetValueCanBeNull() const { return value_can_be_null_; }
4040 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004041
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004042 DECLARE_INSTRUCTION(InstanceFieldSet);
4043
4044 private:
4045 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004046 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004047
4048 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4049};
4050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004051class HArrayGet : public HExpression<2> {
4052 public:
4053 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004054 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055 SetRawInputAt(0, array);
4056 SetRawInputAt(1, index);
4057 }
4058
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004059 bool CanBeMoved() const OVERRIDE { return true; }
4060 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004061 UNUSED(other);
4062 return true;
4063 }
Calin Juravle641547a2015-04-21 22:08:51 +01004064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4065 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004066 // TODO: We can be smarter here.
4067 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4068 // which generates the implicit null check. There are cases when these can be removed
4069 // to produce better code. If we ever add optimizations to do so we should allow an
4070 // implicit check here (as long as the address falls in the first page).
4071 return false;
4072 }
4073
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004074 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004075
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004076 HInstruction* GetArray() const { return InputAt(0); }
4077 HInstruction* GetIndex() const { return InputAt(1); }
4078
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079 DECLARE_INSTRUCTION(ArrayGet);
4080
4081 private:
4082 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4083};
4084
4085class HArraySet : public HTemplateInstruction<3> {
4086 public:
4087 HArraySet(HInstruction* array,
4088 HInstruction* index,
4089 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004090 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004091 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004092 : HTemplateInstruction(
4093 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4094 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004095 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004096 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004097 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4098 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099 SetRawInputAt(0, array);
4100 SetRawInputAt(1, index);
4101 SetRawInputAt(2, value);
4102 }
4103
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 // We currently always call a runtime method to catch array store
4106 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004107 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 }
4109
Mingyao Yang81014cb2015-06-02 03:16:27 -07004110 // Can throw ArrayStoreException.
4111 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4112
Calin Juravle641547a2015-04-21 22:08:51 +01004113 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4114 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004115 // TODO: Same as for ArrayGet.
4116 return false;
4117 }
4118
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004119 void ClearNeedsTypeCheck() {
4120 needs_type_check_ = false;
4121 }
4122
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004123 void ClearValueCanBeNull() {
4124 value_can_be_null_ = false;
4125 }
4126
4127 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004128 bool NeedsTypeCheck() const { return needs_type_check_; }
4129
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004130 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004131
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004132 HInstruction* GetArray() const { return InputAt(0); }
4133 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004134 HInstruction* GetValue() const { return InputAt(2); }
4135
4136 Primitive::Type GetComponentType() const {
4137 // The Dex format does not type floating point index operations. Since the
4138 // `expected_component_type_` is set during building and can therefore not
4139 // be correct, we also check what is the value type. If it is a floating
4140 // point type, we must use that type.
4141 Primitive::Type value_type = GetValue()->GetType();
4142 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4143 ? value_type
4144 : expected_component_type_;
4145 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004146
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004147 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4148 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4149 }
4150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 DECLARE_INSTRUCTION(ArraySet);
4152
4153 private:
4154 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004155 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004156 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004157 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158
4159 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4160};
4161
4162class HArrayLength : public HExpression<1> {
4163 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004164 explicit HArrayLength(HInstruction* array)
4165 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4166 // Note that arrays do not change length, so the instruction does not
4167 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 SetRawInputAt(0, array);
4169 }
4170
Calin Juravle77520bc2015-01-12 18:45:46 +00004171 bool CanBeMoved() const OVERRIDE { return true; }
4172 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004173 UNUSED(other);
4174 return true;
4175 }
Calin Juravle641547a2015-04-21 22:08:51 +01004176 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4177 return obj == InputAt(0);
4178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 DECLARE_INSTRUCTION(ArrayLength);
4181
4182 private:
4183 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4184};
4185
4186class HBoundsCheck : public HExpression<2> {
4187 public:
4188 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004189 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004190 DCHECK(index->GetType() == Primitive::kPrimInt);
4191 SetRawInputAt(0, index);
4192 SetRawInputAt(1, length);
4193 }
4194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004195 bool CanBeMoved() const OVERRIDE { return true; }
4196 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004197 UNUSED(other);
4198 return true;
4199 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004201 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004202
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004203 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004204
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004205 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206
4207 DECLARE_INSTRUCTION(BoundsCheck);
4208
4209 private:
4210 const uint32_t dex_pc_;
4211
4212 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4213};
4214
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004215/**
4216 * Some DEX instructions are folded into multiple HInstructions that need
4217 * to stay live until the last HInstruction. This class
4218 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004219 * HInstruction stays live. `index` represents the stack location index of the
4220 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004221 */
4222class HTemporary : public HTemplateInstruction<0> {
4223 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004224 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004225
4226 size_t GetIndex() const { return index_; }
4227
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004228 Primitive::Type GetType() const OVERRIDE {
4229 // The previous instruction is the one that will be stored in the temporary location.
4230 DCHECK(GetPrevious() != nullptr);
4231 return GetPrevious()->GetType();
4232 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004233
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004234 DECLARE_INSTRUCTION(Temporary);
4235
4236 private:
4237 const size_t index_;
4238
4239 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4240};
4241
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004242class HSuspendCheck : public HTemplateInstruction<0> {
4243 public:
4244 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004245 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004247 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004248 return true;
4249 }
4250
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004251 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004252 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4253 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004254
4255 DECLARE_INSTRUCTION(SuspendCheck);
4256
4257 private:
4258 const uint32_t dex_pc_;
4259
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004260 // Only used for code generation, in order to share the same slow path between back edges
4261 // of a same loop.
4262 SlowPathCode* slow_path_;
4263
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004264 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4265};
4266
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004267/**
4268 * Instruction to load a Class object.
4269 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004270class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004271 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004272 HLoadClass(HCurrentMethod* current_method,
4273 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004274 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004275 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004276 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004277 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004278 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004279 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004281 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004282 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004283 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004284 SetRawInputAt(0, current_method);
4285 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004286
4287 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004288
4289 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4290 return other->AsLoadClass()->type_index_ == type_index_;
4291 }
4292
4293 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4294
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004295 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004296 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004297 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004298 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004299
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004300 bool NeedsEnvironment() const OVERRIDE {
4301 // Will call runtime and load the class if the class is not loaded yet.
4302 // TODO: finer grain decision.
4303 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004304 }
4305
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004306 bool MustGenerateClinitCheck() const {
4307 return generate_clinit_check_;
4308 }
4309
Calin Juravle0ba218d2015-05-19 18:46:01 +01004310 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4311 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004312 }
4313
4314 bool CanCallRuntime() const {
4315 return MustGenerateClinitCheck() || !is_referrers_class_;
4316 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004317
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004318 bool CanThrow() const OVERRIDE {
4319 // May call runtime and and therefore can throw.
4320 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004321 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004322 }
4323
Calin Juravleacf735c2015-02-12 15:25:22 +00004324 ReferenceTypeInfo GetLoadedClassRTI() {
4325 return loaded_class_rti_;
4326 }
4327
4328 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4329 // Make sure we only set exact types (the loaded class should never be merged).
4330 DCHECK(rti.IsExact());
4331 loaded_class_rti_ = rti;
4332 }
4333
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004334 const DexFile& GetDexFile() { return dex_file_; }
4335
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004336 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4337
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004338 static SideEffects SideEffectsForArchRuntimeCalls() {
4339 return SideEffects::CanTriggerGC();
4340 }
4341
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004342 DECLARE_INSTRUCTION(LoadClass);
4343
4344 private:
4345 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004346 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004347 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004348 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004349 // Whether this instruction must generate the initialization check.
4350 // Used for code generation.
4351 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004352
Calin Juravleacf735c2015-02-12 15:25:22 +00004353 ReferenceTypeInfo loaded_class_rti_;
4354
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004355 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4356};
4357
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004358class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004359 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004360 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004361 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004362 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004363 dex_pc_(dex_pc) {
4364 SetRawInputAt(0, current_method);
4365 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004366
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004367 bool CanBeMoved() const OVERRIDE { return true; }
4368
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004369 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4370 return other->AsLoadString()->string_index_ == string_index_;
4371 }
4372
4373 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4374
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004375 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004376 uint32_t GetStringIndex() const { return string_index_; }
4377
4378 // TODO: Can we deopt or debug when we resolve a string?
4379 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004380 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004382 static SideEffects SideEffectsForArchRuntimeCalls() {
4383 return SideEffects::CanTriggerGC();
4384 }
4385
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004386 DECLARE_INSTRUCTION(LoadString);
4387
4388 private:
4389 const uint32_t string_index_;
4390 const uint32_t dex_pc_;
4391
4392 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4393};
4394
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004395/**
4396 * Performs an initialization check on its Class object input.
4397 */
4398class HClinitCheck : public HExpression<1> {
4399 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004400 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004401 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004402 Primitive::kPrimNot,
4403 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004404 dex_pc_(dex_pc) {
4405 SetRawInputAt(0, constant);
4406 }
4407
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004408 bool CanBeMoved() const OVERRIDE { return true; }
4409 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4410 UNUSED(other);
4411 return true;
4412 }
4413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004414 bool NeedsEnvironment() const OVERRIDE {
4415 // May call runtime to initialize the class.
4416 return true;
4417 }
4418
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004419 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004420
4421 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4422
4423 DECLARE_INSTRUCTION(ClinitCheck);
4424
4425 private:
4426 const uint32_t dex_pc_;
4427
4428 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4429};
4430
4431class HStaticFieldGet : public HExpression<1> {
4432 public:
4433 HStaticFieldGet(HInstruction* cls,
4434 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004435 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004436 bool is_volatile,
4437 uint32_t field_idx,
4438 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004439 : HExpression(
4440 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004441 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004442 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004443 SetRawInputAt(0, cls);
4444 }
4445
Calin Juravle52c48962014-12-16 17:02:57 +00004446
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004447 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004448
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004449 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004450 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4451 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004452 }
4453
4454 size_t ComputeHashCode() const OVERRIDE {
4455 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4456 }
4457
Calin Juravle52c48962014-12-16 17:02:57 +00004458 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004459 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4460 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004461 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004462
4463 DECLARE_INSTRUCTION(StaticFieldGet);
4464
4465 private:
4466 const FieldInfo field_info_;
4467
4468 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4469};
4470
4471class HStaticFieldSet : public HTemplateInstruction<2> {
4472 public:
4473 HStaticFieldSet(HInstruction* cls,
4474 HInstruction* value,
4475 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004476 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004477 bool is_volatile,
4478 uint32_t field_idx,
4479 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004480 : HTemplateInstruction(
4481 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004482 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004483 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004484 SetRawInputAt(0, cls);
4485 SetRawInputAt(1, value);
4486 }
4487
Calin Juravle52c48962014-12-16 17:02:57 +00004488 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004489 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4490 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004491 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004492
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004493 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004494 bool GetValueCanBeNull() const { return value_can_be_null_; }
4495 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004496
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004497 DECLARE_INSTRUCTION(StaticFieldSet);
4498
4499 private:
4500 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004501 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004502
4503 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4504};
4505
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004506// Implement the move-exception DEX instruction.
4507class HLoadException : public HExpression<0> {
4508 public:
4509 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4510
4511 DECLARE_INSTRUCTION(LoadException);
4512
4513 private:
4514 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4515};
4516
David Brazdilcb1c0552015-08-04 16:22:25 +01004517// Implicit part of move-exception which clears thread-local exception storage.
4518// Must not be removed because the runtime expects the TLS to get cleared.
4519class HClearException : public HTemplateInstruction<0> {
4520 public:
4521 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4522
4523 DECLARE_INSTRUCTION(ClearException);
4524
4525 private:
4526 DISALLOW_COPY_AND_ASSIGN(HClearException);
4527};
4528
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004529class HThrow : public HTemplateInstruction<1> {
4530 public:
4531 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004532 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004533 SetRawInputAt(0, exception);
4534 }
4535
4536 bool IsControlFlow() const OVERRIDE { return true; }
4537
4538 bool NeedsEnvironment() const OVERRIDE { return true; }
4539
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004540 bool CanThrow() const OVERRIDE { return true; }
4541
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004542 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004543
4544 DECLARE_INSTRUCTION(Throw);
4545
4546 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004547 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004548
4549 DISALLOW_COPY_AND_ASSIGN(HThrow);
4550};
4551
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004552class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004553 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004554 HInstanceOf(HInstruction* object,
4555 HLoadClass* constant,
4556 bool class_is_final,
4557 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004558 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004559 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004560 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004561 dex_pc_(dex_pc) {
4562 SetRawInputAt(0, object);
4563 SetRawInputAt(1, constant);
4564 }
4565
4566 bool CanBeMoved() const OVERRIDE { return true; }
4567
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004568 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004569 return true;
4570 }
4571
4572 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004573 return false;
4574 }
4575
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004576 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004577
4578 bool IsClassFinal() const { return class_is_final_; }
4579
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004580 // Used only in code generation.
4581 bool MustDoNullCheck() const { return must_do_null_check_; }
4582 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4583
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004584 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4585 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4586 }
4587
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004588 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004589
4590 private:
4591 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004592 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004593 const uint32_t dex_pc_;
4594
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004595 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4596};
4597
Calin Juravleb1498f62015-02-16 13:13:29 +00004598class HBoundType : public HExpression<1> {
4599 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004600 // Constructs an HBoundType with the given upper_bound.
4601 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004602 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004603 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004604 upper_bound_(upper_bound),
4605 upper_can_be_null_(upper_can_be_null),
4606 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004607 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004608 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004609 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004610 }
4611
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004612 // GetUpper* should only be used in reference type propagation.
4613 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4614 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004615
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004616 void SetCanBeNull(bool can_be_null) {
4617 DCHECK(upper_can_be_null_ || !can_be_null);
4618 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004619 }
4620
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004621 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4622
Calin Juravleb1498f62015-02-16 13:13:29 +00004623 DECLARE_INSTRUCTION(BoundType);
4624
4625 private:
4626 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004627 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4628 // It is used to bound the type in cases like:
4629 // if (x instanceof ClassX) {
4630 // // uper_bound_ will be ClassX
4631 // }
4632 const ReferenceTypeInfo upper_bound_;
4633 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4634 // is false then can_be_null_ cannot be true).
4635 const bool upper_can_be_null_;
4636 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004637
4638 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4639};
4640
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004641class HCheckCast : public HTemplateInstruction<2> {
4642 public:
4643 HCheckCast(HInstruction* object,
4644 HLoadClass* constant,
4645 bool class_is_final,
4646 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004647 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004648 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004649 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004650 dex_pc_(dex_pc) {
4651 SetRawInputAt(0, object);
4652 SetRawInputAt(1, constant);
4653 }
4654
4655 bool CanBeMoved() const OVERRIDE { return true; }
4656
4657 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4658 return true;
4659 }
4660
4661 bool NeedsEnvironment() const OVERRIDE {
4662 // Instruction may throw a CheckCastError.
4663 return true;
4664 }
4665
4666 bool CanThrow() const OVERRIDE { return true; }
4667
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004668 bool MustDoNullCheck() const { return must_do_null_check_; }
4669 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4670
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004671 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004672
4673 bool IsClassFinal() const { return class_is_final_; }
4674
4675 DECLARE_INSTRUCTION(CheckCast);
4676
4677 private:
4678 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004679 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004680 const uint32_t dex_pc_;
4681
4682 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004683};
4684
Calin Juravle27df7582015-04-17 19:12:31 +01004685class HMemoryBarrier : public HTemplateInstruction<0> {
4686 public:
4687 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004688 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004689 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004690 barrier_kind_(barrier_kind) {}
4691
4692 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4693
4694 DECLARE_INSTRUCTION(MemoryBarrier);
4695
4696 private:
4697 const MemBarrierKind barrier_kind_;
4698
4699 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4700};
4701
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004702class HMonitorOperation : public HTemplateInstruction<1> {
4703 public:
4704 enum OperationKind {
4705 kEnter,
4706 kExit,
4707 };
4708
4709 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004710 : HTemplateInstruction(
4711 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004712 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004713 SetRawInputAt(0, object);
4714 }
4715
4716 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004717 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4718
4719 bool CanThrow() const OVERRIDE {
4720 // Verifier guarantees that monitor-exit cannot throw.
4721 // This is important because it allows the HGraphBuilder to remove
4722 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4723 return IsEnter();
4724 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004725
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004726 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004727
4728 bool IsEnter() const { return kind_ == kEnter; }
4729
4730 DECLARE_INSTRUCTION(MonitorOperation);
4731
Calin Juravle52c48962014-12-16 17:02:57 +00004732 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004733 const OperationKind kind_;
4734 const uint32_t dex_pc_;
4735
4736 private:
4737 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4738};
4739
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004740/**
4741 * A HInstruction used as a marker for the replacement of new + <init>
4742 * of a String to a call to a StringFactory. Only baseline will see
4743 * the node at code generation, where it will be be treated as null.
4744 * When compiling non-baseline, `HFakeString` instructions are being removed
4745 * in the instruction simplifier.
4746 */
4747class HFakeString : public HTemplateInstruction<0> {
4748 public:
4749 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4750
4751 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4752
4753 DECLARE_INSTRUCTION(FakeString);
4754
4755 private:
4756 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4757};
4758
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004759class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004760 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004761 MoveOperands(Location source,
4762 Location destination,
4763 Primitive::Type type,
4764 HInstruction* instruction)
4765 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004766
4767 Location GetSource() const { return source_; }
4768 Location GetDestination() const { return destination_; }
4769
4770 void SetSource(Location value) { source_ = value; }
4771 void SetDestination(Location value) { destination_ = value; }
4772
4773 // The parallel move resolver marks moves as "in-progress" by clearing the
4774 // destination (but not the source).
4775 Location MarkPending() {
4776 DCHECK(!IsPending());
4777 Location dest = destination_;
4778 destination_ = Location::NoLocation();
4779 return dest;
4780 }
4781
4782 void ClearPending(Location dest) {
4783 DCHECK(IsPending());
4784 destination_ = dest;
4785 }
4786
4787 bool IsPending() const {
4788 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4789 return destination_.IsInvalid() && !source_.IsInvalid();
4790 }
4791
4792 // True if this blocks a move from the given location.
4793 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004794 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004795 }
4796
4797 // A move is redundant if it's been eliminated, if its source and
4798 // destination are the same, or if its destination is unneeded.
4799 bool IsRedundant() const {
4800 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4801 }
4802
4803 // We clear both operands to indicate move that's been eliminated.
4804 void Eliminate() {
4805 source_ = destination_ = Location::NoLocation();
4806 }
4807
4808 bool IsEliminated() const {
4809 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4810 return source_.IsInvalid();
4811 }
4812
Alexey Frunze4dda3372015-06-01 18:31:49 -07004813 Primitive::Type GetType() const { return type_; }
4814
Nicolas Geoffray90218252015-04-15 11:56:51 +01004815 bool Is64BitMove() const {
4816 return Primitive::Is64BitType(type_);
4817 }
4818
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004819 HInstruction* GetInstruction() const { return instruction_; }
4820
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004821 private:
4822 Location source_;
4823 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004824 // The type this move is for.
4825 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004826 // The instruction this move is assocatied with. Null when this move is
4827 // for moving an input in the expected locations of user (including a phi user).
4828 // This is only used in debug mode, to ensure we do not connect interval siblings
4829 // in the same parallel move.
4830 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004831};
4832
4833static constexpr size_t kDefaultNumberOfMoves = 4;
4834
4835class HParallelMove : public HTemplateInstruction<0> {
4836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004837 explicit HParallelMove(ArenaAllocator* arena)
4838 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004839
Nicolas Geoffray90218252015-04-15 11:56:51 +01004840 void AddMove(Location source,
4841 Location destination,
4842 Primitive::Type type,
4843 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004844 DCHECK(source.IsValid());
4845 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004846 if (kIsDebugBuild) {
4847 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004848 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004849 if (moves_.Get(i).GetInstruction() == instruction) {
4850 // Special case the situation where the move is for the spill slot
4851 // of the instruction.
4852 if ((GetPrevious() == instruction)
4853 || ((GetPrevious() == nullptr)
4854 && instruction->IsPhi()
4855 && instruction->GetBlock() == GetBlock())) {
4856 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4857 << "Doing parallel moves for the same instruction.";
4858 } else {
4859 DCHECK(false) << "Doing parallel moves for the same instruction.";
4860 }
4861 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004862 }
4863 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004864 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004865 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004866 << "Overlapped destination for two moves in a parallel move: "
4867 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4868 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004869 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004870 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004871 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004872 }
4873
4874 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004875 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004876 }
4877
4878 size_t NumMoves() const { return moves_.Size(); }
4879
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004880 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004881
4882 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004883 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004884
4885 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4886};
4887
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004888class HGraphVisitor : public ValueObject {
4889 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004890 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4891 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004892
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004893 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004894 virtual void VisitBasicBlock(HBasicBlock* block);
4895
Roland Levillain633021e2014-10-01 14:12:25 +01004896 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004897 void VisitInsertionOrder();
4898
Roland Levillain633021e2014-10-01 14:12:25 +01004899 // Visit the graph following dominator tree reverse post-order.
4900 void VisitReversePostOrder();
4901
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004902 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004903
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004904 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004905#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004906 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4907
4908 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4909
4910#undef DECLARE_VISIT_INSTRUCTION
4911
4912 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004913 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004914
4915 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4916};
4917
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004918class HGraphDelegateVisitor : public HGraphVisitor {
4919 public:
4920 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4921 virtual ~HGraphDelegateVisitor() {}
4922
4923 // Visit functions that delegate to to super class.
4924#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004925 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004926
4927 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4928
4929#undef DECLARE_VISIT_INSTRUCTION
4930
4931 private:
4932 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4933};
4934
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004935class HInsertionOrderIterator : public ValueObject {
4936 public:
4937 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4938
4939 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4940 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4941 void Advance() { ++index_; }
4942
4943 private:
4944 const HGraph& graph_;
4945 size_t index_;
4946
4947 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4948};
4949
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004950class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004951 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004952 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4953 // Check that reverse post order of the graph has been built.
4954 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4955 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004956
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004957 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4958 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004959 void Advance() { ++index_; }
4960
4961 private:
4962 const HGraph& graph_;
4963 size_t index_;
4964
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004965 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004966};
4967
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004968class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004969 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004970 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004971 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4972 // Check that reverse post order of the graph has been built.
4973 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4974 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004975
4976 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004977 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004978 void Advance() { --index_; }
4979
4980 private:
4981 const HGraph& graph_;
4982 size_t index_;
4983
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004984 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004985};
4986
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004987class HLinearPostOrderIterator : public ValueObject {
4988 public:
4989 explicit HLinearPostOrderIterator(const HGraph& graph)
4990 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4991
4992 bool Done() const { return index_ == 0; }
4993
4994 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4995
4996 void Advance() {
4997 --index_;
4998 DCHECK_GE(index_, 0U);
4999 }
5000
5001 private:
5002 const GrowableArray<HBasicBlock*>& order_;
5003 size_t index_;
5004
5005 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5006};
5007
5008class HLinearOrderIterator : public ValueObject {
5009 public:
5010 explicit HLinearOrderIterator(const HGraph& graph)
5011 : order_(graph.GetLinearOrder()), index_(0) {}
5012
5013 bool Done() const { return index_ == order_.Size(); }
5014 HBasicBlock* Current() const { return order_.Get(index_); }
5015 void Advance() { ++index_; }
5016
5017 private:
5018 const GrowableArray<HBasicBlock*>& order_;
5019 size_t index_;
5020
5021 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5022};
5023
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005024// Iterator over the blocks that art part of the loop. Includes blocks part
5025// of an inner loop. The order in which the blocks are iterated is on their
5026// block id.
5027class HBlocksInLoopIterator : public ValueObject {
5028 public:
5029 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5030 : blocks_in_loop_(info.GetBlocks()),
5031 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5032 index_(0) {
5033 if (!blocks_in_loop_.IsBitSet(index_)) {
5034 Advance();
5035 }
5036 }
5037
5038 bool Done() const { return index_ == blocks_.Size(); }
5039 HBasicBlock* Current() const { return blocks_.Get(index_); }
5040 void Advance() {
5041 ++index_;
5042 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5043 if (blocks_in_loop_.IsBitSet(index_)) {
5044 break;
5045 }
5046 }
5047 }
5048
5049 private:
5050 const BitVector& blocks_in_loop_;
5051 const GrowableArray<HBasicBlock*>& blocks_;
5052 size_t index_;
5053
5054 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5055};
5056
Mingyao Yang3584bce2015-05-19 16:01:59 -07005057// Iterator over the blocks that art part of the loop. Includes blocks part
5058// of an inner loop. The order in which the blocks are iterated is reverse
5059// post order.
5060class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5061 public:
5062 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5063 : blocks_in_loop_(info.GetBlocks()),
5064 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5065 index_(0) {
5066 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5067 Advance();
5068 }
5069 }
5070
5071 bool Done() const { return index_ == blocks_.Size(); }
5072 HBasicBlock* Current() const { return blocks_.Get(index_); }
5073 void Advance() {
5074 ++index_;
5075 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5076 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5077 break;
5078 }
5079 }
5080 }
5081
5082 private:
5083 const BitVector& blocks_in_loop_;
5084 const GrowableArray<HBasicBlock*>& blocks_;
5085 size_t index_;
5086
5087 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5088};
5089
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005090inline int64_t Int64FromConstant(HConstant* constant) {
5091 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5092 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5093 : constant->AsLongConstant()->GetValue();
5094}
5095
Vladimir Marko58155012015-08-19 12:49:41 +00005096inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5097 // For the purposes of the compiler, the dex files must actually be the same object
5098 // if we want to safely treat them as the same. This is especially important for JIT
5099 // as custom class loaders can open the same underlying file (or memory) multiple
5100 // times and provide different class resolution but no two class loaders should ever
5101 // use the same DexFile object - doing so is an unsupported hack that can lead to
5102 // all sorts of weird failures.
5103 return &lhs == &rhs;
5104}
5105
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005106} // namespace art
5107
5108#endif // ART_COMPILER_OPTIMIZING_NODES_H_