blob: e4d280f26dae77d7b542aae7430814a231ac693e [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
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#include "instruction_simplifier.h"
18
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010019#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070021#include "scoped_thread_state_change-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022
Nicolas Geoffray3c049742014-09-24 18:10:46 +010023namespace art {
24
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010025class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000026 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000027 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010028 : HGraphDelegateVisitor(graph),
Alexandre Rames188d4312015-04-09 18:30:21 +010029 stats_(stats) {}
30
31 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000032
33 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010034 void RecordSimplification() {
35 simplification_occurred_ = true;
36 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000037 MaybeRecordStat(kInstructionSimplifications);
38 }
39
40 void MaybeRecordStat(MethodCompilationStat stat) {
41 if (stats_ != nullptr) {
42 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010043 }
44 }
45
Scott Wakeling40a04bf2015-12-11 09:50:36 +000046 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
47 bool TryReplaceWithRotate(HBinaryOperation* instruction);
48 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
49 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
50 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
51
Alexandre Rames188d4312015-04-09 18:30:21 +010052 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000053 // `op` should be either HOr or HAnd.
54 // De Morgan's laws:
55 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
56 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010057 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
58 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
59
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000060 void VisitShift(HBinaryOperation* shift);
61
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000062 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010063 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
64 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010065 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
66 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000067 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000068 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000069 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080070 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000071 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000072 void VisitAdd(HAdd* instruction) OVERRIDE;
73 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040074 void VisitCondition(HCondition* instruction) OVERRIDE;
75 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
76 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
77 void VisitLessThan(HLessThan* condition) OVERRIDE;
78 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060079 void VisitBelow(HBelow* condition) OVERRIDE;
80 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
81 void VisitAbove(HAbove* condition) OVERRIDE;
82 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000083 void VisitDiv(HDiv* instruction) OVERRIDE;
84 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010085 void VisitNeg(HNeg* instruction) OVERRIDE;
86 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000087 void VisitOr(HOr* instruction) OVERRIDE;
88 void VisitShl(HShl* instruction) OVERRIDE;
89 void VisitShr(HShr* instruction) OVERRIDE;
90 void VisitSub(HSub* instruction) OVERRIDE;
91 void VisitUShr(HUShr* instruction) OVERRIDE;
92 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000093 void VisitSelect(HSelect* select) OVERRIDE;
94 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010095 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010096 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070097 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010098
99 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000100
Roland Levillain22c49222016-03-18 14:04:28 +0000101 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100102 void SimplifySystemArrayCopy(HInvoke* invoke);
103 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000104 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800105 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800106 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100107 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100108 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800109 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100110
Calin Juravleacf735c2015-02-12 15:25:22 +0000111 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100112 bool simplification_occurred_ = false;
113 int simplifications_at_current_position_ = 0;
114 // We ensure we do not loop infinitely. The value is a finger in the air guess
115 // that should allow enough simplification.
116 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000117};
118
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100119void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000120 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100121 visitor.Run();
122}
123
124void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100125 // Iterate in reverse post order to open up more simplifications to users
126 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100127 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100128 // The simplification of an instruction to another instruction may yield
129 // possibilities for other simplifications. So although we perform a reverse
130 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100131 do {
132 simplification_occurred_ = false;
133 VisitBasicBlock(block);
134 } while (simplification_occurred_ &&
135 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100136 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100137 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100138}
139
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000140namespace {
141
142bool AreAllBitsSet(HConstant* constant) {
143 return Int64FromConstant(constant) == -1;
144}
145
146} // namespace
147
Alexandre Rames188d4312015-04-09 18:30:21 +0100148// Returns true if the code was simplified to use only one negation operation
149// after the binary operation instead of one on each of the inputs.
150bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
151 DCHECK(binop->IsAdd() || binop->IsSub());
152 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
153 HNeg* left_neg = binop->GetLeft()->AsNeg();
154 HNeg* right_neg = binop->GetRight()->AsNeg();
155 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
156 !right_neg->HasOnlyOneNonEnvironmentUse()) {
157 return false;
158 }
159 // Replace code looking like
160 // NEG tmp1, a
161 // NEG tmp2, b
162 // ADD dst, tmp1, tmp2
163 // with
164 // ADD tmp, a, b
165 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600166 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
167 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
168 // while the later yields `-0.0`.
169 if (!Primitive::IsIntegralType(binop->GetType())) {
170 return false;
171 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100172 binop->ReplaceInput(left_neg->GetInput(), 0);
173 binop->ReplaceInput(right_neg->GetInput(), 1);
174 left_neg->GetBlock()->RemoveInstruction(left_neg);
175 right_neg->GetBlock()->RemoveInstruction(right_neg);
176 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
177 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
178 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
179 RecordSimplification();
180 return true;
181}
182
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000183bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
184 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
185 Primitive::Type type = op->GetType();
186 HInstruction* left = op->GetLeft();
187 HInstruction* right = op->GetRight();
188
189 // We can apply De Morgan's laws if both inputs are Not's and are only used
190 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000191 if (((left->IsNot() && right->IsNot()) ||
192 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000193 left->HasOnlyOneNonEnvironmentUse() &&
194 right->HasOnlyOneNonEnvironmentUse()) {
195 // Replace code looking like
196 // NOT nota, a
197 // NOT notb, b
198 // AND dst, nota, notb (respectively OR)
199 // with
200 // OR or, a, b (respectively AND)
201 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000202 HInstruction* src_left = left->InputAt(0);
203 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000204 uint32_t dex_pc = op->GetDexPc();
205
206 // Remove the negations on the inputs.
207 left->ReplaceWith(src_left);
208 right->ReplaceWith(src_right);
209 left->GetBlock()->RemoveInstruction(left);
210 right->GetBlock()->RemoveInstruction(right);
211
212 // Replace the `HAnd` or `HOr`.
213 HBinaryOperation* hbin;
214 if (op->IsAnd()) {
215 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
216 } else {
217 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
218 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000219 HInstruction* hnot;
220 if (left->IsBooleanNot()) {
221 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
222 } else {
223 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
224 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000225
226 op->GetBlock()->InsertInstructionBefore(hbin, op);
227 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
228
229 RecordSimplification();
230 return true;
231 }
232
233 return false;
234}
235
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000236void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
237 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100238 HInstruction* shift_amount = instruction->GetRight();
239 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000240
Alexandre Rames50518442016-06-27 11:39:19 +0100241 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
242 ? kMaxLongShiftDistance
243 : kMaxIntShiftDistance;
244
245 if (shift_amount->IsConstant()) {
246 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
247 if ((cst & implicit_mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400248 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100249 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400250 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100251 // value
252 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400253 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100254 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100255 return;
256 }
257 }
258
259 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
260 // unnecessary explicit masking operations on the shift amount.
261 // Replace code looking like
262 // AND masked_shift, shift, <superset of implicit mask>
263 // SHL dst, value, masked_shift
264 // with
265 // SHL dst, value, shift
266 if (shift_amount->IsAnd()) {
267 HAnd* and_insn = shift_amount->AsAnd();
268 HConstant* mask = and_insn->GetConstantRight();
269 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
270 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
271 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400272 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000273 }
274}
275
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000276static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
277 return (sub->GetRight() == other &&
278 sub->GetLeft()->IsConstant() &&
279 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
280}
281
282bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
283 HUShr* ushr,
284 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000285 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
286 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000287 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
288 if (!ushr->HasUses()) {
289 ushr->GetBlock()->RemoveInstruction(ushr);
290 }
291 if (!ushr->GetRight()->HasUses()) {
292 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
293 }
294 if (!shl->HasUses()) {
295 shl->GetBlock()->RemoveInstruction(shl);
296 }
297 if (!shl->GetRight()->HasUses()) {
298 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
299 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100300 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000301 return true;
302}
303
304// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
305bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000306 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
307 HInstruction* left = op->GetLeft();
308 HInstruction* right = op->GetRight();
309 // If we have an UShr and a Shl (in either order).
310 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
311 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
312 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
313 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
314 if (ushr->GetType() == shl->GetType() &&
315 ushr->GetLeft() == shl->GetLeft()) {
316 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
317 // Shift distances are both constant, try replacing with Ror if they
318 // add up to the register size.
319 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
320 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
321 // Shift distances are potentially of the form x and (reg_size - x).
322 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
323 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
324 // Shift distances are potentially of the form d and -d.
325 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
326 }
327 }
328 }
329 return false;
330}
331
332// Try replacing code looking like (x >>> #rdist OP x << #ldist):
333// UShr dst, x, #rdist
334// Shl tmp, x, #ldist
335// OP dst, dst, tmp
336// or like (x >>> #rdist OP x << #-ldist):
337// UShr dst, x, #rdist
338// Shl tmp, x, #-ldist
339// OP dst, dst, tmp
340// with
341// Ror dst, x, #rdist
342bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
343 HUShr* ushr,
344 HShl* shl) {
345 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
346 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
347 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
348 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
349 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
350 ReplaceRotateWithRor(op, ushr, shl);
351 return true;
352 }
353 return false;
354}
355
356// Replace code looking like (x >>> -d OP x << d):
357// Neg neg, d
358// UShr dst, x, neg
359// Shl tmp, x, d
360// OP dst, dst, tmp
361// with
362// Neg neg, d
363// Ror dst, x, neg
364// *** OR ***
365// Replace code looking like (x >>> d OP x << -d):
366// UShr dst, x, d
367// Neg neg, d
368// Shl tmp, x, neg
369// OP dst, dst, tmp
370// with
371// Ror dst, x, d
372bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
373 HUShr* ushr,
374 HShl* shl) {
375 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
376 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
377 bool neg_is_left = shl->GetRight()->IsNeg();
378 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
379 // And the shift distance being negated is the distance being shifted the other way.
380 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
381 ReplaceRotateWithRor(op, ushr, shl);
382 }
383 return false;
384}
385
386// Try replacing code looking like (x >>> d OP x << (#bits - d)):
387// UShr dst, x, d
388// Sub ld, #bits, d
389// Shl tmp, x, ld
390// OP dst, dst, tmp
391// with
392// Ror dst, x, d
393// *** OR ***
394// Replace code looking like (x >>> (#bits - d) OP x << d):
395// Sub rd, #bits, d
396// UShr dst, x, rd
397// Shl tmp, x, d
398// OP dst, dst, tmp
399// with
400// Neg neg, d
401// Ror dst, x, neg
402bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
403 HUShr* ushr,
404 HShl* shl) {
405 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
406 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
407 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
408 HInstruction* shl_shift = shl->GetRight();
409 HInstruction* ushr_shift = ushr->GetRight();
410 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
411 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
412 return ReplaceRotateWithRor(op, ushr, shl);
413 }
414 return false;
415}
416
Calin Juravle10e244f2015-01-26 18:54:32 +0000417void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
418 HInstruction* obj = null_check->InputAt(0);
419 if (!obj->CanBeNull()) {
420 null_check->ReplaceWith(obj);
421 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000422 if (stats_ != nullptr) {
423 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
424 }
425 }
426}
427
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100428bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
429 if (!input->CanBeNull()) {
430 return true;
431 }
432
Vladimir Marko46817b82016-03-29 12:21:58 +0100433 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
434 HInstruction* user = use.GetUser();
435 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100436 return true;
437 }
438 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100439
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100440 return false;
441}
442
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100443// Returns whether doing a type test between the class of `object` against `klass` has
444// a statically known outcome. The result of the test is stored in `outcome`.
445static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000446 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
447 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
448 ScopedObjectAccess soa(Thread::Current());
449 if (!obj_rti.IsValid()) {
450 // We run the simplifier before the reference type propagation so type info might not be
451 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100452 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000453 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100454
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100455 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100456 if (!class_rti.IsValid()) {
457 // Happens when the loaded class is unresolved.
458 return false;
459 }
460 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000461 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100462 *outcome = true;
463 return true;
464 } else if (obj_rti.IsExact()) {
465 // The test failed at compile time so will also fail at runtime.
466 *outcome = false;
467 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100468 } else if (!class_rti.IsInterface()
469 && !obj_rti.IsInterface()
470 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100471 // Different type hierarchy. The test will fail.
472 *outcome = false;
473 return true;
474 }
475 return false;
476}
477
478void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
479 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100480 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
481 if (load_class->NeedsAccessCheck()) {
482 // If we need to perform an access check we cannot remove the instruction.
483 return;
484 }
485
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100486 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100487 check_cast->ClearMustDoNullCheck();
488 }
489
490 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000491 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000492 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100493 return;
494 }
495
Vladimir Markoa65ed302016-03-14 21:21:29 +0000496 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
497 // the return value check with the `outcome` check, b/27651442 .
498 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700499 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100500 if (outcome) {
501 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000502 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700503 if (!load_class->HasUses()) {
504 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
505 // However, here we know that it cannot because the checkcast was successfull, hence
506 // the class was already loaded.
507 load_class->GetBlock()->RemoveInstruction(load_class);
508 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100509 } else {
510 // Don't do anything for exceptional cases for now. Ideally we should remove
511 // all instructions and blocks this instruction dominates.
512 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000513 }
514}
515
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100516void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100517 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100518 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
519 if (load_class->NeedsAccessCheck()) {
520 // If we need to perform an access check we cannot remove the instruction.
521 return;
522 }
523
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100524 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100525 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100526 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100527 instruction->ClearMustDoNullCheck();
528 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100529
530 HGraph* graph = GetGraph();
531 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000532 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100533 instruction->ReplaceWith(graph->GetIntConstant(0));
534 instruction->GetBlock()->RemoveInstruction(instruction);
535 RecordSimplification();
536 return;
537 }
538
Vladimir Marko24bd8952016-03-15 10:40:33 +0000539 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
540 // the return value check with the `outcome` check, b/27651442 .
541 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700542 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000543 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100544 if (outcome && can_be_null) {
545 // Type test will succeed, we just need a null test.
546 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
547 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
548 instruction->ReplaceWith(test);
549 } else {
550 // We've statically determined the result of the instanceof.
551 instruction->ReplaceWith(graph->GetIntConstant(outcome));
552 }
553 RecordSimplification();
554 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700555 if (outcome && !load_class->HasUses()) {
556 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
557 // However, here we know that it cannot because the instanceof check was successfull, hence
558 // the class was already loaded.
559 load_class->GetBlock()->RemoveInstruction(load_class);
560 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100561 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100562}
563
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100564void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
565 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100566 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100567 instruction->ClearValueCanBeNull();
568 }
569}
570
571void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
572 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100573 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100574 instruction->ClearValueCanBeNull();
575 }
576}
577
Anton Shaminbdd79352016-02-15 12:48:36 +0600578static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
579 HInstruction *lhs = cond->InputAt(0);
580 HInstruction *rhs = cond->InputAt(1);
581 switch (cond->GetKind()) {
582 case HInstruction::kEqual:
583 return new (arena) HEqual(rhs, lhs);
584 case HInstruction::kNotEqual:
585 return new (arena) HNotEqual(rhs, lhs);
586 case HInstruction::kLessThan:
587 return new (arena) HGreaterThan(rhs, lhs);
588 case HInstruction::kLessThanOrEqual:
589 return new (arena) HGreaterThanOrEqual(rhs, lhs);
590 case HInstruction::kGreaterThan:
591 return new (arena) HLessThan(rhs, lhs);
592 case HInstruction::kGreaterThanOrEqual:
593 return new (arena) HLessThanOrEqual(rhs, lhs);
594 case HInstruction::kBelow:
595 return new (arena) HAbove(rhs, lhs);
596 case HInstruction::kBelowOrEqual:
597 return new (arena) HAboveOrEqual(rhs, lhs);
598 case HInstruction::kAbove:
599 return new (arena) HBelow(rhs, lhs);
600 case HInstruction::kAboveOrEqual:
601 return new (arena) HBelowOrEqual(rhs, lhs);
602 default:
603 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
604 }
605 return nullptr;
606}
607
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000608void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100609 HInstruction* input_const = equal->GetConstantRight();
610 if (input_const != nullptr) {
611 HInstruction* input_value = equal->GetLeastConstantLeft();
612 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
613 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100614 // We are comparing the boolean to a constant which is of type int and can
615 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000616 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100617 // Replace (bool_value == true) with bool_value
618 equal->ReplaceWith(input_value);
619 block->RemoveInstruction(equal);
620 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000621 } else if (input_const->AsIntConstant()->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500622 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
623 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100624 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100625 } else {
626 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
627 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
628 block->RemoveInstruction(equal);
629 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100630 }
Mark Mendellc4701932015-04-10 13:18:51 -0400631 } else {
632 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100633 }
Mark Mendellc4701932015-04-10 13:18:51 -0400634 } else {
635 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100636 }
637}
638
David Brazdil0d13fee2015-04-17 14:52:19 +0100639void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
640 HInstruction* input_const = not_equal->GetConstantRight();
641 if (input_const != nullptr) {
642 HInstruction* input_value = not_equal->GetLeastConstantLeft();
643 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
644 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100645 // We are comparing the boolean to a constant which is of type int and can
646 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000647 if (input_const->AsIntConstant()->IsTrue()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500648 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
649 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100650 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000651 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100652 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100653 not_equal->ReplaceWith(input_value);
654 block->RemoveInstruction(not_equal);
655 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100656 } else {
657 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
658 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
659 block->RemoveInstruction(not_equal);
660 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100661 }
Mark Mendellc4701932015-04-10 13:18:51 -0400662 } else {
663 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100664 }
Mark Mendellc4701932015-04-10 13:18:51 -0400665 } else {
666 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100667 }
668}
669
670void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000671 HInstruction* input = bool_not->InputAt(0);
672 HInstruction* replace_with = nullptr;
673
674 if (input->IsIntConstant()) {
675 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000676 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000677 replace_with = GetGraph()->GetIntConstant(0);
678 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000679 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000680 replace_with = GetGraph()->GetIntConstant(1);
681 }
682 } else if (input->IsBooleanNot()) {
683 // Replace (!(!bool_value)) with bool_value.
684 replace_with = input->InputAt(0);
685 } else if (input->IsCondition() &&
686 // Don't change FP compares. The definition of compares involving
687 // NaNs forces the compares to be done as written by the user.
688 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
689 // Replace condition with its opposite.
690 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
691 }
692
693 if (replace_with != nullptr) {
694 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100695 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000696 RecordSimplification();
697 }
698}
699
700void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
701 HInstruction* replace_with = nullptr;
702 HInstruction* condition = select->GetCondition();
703 HInstruction* true_value = select->GetTrueValue();
704 HInstruction* false_value = select->GetFalseValue();
705
706 if (condition->IsBooleanNot()) {
707 // Change ((!cond) ? x : y) to (cond ? y : x).
708 condition = condition->InputAt(0);
709 std::swap(true_value, false_value);
710 select->ReplaceInput(false_value, 0);
711 select->ReplaceInput(true_value, 1);
712 select->ReplaceInput(condition, 2);
713 RecordSimplification();
714 }
715
716 if (true_value == false_value) {
717 // Replace (cond ? x : x) with (x).
718 replace_with = true_value;
719 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000720 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000721 // Replace (true ? x : y) with (x).
722 replace_with = true_value;
723 } else {
724 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000725 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000726 replace_with = false_value;
727 }
728 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000729 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000730 // Replace (cond ? true : false) with (cond).
731 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000732 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000733 // Replace (cond ? false : true) with (!cond).
734 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
735 }
736 }
737
738 if (replace_with != nullptr) {
739 select->ReplaceWith(replace_with);
740 select->GetBlock()->RemoveInstruction(select);
741 RecordSimplification();
742 }
743}
744
745void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
746 HInstruction* condition = instruction->InputAt(0);
747 if (condition->IsBooleanNot()) {
748 // Swap successors if input is negated.
749 instruction->ReplaceInput(condition->InputAt(0), 0);
750 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100751 RecordSimplification();
752 }
753}
754
Mingyao Yang0304e182015-01-30 16:41:29 -0800755void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
756 HInstruction* input = instruction->InputAt(0);
757 // If the array is a NewArray with constant size, replace the array length
758 // with the constant instruction. This helps the bounds check elimination phase.
759 if (input->IsNewArray()) {
760 input = input->InputAt(0);
761 if (input->IsIntConstant()) {
762 instruction->ReplaceWith(input);
763 }
764 }
765}
766
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000767void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000768 HInstruction* value = instruction->GetValue();
769 if (value->GetType() != Primitive::kPrimNot) return;
770
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100771 if (CanEnsureNotNullAt(value, instruction)) {
772 instruction->ClearValueCanBeNull();
773 }
774
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000775 if (value->IsArrayGet()) {
776 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
777 // If the code is just swapping elements in the array, no need for a type check.
778 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100779 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000780 }
781 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100782
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100783 if (value->IsNullConstant()) {
784 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100785 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100786 }
787
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100788 ScopedObjectAccess soa(Thread::Current());
789 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
790 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
791 if (!array_rti.IsValid()) {
792 return;
793 }
794
795 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
796 instruction->ClearNeedsTypeCheck();
797 return;
798 }
799
800 if (array_rti.IsObjectArray()) {
801 if (array_rti.IsExact()) {
802 instruction->ClearNeedsTypeCheck();
803 return;
804 }
805 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100806 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000807}
808
Vladimir Markob52bbde2016-02-12 12:06:05 +0000809static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100810 // Invariant: We should never generate a conversion to a Boolean value.
811 DCHECK_NE(Primitive::kPrimBoolean, result_type);
812
Vladimir Markob52bbde2016-02-12 12:06:05 +0000813 // Besides conversion to the same type, widening integral conversions are implicit,
814 // excluding conversions to long and the byte->char conversion where we need to
815 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
816 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100817 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
818 input_type == Primitive::kPrimByte ||
819 input_type == Primitive::kPrimShort ||
820 input_type == Primitive::kPrimChar)) ||
821 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
822 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
823 input_type == Primitive::kPrimByte)) ||
824 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000825}
826
827static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
828 // The conversion to a larger type is loss-less with the exception of two cases,
829 // - conversion to char, the only unsigned type, where we may lose some bits, and
830 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
831 // For integral to FP conversions this holds because the FP mantissa is large enough.
832 DCHECK_NE(input_type, result_type);
833 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
834 result_type != Primitive::kPrimChar &&
835 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
836}
837
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000838void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000839 HInstruction* input = instruction->GetInput();
840 Primitive::Type input_type = input->GetType();
841 Primitive::Type result_type = instruction->GetResultType();
842 if (IsTypeConversionImplicit(input_type, result_type)) {
843 // Remove the implicit conversion; this includes conversion to the same type.
844 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000845 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000846 RecordSimplification();
847 return;
848 }
849
850 if (input->IsTypeConversion()) {
851 HTypeConversion* input_conversion = input->AsTypeConversion();
852 HInstruction* original_input = input_conversion->GetInput();
853 Primitive::Type original_type = original_input->GetType();
854
855 // When the first conversion is lossless, a direct conversion from the original type
856 // to the final type yields the same result, even for a lossy second conversion, for
857 // example float->double->int or int->double->float.
858 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
859
860 // For integral conversions, see if the first conversion loses only bits that the second
861 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
862 // conversion yields the same result, for example long->int->short or int->char->short.
863 bool integral_conversions_with_non_widening_second =
864 Primitive::IsIntegralType(input_type) &&
865 Primitive::IsIntegralType(original_type) &&
866 Primitive::IsIntegralType(result_type) &&
867 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
868
869 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
870 // If the merged conversion is implicit, do the simplification unconditionally.
871 if (IsTypeConversionImplicit(original_type, result_type)) {
872 instruction->ReplaceWith(original_input);
873 instruction->GetBlock()->RemoveInstruction(instruction);
874 if (!input_conversion->HasUses()) {
875 // Don't wait for DCE.
876 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
877 }
878 RecordSimplification();
879 return;
880 }
881 // Otherwise simplify only if the first conversion has no other use.
882 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
883 input_conversion->ReplaceWith(original_input);
884 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
885 RecordSimplification();
886 return;
887 }
888 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000889 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000890 DCHECK(Primitive::IsIntegralType(input_type));
891 HAnd* input_and = input->AsAnd();
892 HConstant* constant = input_and->GetConstantRight();
893 if (constant != nullptr) {
894 int64_t value = Int64FromConstant(constant);
895 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
896 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
897 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
898 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000899 HInstruction* original_input = input_and->GetLeastConstantLeft();
900 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
901 instruction->ReplaceWith(original_input);
902 instruction->GetBlock()->RemoveInstruction(instruction);
903 RecordSimplification();
904 return;
905 } else if (input->HasOnlyOneNonEnvironmentUse()) {
906 input_and->ReplaceWith(original_input);
907 input_and->GetBlock()->RemoveInstruction(input_and);
908 RecordSimplification();
909 return;
910 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000911 }
912 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000913 }
914}
915
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000916void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
917 HConstant* input_cst = instruction->GetConstantRight();
918 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600919 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +0000920 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000921 // Replace code looking like
922 // ADD dst, src, 0
923 // with
924 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600925 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
926 // `x` is `-0.0`, the former expression yields `0.0`, while the later
927 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600928 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +0600929 instruction->ReplaceWith(input_other);
930 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100931 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +0600932 return;
933 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100934 }
935
936 HInstruction* left = instruction->GetLeft();
937 HInstruction* right = instruction->GetRight();
938 bool left_is_neg = left->IsNeg();
939 bool right_is_neg = right->IsNeg();
940
941 if (left_is_neg && right_is_neg) {
942 if (TryMoveNegOnInputsAfterBinop(instruction)) {
943 return;
944 }
945 }
946
947 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
948 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
949 // Replace code looking like
950 // NEG tmp, b
951 // ADD dst, a, tmp
952 // with
953 // SUB dst, a, b
954 // We do not perform the optimization if the input negation has environment
955 // uses or multiple non-environment uses as it could lead to worse code. In
956 // particular, we do not want the live range of `b` to be extended if we are
957 // not sure the initial 'NEG' instruction can be removed.
958 HInstruction* other = left_is_neg ? right : left;
959 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
960 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
961 RecordSimplification();
962 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000963 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000964 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000965
Anton Kirilove14dc862016-05-13 17:56:15 +0100966 if (TryReplaceWithRotate(instruction)) {
967 return;
968 }
969
970 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
971 // so no need to return.
972 TryHandleAssociativeAndCommutativeOperation(instruction);
973
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600974 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +0100975 TrySubtractionChainSimplification(instruction)) {
976 return;
977 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600978
979 if (integral_type) {
980 // Replace code patterns looking like
981 // SUB dst1, x, y SUB dst1, x, y
982 // ADD dst2, dst1, y ADD dst2, y, dst1
983 // with
984 // SUB dst1, x, y
985 // ADD instruction is not needed in this case, we may use
986 // one of inputs of SUB instead.
987 if (left->IsSub() && left->InputAt(1) == right) {
988 instruction->ReplaceWith(left->InputAt(0));
989 RecordSimplification();
990 instruction->GetBlock()->RemoveInstruction(instruction);
991 return;
992 } else if (right->IsSub() && right->InputAt(1) == left) {
993 instruction->ReplaceWith(right->InputAt(0));
994 RecordSimplification();
995 instruction->GetBlock()->RemoveInstruction(instruction);
996 return;
997 }
998 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000999}
1000
1001void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1002 HConstant* input_cst = instruction->GetConstantRight();
1003 HInstruction* input_other = instruction->GetLeastConstantLeft();
1004
Vladimir Marko452c1b62015-09-25 14:44:17 +01001005 if (input_cst != nullptr) {
1006 int64_t value = Int64FromConstant(input_cst);
1007 if (value == -1) {
1008 // Replace code looking like
1009 // AND dst, src, 0xFFF...FF
1010 // with
1011 // src
1012 instruction->ReplaceWith(input_other);
1013 instruction->GetBlock()->RemoveInstruction(instruction);
1014 RecordSimplification();
1015 return;
1016 }
1017 // Eliminate And from UShr+And if the And-mask contains all the bits that
1018 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1019 // precisely clears the shifted-in sign bits.
1020 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1021 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1022 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1023 size_t num_tail_bits_set = CTZ(value + 1);
1024 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1025 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1026 instruction->ReplaceWith(input_other);
1027 instruction->GetBlock()->RemoveInstruction(instruction);
1028 RecordSimplification();
1029 return;
1030 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1031 input_other->HasOnlyOneNonEnvironmentUse()) {
1032 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1033 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1034 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1035 input_other->InputAt(0),
1036 input_other->InputAt(1),
1037 input_other->GetDexPc());
1038 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1039 input_other->GetBlock()->RemoveInstruction(input_other);
1040 RecordSimplification();
1041 return;
1042 }
1043 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001044 }
1045
1046 // We assume that GVN has run before, so we only perform a pointer comparison.
1047 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001048 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001049 if (instruction->GetLeft() == instruction->GetRight()) {
1050 // Replace code looking like
1051 // AND dst, src, src
1052 // with
1053 // src
1054 instruction->ReplaceWith(instruction->GetLeft());
1055 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001056 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001057 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001058 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001059
Anton Kirilove14dc862016-05-13 17:56:15 +01001060 if (TryDeMorganNegationFactoring(instruction)) {
1061 return;
1062 }
1063
1064 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1065 // so no need to return.
1066 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001067}
1068
Mark Mendellc4701932015-04-10 13:18:51 -04001069void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1070 VisitCondition(condition);
1071}
1072
1073void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1074 VisitCondition(condition);
1075}
1076
1077void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1078 VisitCondition(condition);
1079}
1080
1081void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1082 VisitCondition(condition);
1083}
1084
Anton Shaminbdd79352016-02-15 12:48:36 +06001085void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1086 VisitCondition(condition);
1087}
1088
1089void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1090 VisitCondition(condition);
1091}
1092
1093void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1094 VisitCondition(condition);
1095}
1096
1097void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1098 VisitCondition(condition);
1099}
Aart Bike9f37602015-10-09 11:15:55 -07001100
Mark Mendellc4701932015-04-10 13:18:51 -04001101void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001102 // Reverse condition if left is constant. Our code generators prefer constant
1103 // on the right hand side.
1104 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1105 HBasicBlock* block = condition->GetBlock();
1106 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1107 // If it is a fp we must set the opposite bias.
1108 if (replacement != nullptr) {
1109 if (condition->IsLtBias()) {
1110 replacement->SetBias(ComparisonBias::kGtBias);
1111 } else if (condition->IsGtBias()) {
1112 replacement->SetBias(ComparisonBias::kLtBias);
1113 }
1114 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1115 RecordSimplification();
1116
1117 condition = replacement;
1118 }
1119 }
Mark Mendellc4701932015-04-10 13:18:51 -04001120
Mark Mendellc4701932015-04-10 13:18:51 -04001121 HInstruction* left = condition->GetLeft();
1122 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001123
1124 // Try to fold an HCompare into this HCondition.
1125
Mark Mendellc4701932015-04-10 13:18:51 -04001126 // We can only replace an HCondition which compares a Compare to 0.
1127 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1128 // condition with a long, float or double comparison as input.
1129 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1130 // Conversion is not possible.
1131 return;
1132 }
1133
1134 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001135 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001136 // Someone else also wants the result of the compare.
1137 return;
1138 }
1139
Vladimir Marko46817b82016-03-29 12:21:58 +01001140 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001141 // There is a reference to the compare result in an environment. Do we really need it?
1142 if (GetGraph()->IsDebuggable()) {
1143 return;
1144 }
1145
1146 // We have to ensure that there are no deopt points in the sequence.
1147 if (left->HasAnyEnvironmentUseBefore(condition)) {
1148 return;
1149 }
1150 }
1151
1152 // Clean up any environment uses from the HCompare, if any.
1153 left->RemoveEnvironmentUsers();
1154
1155 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1156 condition->SetBias(left->AsCompare()->GetBias());
1157
1158 // Replace the operands of the HCondition.
1159 condition->ReplaceInput(left->InputAt(0), 0);
1160 condition->ReplaceInput(left->InputAt(1), 1);
1161
1162 // Remove the HCompare.
1163 left->GetBlock()->RemoveInstruction(left);
1164
1165 RecordSimplification();
1166}
1167
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001168void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1169 HConstant* input_cst = instruction->GetConstantRight();
1170 HInstruction* input_other = instruction->GetLeastConstantLeft();
1171 Primitive::Type type = instruction->GetType();
1172
1173 if ((input_cst != nullptr) && input_cst->IsOne()) {
1174 // Replace code looking like
1175 // DIV dst, src, 1
1176 // with
1177 // src
1178 instruction->ReplaceWith(input_other);
1179 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001180 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001181 return;
1182 }
1183
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001184 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001185 // Replace code looking like
1186 // DIV dst, src, -1
1187 // with
1188 // NEG dst, src
1189 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001190 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001191 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001192 return;
1193 }
1194
1195 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1196 // Try replacing code looking like
1197 // DIV dst, src, constant
1198 // with
1199 // MUL dst, src, 1 / constant
1200 HConstant* reciprocal = nullptr;
1201 if (type == Primitive::Primitive::kPrimDouble) {
1202 double value = input_cst->AsDoubleConstant()->GetValue();
1203 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1204 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1205 }
1206 } else {
1207 DCHECK_EQ(type, Primitive::kPrimFloat);
1208 float value = input_cst->AsFloatConstant()->GetValue();
1209 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1210 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1211 }
1212 }
1213
1214 if (reciprocal != nullptr) {
1215 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1216 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1217 RecordSimplification();
1218 return;
1219 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001220 }
1221}
1222
1223void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1224 HConstant* input_cst = instruction->GetConstantRight();
1225 HInstruction* input_other = instruction->GetLeastConstantLeft();
1226 Primitive::Type type = instruction->GetType();
1227 HBasicBlock* block = instruction->GetBlock();
1228 ArenaAllocator* allocator = GetGraph()->GetArena();
1229
1230 if (input_cst == nullptr) {
1231 return;
1232 }
1233
1234 if (input_cst->IsOne()) {
1235 // Replace code looking like
1236 // MUL dst, src, 1
1237 // with
1238 // src
1239 instruction->ReplaceWith(input_other);
1240 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001241 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001242 return;
1243 }
1244
1245 if (input_cst->IsMinusOne() &&
1246 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1247 // Replace code looking like
1248 // MUL dst, src, -1
1249 // with
1250 // NEG dst, src
1251 HNeg* neg = new (allocator) HNeg(type, input_other);
1252 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001253 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001254 return;
1255 }
1256
1257 if (Primitive::IsFloatingPointType(type) &&
1258 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1259 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1260 // Replace code looking like
1261 // FP_MUL dst, src, 2.0
1262 // with
1263 // FP_ADD dst, src, src
1264 // The 'int' and 'long' cases are handled below.
1265 block->ReplaceAndRemoveInstructionWith(instruction,
1266 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001267 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001268 return;
1269 }
1270
1271 if (Primitive::IsIntOrLongType(type)) {
1272 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001273 // Even though constant propagation also takes care of the zero case, other
1274 // optimizations can lead to having a zero multiplication.
1275 if (factor == 0) {
1276 // Replace code looking like
1277 // MUL dst, src, 0
1278 // with
1279 // 0
1280 instruction->ReplaceWith(input_cst);
1281 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001282 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001283 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001284 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001285 // Replace code looking like
1286 // MUL dst, src, pow_of_2
1287 // with
1288 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001289 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001290 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001291 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001292 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001293 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001294 } else if (IsPowerOfTwo(factor - 1)) {
1295 // Transform code looking like
1296 // MUL dst, src, (2^n + 1)
1297 // into
1298 // SHL tmp, src, n
1299 // ADD dst, src, tmp
1300 HShl* shl = new (allocator) HShl(type,
1301 input_other,
1302 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1303 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1304
1305 block->InsertInstructionBefore(shl, instruction);
1306 block->ReplaceAndRemoveInstructionWith(instruction, add);
1307 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001308 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001309 } else if (IsPowerOfTwo(factor + 1)) {
1310 // Transform code looking like
1311 // MUL dst, src, (2^n - 1)
1312 // into
1313 // SHL tmp, src, n
1314 // SUB dst, tmp, src
1315 HShl* shl = new (allocator) HShl(type,
1316 input_other,
1317 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1318 HSub* sub = new (allocator) HSub(type, shl, input_other);
1319
1320 block->InsertInstructionBefore(shl, instruction);
1321 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1322 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001323 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001324 }
1325 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001326
1327 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1328 // so no need to return.
1329 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001330}
1331
Alexandre Rames188d4312015-04-09 18:30:21 +01001332void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1333 HInstruction* input = instruction->GetInput();
1334 if (input->IsNeg()) {
1335 // Replace code looking like
1336 // NEG tmp, src
1337 // NEG dst, tmp
1338 // with
1339 // src
1340 HNeg* previous_neg = input->AsNeg();
1341 instruction->ReplaceWith(previous_neg->GetInput());
1342 instruction->GetBlock()->RemoveInstruction(instruction);
1343 // We perform the optimization even if the input negation has environment
1344 // uses since it allows removing the current instruction. But we only delete
1345 // the input negation only if it is does not have any uses left.
1346 if (!previous_neg->HasUses()) {
1347 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1348 }
1349 RecordSimplification();
1350 return;
1351 }
1352
Serguei Katkov339dfc22015-04-20 12:29:32 +06001353 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1354 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001355 // Replace code looking like
1356 // SUB tmp, a, b
1357 // NEG dst, tmp
1358 // with
1359 // SUB dst, b, a
1360 // We do not perform the optimization if the input subtraction has
1361 // environment uses or multiple non-environment uses as it could lead to
1362 // worse code. In particular, we do not want the live ranges of `a` and `b`
1363 // to be extended if we are not sure the initial 'SUB' instruction can be
1364 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001365 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001366 HSub* sub = input->AsSub();
1367 HSub* new_sub =
1368 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1369 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1370 if (!sub->HasUses()) {
1371 sub->GetBlock()->RemoveInstruction(sub);
1372 }
1373 RecordSimplification();
1374 }
1375}
1376
1377void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1378 HInstruction* input = instruction->GetInput();
1379 if (input->IsNot()) {
1380 // Replace code looking like
1381 // NOT tmp, src
1382 // NOT dst, tmp
1383 // with
1384 // src
1385 // We perform the optimization even if the input negation has environment
1386 // uses since it allows removing the current instruction. But we only delete
1387 // the input negation only if it is does not have any uses left.
1388 HNot* previous_not = input->AsNot();
1389 instruction->ReplaceWith(previous_not->GetInput());
1390 instruction->GetBlock()->RemoveInstruction(instruction);
1391 if (!previous_not->HasUses()) {
1392 previous_not->GetBlock()->RemoveInstruction(previous_not);
1393 }
1394 RecordSimplification();
1395 }
1396}
1397
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001398void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1399 HConstant* input_cst = instruction->GetConstantRight();
1400 HInstruction* input_other = instruction->GetLeastConstantLeft();
1401
Roland Levillain1a653882016-03-18 18:05:57 +00001402 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001403 // Replace code looking like
1404 // OR dst, src, 0
1405 // with
1406 // src
1407 instruction->ReplaceWith(input_other);
1408 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001409 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001410 return;
1411 }
1412
1413 // We assume that GVN has run before, so we only perform a pointer comparison.
1414 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001415 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001416 if (instruction->GetLeft() == instruction->GetRight()) {
1417 // Replace code looking like
1418 // OR dst, src, src
1419 // with
1420 // src
1421 instruction->ReplaceWith(instruction->GetLeft());
1422 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001423 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001424 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001425 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001426
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001427 if (TryDeMorganNegationFactoring(instruction)) return;
1428
Anton Kirilove14dc862016-05-13 17:56:15 +01001429 if (TryReplaceWithRotate(instruction)) {
1430 return;
1431 }
1432
1433 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1434 // so no need to return.
1435 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001436}
1437
1438void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1439 VisitShift(instruction);
1440}
1441
1442void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1443 VisitShift(instruction);
1444}
1445
1446void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1447 HConstant* input_cst = instruction->GetConstantRight();
1448 HInstruction* input_other = instruction->GetLeastConstantLeft();
1449
Serguei Katkov115b53f2015-08-05 17:03:30 +06001450 Primitive::Type type = instruction->GetType();
1451 if (Primitive::IsFloatingPointType(type)) {
1452 return;
1453 }
1454
Roland Levillain1a653882016-03-18 18:05:57 +00001455 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001456 // Replace code looking like
1457 // SUB dst, src, 0
1458 // with
1459 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001460 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1461 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1462 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001463 instruction->ReplaceWith(input_other);
1464 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001465 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001466 return;
1467 }
1468
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001469 HBasicBlock* block = instruction->GetBlock();
1470 ArenaAllocator* allocator = GetGraph()->GetArena();
1471
Alexandre Rames188d4312015-04-09 18:30:21 +01001472 HInstruction* left = instruction->GetLeft();
1473 HInstruction* right = instruction->GetRight();
1474 if (left->IsConstant()) {
1475 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001476 // Replace code looking like
1477 // SUB dst, 0, src
1478 // with
1479 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001480 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001481 // `x` is `0.0`, the former expression yields `0.0`, while the later
1482 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001483 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001484 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001485 RecordSimplification();
1486 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001487 }
1488 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001489
1490 if (left->IsNeg() && right->IsNeg()) {
1491 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1492 return;
1493 }
1494 }
1495
1496 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1497 // Replace code looking like
1498 // NEG tmp, b
1499 // SUB dst, a, tmp
1500 // with
1501 // ADD dst, a, b
1502 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1503 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1504 RecordSimplification();
1505 right->GetBlock()->RemoveInstruction(right);
1506 return;
1507 }
1508
1509 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1510 // Replace code looking like
1511 // NEG tmp, a
1512 // SUB dst, tmp, b
1513 // with
1514 // ADD tmp, a, b
1515 // NEG dst, tmp
1516 // The second version is not intrinsically better, but enables more
1517 // transformations.
1518 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1519 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1520 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1521 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1522 instruction->ReplaceWith(neg);
1523 instruction->GetBlock()->RemoveInstruction(instruction);
1524 RecordSimplification();
1525 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001526 return;
1527 }
1528
1529 if (TrySubtractionChainSimplification(instruction)) {
1530 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001531 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001532
1533 if (left->IsAdd()) {
1534 // Replace code patterns looking like
1535 // ADD dst1, x, y ADD dst1, x, y
1536 // SUB dst2, dst1, y SUB dst2, dst1, x
1537 // with
1538 // ADD dst1, x, y
1539 // SUB instruction is not needed in this case, we may use
1540 // one of inputs of ADD instead.
1541 // It is applicable to integral types only.
1542 DCHECK(Primitive::IsIntegralType(type));
1543 if (left->InputAt(1) == right) {
1544 instruction->ReplaceWith(left->InputAt(0));
1545 RecordSimplification();
1546 instruction->GetBlock()->RemoveInstruction(instruction);
1547 return;
1548 } else if (left->InputAt(0) == right) {
1549 instruction->ReplaceWith(left->InputAt(1));
1550 RecordSimplification();
1551 instruction->GetBlock()->RemoveInstruction(instruction);
1552 return;
1553 }
1554 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001555}
1556
1557void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1558 VisitShift(instruction);
1559}
1560
1561void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1562 HConstant* input_cst = instruction->GetConstantRight();
1563 HInstruction* input_other = instruction->GetLeastConstantLeft();
1564
Roland Levillain1a653882016-03-18 18:05:57 +00001565 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001566 // Replace code looking like
1567 // XOR dst, src, 0
1568 // with
1569 // src
1570 instruction->ReplaceWith(input_other);
1571 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001572 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001573 return;
1574 }
1575
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001576 if ((input_cst != nullptr) && input_cst->IsOne()
1577 && input_other->GetType() == Primitive::kPrimBoolean) {
1578 // Replace code looking like
1579 // XOR dst, src, 1
1580 // with
1581 // BOOLEAN_NOT dst, src
1582 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1583 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1584 RecordSimplification();
1585 return;
1586 }
1587
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001588 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1589 // Replace code looking like
1590 // XOR dst, src, 0xFFF...FF
1591 // with
1592 // NOT dst, src
1593 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1594 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001595 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001596 return;
1597 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001598
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001599 HInstruction* left = instruction->GetLeft();
1600 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001601 if (((left->IsNot() && right->IsNot()) ||
1602 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001603 left->HasOnlyOneNonEnvironmentUse() &&
1604 right->HasOnlyOneNonEnvironmentUse()) {
1605 // Replace code looking like
1606 // NOT nota, a
1607 // NOT notb, b
1608 // XOR dst, nota, notb
1609 // with
1610 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001611 instruction->ReplaceInput(left->InputAt(0), 0);
1612 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001613 left->GetBlock()->RemoveInstruction(left);
1614 right->GetBlock()->RemoveInstruction(right);
1615 RecordSimplification();
1616 return;
1617 }
1618
Anton Kirilove14dc862016-05-13 17:56:15 +01001619 if (TryReplaceWithRotate(instruction)) {
1620 return;
1621 }
1622
1623 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1624 // so no need to return.
1625 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001626}
1627
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001628void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1629 HInstruction* argument = instruction->InputAt(1);
1630 HInstruction* receiver = instruction->InputAt(0);
1631 if (receiver == argument) {
1632 // Because String.equals is an instance call, the receiver is
1633 // a null check if we don't know it's null. The argument however, will
1634 // be the actual object. So we cannot end up in a situation where both
1635 // are equal but could be null.
1636 DCHECK(CanEnsureNotNullAt(argument, instruction));
1637 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1638 instruction->GetBlock()->RemoveInstruction(instruction);
1639 } else {
1640 StringEqualsOptimizations optimizations(instruction);
1641 if (CanEnsureNotNullAt(argument, instruction)) {
1642 optimizations.SetArgumentNotNull();
1643 }
1644 ScopedObjectAccess soa(Thread::Current());
1645 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1646 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1647 optimizations.SetArgumentIsString();
1648 }
1649 }
1650}
1651
Roland Levillain22c49222016-03-18 14:04:28 +00001652void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1653 bool is_left,
1654 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001655 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001656 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001657 HInstruction* value = invoke->InputAt(0);
1658 HInstruction* distance = invoke->InputAt(1);
1659 // Replace the invoke with an HRor.
1660 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001661 // Unconditionally set the type of the negated distance to `int`,
1662 // as shift and rotate operations expect a 32-bit (or narrower)
1663 // value for their distance input.
1664 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001665 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1666 }
Roland Levillain22c49222016-03-18 14:04:28 +00001667 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001668 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1669 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001670 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001671 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1672 clinit->GetBlock()->RemoveInstruction(clinit);
1673 HInstruction* ldclass = clinit->InputAt(0);
1674 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1675 ldclass->GetBlock()->RemoveInstruction(ldclass);
1676 }
1677 }
1678}
1679
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001680static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1681 if (potential_length->IsArrayLength()) {
1682 return potential_length->InputAt(0) == potential_array;
1683 }
1684
1685 if (potential_array->IsNewArray()) {
1686 return potential_array->InputAt(0) == potential_length;
1687 }
1688
1689 return false;
1690}
1691
1692void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1693 HInstruction* source = instruction->InputAt(0);
1694 HInstruction* destination = instruction->InputAt(2);
1695 HInstruction* count = instruction->InputAt(4);
1696 SystemArrayCopyOptimizations optimizations(instruction);
1697 if (CanEnsureNotNullAt(source, instruction)) {
1698 optimizations.SetSourceIsNotNull();
1699 }
1700 if (CanEnsureNotNullAt(destination, instruction)) {
1701 optimizations.SetDestinationIsNotNull();
1702 }
1703 if (destination == source) {
1704 optimizations.SetDestinationIsSource();
1705 }
1706
1707 if (IsArrayLengthOf(count, source)) {
1708 optimizations.SetCountIsSourceLength();
1709 }
1710
1711 if (IsArrayLengthOf(count, destination)) {
1712 optimizations.SetCountIsDestinationLength();
1713 }
1714
1715 {
1716 ScopedObjectAccess soa(Thread::Current());
1717 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1718 if (destination_rti.IsValid()) {
1719 if (destination_rti.IsObjectArray()) {
1720 if (destination_rti.IsExact()) {
1721 optimizations.SetDoesNotNeedTypeCheck();
1722 }
1723 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001724 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001725 if (destination_rti.IsPrimitiveArrayClass()) {
1726 optimizations.SetDestinationIsPrimitiveArray();
1727 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1728 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001729 }
1730 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001731 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1732 if (source_rti.IsValid()) {
1733 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1734 optimizations.SetDoesNotNeedTypeCheck();
1735 }
1736 if (source_rti.IsPrimitiveArrayClass()) {
1737 optimizations.SetSourceIsPrimitiveArray();
1738 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1739 optimizations.SetSourceIsNonPrimitiveArray();
1740 }
1741 }
1742 }
1743}
1744
Roland Levillaina5c4a402016-03-15 15:02:50 +00001745void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1746 bool is_signum,
1747 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001748 DCHECK(invoke->IsInvokeStaticOrDirect());
1749 uint32_t dex_pc = invoke->GetDexPc();
1750 HInstruction* left = invoke->InputAt(0);
1751 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001752 if (!is_signum) {
1753 right = invoke->InputAt(1);
1754 } else if (type == Primitive::kPrimLong) {
1755 right = GetGraph()->GetLongConstant(0);
1756 } else {
1757 right = GetGraph()->GetIntConstant(0);
1758 }
1759 HCompare* compare = new (GetGraph()->GetArena())
1760 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1761 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1762}
1763
Aart Bik75a38b22016-02-17 10:41:50 -08001764void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1765 DCHECK(invoke->IsInvokeStaticOrDirect());
1766 uint32_t dex_pc = invoke->GetDexPc();
1767 // IsNaN(x) is the same as x != x.
1768 HInstruction* x = invoke->InputAt(0);
1769 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001770 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001771 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1772}
1773
Aart Bik2a6aad92016-02-25 11:32:32 -08001774void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1775 DCHECK(invoke->IsInvokeStaticOrDirect());
1776 uint32_t dex_pc = invoke->GetDexPc();
1777 HInstruction* x = invoke->InputAt(0);
1778 Primitive::Type type = x->GetType();
1779 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1780 HInstruction* nan;
1781 if (type == Primitive::kPrimDouble) {
1782 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1783 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1784 kNeedsEnvironmentOrCache,
1785 kNoSideEffects,
1786 kNoThrow);
1787 } else {
1788 DCHECK_EQ(type, Primitive::kPrimFloat);
1789 nan = GetGraph()->GetIntConstant(0x7fc00000);
1790 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1791 kNeedsEnvironmentOrCache,
1792 kNoSideEffects,
1793 kNoThrow);
1794 }
1795 // Test IsNaN(x), which is the same as x != x.
1796 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1797 condition->SetBias(ComparisonBias::kLtBias);
1798 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1799 // Select between the two.
1800 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1801 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1802 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1803}
1804
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001805void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1806 HInstruction* str = invoke->InputAt(0);
1807 HInstruction* index = invoke->InputAt(1);
1808 uint32_t dex_pc = invoke->GetDexPc();
1809 ArenaAllocator* arena = GetGraph()->GetArena();
1810 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1811 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1812 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1813 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1814 HBoundsCheck* bounds_check =
1815 new (arena) HBoundsCheck(index, length, dex_pc, invoke->GetDexMethodIndex());
1816 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
1817 HArrayGet* array_get =
1818 new (arena) HArrayGet(str, index, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
1819 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1820 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1821 GetGraph()->SetHasBoundsChecks(true);
1822}
1823
Vladimir Markodce016e2016-04-28 13:10:02 +01001824void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1825 HInstruction* str = invoke->InputAt(0);
1826 uint32_t dex_pc = invoke->GetDexPc();
1827 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1828 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001829 HArrayLength* length =
1830 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001831 HInstruction* replacement;
1832 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1833 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1834 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1835 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1836 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1837 replacement = equal;
1838 } else {
1839 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1840 replacement = length;
1841 }
1842 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
1843}
1844
Aart Bik11932592016-03-08 12:42:25 -08001845void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1846 uint32_t dex_pc = invoke->GetDexPc();
1847 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1848 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1849}
1850
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001851void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001852 switch (instruction->GetIntrinsic()) {
1853 case Intrinsics::kStringEquals:
1854 SimplifyStringEquals(instruction);
1855 break;
1856 case Intrinsics::kSystemArrayCopy:
1857 SimplifySystemArrayCopy(instruction);
1858 break;
1859 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001860 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1861 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001862 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001863 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001864 break;
1865 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001866 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1867 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001868 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001869 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001870 break;
1871 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001872 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1873 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001874 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001875 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001876 break;
1877 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001878 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1879 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001880 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001881 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001882 break;
1883 case Intrinsics::kFloatIsNaN:
1884 case Intrinsics::kDoubleIsNaN:
1885 SimplifyIsNaN(instruction);
1886 break;
1887 case Intrinsics::kFloatFloatToIntBits:
1888 case Intrinsics::kDoubleDoubleToLongBits:
1889 SimplifyFP2Int(instruction);
1890 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001891 case Intrinsics::kStringCharAt:
1892 SimplifyStringCharAt(instruction);
1893 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01001894 case Intrinsics::kStringIsEmpty:
1895 case Intrinsics::kStringLength:
1896 SimplifyStringIsEmptyOrLength(instruction);
1897 break;
Aart Bik11932592016-03-08 12:42:25 -08001898 case Intrinsics::kUnsafeLoadFence:
1899 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1900 break;
1901 case Intrinsics::kUnsafeStoreFence:
1902 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1903 break;
1904 case Intrinsics::kUnsafeFullFence:
1905 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1906 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001907 default:
1908 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001909 }
1910}
1911
Aart Bikbb245d12015-10-19 11:05:03 -07001912void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1913 HInstruction* cond = deoptimize->InputAt(0);
1914 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001915 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001916 // Never deopt: instruction can be removed.
1917 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1918 } else {
1919 // Always deopt.
1920 }
1921 }
1922}
1923
Anton Kirilove14dc862016-05-13 17:56:15 +01001924// Replace code looking like
1925// OP y, x, const1
1926// OP z, y, const2
1927// with
1928// OP z, x, const3
1929// where OP is both an associative and a commutative operation.
1930bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
1931 HBinaryOperation* instruction) {
1932 DCHECK(instruction->IsCommutative());
1933
1934 if (!Primitive::IsIntegralType(instruction->GetType())) {
1935 return false;
1936 }
1937
1938 HInstruction* left = instruction->GetLeft();
1939 HInstruction* right = instruction->GetRight();
1940 // Variable names as described above.
1941 HConstant* const2;
1942 HBinaryOperation* y;
1943
1944 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
1945 const2 = right->AsConstant();
1946 y = left->AsBinaryOperation();
1947 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
1948 const2 = left->AsConstant();
1949 y = right->AsBinaryOperation();
1950 } else {
1951 // The node does not match the pattern.
1952 return false;
1953 }
1954
1955 // If `y` has more than one use, we do not perform the optimization
1956 // because it might increase code size (e.g. if the new constant is
1957 // no longer encodable as an immediate operand in the target ISA).
1958 if (!y->HasOnlyOneNonEnvironmentUse()) {
1959 return false;
1960 }
1961
1962 // GetConstantRight() can return both left and right constants
1963 // for commutative operations.
1964 HConstant* const1 = y->GetConstantRight();
1965 if (const1 == nullptr) {
1966 return false;
1967 }
1968
1969 instruction->ReplaceInput(const1, 0);
1970 instruction->ReplaceInput(const2, 1);
1971 HConstant* const3 = instruction->TryStaticEvaluation();
1972 DCHECK(const3 != nullptr);
1973 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
1974 instruction->ReplaceInput(const3, 1);
1975 RecordSimplification();
1976 return true;
1977}
1978
1979static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
1980 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
1981}
1982
1983// Helper function that performs addition statically, considering the result type.
1984static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
1985 // Use the Compute() method for consistency with TryStaticEvaluation().
1986 if (type == Primitive::kPrimInt) {
1987 return HAdd::Compute<int32_t>(x, y);
1988 } else {
1989 DCHECK_EQ(type, Primitive::kPrimLong);
1990 return HAdd::Compute<int64_t>(x, y);
1991 }
1992}
1993
1994// Helper function that handles the child classes of HConstant
1995// and returns an integer with the appropriate sign.
1996static int64_t GetValue(HConstant* constant, bool is_negated) {
1997 int64_t ret = Int64FromConstant(constant);
1998 return is_negated ? -ret : ret;
1999}
2000
2001// Replace code looking like
2002// OP1 y, x, const1
2003// OP2 z, y, const2
2004// with
2005// OP3 z, x, const3
2006// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2007bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2008 HBinaryOperation* instruction) {
2009 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2010
2011 Primitive::Type type = instruction->GetType();
2012 if (!Primitive::IsIntegralType(type)) {
2013 return false;
2014 }
2015
2016 HInstruction* left = instruction->GetLeft();
2017 HInstruction* right = instruction->GetRight();
2018 // Variable names as described above.
2019 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2020 if (const2 == nullptr) {
2021 return false;
2022 }
2023
2024 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2025 ? left->AsBinaryOperation()
2026 : AsAddOrSub(right);
2027 // If y has more than one use, we do not perform the optimization because
2028 // it might increase code size (e.g. if the new constant is no longer
2029 // encodable as an immediate operand in the target ISA).
2030 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2031 return false;
2032 }
2033
2034 left = y->GetLeft();
2035 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2036 if (const1 == nullptr) {
2037 return false;
2038 }
2039
2040 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2041 // If both inputs are constants, let the constant folding pass deal with it.
2042 if (x->IsConstant()) {
2043 return false;
2044 }
2045
2046 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2047 int64_t const2_val = GetValue(const2, is_const2_negated);
2048 bool is_y_negated = (y == right) && instruction->IsSub();
2049 right = y->GetRight();
2050 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2051 int64_t const1_val = GetValue(const1, is_const1_negated);
2052 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2053 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2054 HBasicBlock* block = instruction->GetBlock();
2055 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2056 ArenaAllocator* arena = instruction->GetArena();
2057 HInstruction* z;
2058
2059 if (is_x_negated) {
2060 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2061 } else {
2062 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2063 }
2064
2065 block->ReplaceAndRemoveInstructionWith(instruction, z);
2066 RecordSimplification();
2067 return true;
2068}
2069
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002070} // namespace art