blob: 3041c4d2c751846b58844b3e91258e72c4ac1651 [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"
21#include "scoped_thread_state_change.h"
22
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);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000057 void VisitShift(HBinaryOperation* shift);
58
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000059 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010060 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
61 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010062 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
63 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000064 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000065 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000066 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080067 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000068 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000069 void VisitAdd(HAdd* instruction) OVERRIDE;
70 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040071 void VisitCondition(HCondition* instruction) OVERRIDE;
72 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
73 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
74 void VisitLessThan(HLessThan* condition) OVERRIDE;
75 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060076 void VisitBelow(HBelow* condition) OVERRIDE;
77 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
78 void VisitAbove(HAbove* condition) OVERRIDE;
79 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000080 void VisitDiv(HDiv* instruction) OVERRIDE;
81 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010082 void VisitNeg(HNeg* instruction) OVERRIDE;
83 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000084 void VisitOr(HOr* instruction) OVERRIDE;
85 void VisitShl(HShl* instruction) OVERRIDE;
86 void VisitShr(HShr* instruction) OVERRIDE;
87 void VisitSub(HSub* instruction) OVERRIDE;
88 void VisitUShr(HUShr* instruction) OVERRIDE;
89 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000090 void VisitSelect(HSelect* select) OVERRIDE;
91 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010092 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010093 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070094 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010095
96 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +000097
Roland Levillain22c49222016-03-18 14:04:28 +000098 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010099 void SimplifySystemArrayCopy(HInvoke* invoke);
100 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000101 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800102 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800103 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100104 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100105 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800106 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100107
Calin Juravleacf735c2015-02-12 15:25:22 +0000108 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100109 bool simplification_occurred_ = false;
110 int simplifications_at_current_position_ = 0;
111 // We ensure we do not loop infinitely. The value is a finger in the air guess
112 // that should allow enough simplification.
113 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000114};
115
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100116void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000117 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100118 visitor.Run();
119}
120
121void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100122 // Iterate in reverse post order to open up more simplifications to users
123 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100124 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
125 // The simplification of an instruction to another instruction may yield
126 // possibilities for other simplifications. So although we perform a reverse
127 // post order visit, we sometimes need to revisit an instruction index.
128 simplification_occurred_ = false;
129 VisitBasicBlock(it.Current());
130 if (simplification_occurred_ &&
131 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
132 // New simplifications may be applicable to the instruction at the
133 // current index, so don't advance the iterator.
134 continue;
135 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100136 simplifications_at_current_position_ = 0;
137 it.Advance();
138 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100139}
140
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000141namespace {
142
143bool AreAllBitsSet(HConstant* constant) {
144 return Int64FromConstant(constant) == -1;
145}
146
147} // namespace
148
Alexandre Rames188d4312015-04-09 18:30:21 +0100149// Returns true if the code was simplified to use only one negation operation
150// after the binary operation instead of one on each of the inputs.
151bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
152 DCHECK(binop->IsAdd() || binop->IsSub());
153 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
154 HNeg* left_neg = binop->GetLeft()->AsNeg();
155 HNeg* right_neg = binop->GetRight()->AsNeg();
156 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
157 !right_neg->HasOnlyOneNonEnvironmentUse()) {
158 return false;
159 }
160 // Replace code looking like
161 // NEG tmp1, a
162 // NEG tmp2, b
163 // ADD dst, tmp1, tmp2
164 // with
165 // ADD tmp, a, b
166 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600167 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
168 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
169 // while the later yields `-0.0`.
170 if (!Primitive::IsIntegralType(binop->GetType())) {
171 return false;
172 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100173 binop->ReplaceInput(left_neg->GetInput(), 0);
174 binop->ReplaceInput(right_neg->GetInput(), 1);
175 left_neg->GetBlock()->RemoveInstruction(left_neg);
176 right_neg->GetBlock()->RemoveInstruction(right_neg);
177 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
178 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
179 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
180 RecordSimplification();
181 return true;
182}
183
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000184bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
185 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
186 Primitive::Type type = op->GetType();
187 HInstruction* left = op->GetLeft();
188 HInstruction* right = op->GetRight();
189
190 // We can apply De Morgan's laws if both inputs are Not's and are only used
191 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000192 if (((left->IsNot() && right->IsNot()) ||
193 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000194 left->HasOnlyOneNonEnvironmentUse() &&
195 right->HasOnlyOneNonEnvironmentUse()) {
196 // Replace code looking like
197 // NOT nota, a
198 // NOT notb, b
199 // AND dst, nota, notb (respectively OR)
200 // with
201 // OR or, a, b (respectively AND)
202 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000203 HInstruction* src_left = left->InputAt(0);
204 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000205 uint32_t dex_pc = op->GetDexPc();
206
207 // Remove the negations on the inputs.
208 left->ReplaceWith(src_left);
209 right->ReplaceWith(src_right);
210 left->GetBlock()->RemoveInstruction(left);
211 right->GetBlock()->RemoveInstruction(right);
212
213 // Replace the `HAnd` or `HOr`.
214 HBinaryOperation* hbin;
215 if (op->IsAnd()) {
216 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
217 } else {
218 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
219 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000220 HInstruction* hnot;
221 if (left->IsBooleanNot()) {
222 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
223 } else {
224 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
225 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000226
227 op->GetBlock()->InsertInstructionBefore(hbin, op);
228 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
229
230 RecordSimplification();
231 return true;
232 }
233
234 return false;
235}
236
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000237void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
238 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100239 HInstruction* shift_amount = instruction->GetRight();
240 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000241
Alexandre Rames50518442016-06-27 11:39:19 +0100242 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
243 ? kMaxLongShiftDistance
244 : kMaxIntShiftDistance;
245
246 if (shift_amount->IsConstant()) {
247 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
248 if ((cst & implicit_mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400249 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100250 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400251 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100252 // value
253 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400254 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100255 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100256 return;
257 }
258 }
259
260 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
261 // unnecessary explicit masking operations on the shift amount.
262 // Replace code looking like
263 // AND masked_shift, shift, <superset of implicit mask>
264 // SHL dst, value, masked_shift
265 // with
266 // SHL dst, value, shift
267 if (shift_amount->IsAnd()) {
268 HAnd* and_insn = shift_amount->AsAnd();
269 HConstant* mask = and_insn->GetConstantRight();
270 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
271 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
272 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400273 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000274 }
275}
276
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000277static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
278 return (sub->GetRight() == other &&
279 sub->GetLeft()->IsConstant() &&
280 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
281}
282
283bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
284 HUShr* ushr,
285 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000286 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
287 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000288 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
289 if (!ushr->HasUses()) {
290 ushr->GetBlock()->RemoveInstruction(ushr);
291 }
292 if (!ushr->GetRight()->HasUses()) {
293 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
294 }
295 if (!shl->HasUses()) {
296 shl->GetBlock()->RemoveInstruction(shl);
297 }
298 if (!shl->GetRight()->HasUses()) {
299 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
300 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100301 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000302 return true;
303}
304
305// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
306bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000307 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
308 HInstruction* left = op->GetLeft();
309 HInstruction* right = op->GetRight();
310 // If we have an UShr and a Shl (in either order).
311 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
312 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
313 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
314 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
315 if (ushr->GetType() == shl->GetType() &&
316 ushr->GetLeft() == shl->GetLeft()) {
317 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
318 // Shift distances are both constant, try replacing with Ror if they
319 // add up to the register size.
320 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
321 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
322 // Shift distances are potentially of the form x and (reg_size - x).
323 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
324 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
325 // Shift distances are potentially of the form d and -d.
326 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
327 }
328 }
329 }
330 return false;
331}
332
333// Try replacing code looking like (x >>> #rdist OP x << #ldist):
334// UShr dst, x, #rdist
335// Shl tmp, x, #ldist
336// OP dst, dst, tmp
337// or like (x >>> #rdist OP x << #-ldist):
338// UShr dst, x, #rdist
339// Shl tmp, x, #-ldist
340// OP dst, dst, tmp
341// with
342// Ror dst, x, #rdist
343bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
344 HUShr* ushr,
345 HShl* shl) {
346 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
347 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
348 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
349 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
350 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
351 ReplaceRotateWithRor(op, ushr, shl);
352 return true;
353 }
354 return false;
355}
356
357// Replace code looking like (x >>> -d OP x << d):
358// Neg neg, d
359// UShr dst, x, neg
360// Shl tmp, x, d
361// OP dst, dst, tmp
362// with
363// Neg neg, d
364// Ror dst, x, neg
365// *** OR ***
366// Replace code looking like (x >>> d OP x << -d):
367// UShr dst, x, d
368// Neg neg, d
369// Shl tmp, x, neg
370// OP dst, dst, tmp
371// with
372// Ror dst, x, d
373bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
374 HUShr* ushr,
375 HShl* shl) {
376 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
377 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
378 bool neg_is_left = shl->GetRight()->IsNeg();
379 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
380 // And the shift distance being negated is the distance being shifted the other way.
381 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
382 ReplaceRotateWithRor(op, ushr, shl);
383 }
384 return false;
385}
386
387// Try replacing code looking like (x >>> d OP x << (#bits - d)):
388// UShr dst, x, d
389// Sub ld, #bits, d
390// Shl tmp, x, ld
391// OP dst, dst, tmp
392// with
393// Ror dst, x, d
394// *** OR ***
395// Replace code looking like (x >>> (#bits - d) OP x << d):
396// Sub rd, #bits, d
397// UShr dst, x, rd
398// Shl tmp, x, d
399// OP dst, dst, tmp
400// with
401// Neg neg, d
402// Ror dst, x, neg
403bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
404 HUShr* ushr,
405 HShl* shl) {
406 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
407 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
408 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
409 HInstruction* shl_shift = shl->GetRight();
410 HInstruction* ushr_shift = ushr->GetRight();
411 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
412 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
413 return ReplaceRotateWithRor(op, ushr, shl);
414 }
415 return false;
416}
417
Calin Juravle10e244f2015-01-26 18:54:32 +0000418void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
419 HInstruction* obj = null_check->InputAt(0);
420 if (!obj->CanBeNull()) {
421 null_check->ReplaceWith(obj);
422 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000423 if (stats_ != nullptr) {
424 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
425 }
426 }
427}
428
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100429bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
430 if (!input->CanBeNull()) {
431 return true;
432 }
433
Vladimir Marko46817b82016-03-29 12:21:58 +0100434 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
435 HInstruction* user = use.GetUser();
436 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100437 return true;
438 }
439 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100440
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100441 return false;
442}
443
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100444// Returns whether doing a type test between the class of `object` against `klass` has
445// a statically known outcome. The result of the test is stored in `outcome`.
446static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000447 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
448 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
449 ScopedObjectAccess soa(Thread::Current());
450 if (!obj_rti.IsValid()) {
451 // We run the simplifier before the reference type propagation so type info might not be
452 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100453 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000454 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100455
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100456 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100457 if (!class_rti.IsValid()) {
458 // Happens when the loaded class is unresolved.
459 return false;
460 }
461 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000462 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100463 *outcome = true;
464 return true;
465 } else if (obj_rti.IsExact()) {
466 // The test failed at compile time so will also fail at runtime.
467 *outcome = false;
468 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100469 } else if (!class_rti.IsInterface()
470 && !obj_rti.IsInterface()
471 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100472 // Different type hierarchy. The test will fail.
473 *outcome = false;
474 return true;
475 }
476 return false;
477}
478
479void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
480 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100481 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
482 if (load_class->NeedsAccessCheck()) {
483 // If we need to perform an access check we cannot remove the instruction.
484 return;
485 }
486
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100487 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100488 check_cast->ClearMustDoNullCheck();
489 }
490
491 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000492 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000493 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100494 return;
495 }
496
Vladimir Markoa65ed302016-03-14 21:21:29 +0000497 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
498 // the return value check with the `outcome` check, b/27651442 .
499 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700500 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100501 if (outcome) {
502 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000503 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700504 if (!load_class->HasUses()) {
505 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
506 // However, here we know that it cannot because the checkcast was successfull, hence
507 // the class was already loaded.
508 load_class->GetBlock()->RemoveInstruction(load_class);
509 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100510 } else {
511 // Don't do anything for exceptional cases for now. Ideally we should remove
512 // all instructions and blocks this instruction dominates.
513 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000514 }
515}
516
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100517void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100518 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100519 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
520 if (load_class->NeedsAccessCheck()) {
521 // If we need to perform an access check we cannot remove the instruction.
522 return;
523 }
524
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100525 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100526 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100527 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100528 instruction->ClearMustDoNullCheck();
529 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100530
531 HGraph* graph = GetGraph();
532 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000533 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100534 instruction->ReplaceWith(graph->GetIntConstant(0));
535 instruction->GetBlock()->RemoveInstruction(instruction);
536 RecordSimplification();
537 return;
538 }
539
Vladimir Marko24bd8952016-03-15 10:40:33 +0000540 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
541 // the return value check with the `outcome` check, b/27651442 .
542 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700543 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000544 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100545 if (outcome && can_be_null) {
546 // Type test will succeed, we just need a null test.
547 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
548 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
549 instruction->ReplaceWith(test);
550 } else {
551 // We've statically determined the result of the instanceof.
552 instruction->ReplaceWith(graph->GetIntConstant(outcome));
553 }
554 RecordSimplification();
555 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700556 if (outcome && !load_class->HasUses()) {
557 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
558 // However, here we know that it cannot because the instanceof check was successfull, hence
559 // the class was already loaded.
560 load_class->GetBlock()->RemoveInstruction(load_class);
561 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100562 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100563}
564
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100565void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
566 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100567 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100568 instruction->ClearValueCanBeNull();
569 }
570}
571
572void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
573 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100574 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100575 instruction->ClearValueCanBeNull();
576 }
577}
578
Anton Shaminbdd79352016-02-15 12:48:36 +0600579static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
580 HInstruction *lhs = cond->InputAt(0);
581 HInstruction *rhs = cond->InputAt(1);
582 switch (cond->GetKind()) {
583 case HInstruction::kEqual:
584 return new (arena) HEqual(rhs, lhs);
585 case HInstruction::kNotEqual:
586 return new (arena) HNotEqual(rhs, lhs);
587 case HInstruction::kLessThan:
588 return new (arena) HGreaterThan(rhs, lhs);
589 case HInstruction::kLessThanOrEqual:
590 return new (arena) HGreaterThanOrEqual(rhs, lhs);
591 case HInstruction::kGreaterThan:
592 return new (arena) HLessThan(rhs, lhs);
593 case HInstruction::kGreaterThanOrEqual:
594 return new (arena) HLessThanOrEqual(rhs, lhs);
595 case HInstruction::kBelow:
596 return new (arena) HAbove(rhs, lhs);
597 case HInstruction::kBelowOrEqual:
598 return new (arena) HAboveOrEqual(rhs, lhs);
599 case HInstruction::kAbove:
600 return new (arena) HBelow(rhs, lhs);
601 case HInstruction::kAboveOrEqual:
602 return new (arena) HBelowOrEqual(rhs, lhs);
603 default:
604 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
605 }
606 return nullptr;
607}
608
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000609void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100610 HInstruction* input_const = equal->GetConstantRight();
611 if (input_const != nullptr) {
612 HInstruction* input_value = equal->GetLeastConstantLeft();
613 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
614 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100615 // We are comparing the boolean to a constant which is of type int and can
616 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000617 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100618 // Replace (bool_value == true) with bool_value
619 equal->ReplaceWith(input_value);
620 block->RemoveInstruction(equal);
621 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000622 } else if (input_const->AsIntConstant()->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500623 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
624 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100625 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100626 } else {
627 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
628 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
629 block->RemoveInstruction(equal);
630 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100631 }
Mark Mendellc4701932015-04-10 13:18:51 -0400632 } else {
633 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100634 }
Mark Mendellc4701932015-04-10 13:18:51 -0400635 } else {
636 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100637 }
638}
639
David Brazdil0d13fee2015-04-17 14:52:19 +0100640void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
641 HInstruction* input_const = not_equal->GetConstantRight();
642 if (input_const != nullptr) {
643 HInstruction* input_value = not_equal->GetLeastConstantLeft();
644 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
645 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100646 // We are comparing the boolean to a constant which is of type int and can
647 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000648 if (input_const->AsIntConstant()->IsTrue()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500649 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
650 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100651 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000652 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100653 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100654 not_equal->ReplaceWith(input_value);
655 block->RemoveInstruction(not_equal);
656 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100657 } else {
658 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
659 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
660 block->RemoveInstruction(not_equal);
661 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100662 }
Mark Mendellc4701932015-04-10 13:18:51 -0400663 } else {
664 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100665 }
Mark Mendellc4701932015-04-10 13:18:51 -0400666 } else {
667 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100668 }
669}
670
671void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000672 HInstruction* input = bool_not->InputAt(0);
673 HInstruction* replace_with = nullptr;
674
675 if (input->IsIntConstant()) {
676 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000677 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000678 replace_with = GetGraph()->GetIntConstant(0);
679 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000680 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000681 replace_with = GetGraph()->GetIntConstant(1);
682 }
683 } else if (input->IsBooleanNot()) {
684 // Replace (!(!bool_value)) with bool_value.
685 replace_with = input->InputAt(0);
686 } else if (input->IsCondition() &&
687 // Don't change FP compares. The definition of compares involving
688 // NaNs forces the compares to be done as written by the user.
689 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
690 // Replace condition with its opposite.
691 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
692 }
693
694 if (replace_with != nullptr) {
695 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100696 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000697 RecordSimplification();
698 }
699}
700
701void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
702 HInstruction* replace_with = nullptr;
703 HInstruction* condition = select->GetCondition();
704 HInstruction* true_value = select->GetTrueValue();
705 HInstruction* false_value = select->GetFalseValue();
706
707 if (condition->IsBooleanNot()) {
708 // Change ((!cond) ? x : y) to (cond ? y : x).
709 condition = condition->InputAt(0);
710 std::swap(true_value, false_value);
711 select->ReplaceInput(false_value, 0);
712 select->ReplaceInput(true_value, 1);
713 select->ReplaceInput(condition, 2);
714 RecordSimplification();
715 }
716
717 if (true_value == false_value) {
718 // Replace (cond ? x : x) with (x).
719 replace_with = true_value;
720 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000721 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000722 // Replace (true ? x : y) with (x).
723 replace_with = true_value;
724 } else {
725 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000726 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000727 replace_with = false_value;
728 }
729 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000730 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000731 // Replace (cond ? true : false) with (cond).
732 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000733 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000734 // Replace (cond ? false : true) with (!cond).
735 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
736 }
737 }
738
739 if (replace_with != nullptr) {
740 select->ReplaceWith(replace_with);
741 select->GetBlock()->RemoveInstruction(select);
742 RecordSimplification();
743 }
744}
745
746void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
747 HInstruction* condition = instruction->InputAt(0);
748 if (condition->IsBooleanNot()) {
749 // Swap successors if input is negated.
750 instruction->ReplaceInput(condition->InputAt(0), 0);
751 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100752 RecordSimplification();
753 }
754}
755
Mingyao Yang0304e182015-01-30 16:41:29 -0800756void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
757 HInstruction* input = instruction->InputAt(0);
758 // If the array is a NewArray with constant size, replace the array length
759 // with the constant instruction. This helps the bounds check elimination phase.
760 if (input->IsNewArray()) {
761 input = input->InputAt(0);
762 if (input->IsIntConstant()) {
763 instruction->ReplaceWith(input);
764 }
765 }
766}
767
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000768void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000769 HInstruction* value = instruction->GetValue();
770 if (value->GetType() != Primitive::kPrimNot) return;
771
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100772 if (CanEnsureNotNullAt(value, instruction)) {
773 instruction->ClearValueCanBeNull();
774 }
775
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000776 if (value->IsArrayGet()) {
777 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
778 // If the code is just swapping elements in the array, no need for a type check.
779 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100780 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000781 }
782 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100783
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100784 if (value->IsNullConstant()) {
785 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100786 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100787 }
788
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100789 ScopedObjectAccess soa(Thread::Current());
790 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
791 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
792 if (!array_rti.IsValid()) {
793 return;
794 }
795
796 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
797 instruction->ClearNeedsTypeCheck();
798 return;
799 }
800
801 if (array_rti.IsObjectArray()) {
802 if (array_rti.IsExact()) {
803 instruction->ClearNeedsTypeCheck();
804 return;
805 }
806 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100807 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000808}
809
Vladimir Markob52bbde2016-02-12 12:06:05 +0000810static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100811 // Invariant: We should never generate a conversion to a Boolean value.
812 DCHECK_NE(Primitive::kPrimBoolean, result_type);
813
Vladimir Markob52bbde2016-02-12 12:06:05 +0000814 // Besides conversion to the same type, widening integral conversions are implicit,
815 // excluding conversions to long and the byte->char conversion where we need to
816 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
817 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100818 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
819 input_type == Primitive::kPrimByte ||
820 input_type == Primitive::kPrimShort ||
821 input_type == Primitive::kPrimChar)) ||
822 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
823 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
824 input_type == Primitive::kPrimByte)) ||
825 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000826}
827
828static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
829 // The conversion to a larger type is loss-less with the exception of two cases,
830 // - conversion to char, the only unsigned type, where we may lose some bits, and
831 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
832 // For integral to FP conversions this holds because the FP mantissa is large enough.
833 DCHECK_NE(input_type, result_type);
834 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
835 result_type != Primitive::kPrimChar &&
836 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
837}
838
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000839void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000840 HInstruction* input = instruction->GetInput();
841 Primitive::Type input_type = input->GetType();
842 Primitive::Type result_type = instruction->GetResultType();
843 if (IsTypeConversionImplicit(input_type, result_type)) {
844 // Remove the implicit conversion; this includes conversion to the same type.
845 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000846 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000847 RecordSimplification();
848 return;
849 }
850
851 if (input->IsTypeConversion()) {
852 HTypeConversion* input_conversion = input->AsTypeConversion();
853 HInstruction* original_input = input_conversion->GetInput();
854 Primitive::Type original_type = original_input->GetType();
855
856 // When the first conversion is lossless, a direct conversion from the original type
857 // to the final type yields the same result, even for a lossy second conversion, for
858 // example float->double->int or int->double->float.
859 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
860
861 // For integral conversions, see if the first conversion loses only bits that the second
862 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
863 // conversion yields the same result, for example long->int->short or int->char->short.
864 bool integral_conversions_with_non_widening_second =
865 Primitive::IsIntegralType(input_type) &&
866 Primitive::IsIntegralType(original_type) &&
867 Primitive::IsIntegralType(result_type) &&
868 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
869
870 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
871 // If the merged conversion is implicit, do the simplification unconditionally.
872 if (IsTypeConversionImplicit(original_type, result_type)) {
873 instruction->ReplaceWith(original_input);
874 instruction->GetBlock()->RemoveInstruction(instruction);
875 if (!input_conversion->HasUses()) {
876 // Don't wait for DCE.
877 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
878 }
879 RecordSimplification();
880 return;
881 }
882 // Otherwise simplify only if the first conversion has no other use.
883 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
884 input_conversion->ReplaceWith(original_input);
885 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
886 RecordSimplification();
887 return;
888 }
889 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000890 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000891 DCHECK(Primitive::IsIntegralType(input_type));
892 HAnd* input_and = input->AsAnd();
893 HConstant* constant = input_and->GetConstantRight();
894 if (constant != nullptr) {
895 int64_t value = Int64FromConstant(constant);
896 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
897 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
898 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
899 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000900 HInstruction* original_input = input_and->GetLeastConstantLeft();
901 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
902 instruction->ReplaceWith(original_input);
903 instruction->GetBlock()->RemoveInstruction(instruction);
904 RecordSimplification();
905 return;
906 } else if (input->HasOnlyOneNonEnvironmentUse()) {
907 input_and->ReplaceWith(original_input);
908 input_and->GetBlock()->RemoveInstruction(input_and);
909 RecordSimplification();
910 return;
911 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000912 }
913 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000914 }
915}
916
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000917void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
918 HConstant* input_cst = instruction->GetConstantRight();
919 HInstruction* input_other = instruction->GetLeastConstantLeft();
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`.
928 if (Primitive::IsIntegralType(instruction->GetType())) {
929 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
966 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000967}
968
969void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
970 HConstant* input_cst = instruction->GetConstantRight();
971 HInstruction* input_other = instruction->GetLeastConstantLeft();
972
Vladimir Marko452c1b62015-09-25 14:44:17 +0100973 if (input_cst != nullptr) {
974 int64_t value = Int64FromConstant(input_cst);
975 if (value == -1) {
976 // Replace code looking like
977 // AND dst, src, 0xFFF...FF
978 // with
979 // src
980 instruction->ReplaceWith(input_other);
981 instruction->GetBlock()->RemoveInstruction(instruction);
982 RecordSimplification();
983 return;
984 }
985 // Eliminate And from UShr+And if the And-mask contains all the bits that
986 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
987 // precisely clears the shifted-in sign bits.
988 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
989 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
990 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
991 size_t num_tail_bits_set = CTZ(value + 1);
992 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
993 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
994 instruction->ReplaceWith(input_other);
995 instruction->GetBlock()->RemoveInstruction(instruction);
996 RecordSimplification();
997 return;
998 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
999 input_other->HasOnlyOneNonEnvironmentUse()) {
1000 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1001 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1002 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1003 input_other->InputAt(0),
1004 input_other->InputAt(1),
1005 input_other->GetDexPc());
1006 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1007 input_other->GetBlock()->RemoveInstruction(input_other);
1008 RecordSimplification();
1009 return;
1010 }
1011 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001012 }
1013
1014 // We assume that GVN has run before, so we only perform a pointer comparison.
1015 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001016 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001017 if (instruction->GetLeft() == instruction->GetRight()) {
1018 // Replace code looking like
1019 // AND dst, src, src
1020 // with
1021 // src
1022 instruction->ReplaceWith(instruction->GetLeft());
1023 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001024 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001025 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001026 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001027
1028 TryDeMorganNegationFactoring(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001029}
1030
Mark Mendellc4701932015-04-10 13:18:51 -04001031void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1032 VisitCondition(condition);
1033}
1034
1035void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1036 VisitCondition(condition);
1037}
1038
1039void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1040 VisitCondition(condition);
1041}
1042
1043void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1044 VisitCondition(condition);
1045}
1046
Anton Shaminbdd79352016-02-15 12:48:36 +06001047void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1048 VisitCondition(condition);
1049}
1050
1051void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1052 VisitCondition(condition);
1053}
1054
1055void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1056 VisitCondition(condition);
1057}
1058
1059void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1060 VisitCondition(condition);
1061}
Aart Bike9f37602015-10-09 11:15:55 -07001062
Mark Mendellc4701932015-04-10 13:18:51 -04001063void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001064 // Reverse condition if left is constant. Our code generators prefer constant
1065 // on the right hand side.
1066 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1067 HBasicBlock* block = condition->GetBlock();
1068 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1069 // If it is a fp we must set the opposite bias.
1070 if (replacement != nullptr) {
1071 if (condition->IsLtBias()) {
1072 replacement->SetBias(ComparisonBias::kGtBias);
1073 } else if (condition->IsGtBias()) {
1074 replacement->SetBias(ComparisonBias::kLtBias);
1075 }
1076 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1077 RecordSimplification();
1078
1079 condition = replacement;
1080 }
1081 }
Mark Mendellc4701932015-04-10 13:18:51 -04001082
Mark Mendellc4701932015-04-10 13:18:51 -04001083 HInstruction* left = condition->GetLeft();
1084 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001085
1086 // Try to fold an HCompare into this HCondition.
1087
Mark Mendellc4701932015-04-10 13:18:51 -04001088 // We can only replace an HCondition which compares a Compare to 0.
1089 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1090 // condition with a long, float or double comparison as input.
1091 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1092 // Conversion is not possible.
1093 return;
1094 }
1095
1096 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001097 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001098 // Someone else also wants the result of the compare.
1099 return;
1100 }
1101
Vladimir Marko46817b82016-03-29 12:21:58 +01001102 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001103 // There is a reference to the compare result in an environment. Do we really need it?
1104 if (GetGraph()->IsDebuggable()) {
1105 return;
1106 }
1107
1108 // We have to ensure that there are no deopt points in the sequence.
1109 if (left->HasAnyEnvironmentUseBefore(condition)) {
1110 return;
1111 }
1112 }
1113
1114 // Clean up any environment uses from the HCompare, if any.
1115 left->RemoveEnvironmentUsers();
1116
1117 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1118 condition->SetBias(left->AsCompare()->GetBias());
1119
1120 // Replace the operands of the HCondition.
1121 condition->ReplaceInput(left->InputAt(0), 0);
1122 condition->ReplaceInput(left->InputAt(1), 1);
1123
1124 // Remove the HCompare.
1125 left->GetBlock()->RemoveInstruction(left);
1126
1127 RecordSimplification();
1128}
1129
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001130void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1131 HConstant* input_cst = instruction->GetConstantRight();
1132 HInstruction* input_other = instruction->GetLeastConstantLeft();
1133 Primitive::Type type = instruction->GetType();
1134
1135 if ((input_cst != nullptr) && input_cst->IsOne()) {
1136 // Replace code looking like
1137 // DIV dst, src, 1
1138 // with
1139 // src
1140 instruction->ReplaceWith(input_other);
1141 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001142 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001143 return;
1144 }
1145
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001146 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001147 // Replace code looking like
1148 // DIV dst, src, -1
1149 // with
1150 // NEG dst, src
1151 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001152 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001153 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001154 return;
1155 }
1156
1157 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1158 // Try replacing code looking like
1159 // DIV dst, src, constant
1160 // with
1161 // MUL dst, src, 1 / constant
1162 HConstant* reciprocal = nullptr;
1163 if (type == Primitive::Primitive::kPrimDouble) {
1164 double value = input_cst->AsDoubleConstant()->GetValue();
1165 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1166 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1167 }
1168 } else {
1169 DCHECK_EQ(type, Primitive::kPrimFloat);
1170 float value = input_cst->AsFloatConstant()->GetValue();
1171 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1172 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1173 }
1174 }
1175
1176 if (reciprocal != nullptr) {
1177 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1178 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1179 RecordSimplification();
1180 return;
1181 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001182 }
1183}
1184
1185void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1186 HConstant* input_cst = instruction->GetConstantRight();
1187 HInstruction* input_other = instruction->GetLeastConstantLeft();
1188 Primitive::Type type = instruction->GetType();
1189 HBasicBlock* block = instruction->GetBlock();
1190 ArenaAllocator* allocator = GetGraph()->GetArena();
1191
1192 if (input_cst == nullptr) {
1193 return;
1194 }
1195
1196 if (input_cst->IsOne()) {
1197 // Replace code looking like
1198 // MUL dst, src, 1
1199 // with
1200 // src
1201 instruction->ReplaceWith(input_other);
1202 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001203 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001204 return;
1205 }
1206
1207 if (input_cst->IsMinusOne() &&
1208 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1209 // Replace code looking like
1210 // MUL dst, src, -1
1211 // with
1212 // NEG dst, src
1213 HNeg* neg = new (allocator) HNeg(type, input_other);
1214 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001215 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001216 return;
1217 }
1218
1219 if (Primitive::IsFloatingPointType(type) &&
1220 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1221 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1222 // Replace code looking like
1223 // FP_MUL dst, src, 2.0
1224 // with
1225 // FP_ADD dst, src, src
1226 // The 'int' and 'long' cases are handled below.
1227 block->ReplaceAndRemoveInstructionWith(instruction,
1228 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001229 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001230 return;
1231 }
1232
1233 if (Primitive::IsIntOrLongType(type)) {
1234 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001235 // Even though constant propagation also takes care of the zero case, other
1236 // optimizations can lead to having a zero multiplication.
1237 if (factor == 0) {
1238 // Replace code looking like
1239 // MUL dst, src, 0
1240 // with
1241 // 0
1242 instruction->ReplaceWith(input_cst);
1243 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001244 RecordSimplification();
Serguei Katkov53849192015-04-20 14:22:27 +06001245 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001246 // Replace code looking like
1247 // MUL dst, src, pow_of_2
1248 // with
1249 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001250 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001251 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001252 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001253 RecordSimplification();
Alexandre Rames38db7852015-11-20 15:02:45 +00001254 } else if (IsPowerOfTwo(factor - 1)) {
1255 // Transform code looking like
1256 // MUL dst, src, (2^n + 1)
1257 // into
1258 // SHL tmp, src, n
1259 // ADD dst, src, tmp
1260 HShl* shl = new (allocator) HShl(type,
1261 input_other,
1262 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1263 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1264
1265 block->InsertInstructionBefore(shl, instruction);
1266 block->ReplaceAndRemoveInstructionWith(instruction, add);
1267 RecordSimplification();
1268 } else if (IsPowerOfTwo(factor + 1)) {
1269 // Transform code looking like
1270 // MUL dst, src, (2^n - 1)
1271 // into
1272 // SHL tmp, src, n
1273 // SUB dst, tmp, src
1274 HShl* shl = new (allocator) HShl(type,
1275 input_other,
1276 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1277 HSub* sub = new (allocator) HSub(type, shl, input_other);
1278
1279 block->InsertInstructionBefore(shl, instruction);
1280 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1281 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001282 }
1283 }
1284}
1285
Alexandre Rames188d4312015-04-09 18:30:21 +01001286void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1287 HInstruction* input = instruction->GetInput();
1288 if (input->IsNeg()) {
1289 // Replace code looking like
1290 // NEG tmp, src
1291 // NEG dst, tmp
1292 // with
1293 // src
1294 HNeg* previous_neg = input->AsNeg();
1295 instruction->ReplaceWith(previous_neg->GetInput());
1296 instruction->GetBlock()->RemoveInstruction(instruction);
1297 // We perform the optimization even if the input negation has environment
1298 // uses since it allows removing the current instruction. But we only delete
1299 // the input negation only if it is does not have any uses left.
1300 if (!previous_neg->HasUses()) {
1301 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1302 }
1303 RecordSimplification();
1304 return;
1305 }
1306
Serguei Katkov339dfc22015-04-20 12:29:32 +06001307 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1308 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001309 // Replace code looking like
1310 // SUB tmp, a, b
1311 // NEG dst, tmp
1312 // with
1313 // SUB dst, b, a
1314 // We do not perform the optimization if the input subtraction has
1315 // environment uses or multiple non-environment uses as it could lead to
1316 // worse code. In particular, we do not want the live ranges of `a` and `b`
1317 // to be extended if we are not sure the initial 'SUB' instruction can be
1318 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001319 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001320 HSub* sub = input->AsSub();
1321 HSub* new_sub =
1322 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1323 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1324 if (!sub->HasUses()) {
1325 sub->GetBlock()->RemoveInstruction(sub);
1326 }
1327 RecordSimplification();
1328 }
1329}
1330
1331void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1332 HInstruction* input = instruction->GetInput();
1333 if (input->IsNot()) {
1334 // Replace code looking like
1335 // NOT tmp, src
1336 // NOT dst, tmp
1337 // with
1338 // src
1339 // We perform the optimization even if the input negation has environment
1340 // uses since it allows removing the current instruction. But we only delete
1341 // the input negation only if it is does not have any uses left.
1342 HNot* previous_not = input->AsNot();
1343 instruction->ReplaceWith(previous_not->GetInput());
1344 instruction->GetBlock()->RemoveInstruction(instruction);
1345 if (!previous_not->HasUses()) {
1346 previous_not->GetBlock()->RemoveInstruction(previous_not);
1347 }
1348 RecordSimplification();
1349 }
1350}
1351
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001352void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1353 HConstant* input_cst = instruction->GetConstantRight();
1354 HInstruction* input_other = instruction->GetLeastConstantLeft();
1355
Roland Levillain1a653882016-03-18 18:05:57 +00001356 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001357 // Replace code looking like
1358 // OR dst, src, 0
1359 // with
1360 // src
1361 instruction->ReplaceWith(input_other);
1362 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001363 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001364 return;
1365 }
1366
1367 // We assume that GVN has run before, so we only perform a pointer comparison.
1368 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001369 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001370 if (instruction->GetLeft() == instruction->GetRight()) {
1371 // Replace code looking like
1372 // OR dst, src, src
1373 // with
1374 // src
1375 instruction->ReplaceWith(instruction->GetLeft());
1376 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001377 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001378 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001379 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001380
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001381 if (TryDeMorganNegationFactoring(instruction)) return;
1382
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001383 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001384}
1385
1386void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1387 VisitShift(instruction);
1388}
1389
1390void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1391 VisitShift(instruction);
1392}
1393
1394void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1395 HConstant* input_cst = instruction->GetConstantRight();
1396 HInstruction* input_other = instruction->GetLeastConstantLeft();
1397
Serguei Katkov115b53f2015-08-05 17:03:30 +06001398 Primitive::Type type = instruction->GetType();
1399 if (Primitive::IsFloatingPointType(type)) {
1400 return;
1401 }
1402
Roland Levillain1a653882016-03-18 18:05:57 +00001403 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001404 // Replace code looking like
1405 // SUB dst, src, 0
1406 // with
1407 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001408 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1409 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1410 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001411 instruction->ReplaceWith(input_other);
1412 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001413 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001414 return;
1415 }
1416
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001417 HBasicBlock* block = instruction->GetBlock();
1418 ArenaAllocator* allocator = GetGraph()->GetArena();
1419
Alexandre Rames188d4312015-04-09 18:30:21 +01001420 HInstruction* left = instruction->GetLeft();
1421 HInstruction* right = instruction->GetRight();
1422 if (left->IsConstant()) {
1423 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001424 // Replace code looking like
1425 // SUB dst, 0, src
1426 // with
1427 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001428 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001429 // `x` is `0.0`, the former expression yields `0.0`, while the later
1430 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001431 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001432 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001433 RecordSimplification();
1434 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001435 }
1436 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001437
1438 if (left->IsNeg() && right->IsNeg()) {
1439 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1440 return;
1441 }
1442 }
1443
1444 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1445 // Replace code looking like
1446 // NEG tmp, b
1447 // SUB dst, a, tmp
1448 // with
1449 // ADD dst, a, b
1450 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1451 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1452 RecordSimplification();
1453 right->GetBlock()->RemoveInstruction(right);
1454 return;
1455 }
1456
1457 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1458 // Replace code looking like
1459 // NEG tmp, a
1460 // SUB dst, tmp, b
1461 // with
1462 // ADD tmp, a, b
1463 // NEG dst, tmp
1464 // The second version is not intrinsically better, but enables more
1465 // transformations.
1466 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1467 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1468 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1469 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1470 instruction->ReplaceWith(neg);
1471 instruction->GetBlock()->RemoveInstruction(instruction);
1472 RecordSimplification();
1473 left->GetBlock()->RemoveInstruction(left);
1474 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001475}
1476
1477void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1478 VisitShift(instruction);
1479}
1480
1481void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1482 HConstant* input_cst = instruction->GetConstantRight();
1483 HInstruction* input_other = instruction->GetLeastConstantLeft();
1484
Roland Levillain1a653882016-03-18 18:05:57 +00001485 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001486 // Replace code looking like
1487 // XOR dst, src, 0
1488 // with
1489 // src
1490 instruction->ReplaceWith(input_other);
1491 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001492 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001493 return;
1494 }
1495
1496 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1497 // Replace code looking like
1498 // XOR dst, src, 0xFFF...FF
1499 // with
1500 // NOT dst, src
1501 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1502 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001503 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001504 return;
1505 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001506
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001507 HInstruction* left = instruction->GetLeft();
1508 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001509 if (((left->IsNot() && right->IsNot()) ||
1510 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001511 left->HasOnlyOneNonEnvironmentUse() &&
1512 right->HasOnlyOneNonEnvironmentUse()) {
1513 // Replace code looking like
1514 // NOT nota, a
1515 // NOT notb, b
1516 // XOR dst, nota, notb
1517 // with
1518 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001519 instruction->ReplaceInput(left->InputAt(0), 0);
1520 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001521 left->GetBlock()->RemoveInstruction(left);
1522 right->GetBlock()->RemoveInstruction(right);
1523 RecordSimplification();
1524 return;
1525 }
1526
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001527 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001528}
1529
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001530void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1531 HInstruction* argument = instruction->InputAt(1);
1532 HInstruction* receiver = instruction->InputAt(0);
1533 if (receiver == argument) {
1534 // Because String.equals is an instance call, the receiver is
1535 // a null check if we don't know it's null. The argument however, will
1536 // be the actual object. So we cannot end up in a situation where both
1537 // are equal but could be null.
1538 DCHECK(CanEnsureNotNullAt(argument, instruction));
1539 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1540 instruction->GetBlock()->RemoveInstruction(instruction);
1541 } else {
1542 StringEqualsOptimizations optimizations(instruction);
1543 if (CanEnsureNotNullAt(argument, instruction)) {
1544 optimizations.SetArgumentNotNull();
1545 }
1546 ScopedObjectAccess soa(Thread::Current());
1547 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1548 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1549 optimizations.SetArgumentIsString();
1550 }
1551 }
1552}
1553
Roland Levillain22c49222016-03-18 14:04:28 +00001554void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1555 bool is_left,
1556 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001557 DCHECK(invoke->IsInvokeStaticOrDirect());
1558 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001559 HInstruction* value = invoke->InputAt(0);
1560 HInstruction* distance = invoke->InputAt(1);
1561 // Replace the invoke with an HRor.
1562 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001563 // Unconditionally set the type of the negated distance to `int`,
1564 // as shift and rotate operations expect a 32-bit (or narrower)
1565 // value for their distance input.
1566 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001567 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1568 }
Roland Levillain22c49222016-03-18 14:04:28 +00001569 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001570 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1571 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001572 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001573 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1574 clinit->GetBlock()->RemoveInstruction(clinit);
1575 HInstruction* ldclass = clinit->InputAt(0);
1576 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1577 ldclass->GetBlock()->RemoveInstruction(ldclass);
1578 }
1579 }
1580}
1581
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001582static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1583 if (potential_length->IsArrayLength()) {
1584 return potential_length->InputAt(0) == potential_array;
1585 }
1586
1587 if (potential_array->IsNewArray()) {
1588 return potential_array->InputAt(0) == potential_length;
1589 }
1590
1591 return false;
1592}
1593
1594void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1595 HInstruction* source = instruction->InputAt(0);
1596 HInstruction* destination = instruction->InputAt(2);
1597 HInstruction* count = instruction->InputAt(4);
1598 SystemArrayCopyOptimizations optimizations(instruction);
1599 if (CanEnsureNotNullAt(source, instruction)) {
1600 optimizations.SetSourceIsNotNull();
1601 }
1602 if (CanEnsureNotNullAt(destination, instruction)) {
1603 optimizations.SetDestinationIsNotNull();
1604 }
1605 if (destination == source) {
1606 optimizations.SetDestinationIsSource();
1607 }
1608
1609 if (IsArrayLengthOf(count, source)) {
1610 optimizations.SetCountIsSourceLength();
1611 }
1612
1613 if (IsArrayLengthOf(count, destination)) {
1614 optimizations.SetCountIsDestinationLength();
1615 }
1616
1617 {
1618 ScopedObjectAccess soa(Thread::Current());
1619 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1620 if (destination_rti.IsValid()) {
1621 if (destination_rti.IsObjectArray()) {
1622 if (destination_rti.IsExact()) {
1623 optimizations.SetDoesNotNeedTypeCheck();
1624 }
1625 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001626 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001627 if (destination_rti.IsPrimitiveArrayClass()) {
1628 optimizations.SetDestinationIsPrimitiveArray();
1629 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1630 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001631 }
1632 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001633 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1634 if (source_rti.IsValid()) {
1635 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1636 optimizations.SetDoesNotNeedTypeCheck();
1637 }
1638 if (source_rti.IsPrimitiveArrayClass()) {
1639 optimizations.SetSourceIsPrimitiveArray();
1640 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1641 optimizations.SetSourceIsNonPrimitiveArray();
1642 }
1643 }
1644 }
1645}
1646
Roland Levillaina5c4a402016-03-15 15:02:50 +00001647void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1648 bool is_signum,
1649 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001650 DCHECK(invoke->IsInvokeStaticOrDirect());
1651 uint32_t dex_pc = invoke->GetDexPc();
1652 HInstruction* left = invoke->InputAt(0);
1653 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001654 if (!is_signum) {
1655 right = invoke->InputAt(1);
1656 } else if (type == Primitive::kPrimLong) {
1657 right = GetGraph()->GetLongConstant(0);
1658 } else {
1659 right = GetGraph()->GetIntConstant(0);
1660 }
1661 HCompare* compare = new (GetGraph()->GetArena())
1662 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1663 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1664}
1665
Aart Bik75a38b22016-02-17 10:41:50 -08001666void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1667 DCHECK(invoke->IsInvokeStaticOrDirect());
1668 uint32_t dex_pc = invoke->GetDexPc();
1669 // IsNaN(x) is the same as x != x.
1670 HInstruction* x = invoke->InputAt(0);
1671 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001672 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001673 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1674}
1675
Aart Bik2a6aad92016-02-25 11:32:32 -08001676void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1677 DCHECK(invoke->IsInvokeStaticOrDirect());
1678 uint32_t dex_pc = invoke->GetDexPc();
1679 HInstruction* x = invoke->InputAt(0);
1680 Primitive::Type type = x->GetType();
1681 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1682 HInstruction* nan;
1683 if (type == Primitive::kPrimDouble) {
1684 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1685 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1686 kNeedsEnvironmentOrCache,
1687 kNoSideEffects,
1688 kNoThrow);
1689 } else {
1690 DCHECK_EQ(type, Primitive::kPrimFloat);
1691 nan = GetGraph()->GetIntConstant(0x7fc00000);
1692 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1693 kNeedsEnvironmentOrCache,
1694 kNoSideEffects,
1695 kNoThrow);
1696 }
1697 // Test IsNaN(x), which is the same as x != x.
1698 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1699 condition->SetBias(ComparisonBias::kLtBias);
1700 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1701 // Select between the two.
1702 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1703 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1704 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1705}
1706
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001707void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1708 HInstruction* str = invoke->InputAt(0);
1709 HInstruction* index = invoke->InputAt(1);
1710 uint32_t dex_pc = invoke->GetDexPc();
1711 ArenaAllocator* arena = GetGraph()->GetArena();
1712 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1713 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1714 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1715 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1716 HBoundsCheck* bounds_check =
1717 new (arena) HBoundsCheck(index, length, dex_pc, invoke->GetDexMethodIndex());
1718 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
1719 HArrayGet* array_get =
1720 new (arena) HArrayGet(str, index, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
1721 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1722 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1723 GetGraph()->SetHasBoundsChecks(true);
1724}
1725
Vladimir Markodce016e2016-04-28 13:10:02 +01001726void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1727 HInstruction* str = invoke->InputAt(0);
1728 uint32_t dex_pc = invoke->GetDexPc();
1729 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1730 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001731 HArrayLength* length =
1732 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001733 HInstruction* replacement;
1734 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1735 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1736 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1737 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1738 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1739 replacement = equal;
1740 } else {
1741 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1742 replacement = length;
1743 }
1744 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
1745}
1746
Aart Bik11932592016-03-08 12:42:25 -08001747void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1748 uint32_t dex_pc = invoke->GetDexPc();
1749 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1750 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1751}
1752
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001753void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001754 switch (instruction->GetIntrinsic()) {
1755 case Intrinsics::kStringEquals:
1756 SimplifyStringEquals(instruction);
1757 break;
1758 case Intrinsics::kSystemArrayCopy:
1759 SimplifySystemArrayCopy(instruction);
1760 break;
1761 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001762 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1763 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001764 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001765 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001766 break;
1767 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001768 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1769 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001770 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001771 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001772 break;
1773 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001774 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1775 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001776 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001777 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001778 break;
1779 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001780 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1781 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001782 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001783 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001784 break;
1785 case Intrinsics::kFloatIsNaN:
1786 case Intrinsics::kDoubleIsNaN:
1787 SimplifyIsNaN(instruction);
1788 break;
1789 case Intrinsics::kFloatFloatToIntBits:
1790 case Intrinsics::kDoubleDoubleToLongBits:
1791 SimplifyFP2Int(instruction);
1792 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001793 case Intrinsics::kStringCharAt:
1794 SimplifyStringCharAt(instruction);
1795 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01001796 case Intrinsics::kStringIsEmpty:
1797 case Intrinsics::kStringLength:
1798 SimplifyStringIsEmptyOrLength(instruction);
1799 break;
Aart Bik11932592016-03-08 12:42:25 -08001800 case Intrinsics::kUnsafeLoadFence:
1801 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1802 break;
1803 case Intrinsics::kUnsafeStoreFence:
1804 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1805 break;
1806 case Intrinsics::kUnsafeFullFence:
1807 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1808 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001809 default:
1810 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001811 }
1812}
1813
Aart Bikbb245d12015-10-19 11:05:03 -07001814void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1815 HInstruction* cond = deoptimize->InputAt(0);
1816 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001817 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001818 // Never deopt: instruction can be removed.
1819 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1820 } else {
1821 // Always deopt.
1822 }
1823 }
1824}
1825
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001826} // namespace art