blob: 2cedde900e2b3b9959152dab9269529b77d6dc04 [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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080019#include "art_method-inl.h"
20#include "class_linker-inl.h"
Aart Bik71bf7b42016-11-16 10:17:46 -080021#include "escape.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010022#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "mirror/class-inl.h"
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000024#include "sharpening.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026
Nicolas Geoffray3c049742014-09-24 18:10:46 +010027namespace art {
28
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010029class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000030 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000031 InstructionSimplifierVisitor(HGraph* graph,
32 CodeGenerator* codegen,
33 OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010034 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000035 codegen_(codegen),
Alexandre Rames188d4312015-04-09 18:30:21 +010036 stats_(stats) {}
37
38 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000039
40 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010041 void RecordSimplification() {
42 simplification_occurred_ = true;
43 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000044 MaybeRecordStat(kInstructionSimplifications);
45 }
46
47 void MaybeRecordStat(MethodCompilationStat stat) {
48 if (stats_ != nullptr) {
49 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010050 }
51 }
52
Scott Wakeling40a04bf2015-12-11 09:50:36 +000053 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
54 bool TryReplaceWithRotate(HBinaryOperation* instruction);
55 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
56 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
57 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
58
Alexandre Rames188d4312015-04-09 18:30:21 +010059 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000060 // `op` should be either HOr or HAnd.
61 // De Morgan's laws:
62 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
63 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010064 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
65 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
66
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000067 void VisitShift(HBinaryOperation* shift);
68
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000069 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010070 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
71 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010072 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
73 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000074 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000075 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000076 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080077 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000078 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000079 void VisitAdd(HAdd* instruction) OVERRIDE;
80 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040081 void VisitCondition(HCondition* instruction) OVERRIDE;
82 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
83 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
84 void VisitLessThan(HLessThan* condition) OVERRIDE;
85 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060086 void VisitBelow(HBelow* condition) OVERRIDE;
87 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
88 void VisitAbove(HAbove* condition) OVERRIDE;
89 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000090 void VisitDiv(HDiv* instruction) OVERRIDE;
91 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010092 void VisitNeg(HNeg* instruction) OVERRIDE;
93 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000094 void VisitOr(HOr* instruction) OVERRIDE;
95 void VisitShl(HShl* instruction) OVERRIDE;
96 void VisitShr(HShr* instruction) OVERRIDE;
97 void VisitSub(HSub* instruction) OVERRIDE;
98 void VisitUShr(HUShr* instruction) OVERRIDE;
99 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +0000100 void VisitSelect(HSelect* select) OVERRIDE;
101 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100102 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100103 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -0700104 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100105
106 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000107
Roland Levillain22c49222016-03-18 14:04:28 +0000108 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100109 void SimplifySystemArrayCopy(HInvoke* invoke);
110 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000111 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800112 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800113 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100114 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100115 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800116 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800117 void SimplifyReturnThis(HInvoke* invoke);
118 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800119 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100120
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000121 CodeGenerator* codegen_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000122 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100123 bool simplification_occurred_ = false;
124 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700125 // We ensure we do not loop infinitely. The value should not be too high, since that
126 // would allow looping around the same basic block too many times. The value should
127 // not be too low either, however, since we want to allow revisiting a basic block
128 // with many statements and simplifications at least once.
129 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000130};
131
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132void InstructionSimplifier::Run() {
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000133 InstructionSimplifierVisitor visitor(graph_, codegen_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100134 visitor.Run();
135}
136
137void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100138 // Iterate in reverse post order to open up more simplifications to users
139 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100140 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100141 // The simplification of an instruction to another instruction may yield
142 // possibilities for other simplifications. So although we perform a reverse
143 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100144 do {
145 simplification_occurred_ = false;
146 VisitBasicBlock(block);
147 } while (simplification_occurred_ &&
148 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100149 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100150 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151}
152
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000153namespace {
154
155bool AreAllBitsSet(HConstant* constant) {
156 return Int64FromConstant(constant) == -1;
157}
158
159} // namespace
160
Alexandre Rames188d4312015-04-09 18:30:21 +0100161// Returns true if the code was simplified to use only one negation operation
162// after the binary operation instead of one on each of the inputs.
163bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
164 DCHECK(binop->IsAdd() || binop->IsSub());
165 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
166 HNeg* left_neg = binop->GetLeft()->AsNeg();
167 HNeg* right_neg = binop->GetRight()->AsNeg();
168 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
169 !right_neg->HasOnlyOneNonEnvironmentUse()) {
170 return false;
171 }
172 // Replace code looking like
173 // NEG tmp1, a
174 // NEG tmp2, b
175 // ADD dst, tmp1, tmp2
176 // with
177 // ADD tmp, a, b
178 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600179 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
180 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
181 // while the later yields `-0.0`.
182 if (!Primitive::IsIntegralType(binop->GetType())) {
183 return false;
184 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100185 binop->ReplaceInput(left_neg->GetInput(), 0);
186 binop->ReplaceInput(right_neg->GetInput(), 1);
187 left_neg->GetBlock()->RemoveInstruction(left_neg);
188 right_neg->GetBlock()->RemoveInstruction(right_neg);
189 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
190 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
191 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
192 RecordSimplification();
193 return true;
194}
195
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000196bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
197 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
198 Primitive::Type type = op->GetType();
199 HInstruction* left = op->GetLeft();
200 HInstruction* right = op->GetRight();
201
202 // We can apply De Morgan's laws if both inputs are Not's and are only used
203 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000204 if (((left->IsNot() && right->IsNot()) ||
205 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000206 left->HasOnlyOneNonEnvironmentUse() &&
207 right->HasOnlyOneNonEnvironmentUse()) {
208 // Replace code looking like
209 // NOT nota, a
210 // NOT notb, b
211 // AND dst, nota, notb (respectively OR)
212 // with
213 // OR or, a, b (respectively AND)
214 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000215 HInstruction* src_left = left->InputAt(0);
216 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000217 uint32_t dex_pc = op->GetDexPc();
218
219 // Remove the negations on the inputs.
220 left->ReplaceWith(src_left);
221 right->ReplaceWith(src_right);
222 left->GetBlock()->RemoveInstruction(left);
223 right->GetBlock()->RemoveInstruction(right);
224
225 // Replace the `HAnd` or `HOr`.
226 HBinaryOperation* hbin;
227 if (op->IsAnd()) {
228 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
229 } else {
230 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
231 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000232 HInstruction* hnot;
233 if (left->IsBooleanNot()) {
234 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
235 } else {
236 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
237 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000238
239 op->GetBlock()->InsertInstructionBefore(hbin, op);
240 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
241
242 RecordSimplification();
243 return true;
244 }
245
246 return false;
247}
248
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000249void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
250 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100251 HInstruction* shift_amount = instruction->GetRight();
252 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000253
Alexandre Rames50518442016-06-27 11:39:19 +0100254 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
255 ? kMaxLongShiftDistance
256 : kMaxIntShiftDistance;
257
258 if (shift_amount->IsConstant()) {
259 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700260 int64_t masked_cst = cst & implicit_mask;
261 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400262 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100263 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400264 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100265 // value
266 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400267 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100268 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100269 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700270 } else if (masked_cst != cst) {
271 // Replace code looking like
272 // SHL dst, value, cst
273 // where cst exceeds maximum distance with the equivalent
274 // SHL dst, value, cst & implicit_mask
275 // (as defined by shift semantics). This ensures other
276 // optimizations do not need to special case for such situations.
277 DCHECK_EQ(shift_amount->GetType(), Primitive::kPrimInt);
278 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index */ 1);
279 RecordSimplification();
280 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100281 }
282 }
283
284 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
285 // unnecessary explicit masking operations on the shift amount.
286 // Replace code looking like
287 // AND masked_shift, shift, <superset of implicit mask>
288 // SHL dst, value, masked_shift
289 // with
290 // SHL dst, value, shift
291 if (shift_amount->IsAnd()) {
292 HAnd* and_insn = shift_amount->AsAnd();
293 HConstant* mask = and_insn->GetConstantRight();
294 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
295 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
296 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400297 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000298 }
299}
300
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000301static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
302 return (sub->GetRight() == other &&
303 sub->GetLeft()->IsConstant() &&
304 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
305}
306
307bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
308 HUShr* ushr,
309 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000310 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
311 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000312 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
313 if (!ushr->HasUses()) {
314 ushr->GetBlock()->RemoveInstruction(ushr);
315 }
316 if (!ushr->GetRight()->HasUses()) {
317 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
318 }
319 if (!shl->HasUses()) {
320 shl->GetBlock()->RemoveInstruction(shl);
321 }
322 if (!shl->GetRight()->HasUses()) {
323 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
324 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100325 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000326 return true;
327}
328
329// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
330bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000331 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
332 HInstruction* left = op->GetLeft();
333 HInstruction* right = op->GetRight();
334 // If we have an UShr and a Shl (in either order).
335 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
336 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
337 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
338 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
339 if (ushr->GetType() == shl->GetType() &&
340 ushr->GetLeft() == shl->GetLeft()) {
341 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
342 // Shift distances are both constant, try replacing with Ror if they
343 // add up to the register size.
344 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
345 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
346 // Shift distances are potentially of the form x and (reg_size - x).
347 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
348 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
349 // Shift distances are potentially of the form d and -d.
350 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
351 }
352 }
353 }
354 return false;
355}
356
357// Try replacing code looking like (x >>> #rdist OP x << #ldist):
358// UShr dst, x, #rdist
359// Shl tmp, x, #ldist
360// OP dst, dst, tmp
361// or like (x >>> #rdist OP x << #-ldist):
362// UShr dst, x, #rdist
363// Shl tmp, x, #-ldist
364// OP dst, dst, tmp
365// with
366// Ror dst, x, #rdist
367bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
368 HUShr* ushr,
369 HShl* shl) {
370 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
371 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
372 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
373 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
374 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
375 ReplaceRotateWithRor(op, ushr, shl);
376 return true;
377 }
378 return false;
379}
380
381// Replace code looking like (x >>> -d OP x << d):
382// Neg neg, d
383// UShr dst, x, neg
384// Shl tmp, x, d
385// OP dst, dst, tmp
386// with
387// Neg neg, d
388// Ror dst, x, neg
389// *** OR ***
390// Replace code looking like (x >>> d OP x << -d):
391// UShr dst, x, d
392// Neg neg, d
393// Shl tmp, x, neg
394// OP dst, dst, tmp
395// with
396// Ror dst, x, d
397bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
398 HUShr* ushr,
399 HShl* shl) {
400 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
401 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
402 bool neg_is_left = shl->GetRight()->IsNeg();
403 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
404 // And the shift distance being negated is the distance being shifted the other way.
405 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
406 ReplaceRotateWithRor(op, ushr, shl);
407 }
408 return false;
409}
410
411// Try replacing code looking like (x >>> d OP x << (#bits - d)):
412// UShr dst, x, d
413// Sub ld, #bits, d
414// Shl tmp, x, ld
415// OP dst, dst, tmp
416// with
417// Ror dst, x, d
418// *** OR ***
419// Replace code looking like (x >>> (#bits - d) OP x << d):
420// Sub rd, #bits, d
421// UShr dst, x, rd
422// Shl tmp, x, d
423// OP dst, dst, tmp
424// with
425// Neg neg, d
426// Ror dst, x, neg
427bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
428 HUShr* ushr,
429 HShl* shl) {
430 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
431 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
432 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
433 HInstruction* shl_shift = shl->GetRight();
434 HInstruction* ushr_shift = ushr->GetRight();
435 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
436 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
437 return ReplaceRotateWithRor(op, ushr, shl);
438 }
439 return false;
440}
441
Calin Juravle10e244f2015-01-26 18:54:32 +0000442void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
443 HInstruction* obj = null_check->InputAt(0);
444 if (!obj->CanBeNull()) {
445 null_check->ReplaceWith(obj);
446 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000447 if (stats_ != nullptr) {
448 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
449 }
450 }
451}
452
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100453bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
454 if (!input->CanBeNull()) {
455 return true;
456 }
457
Vladimir Marko46817b82016-03-29 12:21:58 +0100458 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
459 HInstruction* user = use.GetUser();
460 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100461 return true;
462 }
463 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100464
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100465 return false;
466}
467
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100468// Returns whether doing a type test between the class of `object` against `klass` has
469// a statically known outcome. The result of the test is stored in `outcome`.
470static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000471 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
472 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
473 ScopedObjectAccess soa(Thread::Current());
474 if (!obj_rti.IsValid()) {
475 // We run the simplifier before the reference type propagation so type info might not be
476 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100477 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000478 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100479
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100480 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100481 if (!class_rti.IsValid()) {
482 // Happens when the loaded class is unresolved.
483 return false;
484 }
485 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000486 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100487 *outcome = true;
488 return true;
489 } else if (obj_rti.IsExact()) {
490 // The test failed at compile time so will also fail at runtime.
491 *outcome = false;
492 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100493 } else if (!class_rti.IsInterface()
494 && !obj_rti.IsInterface()
495 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100496 // Different type hierarchy. The test will fail.
497 *outcome = false;
498 return true;
499 }
500 return false;
501}
502
503void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
504 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100505 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
506 if (load_class->NeedsAccessCheck()) {
507 // If we need to perform an access check we cannot remove the instruction.
508 return;
509 }
510
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100511 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100512 check_cast->ClearMustDoNullCheck();
513 }
514
515 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000516 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000517 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100518 return;
519 }
520
Vladimir Markoa65ed302016-03-14 21:21:29 +0000521 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
522 // the return value check with the `outcome` check, b/27651442 .
523 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700524 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100525 if (outcome) {
526 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000527 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700528 if (!load_class->HasUses()) {
529 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
530 // However, here we know that it cannot because the checkcast was successfull, hence
531 // the class was already loaded.
532 load_class->GetBlock()->RemoveInstruction(load_class);
533 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100534 } else {
535 // Don't do anything for exceptional cases for now. Ideally we should remove
536 // all instructions and blocks this instruction dominates.
537 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000538 }
539}
540
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100541void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100542 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100543 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
544 if (load_class->NeedsAccessCheck()) {
545 // If we need to perform an access check we cannot remove the instruction.
546 return;
547 }
548
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100549 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100550 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100551 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100552 instruction->ClearMustDoNullCheck();
553 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100554
555 HGraph* graph = GetGraph();
556 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000557 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100558 instruction->ReplaceWith(graph->GetIntConstant(0));
559 instruction->GetBlock()->RemoveInstruction(instruction);
560 RecordSimplification();
561 return;
562 }
563
Vladimir Marko24bd8952016-03-15 10:40:33 +0000564 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
565 // the return value check with the `outcome` check, b/27651442 .
566 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700567 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000568 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100569 if (outcome && can_be_null) {
570 // Type test will succeed, we just need a null test.
571 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
572 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
573 instruction->ReplaceWith(test);
574 } else {
575 // We've statically determined the result of the instanceof.
576 instruction->ReplaceWith(graph->GetIntConstant(outcome));
577 }
578 RecordSimplification();
579 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700580 if (outcome && !load_class->HasUses()) {
581 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
582 // However, here we know that it cannot because the instanceof check was successfull, hence
583 // the class was already loaded.
584 load_class->GetBlock()->RemoveInstruction(load_class);
585 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100586 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100587}
588
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100589void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
590 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100591 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100592 instruction->ClearValueCanBeNull();
593 }
594}
595
596void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
597 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100598 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100599 instruction->ClearValueCanBeNull();
600 }
601}
602
Anton Shaminbdd79352016-02-15 12:48:36 +0600603static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
604 HInstruction *lhs = cond->InputAt(0);
605 HInstruction *rhs = cond->InputAt(1);
606 switch (cond->GetKind()) {
607 case HInstruction::kEqual:
608 return new (arena) HEqual(rhs, lhs);
609 case HInstruction::kNotEqual:
610 return new (arena) HNotEqual(rhs, lhs);
611 case HInstruction::kLessThan:
612 return new (arena) HGreaterThan(rhs, lhs);
613 case HInstruction::kLessThanOrEqual:
614 return new (arena) HGreaterThanOrEqual(rhs, lhs);
615 case HInstruction::kGreaterThan:
616 return new (arena) HLessThan(rhs, lhs);
617 case HInstruction::kGreaterThanOrEqual:
618 return new (arena) HLessThanOrEqual(rhs, lhs);
619 case HInstruction::kBelow:
620 return new (arena) HAbove(rhs, lhs);
621 case HInstruction::kBelowOrEqual:
622 return new (arena) HAboveOrEqual(rhs, lhs);
623 case HInstruction::kAbove:
624 return new (arena) HBelow(rhs, lhs);
625 case HInstruction::kAboveOrEqual:
626 return new (arena) HBelowOrEqual(rhs, lhs);
627 default:
628 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
629 }
630 return nullptr;
631}
632
Aart Bik2767f4b2016-10-28 15:03:53 -0700633static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
634 if (input->GetType() == Primitive::kPrimBoolean) {
635 return true; // input has direct boolean type
636 } else if (cmp->GetUses().HasExactlyOneElement()) {
637 // Comparison also has boolean type if both its input and the instruction
638 // itself feed into the same phi node.
639 HInstruction* user = cmp->GetUses().front().GetUser();
640 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
641 }
642 return false;
643}
644
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000645void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100646 HInstruction* input_const = equal->GetConstantRight();
647 if (input_const != nullptr) {
648 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700649 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100650 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100651 // We are comparing the boolean to a constant which is of type int and can
652 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000653 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100654 // Replace (bool_value == true) with bool_value
655 equal->ReplaceWith(input_value);
656 block->RemoveInstruction(equal);
657 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000658 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700659 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500660 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
661 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100662 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100663 } else {
664 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
665 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
666 block->RemoveInstruction(equal);
667 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100668 }
Mark Mendellc4701932015-04-10 13:18:51 -0400669 } else {
670 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100671 }
Mark Mendellc4701932015-04-10 13:18:51 -0400672 } else {
673 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100674 }
675}
676
David Brazdil0d13fee2015-04-17 14:52:19 +0100677void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
678 HInstruction* input_const = not_equal->GetConstantRight();
679 if (input_const != nullptr) {
680 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700681 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100682 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100683 // We are comparing the boolean to a constant which is of type int and can
684 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000685 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700686 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500687 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
688 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100689 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000690 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100691 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100692 not_equal->ReplaceWith(input_value);
693 block->RemoveInstruction(not_equal);
694 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100695 } else {
696 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
697 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
698 block->RemoveInstruction(not_equal);
699 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100700 }
Mark Mendellc4701932015-04-10 13:18:51 -0400701 } else {
702 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100703 }
Mark Mendellc4701932015-04-10 13:18:51 -0400704 } else {
705 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100706 }
707}
708
709void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000710 HInstruction* input = bool_not->InputAt(0);
711 HInstruction* replace_with = nullptr;
712
713 if (input->IsIntConstant()) {
714 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000715 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000716 replace_with = GetGraph()->GetIntConstant(0);
717 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000718 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000719 replace_with = GetGraph()->GetIntConstant(1);
720 }
721 } else if (input->IsBooleanNot()) {
722 // Replace (!(!bool_value)) with bool_value.
723 replace_with = input->InputAt(0);
724 } else if (input->IsCondition() &&
725 // Don't change FP compares. The definition of compares involving
726 // NaNs forces the compares to be done as written by the user.
727 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
728 // Replace condition with its opposite.
729 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
730 }
731
732 if (replace_with != nullptr) {
733 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100734 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000735 RecordSimplification();
736 }
737}
738
739void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
740 HInstruction* replace_with = nullptr;
741 HInstruction* condition = select->GetCondition();
742 HInstruction* true_value = select->GetTrueValue();
743 HInstruction* false_value = select->GetFalseValue();
744
745 if (condition->IsBooleanNot()) {
746 // Change ((!cond) ? x : y) to (cond ? y : x).
747 condition = condition->InputAt(0);
748 std::swap(true_value, false_value);
749 select->ReplaceInput(false_value, 0);
750 select->ReplaceInput(true_value, 1);
751 select->ReplaceInput(condition, 2);
752 RecordSimplification();
753 }
754
755 if (true_value == false_value) {
756 // Replace (cond ? x : x) with (x).
757 replace_with = true_value;
758 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000759 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000760 // Replace (true ? x : y) with (x).
761 replace_with = true_value;
762 } else {
763 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000764 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000765 replace_with = false_value;
766 }
767 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000768 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000769 // Replace (cond ? true : false) with (cond).
770 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000771 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000772 // Replace (cond ? false : true) with (!cond).
773 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
774 }
775 }
776
777 if (replace_with != nullptr) {
778 select->ReplaceWith(replace_with);
779 select->GetBlock()->RemoveInstruction(select);
780 RecordSimplification();
781 }
782}
783
784void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
785 HInstruction* condition = instruction->InputAt(0);
786 if (condition->IsBooleanNot()) {
787 // Swap successors if input is negated.
788 instruction->ReplaceInput(condition->InputAt(0), 0);
789 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100790 RecordSimplification();
791 }
792}
793
Mingyao Yang0304e182015-01-30 16:41:29 -0800794void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
795 HInstruction* input = instruction->InputAt(0);
796 // If the array is a NewArray with constant size, replace the array length
797 // with the constant instruction. This helps the bounds check elimination phase.
798 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000799 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -0800800 if (input->IsIntConstant()) {
801 instruction->ReplaceWith(input);
802 }
803 }
804}
805
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000806void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000807 HInstruction* value = instruction->GetValue();
808 if (value->GetType() != Primitive::kPrimNot) return;
809
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100810 if (CanEnsureNotNullAt(value, instruction)) {
811 instruction->ClearValueCanBeNull();
812 }
813
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000814 if (value->IsArrayGet()) {
815 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
816 // If the code is just swapping elements in the array, no need for a type check.
817 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100818 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000819 }
820 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100821
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100822 if (value->IsNullConstant()) {
823 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100824 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100825 }
826
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100827 ScopedObjectAccess soa(Thread::Current());
828 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
829 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
830 if (!array_rti.IsValid()) {
831 return;
832 }
833
834 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
835 instruction->ClearNeedsTypeCheck();
836 return;
837 }
838
839 if (array_rti.IsObjectArray()) {
840 if (array_rti.IsExact()) {
841 instruction->ClearNeedsTypeCheck();
842 return;
843 }
844 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100845 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000846}
847
Vladimir Markob52bbde2016-02-12 12:06:05 +0000848static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100849 // Invariant: We should never generate a conversion to a Boolean value.
850 DCHECK_NE(Primitive::kPrimBoolean, result_type);
851
Vladimir Markob52bbde2016-02-12 12:06:05 +0000852 // Besides conversion to the same type, widening integral conversions are implicit,
853 // excluding conversions to long and the byte->char conversion where we need to
854 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
855 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100856 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
857 input_type == Primitive::kPrimByte ||
858 input_type == Primitive::kPrimShort ||
859 input_type == Primitive::kPrimChar)) ||
860 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
861 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
862 input_type == Primitive::kPrimByte)) ||
863 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000864}
865
866static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
867 // The conversion to a larger type is loss-less with the exception of two cases,
868 // - conversion to char, the only unsigned type, where we may lose some bits, and
869 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
870 // For integral to FP conversions this holds because the FP mantissa is large enough.
871 DCHECK_NE(input_type, result_type);
872 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
873 result_type != Primitive::kPrimChar &&
874 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
875}
876
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000877void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000878 HInstruction* input = instruction->GetInput();
879 Primitive::Type input_type = input->GetType();
880 Primitive::Type result_type = instruction->GetResultType();
881 if (IsTypeConversionImplicit(input_type, result_type)) {
882 // Remove the implicit conversion; this includes conversion to the same type.
883 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000884 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000885 RecordSimplification();
886 return;
887 }
888
889 if (input->IsTypeConversion()) {
890 HTypeConversion* input_conversion = input->AsTypeConversion();
891 HInstruction* original_input = input_conversion->GetInput();
892 Primitive::Type original_type = original_input->GetType();
893
894 // When the first conversion is lossless, a direct conversion from the original type
895 // to the final type yields the same result, even for a lossy second conversion, for
896 // example float->double->int or int->double->float.
897 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
898
899 // For integral conversions, see if the first conversion loses only bits that the second
900 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
901 // conversion yields the same result, for example long->int->short or int->char->short.
902 bool integral_conversions_with_non_widening_second =
903 Primitive::IsIntegralType(input_type) &&
904 Primitive::IsIntegralType(original_type) &&
905 Primitive::IsIntegralType(result_type) &&
906 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
907
908 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
909 // If the merged conversion is implicit, do the simplification unconditionally.
910 if (IsTypeConversionImplicit(original_type, result_type)) {
911 instruction->ReplaceWith(original_input);
912 instruction->GetBlock()->RemoveInstruction(instruction);
913 if (!input_conversion->HasUses()) {
914 // Don't wait for DCE.
915 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
916 }
917 RecordSimplification();
918 return;
919 }
920 // Otherwise simplify only if the first conversion has no other use.
921 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
922 input_conversion->ReplaceWith(original_input);
923 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
924 RecordSimplification();
925 return;
926 }
927 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000928 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000929 DCHECK(Primitive::IsIntegralType(input_type));
930 HAnd* input_and = input->AsAnd();
931 HConstant* constant = input_and->GetConstantRight();
932 if (constant != nullptr) {
933 int64_t value = Int64FromConstant(constant);
934 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
935 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
936 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
937 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000938 HInstruction* original_input = input_and->GetLeastConstantLeft();
939 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
940 instruction->ReplaceWith(original_input);
941 instruction->GetBlock()->RemoveInstruction(instruction);
942 RecordSimplification();
943 return;
944 } else if (input->HasOnlyOneNonEnvironmentUse()) {
945 input_and->ReplaceWith(original_input);
946 input_and->GetBlock()->RemoveInstruction(input_and);
947 RecordSimplification();
948 return;
949 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000950 }
951 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000952 }
953}
954
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000955void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
956 HConstant* input_cst = instruction->GetConstantRight();
957 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600958 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +0000959 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000960 // Replace code looking like
961 // ADD dst, src, 0
962 // with
963 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600964 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
965 // `x` is `-0.0`, the former expression yields `0.0`, while the later
966 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600967 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +0600968 instruction->ReplaceWith(input_other);
969 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100970 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +0600971 return;
972 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100973 }
974
975 HInstruction* left = instruction->GetLeft();
976 HInstruction* right = instruction->GetRight();
977 bool left_is_neg = left->IsNeg();
978 bool right_is_neg = right->IsNeg();
979
980 if (left_is_neg && right_is_neg) {
981 if (TryMoveNegOnInputsAfterBinop(instruction)) {
982 return;
983 }
984 }
985
986 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
987 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
988 // Replace code looking like
989 // NEG tmp, b
990 // ADD dst, a, tmp
991 // with
992 // SUB dst, a, b
993 // We do not perform the optimization if the input negation has environment
994 // uses or multiple non-environment uses as it could lead to worse code. In
995 // particular, we do not want the live range of `b` to be extended if we are
996 // not sure the initial 'NEG' instruction can be removed.
997 HInstruction* other = left_is_neg ? right : left;
998 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
999 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1000 RecordSimplification();
1001 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001002 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001003 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001004
Anton Kirilove14dc862016-05-13 17:56:15 +01001005 if (TryReplaceWithRotate(instruction)) {
1006 return;
1007 }
1008
1009 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1010 // so no need to return.
1011 TryHandleAssociativeAndCommutativeOperation(instruction);
1012
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001013 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001014 TrySubtractionChainSimplification(instruction)) {
1015 return;
1016 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001017
1018 if (integral_type) {
1019 // Replace code patterns looking like
1020 // SUB dst1, x, y SUB dst1, x, y
1021 // ADD dst2, dst1, y ADD dst2, y, dst1
1022 // with
1023 // SUB dst1, x, y
1024 // ADD instruction is not needed in this case, we may use
1025 // one of inputs of SUB instead.
1026 if (left->IsSub() && left->InputAt(1) == right) {
1027 instruction->ReplaceWith(left->InputAt(0));
1028 RecordSimplification();
1029 instruction->GetBlock()->RemoveInstruction(instruction);
1030 return;
1031 } else if (right->IsSub() && right->InputAt(1) == left) {
1032 instruction->ReplaceWith(right->InputAt(0));
1033 RecordSimplification();
1034 instruction->GetBlock()->RemoveInstruction(instruction);
1035 return;
1036 }
1037 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001038}
1039
1040void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1041 HConstant* input_cst = instruction->GetConstantRight();
1042 HInstruction* input_other = instruction->GetLeastConstantLeft();
1043
Vladimir Marko452c1b62015-09-25 14:44:17 +01001044 if (input_cst != nullptr) {
1045 int64_t value = Int64FromConstant(input_cst);
1046 if (value == -1) {
1047 // Replace code looking like
1048 // AND dst, src, 0xFFF...FF
1049 // with
1050 // src
1051 instruction->ReplaceWith(input_other);
1052 instruction->GetBlock()->RemoveInstruction(instruction);
1053 RecordSimplification();
1054 return;
1055 }
1056 // Eliminate And from UShr+And if the And-mask contains all the bits that
1057 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1058 // precisely clears the shifted-in sign bits.
1059 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1060 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1061 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1062 size_t num_tail_bits_set = CTZ(value + 1);
1063 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1064 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1065 instruction->ReplaceWith(input_other);
1066 instruction->GetBlock()->RemoveInstruction(instruction);
1067 RecordSimplification();
1068 return;
1069 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1070 input_other->HasOnlyOneNonEnvironmentUse()) {
1071 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1072 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1073 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1074 input_other->InputAt(0),
1075 input_other->InputAt(1),
1076 input_other->GetDexPc());
1077 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1078 input_other->GetBlock()->RemoveInstruction(input_other);
1079 RecordSimplification();
1080 return;
1081 }
1082 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001083 }
1084
1085 // We assume that GVN has run before, so we only perform a pointer comparison.
1086 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001087 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001088 if (instruction->GetLeft() == instruction->GetRight()) {
1089 // Replace code looking like
1090 // AND dst, src, src
1091 // with
1092 // src
1093 instruction->ReplaceWith(instruction->GetLeft());
1094 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001095 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001096 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001097 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001098
Anton Kirilove14dc862016-05-13 17:56:15 +01001099 if (TryDeMorganNegationFactoring(instruction)) {
1100 return;
1101 }
1102
1103 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1104 // so no need to return.
1105 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001106}
1107
Mark Mendellc4701932015-04-10 13:18:51 -04001108void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1109 VisitCondition(condition);
1110}
1111
1112void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1113 VisitCondition(condition);
1114}
1115
1116void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1117 VisitCondition(condition);
1118}
1119
1120void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1121 VisitCondition(condition);
1122}
1123
Anton Shaminbdd79352016-02-15 12:48:36 +06001124void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1125 VisitCondition(condition);
1126}
1127
1128void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1129 VisitCondition(condition);
1130}
1131
1132void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1133 VisitCondition(condition);
1134}
1135
1136void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1137 VisitCondition(condition);
1138}
Aart Bike9f37602015-10-09 11:15:55 -07001139
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001140// Recognize the following pattern:
1141// obj.getClass() ==/!= Foo.class
1142// And replace it with a constant value if the type of `obj` is statically known.
1143static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1144 HInstruction* input_one = condition->InputAt(0);
1145 HInstruction* input_two = condition->InputAt(1);
1146 HLoadClass* load_class = input_one->IsLoadClass()
1147 ? input_one->AsLoadClass()
1148 : input_two->AsLoadClass();
1149 if (load_class == nullptr) {
1150 return false;
1151 }
1152
1153 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1154 if (!class_rti.IsValid()) {
1155 // Unresolved class.
1156 return false;
1157 }
1158
1159 HInstanceFieldGet* field_get = (load_class == input_one)
1160 ? input_two->AsInstanceFieldGet()
1161 : input_one->AsInstanceFieldGet();
1162 if (field_get == nullptr) {
1163 return false;
1164 }
1165
1166 HInstruction* receiver = field_get->InputAt(0);
1167 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1168 if (!receiver_type.IsExact()) {
1169 return false;
1170 }
1171
1172 {
1173 ScopedObjectAccess soa(Thread::Current());
1174 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1175 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
1176 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1177 if (field_get->GetFieldInfo().GetField() != field) {
1178 return false;
1179 }
1180
1181 // We can replace the compare.
1182 int value = 0;
1183 if (receiver_type.IsEqual(class_rti)) {
1184 value = condition->IsEqual() ? 1 : 0;
1185 } else {
1186 value = condition->IsNotEqual() ? 1 : 0;
1187 }
1188 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1189 return true;
1190 }
1191}
1192
Mark Mendellc4701932015-04-10 13:18:51 -04001193void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001194 if (condition->IsEqual() || condition->IsNotEqual()) {
1195 if (RecognizeAndSimplifyClassCheck(condition)) {
1196 return;
1197 }
1198 }
1199
Anton Shaminbdd79352016-02-15 12:48:36 +06001200 // Reverse condition if left is constant. Our code generators prefer constant
1201 // on the right hand side.
1202 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1203 HBasicBlock* block = condition->GetBlock();
1204 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1205 // If it is a fp we must set the opposite bias.
1206 if (replacement != nullptr) {
1207 if (condition->IsLtBias()) {
1208 replacement->SetBias(ComparisonBias::kGtBias);
1209 } else if (condition->IsGtBias()) {
1210 replacement->SetBias(ComparisonBias::kLtBias);
1211 }
1212 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1213 RecordSimplification();
1214
1215 condition = replacement;
1216 }
1217 }
Mark Mendellc4701932015-04-10 13:18:51 -04001218
Mark Mendellc4701932015-04-10 13:18:51 -04001219 HInstruction* left = condition->GetLeft();
1220 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001221
1222 // Try to fold an HCompare into this HCondition.
1223
Mark Mendellc4701932015-04-10 13:18:51 -04001224 // We can only replace an HCondition which compares a Compare to 0.
1225 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1226 // condition with a long, float or double comparison as input.
1227 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1228 // Conversion is not possible.
1229 return;
1230 }
1231
1232 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001233 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001234 // Someone else also wants the result of the compare.
1235 return;
1236 }
1237
Vladimir Marko46817b82016-03-29 12:21:58 +01001238 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001239 // There is a reference to the compare result in an environment. Do we really need it?
1240 if (GetGraph()->IsDebuggable()) {
1241 return;
1242 }
1243
1244 // We have to ensure that there are no deopt points in the sequence.
1245 if (left->HasAnyEnvironmentUseBefore(condition)) {
1246 return;
1247 }
1248 }
1249
1250 // Clean up any environment uses from the HCompare, if any.
1251 left->RemoveEnvironmentUsers();
1252
1253 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1254 condition->SetBias(left->AsCompare()->GetBias());
1255
1256 // Replace the operands of the HCondition.
1257 condition->ReplaceInput(left->InputAt(0), 0);
1258 condition->ReplaceInput(left->InputAt(1), 1);
1259
1260 // Remove the HCompare.
1261 left->GetBlock()->RemoveInstruction(left);
1262
1263 RecordSimplification();
1264}
1265
Andreas Gampe9186ced2016-12-12 14:28:21 -08001266// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1267static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1268 // True, if the most significant bits of divisor are 0.
1269 return ((divisor & 0x7fffff) == 0);
1270}
1271
1272// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1273static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1274 // True, if the most significant bits of divisor are 0.
1275 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1276}
1277
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001278void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1279 HConstant* input_cst = instruction->GetConstantRight();
1280 HInstruction* input_other = instruction->GetLeastConstantLeft();
1281 Primitive::Type type = instruction->GetType();
1282
1283 if ((input_cst != nullptr) && input_cst->IsOne()) {
1284 // Replace code looking like
1285 // DIV dst, src, 1
1286 // with
1287 // src
1288 instruction->ReplaceWith(input_other);
1289 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001290 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001291 return;
1292 }
1293
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001294 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001295 // Replace code looking like
1296 // DIV dst, src, -1
1297 // with
1298 // NEG dst, src
1299 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001300 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001301 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001302 return;
1303 }
1304
1305 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1306 // Try replacing code looking like
1307 // DIV dst, src, constant
1308 // with
1309 // MUL dst, src, 1 / constant
1310 HConstant* reciprocal = nullptr;
1311 if (type == Primitive::Primitive::kPrimDouble) {
1312 double value = input_cst->AsDoubleConstant()->GetValue();
1313 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1314 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1315 }
1316 } else {
1317 DCHECK_EQ(type, Primitive::kPrimFloat);
1318 float value = input_cst->AsFloatConstant()->GetValue();
1319 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1320 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1321 }
1322 }
1323
1324 if (reciprocal != nullptr) {
1325 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1326 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1327 RecordSimplification();
1328 return;
1329 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001330 }
1331}
1332
1333void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1334 HConstant* input_cst = instruction->GetConstantRight();
1335 HInstruction* input_other = instruction->GetLeastConstantLeft();
1336 Primitive::Type type = instruction->GetType();
1337 HBasicBlock* block = instruction->GetBlock();
1338 ArenaAllocator* allocator = GetGraph()->GetArena();
1339
1340 if (input_cst == nullptr) {
1341 return;
1342 }
1343
1344 if (input_cst->IsOne()) {
1345 // Replace code looking like
1346 // MUL dst, src, 1
1347 // with
1348 // src
1349 instruction->ReplaceWith(input_other);
1350 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001351 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001352 return;
1353 }
1354
1355 if (input_cst->IsMinusOne() &&
1356 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1357 // Replace code looking like
1358 // MUL dst, src, -1
1359 // with
1360 // NEG dst, src
1361 HNeg* neg = new (allocator) HNeg(type, input_other);
1362 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001363 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001364 return;
1365 }
1366
1367 if (Primitive::IsFloatingPointType(type) &&
1368 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1369 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1370 // Replace code looking like
1371 // FP_MUL dst, src, 2.0
1372 // with
1373 // FP_ADD dst, src, src
1374 // The 'int' and 'long' cases are handled below.
1375 block->ReplaceAndRemoveInstructionWith(instruction,
1376 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001377 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001378 return;
1379 }
1380
1381 if (Primitive::IsIntOrLongType(type)) {
1382 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001383 // Even though constant propagation also takes care of the zero case, other
1384 // optimizations can lead to having a zero multiplication.
1385 if (factor == 0) {
1386 // Replace code looking like
1387 // MUL dst, src, 0
1388 // with
1389 // 0
1390 instruction->ReplaceWith(input_cst);
1391 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001392 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001393 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001394 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001395 // Replace code looking like
1396 // MUL dst, src, pow_of_2
1397 // with
1398 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001399 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001400 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001401 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001402 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001403 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001404 } else if (IsPowerOfTwo(factor - 1)) {
1405 // Transform code looking like
1406 // MUL dst, src, (2^n + 1)
1407 // into
1408 // SHL tmp, src, n
1409 // ADD dst, src, tmp
1410 HShl* shl = new (allocator) HShl(type,
1411 input_other,
1412 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1413 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1414
1415 block->InsertInstructionBefore(shl, instruction);
1416 block->ReplaceAndRemoveInstructionWith(instruction, add);
1417 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001418 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001419 } else if (IsPowerOfTwo(factor + 1)) {
1420 // Transform code looking like
1421 // MUL dst, src, (2^n - 1)
1422 // into
1423 // SHL tmp, src, n
1424 // SUB dst, tmp, src
1425 HShl* shl = new (allocator) HShl(type,
1426 input_other,
1427 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1428 HSub* sub = new (allocator) HSub(type, shl, input_other);
1429
1430 block->InsertInstructionBefore(shl, instruction);
1431 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1432 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001433 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001434 }
1435 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001436
1437 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1438 // so no need to return.
1439 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001440}
1441
Alexandre Rames188d4312015-04-09 18:30:21 +01001442void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1443 HInstruction* input = instruction->GetInput();
1444 if (input->IsNeg()) {
1445 // Replace code looking like
1446 // NEG tmp, src
1447 // NEG dst, tmp
1448 // with
1449 // src
1450 HNeg* previous_neg = input->AsNeg();
1451 instruction->ReplaceWith(previous_neg->GetInput());
1452 instruction->GetBlock()->RemoveInstruction(instruction);
1453 // We perform the optimization even if the input negation has environment
1454 // uses since it allows removing the current instruction. But we only delete
1455 // the input negation only if it is does not have any uses left.
1456 if (!previous_neg->HasUses()) {
1457 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1458 }
1459 RecordSimplification();
1460 return;
1461 }
1462
Serguei Katkov339dfc22015-04-20 12:29:32 +06001463 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1464 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001465 // Replace code looking like
1466 // SUB tmp, a, b
1467 // NEG dst, tmp
1468 // with
1469 // SUB dst, b, a
1470 // We do not perform the optimization if the input subtraction has
1471 // environment uses or multiple non-environment uses as it could lead to
1472 // worse code. In particular, we do not want the live ranges of `a` and `b`
1473 // to be extended if we are not sure the initial 'SUB' instruction can be
1474 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001475 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001476 HSub* sub = input->AsSub();
1477 HSub* new_sub =
1478 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1479 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1480 if (!sub->HasUses()) {
1481 sub->GetBlock()->RemoveInstruction(sub);
1482 }
1483 RecordSimplification();
1484 }
1485}
1486
1487void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1488 HInstruction* input = instruction->GetInput();
1489 if (input->IsNot()) {
1490 // Replace code looking like
1491 // NOT tmp, src
1492 // NOT dst, tmp
1493 // with
1494 // src
1495 // We perform the optimization even if the input negation has environment
1496 // uses since it allows removing the current instruction. But we only delete
1497 // the input negation only if it is does not have any uses left.
1498 HNot* previous_not = input->AsNot();
1499 instruction->ReplaceWith(previous_not->GetInput());
1500 instruction->GetBlock()->RemoveInstruction(instruction);
1501 if (!previous_not->HasUses()) {
1502 previous_not->GetBlock()->RemoveInstruction(previous_not);
1503 }
1504 RecordSimplification();
1505 }
1506}
1507
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001508void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1509 HConstant* input_cst = instruction->GetConstantRight();
1510 HInstruction* input_other = instruction->GetLeastConstantLeft();
1511
Roland Levillain1a653882016-03-18 18:05:57 +00001512 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001513 // Replace code looking like
1514 // OR dst, src, 0
1515 // with
1516 // src
1517 instruction->ReplaceWith(input_other);
1518 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001519 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001520 return;
1521 }
1522
1523 // We assume that GVN has run before, so we only perform a pointer comparison.
1524 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001525 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001526 if (instruction->GetLeft() == instruction->GetRight()) {
1527 // Replace code looking like
1528 // OR dst, src, src
1529 // with
1530 // src
1531 instruction->ReplaceWith(instruction->GetLeft());
1532 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001533 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001534 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001535 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001536
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001537 if (TryDeMorganNegationFactoring(instruction)) return;
1538
Anton Kirilove14dc862016-05-13 17:56:15 +01001539 if (TryReplaceWithRotate(instruction)) {
1540 return;
1541 }
1542
1543 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1544 // so no need to return.
1545 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001546}
1547
1548void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1549 VisitShift(instruction);
1550}
1551
1552void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1553 VisitShift(instruction);
1554}
1555
1556void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1557 HConstant* input_cst = instruction->GetConstantRight();
1558 HInstruction* input_other = instruction->GetLeastConstantLeft();
1559
Serguei Katkov115b53f2015-08-05 17:03:30 +06001560 Primitive::Type type = instruction->GetType();
1561 if (Primitive::IsFloatingPointType(type)) {
1562 return;
1563 }
1564
Roland Levillain1a653882016-03-18 18:05:57 +00001565 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001566 // Replace code looking like
1567 // SUB dst, src, 0
1568 // with
1569 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001570 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1571 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1572 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001573 instruction->ReplaceWith(input_other);
1574 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001575 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001576 return;
1577 }
1578
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001579 HBasicBlock* block = instruction->GetBlock();
1580 ArenaAllocator* allocator = GetGraph()->GetArena();
1581
Alexandre Rames188d4312015-04-09 18:30:21 +01001582 HInstruction* left = instruction->GetLeft();
1583 HInstruction* right = instruction->GetRight();
1584 if (left->IsConstant()) {
1585 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001586 // Replace code looking like
1587 // SUB dst, 0, src
1588 // with
1589 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001590 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001591 // `x` is `0.0`, the former expression yields `0.0`, while the later
1592 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001593 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001594 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001595 RecordSimplification();
1596 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001597 }
1598 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001599
1600 if (left->IsNeg() && right->IsNeg()) {
1601 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1602 return;
1603 }
1604 }
1605
1606 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1607 // Replace code looking like
1608 // NEG tmp, b
1609 // SUB dst, a, tmp
1610 // with
1611 // ADD dst, a, b
1612 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1613 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1614 RecordSimplification();
1615 right->GetBlock()->RemoveInstruction(right);
1616 return;
1617 }
1618
1619 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1620 // Replace code looking like
1621 // NEG tmp, a
1622 // SUB dst, tmp, b
1623 // with
1624 // ADD tmp, a, b
1625 // NEG dst, tmp
1626 // The second version is not intrinsically better, but enables more
1627 // transformations.
1628 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1629 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1630 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1631 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1632 instruction->ReplaceWith(neg);
1633 instruction->GetBlock()->RemoveInstruction(instruction);
1634 RecordSimplification();
1635 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001636 return;
1637 }
1638
1639 if (TrySubtractionChainSimplification(instruction)) {
1640 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001641 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001642
1643 if (left->IsAdd()) {
1644 // Replace code patterns looking like
1645 // ADD dst1, x, y ADD dst1, x, y
1646 // SUB dst2, dst1, y SUB dst2, dst1, x
1647 // with
1648 // ADD dst1, x, y
1649 // SUB instruction is not needed in this case, we may use
1650 // one of inputs of ADD instead.
1651 // It is applicable to integral types only.
1652 DCHECK(Primitive::IsIntegralType(type));
1653 if (left->InputAt(1) == right) {
1654 instruction->ReplaceWith(left->InputAt(0));
1655 RecordSimplification();
1656 instruction->GetBlock()->RemoveInstruction(instruction);
1657 return;
1658 } else if (left->InputAt(0) == right) {
1659 instruction->ReplaceWith(left->InputAt(1));
1660 RecordSimplification();
1661 instruction->GetBlock()->RemoveInstruction(instruction);
1662 return;
1663 }
1664 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001665}
1666
1667void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1668 VisitShift(instruction);
1669}
1670
1671void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1672 HConstant* input_cst = instruction->GetConstantRight();
1673 HInstruction* input_other = instruction->GetLeastConstantLeft();
1674
Roland Levillain1a653882016-03-18 18:05:57 +00001675 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001676 // Replace code looking like
1677 // XOR dst, src, 0
1678 // with
1679 // src
1680 instruction->ReplaceWith(input_other);
1681 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001682 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001683 return;
1684 }
1685
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001686 if ((input_cst != nullptr) && input_cst->IsOne()
1687 && input_other->GetType() == Primitive::kPrimBoolean) {
1688 // Replace code looking like
1689 // XOR dst, src, 1
1690 // with
1691 // BOOLEAN_NOT dst, src
1692 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1693 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1694 RecordSimplification();
1695 return;
1696 }
1697
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001698 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1699 // Replace code looking like
1700 // XOR dst, src, 0xFFF...FF
1701 // with
1702 // NOT dst, src
1703 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1704 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001705 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001706 return;
1707 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001708
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001709 HInstruction* left = instruction->GetLeft();
1710 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001711 if (((left->IsNot() && right->IsNot()) ||
1712 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001713 left->HasOnlyOneNonEnvironmentUse() &&
1714 right->HasOnlyOneNonEnvironmentUse()) {
1715 // Replace code looking like
1716 // NOT nota, a
1717 // NOT notb, b
1718 // XOR dst, nota, notb
1719 // with
1720 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001721 instruction->ReplaceInput(left->InputAt(0), 0);
1722 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001723 left->GetBlock()->RemoveInstruction(left);
1724 right->GetBlock()->RemoveInstruction(right);
1725 RecordSimplification();
1726 return;
1727 }
1728
Anton Kirilove14dc862016-05-13 17:56:15 +01001729 if (TryReplaceWithRotate(instruction)) {
1730 return;
1731 }
1732
1733 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1734 // so no need to return.
1735 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001736}
1737
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001738void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1739 HInstruction* argument = instruction->InputAt(1);
1740 HInstruction* receiver = instruction->InputAt(0);
1741 if (receiver == argument) {
1742 // Because String.equals is an instance call, the receiver is
1743 // a null check if we don't know it's null. The argument however, will
1744 // be the actual object. So we cannot end up in a situation where both
1745 // are equal but could be null.
1746 DCHECK(CanEnsureNotNullAt(argument, instruction));
1747 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1748 instruction->GetBlock()->RemoveInstruction(instruction);
1749 } else {
1750 StringEqualsOptimizations optimizations(instruction);
1751 if (CanEnsureNotNullAt(argument, instruction)) {
1752 optimizations.SetArgumentNotNull();
1753 }
1754 ScopedObjectAccess soa(Thread::Current());
1755 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1756 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1757 optimizations.SetArgumentIsString();
1758 }
1759 }
1760}
1761
Roland Levillain22c49222016-03-18 14:04:28 +00001762void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1763 bool is_left,
1764 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001765 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001766 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001767 HInstruction* value = invoke->InputAt(0);
1768 HInstruction* distance = invoke->InputAt(1);
1769 // Replace the invoke with an HRor.
1770 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001771 // Unconditionally set the type of the negated distance to `int`,
1772 // as shift and rotate operations expect a 32-bit (or narrower)
1773 // value for their distance input.
1774 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001775 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1776 }
Roland Levillain22c49222016-03-18 14:04:28 +00001777 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001778 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1779 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001780 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001781 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1782 clinit->GetBlock()->RemoveInstruction(clinit);
1783 HInstruction* ldclass = clinit->InputAt(0);
1784 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1785 ldclass->GetBlock()->RemoveInstruction(ldclass);
1786 }
1787 }
1788}
1789
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001790static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1791 if (potential_length->IsArrayLength()) {
1792 return potential_length->InputAt(0) == potential_array;
1793 }
1794
1795 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001796 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001797 }
1798
1799 return false;
1800}
1801
1802void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1803 HInstruction* source = instruction->InputAt(0);
1804 HInstruction* destination = instruction->InputAt(2);
1805 HInstruction* count = instruction->InputAt(4);
1806 SystemArrayCopyOptimizations optimizations(instruction);
1807 if (CanEnsureNotNullAt(source, instruction)) {
1808 optimizations.SetSourceIsNotNull();
1809 }
1810 if (CanEnsureNotNullAt(destination, instruction)) {
1811 optimizations.SetDestinationIsNotNull();
1812 }
1813 if (destination == source) {
1814 optimizations.SetDestinationIsSource();
1815 }
1816
1817 if (IsArrayLengthOf(count, source)) {
1818 optimizations.SetCountIsSourceLength();
1819 }
1820
1821 if (IsArrayLengthOf(count, destination)) {
1822 optimizations.SetCountIsDestinationLength();
1823 }
1824
1825 {
1826 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001827 Primitive::Type source_component_type = Primitive::kPrimVoid;
1828 Primitive::Type destination_component_type = Primitive::kPrimVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001829 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1830 if (destination_rti.IsValid()) {
1831 if (destination_rti.IsObjectArray()) {
1832 if (destination_rti.IsExact()) {
1833 optimizations.SetDoesNotNeedTypeCheck();
1834 }
1835 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001836 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001837 if (destination_rti.IsPrimitiveArrayClass()) {
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001838 destination_component_type =
1839 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001840 optimizations.SetDestinationIsPrimitiveArray();
1841 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1842 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001843 }
1844 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001845 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1846 if (source_rti.IsValid()) {
1847 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1848 optimizations.SetDoesNotNeedTypeCheck();
1849 }
1850 if (source_rti.IsPrimitiveArrayClass()) {
1851 optimizations.SetSourceIsPrimitiveArray();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001852 source_component_type = source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001853 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1854 optimizations.SetSourceIsNonPrimitiveArray();
1855 }
1856 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001857 // For primitive arrays, use their optimized ArtMethod implementations.
1858 if ((source_component_type != Primitive::kPrimVoid) &&
1859 (source_component_type == destination_component_type)) {
1860 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1861 PointerSize image_size = class_linker->GetImagePointerSize();
1862 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
1863 mirror::Class* system = invoke->GetResolvedMethod()->GetDeclaringClass();
1864 ArtMethod* method = nullptr;
1865 switch (source_component_type) {
1866 case Primitive::kPrimBoolean:
1867 method = system->FindDeclaredDirectMethod("arraycopy", "([ZI[ZII)V", image_size);
1868 break;
1869 case Primitive::kPrimByte:
1870 method = system->FindDeclaredDirectMethod("arraycopy", "([BI[BII)V", image_size);
1871 break;
1872 case Primitive::kPrimChar:
1873 method = system->FindDeclaredDirectMethod("arraycopy", "([CI[CII)V", image_size);
1874 break;
1875 case Primitive::kPrimShort:
1876 method = system->FindDeclaredDirectMethod("arraycopy", "([SI[SII)V", image_size);
1877 break;
1878 case Primitive::kPrimInt:
1879 method = system->FindDeclaredDirectMethod("arraycopy", "([II[III)V", image_size);
1880 break;
1881 case Primitive::kPrimFloat:
1882 method = system->FindDeclaredDirectMethod("arraycopy", "([FI[FII)V", image_size);
1883 break;
1884 case Primitive::kPrimLong:
1885 method = system->FindDeclaredDirectMethod("arraycopy", "([JI[JII)V", image_size);
1886 break;
1887 case Primitive::kPrimDouble:
1888 method = system->FindDeclaredDirectMethod("arraycopy", "([DI[DII)V", image_size);
1889 break;
1890 default:
1891 LOG(FATAL) << "Unreachable";
1892 }
1893 DCHECK(method != nullptr);
1894 invoke->SetResolvedMethod(method);
1895 // Sharpen the new invoke. Note that we do not update the dex method index of
1896 // the invoke, as we would need to look it up in the current dex file, and it
1897 // is unlikely that it exists. The most usual situation for such typed
1898 // arraycopy methods is a direct pointer to the boot image.
1899 HSharpening::SharpenInvokeStaticOrDirect(invoke, codegen_);
1900 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001901 }
1902}
1903
Roland Levillaina5c4a402016-03-15 15:02:50 +00001904void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1905 bool is_signum,
1906 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001907 DCHECK(invoke->IsInvokeStaticOrDirect());
1908 uint32_t dex_pc = invoke->GetDexPc();
1909 HInstruction* left = invoke->InputAt(0);
1910 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001911 if (!is_signum) {
1912 right = invoke->InputAt(1);
1913 } else if (type == Primitive::kPrimLong) {
1914 right = GetGraph()->GetLongConstant(0);
1915 } else {
1916 right = GetGraph()->GetIntConstant(0);
1917 }
1918 HCompare* compare = new (GetGraph()->GetArena())
1919 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1920 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1921}
1922
Aart Bik75a38b22016-02-17 10:41:50 -08001923void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1924 DCHECK(invoke->IsInvokeStaticOrDirect());
1925 uint32_t dex_pc = invoke->GetDexPc();
1926 // IsNaN(x) is the same as x != x.
1927 HInstruction* x = invoke->InputAt(0);
1928 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001929 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001930 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1931}
1932
Aart Bik2a6aad92016-02-25 11:32:32 -08001933void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1934 DCHECK(invoke->IsInvokeStaticOrDirect());
1935 uint32_t dex_pc = invoke->GetDexPc();
1936 HInstruction* x = invoke->InputAt(0);
1937 Primitive::Type type = x->GetType();
1938 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1939 HInstruction* nan;
1940 if (type == Primitive::kPrimDouble) {
1941 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1942 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1943 kNeedsEnvironmentOrCache,
1944 kNoSideEffects,
1945 kNoThrow);
1946 } else {
1947 DCHECK_EQ(type, Primitive::kPrimFloat);
1948 nan = GetGraph()->GetIntConstant(0x7fc00000);
1949 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1950 kNeedsEnvironmentOrCache,
1951 kNoSideEffects,
1952 kNoThrow);
1953 }
1954 // Test IsNaN(x), which is the same as x != x.
1955 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1956 condition->SetBias(ComparisonBias::kLtBias);
1957 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1958 // Select between the two.
1959 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1960 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1961 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1962}
1963
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001964void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1965 HInstruction* str = invoke->InputAt(0);
1966 HInstruction* index = invoke->InputAt(1);
1967 uint32_t dex_pc = invoke->GetDexPc();
1968 ArenaAllocator* arena = GetGraph()->GetArena();
1969 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1970 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1971 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1972 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00001973 HBoundsCheck* bounds_check = new (arena) HBoundsCheck(
1974 index, length, dex_pc, invoke->GetDexMethodIndex());
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001975 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00001976 HArrayGet* array_get = new (arena) HArrayGet(
1977 str, bounds_check, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001978 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1979 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1980 GetGraph()->SetHasBoundsChecks(true);
1981}
1982
Vladimir Markodce016e2016-04-28 13:10:02 +01001983void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1984 HInstruction* str = invoke->InputAt(0);
1985 uint32_t dex_pc = invoke->GetDexPc();
1986 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1987 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001988 HArrayLength* length =
1989 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001990 HInstruction* replacement;
1991 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1992 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1993 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1994 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1995 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1996 replacement = equal;
1997 } else {
1998 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1999 replacement = length;
2000 }
2001 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
2002}
2003
Aart Bikff7d89c2016-11-07 08:49:28 -08002004// This method should only be used on intrinsics whose sole way of throwing an
2005// exception is raising a NPE when the nth argument is null. If that argument
2006// is provably non-null, we can clear the flag.
2007void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2008 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002009 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002010 invoke->SetCanThrow(false);
2011 }
2012}
2013
Aart Bik71bf7b42016-11-16 10:17:46 -08002014// Methods that return "this" can replace the returned value with the receiver.
2015void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2016 if (invoke->HasUses()) {
2017 HInstruction* receiver = invoke->InputAt(0);
2018 invoke->ReplaceWith(receiver);
2019 RecordSimplification();
2020 }
2021}
2022
2023// Helper method for StringBuffer escape analysis.
2024static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2025 if (user->IsInvokeStaticOrDirect()) {
2026 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002027 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2028 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002029 user->InputAt(0) == reference;
2030 } else if (user->IsInvokeVirtual()) {
2031 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2032 case Intrinsics::kStringBufferLength:
2033 case Intrinsics::kStringBufferToString:
2034 DCHECK_EQ(user->InputAt(0), reference);
2035 return true;
2036 case Intrinsics::kStringBufferAppend:
2037 // Returns "this", so only okay if no further uses.
2038 DCHECK_EQ(user->InputAt(0), reference);
2039 DCHECK_NE(user->InputAt(1), reference);
2040 return !user->HasUses();
2041 default:
2042 break;
2043 }
2044 }
2045 return false;
2046}
2047
2048// Certain allocation intrinsics are not removed by dead code elimination
2049// because of potentially throwing an OOM exception or other side effects.
2050// This method removes such intrinsics when special circumstances allow.
2051void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2052 if (!invoke->HasUses()) {
2053 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2054 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2055 // call does not escape since only thread-local synchronization may be removed.
2056 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2057 HInstruction* receiver = invoke->InputAt(0);
2058 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2059 invoke->GetBlock()->RemoveInstruction(invoke);
2060 RecordSimplification();
2061 }
2062 }
2063}
2064
Aart Bik11932592016-03-08 12:42:25 -08002065void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
2066 uint32_t dex_pc = invoke->GetDexPc();
2067 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
2068 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
2069}
2070
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002071void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002072 switch (instruction->GetIntrinsic()) {
2073 case Intrinsics::kStringEquals:
2074 SimplifyStringEquals(instruction);
2075 break;
2076 case Intrinsics::kSystemArrayCopy:
2077 SimplifySystemArrayCopy(instruction);
2078 break;
2079 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002080 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
2081 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002082 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002083 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002084 break;
2085 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002086 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
2087 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002088 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002089 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002090 break;
2091 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002092 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
2093 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002094 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002095 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002096 break;
2097 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002098 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
2099 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002100 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002101 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002102 break;
2103 case Intrinsics::kFloatIsNaN:
2104 case Intrinsics::kDoubleIsNaN:
2105 SimplifyIsNaN(instruction);
2106 break;
2107 case Intrinsics::kFloatFloatToIntBits:
2108 case Intrinsics::kDoubleDoubleToLongBits:
2109 SimplifyFP2Int(instruction);
2110 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002111 case Intrinsics::kStringCharAt:
2112 SimplifyStringCharAt(instruction);
2113 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002114 case Intrinsics::kStringIsEmpty:
2115 case Intrinsics::kStringLength:
2116 SimplifyStringIsEmptyOrLength(instruction);
2117 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002118 case Intrinsics::kStringStringIndexOf:
2119 case Intrinsics::kStringStringIndexOfAfter:
2120 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2121 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002122 case Intrinsics::kStringBufferAppend:
2123 case Intrinsics::kStringBuilderAppend:
2124 SimplifyReturnThis(instruction);
2125 break;
2126 case Intrinsics::kStringBufferToString:
2127 case Intrinsics::kStringBuilderToString:
2128 SimplifyAllocationIntrinsic(instruction);
2129 break;
Aart Bik11932592016-03-08 12:42:25 -08002130 case Intrinsics::kUnsafeLoadFence:
2131 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2132 break;
2133 case Intrinsics::kUnsafeStoreFence:
2134 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2135 break;
2136 case Intrinsics::kUnsafeFullFence:
2137 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2138 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002139 default:
2140 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002141 }
2142}
2143
Aart Bikbb245d12015-10-19 11:05:03 -07002144void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2145 HInstruction* cond = deoptimize->InputAt(0);
2146 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002147 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002148 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002149 if (deoptimize->GuardsAnInput()) {
2150 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2151 }
Aart Bikbb245d12015-10-19 11:05:03 -07002152 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2153 } else {
2154 // Always deopt.
2155 }
2156 }
2157}
2158
Anton Kirilove14dc862016-05-13 17:56:15 +01002159// Replace code looking like
2160// OP y, x, const1
2161// OP z, y, const2
2162// with
2163// OP z, x, const3
2164// where OP is both an associative and a commutative operation.
2165bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2166 HBinaryOperation* instruction) {
2167 DCHECK(instruction->IsCommutative());
2168
2169 if (!Primitive::IsIntegralType(instruction->GetType())) {
2170 return false;
2171 }
2172
2173 HInstruction* left = instruction->GetLeft();
2174 HInstruction* right = instruction->GetRight();
2175 // Variable names as described above.
2176 HConstant* const2;
2177 HBinaryOperation* y;
2178
2179 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
2180 const2 = right->AsConstant();
2181 y = left->AsBinaryOperation();
2182 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
2183 const2 = left->AsConstant();
2184 y = right->AsBinaryOperation();
2185 } else {
2186 // The node does not match the pattern.
2187 return false;
2188 }
2189
2190 // If `y` has more than one use, we do not perform the optimization
2191 // because it might increase code size (e.g. if the new constant is
2192 // no longer encodable as an immediate operand in the target ISA).
2193 if (!y->HasOnlyOneNonEnvironmentUse()) {
2194 return false;
2195 }
2196
2197 // GetConstantRight() can return both left and right constants
2198 // for commutative operations.
2199 HConstant* const1 = y->GetConstantRight();
2200 if (const1 == nullptr) {
2201 return false;
2202 }
2203
2204 instruction->ReplaceInput(const1, 0);
2205 instruction->ReplaceInput(const2, 1);
2206 HConstant* const3 = instruction->TryStaticEvaluation();
2207 DCHECK(const3 != nullptr);
2208 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2209 instruction->ReplaceInput(const3, 1);
2210 RecordSimplification();
2211 return true;
2212}
2213
2214static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2215 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2216}
2217
2218// Helper function that performs addition statically, considering the result type.
2219static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
2220 // Use the Compute() method for consistency with TryStaticEvaluation().
2221 if (type == Primitive::kPrimInt) {
2222 return HAdd::Compute<int32_t>(x, y);
2223 } else {
2224 DCHECK_EQ(type, Primitive::kPrimLong);
2225 return HAdd::Compute<int64_t>(x, y);
2226 }
2227}
2228
2229// Helper function that handles the child classes of HConstant
2230// and returns an integer with the appropriate sign.
2231static int64_t GetValue(HConstant* constant, bool is_negated) {
2232 int64_t ret = Int64FromConstant(constant);
2233 return is_negated ? -ret : ret;
2234}
2235
2236// Replace code looking like
2237// OP1 y, x, const1
2238// OP2 z, y, const2
2239// with
2240// OP3 z, x, const3
2241// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2242bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2243 HBinaryOperation* instruction) {
2244 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2245
2246 Primitive::Type type = instruction->GetType();
2247 if (!Primitive::IsIntegralType(type)) {
2248 return false;
2249 }
2250
2251 HInstruction* left = instruction->GetLeft();
2252 HInstruction* right = instruction->GetRight();
2253 // Variable names as described above.
2254 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2255 if (const2 == nullptr) {
2256 return false;
2257 }
2258
2259 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2260 ? left->AsBinaryOperation()
2261 : AsAddOrSub(right);
2262 // If y has more than one use, we do not perform the optimization because
2263 // it might increase code size (e.g. if the new constant is no longer
2264 // encodable as an immediate operand in the target ISA).
2265 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2266 return false;
2267 }
2268
2269 left = y->GetLeft();
2270 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2271 if (const1 == nullptr) {
2272 return false;
2273 }
2274
2275 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2276 // If both inputs are constants, let the constant folding pass deal with it.
2277 if (x->IsConstant()) {
2278 return false;
2279 }
2280
2281 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2282 int64_t const2_val = GetValue(const2, is_const2_negated);
2283 bool is_y_negated = (y == right) && instruction->IsSub();
2284 right = y->GetRight();
2285 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2286 int64_t const1_val = GetValue(const1, is_const1_negated);
2287 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2288 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2289 HBasicBlock* block = instruction->GetBlock();
2290 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2291 ArenaAllocator* arena = instruction->GetArena();
2292 HInstruction* z;
2293
2294 if (is_x_negated) {
2295 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2296 } else {
2297 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2298 }
2299
2300 block->ReplaceAndRemoveInstructionWith(instruction, z);
2301 RecordSimplification();
2302 return true;
2303}
2304
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002305} // namespace art