blob: e06fdee370c3f2507712648a449ef563fbc50d13 [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_simplifier.h"
18
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010019#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070021#include "scoped_thread_state_change-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022
Nicolas Geoffray3c049742014-09-24 18:10:46 +010023namespace art {
24
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010025class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000026 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000027 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010028 : HGraphDelegateVisitor(graph),
Alexandre Rames188d4312015-04-09 18:30:21 +010029 stats_(stats) {}
30
31 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000032
33 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010034 void RecordSimplification() {
35 simplification_occurred_ = true;
36 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000037 MaybeRecordStat(kInstructionSimplifications);
38 }
39
40 void MaybeRecordStat(MethodCompilationStat stat) {
41 if (stats_ != nullptr) {
42 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010043 }
44 }
45
Scott Wakeling40a04bf2015-12-11 09:50:36 +000046 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
47 bool TryReplaceWithRotate(HBinaryOperation* instruction);
48 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
49 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
50 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
51
Alexandre Rames188d4312015-04-09 18:30:21 +010052 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000053 // `op` should be either HOr or HAnd.
54 // De Morgan's laws:
55 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
56 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010057 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
58 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
59
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000060 void VisitShift(HBinaryOperation* shift);
61
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000062 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010063 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
64 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010065 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
66 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000067 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000068 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000069 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080070 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000071 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000072 void VisitAdd(HAdd* instruction) OVERRIDE;
73 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040074 void VisitCondition(HCondition* instruction) OVERRIDE;
75 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
76 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
77 void VisitLessThan(HLessThan* condition) OVERRIDE;
78 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060079 void VisitBelow(HBelow* condition) OVERRIDE;
80 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
81 void VisitAbove(HAbove* condition) OVERRIDE;
82 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000083 void VisitDiv(HDiv* instruction) OVERRIDE;
84 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010085 void VisitNeg(HNeg* instruction) OVERRIDE;
86 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000087 void VisitOr(HOr* instruction) OVERRIDE;
88 void VisitShl(HShl* instruction) OVERRIDE;
89 void VisitShr(HShr* instruction) OVERRIDE;
90 void VisitSub(HSub* instruction) OVERRIDE;
91 void VisitUShr(HUShr* instruction) OVERRIDE;
92 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000093 void VisitSelect(HSelect* select) OVERRIDE;
94 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010095 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010096 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070097 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010098
99 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000100
Roland Levillain22c49222016-03-18 14:04:28 +0000101 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100102 void SimplifySystemArrayCopy(HInvoke* invoke);
103 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000104 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800105 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800106 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100107 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100108 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800109 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100110
Calin Juravleacf735c2015-02-12 15:25:22 +0000111 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100112 bool simplification_occurred_ = false;
113 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700114 // We ensure we do not loop infinitely. The value should not be too high, since that
115 // would allow looping around the same basic block too many times. The value should
116 // not be too low either, however, since we want to allow revisiting a basic block
117 // with many statements and simplifications at least once.
118 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000119};
120
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000122 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100123 visitor.Run();
124}
125
126void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100127 // Iterate in reverse post order to open up more simplifications to users
128 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100129 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100130 // The simplification of an instruction to another instruction may yield
131 // possibilities for other simplifications. So although we perform a reverse
132 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100133 do {
134 simplification_occurred_ = false;
135 VisitBasicBlock(block);
136 } while (simplification_occurred_ &&
137 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100138 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100139 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140}
141
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000142namespace {
143
144bool AreAllBitsSet(HConstant* constant) {
145 return Int64FromConstant(constant) == -1;
146}
147
148} // namespace
149
Alexandre Rames188d4312015-04-09 18:30:21 +0100150// Returns true if the code was simplified to use only one negation operation
151// after the binary operation instead of one on each of the inputs.
152bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
153 DCHECK(binop->IsAdd() || binop->IsSub());
154 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
155 HNeg* left_neg = binop->GetLeft()->AsNeg();
156 HNeg* right_neg = binop->GetRight()->AsNeg();
157 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
158 !right_neg->HasOnlyOneNonEnvironmentUse()) {
159 return false;
160 }
161 // Replace code looking like
162 // NEG tmp1, a
163 // NEG tmp2, b
164 // ADD dst, tmp1, tmp2
165 // with
166 // ADD tmp, a, b
167 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600168 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
169 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
170 // while the later yields `-0.0`.
171 if (!Primitive::IsIntegralType(binop->GetType())) {
172 return false;
173 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100174 binop->ReplaceInput(left_neg->GetInput(), 0);
175 binop->ReplaceInput(right_neg->GetInput(), 1);
176 left_neg->GetBlock()->RemoveInstruction(left_neg);
177 right_neg->GetBlock()->RemoveInstruction(right_neg);
178 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
179 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
180 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
181 RecordSimplification();
182 return true;
183}
184
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000185bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
186 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
187 Primitive::Type type = op->GetType();
188 HInstruction* left = op->GetLeft();
189 HInstruction* right = op->GetRight();
190
191 // We can apply De Morgan's laws if both inputs are Not's and are only used
192 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000193 if (((left->IsNot() && right->IsNot()) ||
194 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000195 left->HasOnlyOneNonEnvironmentUse() &&
196 right->HasOnlyOneNonEnvironmentUse()) {
197 // Replace code looking like
198 // NOT nota, a
199 // NOT notb, b
200 // AND dst, nota, notb (respectively OR)
201 // with
202 // OR or, a, b (respectively AND)
203 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000204 HInstruction* src_left = left->InputAt(0);
205 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000206 uint32_t dex_pc = op->GetDexPc();
207
208 // Remove the negations on the inputs.
209 left->ReplaceWith(src_left);
210 right->ReplaceWith(src_right);
211 left->GetBlock()->RemoveInstruction(left);
212 right->GetBlock()->RemoveInstruction(right);
213
214 // Replace the `HAnd` or `HOr`.
215 HBinaryOperation* hbin;
216 if (op->IsAnd()) {
217 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
218 } else {
219 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
220 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000221 HInstruction* hnot;
222 if (left->IsBooleanNot()) {
223 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
224 } else {
225 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
226 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000227
228 op->GetBlock()->InsertInstructionBefore(hbin, op);
229 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
230
231 RecordSimplification();
232 return true;
233 }
234
235 return false;
236}
237
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000238void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
239 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100240 HInstruction* shift_amount = instruction->GetRight();
241 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000242
Alexandre Rames50518442016-06-27 11:39:19 +0100243 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
244 ? kMaxLongShiftDistance
245 : kMaxIntShiftDistance;
246
247 if (shift_amount->IsConstant()) {
248 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
249 if ((cst & implicit_mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400250 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100251 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400252 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100253 // value
254 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400255 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100256 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100257 return;
258 }
259 }
260
261 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
262 // unnecessary explicit masking operations on the shift amount.
263 // Replace code looking like
264 // AND masked_shift, shift, <superset of implicit mask>
265 // SHL dst, value, masked_shift
266 // with
267 // SHL dst, value, shift
268 if (shift_amount->IsAnd()) {
269 HAnd* and_insn = shift_amount->AsAnd();
270 HConstant* mask = and_insn->GetConstantRight();
271 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
272 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
273 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400274 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000275 }
276}
277
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000278static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
279 return (sub->GetRight() == other &&
280 sub->GetLeft()->IsConstant() &&
281 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
282}
283
284bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
285 HUShr* ushr,
286 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000287 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
288 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000289 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
290 if (!ushr->HasUses()) {
291 ushr->GetBlock()->RemoveInstruction(ushr);
292 }
293 if (!ushr->GetRight()->HasUses()) {
294 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
295 }
296 if (!shl->HasUses()) {
297 shl->GetBlock()->RemoveInstruction(shl);
298 }
299 if (!shl->GetRight()->HasUses()) {
300 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
301 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100302 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000303 return true;
304}
305
306// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
307bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000308 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
309 HInstruction* left = op->GetLeft();
310 HInstruction* right = op->GetRight();
311 // If we have an UShr and a Shl (in either order).
312 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
313 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
314 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
315 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
316 if (ushr->GetType() == shl->GetType() &&
317 ushr->GetLeft() == shl->GetLeft()) {
318 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
319 // Shift distances are both constant, try replacing with Ror if they
320 // add up to the register size.
321 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
322 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
323 // Shift distances are potentially of the form x and (reg_size - x).
324 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
325 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
326 // Shift distances are potentially of the form d and -d.
327 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
328 }
329 }
330 }
331 return false;
332}
333
334// Try replacing code looking like (x >>> #rdist OP x << #ldist):
335// UShr dst, x, #rdist
336// Shl tmp, x, #ldist
337// OP dst, dst, tmp
338// or like (x >>> #rdist OP x << #-ldist):
339// UShr dst, x, #rdist
340// Shl tmp, x, #-ldist
341// OP dst, dst, tmp
342// with
343// Ror dst, x, #rdist
344bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
345 HUShr* ushr,
346 HShl* shl) {
347 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
348 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
349 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
350 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
351 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
352 ReplaceRotateWithRor(op, ushr, shl);
353 return true;
354 }
355 return false;
356}
357
358// Replace code looking like (x >>> -d OP x << d):
359// Neg neg, d
360// UShr dst, x, neg
361// Shl tmp, x, d
362// OP dst, dst, tmp
363// with
364// Neg neg, d
365// Ror dst, x, neg
366// *** OR ***
367// Replace code looking like (x >>> d OP x << -d):
368// UShr dst, x, d
369// Neg neg, d
370// Shl tmp, x, neg
371// OP dst, dst, tmp
372// with
373// Ror dst, x, d
374bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
375 HUShr* ushr,
376 HShl* shl) {
377 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
378 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
379 bool neg_is_left = shl->GetRight()->IsNeg();
380 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
381 // And the shift distance being negated is the distance being shifted the other way.
382 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
383 ReplaceRotateWithRor(op, ushr, shl);
384 }
385 return false;
386}
387
388// Try replacing code looking like (x >>> d OP x << (#bits - d)):
389// UShr dst, x, d
390// Sub ld, #bits, d
391// Shl tmp, x, ld
392// OP dst, dst, tmp
393// with
394// Ror dst, x, d
395// *** OR ***
396// Replace code looking like (x >>> (#bits - d) OP x << d):
397// Sub rd, #bits, d
398// UShr dst, x, rd
399// Shl tmp, x, d
400// OP dst, dst, tmp
401// with
402// Neg neg, d
403// Ror dst, x, neg
404bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
405 HUShr* ushr,
406 HShl* shl) {
407 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
408 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
409 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
410 HInstruction* shl_shift = shl->GetRight();
411 HInstruction* ushr_shift = ushr->GetRight();
412 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
413 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
414 return ReplaceRotateWithRor(op, ushr, shl);
415 }
416 return false;
417}
418
Calin Juravle10e244f2015-01-26 18:54:32 +0000419void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
420 HInstruction* obj = null_check->InputAt(0);
421 if (!obj->CanBeNull()) {
422 null_check->ReplaceWith(obj);
423 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000424 if (stats_ != nullptr) {
425 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
426 }
427 }
428}
429
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100430bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
431 if (!input->CanBeNull()) {
432 return true;
433 }
434
Vladimir Marko46817b82016-03-29 12:21:58 +0100435 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
436 HInstruction* user = use.GetUser();
437 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100438 return true;
439 }
440 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100441
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100442 return false;
443}
444
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100445// Returns whether doing a type test between the class of `object` against `klass` has
446// a statically known outcome. The result of the test is stored in `outcome`.
447static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000448 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
449 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
450 ScopedObjectAccess soa(Thread::Current());
451 if (!obj_rti.IsValid()) {
452 // We run the simplifier before the reference type propagation so type info might not be
453 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100454 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000455 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100456
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100457 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100458 if (!class_rti.IsValid()) {
459 // Happens when the loaded class is unresolved.
460 return false;
461 }
462 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000463 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100464 *outcome = true;
465 return true;
466 } else if (obj_rti.IsExact()) {
467 // The test failed at compile time so will also fail at runtime.
468 *outcome = false;
469 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100470 } else if (!class_rti.IsInterface()
471 && !obj_rti.IsInterface()
472 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100473 // Different type hierarchy. The test will fail.
474 *outcome = false;
475 return true;
476 }
477 return false;
478}
479
480void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
481 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100482 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
483 if (load_class->NeedsAccessCheck()) {
484 // If we need to perform an access check we cannot remove the instruction.
485 return;
486 }
487
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100488 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100489 check_cast->ClearMustDoNullCheck();
490 }
491
492 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000493 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000494 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100495 return;
496 }
497
Vladimir Markoa65ed302016-03-14 21:21:29 +0000498 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
499 // the return value check with the `outcome` check, b/27651442 .
500 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700501 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100502 if (outcome) {
503 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000504 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700505 if (!load_class->HasUses()) {
506 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
507 // However, here we know that it cannot because the checkcast was successfull, hence
508 // the class was already loaded.
509 load_class->GetBlock()->RemoveInstruction(load_class);
510 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100511 } else {
512 // Don't do anything for exceptional cases for now. Ideally we should remove
513 // all instructions and blocks this instruction dominates.
514 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000515 }
516}
517
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100518void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100519 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100520 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
521 if (load_class->NeedsAccessCheck()) {
522 // If we need to perform an access check we cannot remove the instruction.
523 return;
524 }
525
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100526 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100527 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100528 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100529 instruction->ClearMustDoNullCheck();
530 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100531
532 HGraph* graph = GetGraph();
533 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000534 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100535 instruction->ReplaceWith(graph->GetIntConstant(0));
536 instruction->GetBlock()->RemoveInstruction(instruction);
537 RecordSimplification();
538 return;
539 }
540
Vladimir Marko24bd8952016-03-15 10:40:33 +0000541 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
542 // the return value check with the `outcome` check, b/27651442 .
543 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700544 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000545 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100546 if (outcome && can_be_null) {
547 // Type test will succeed, we just need a null test.
548 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
549 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
550 instruction->ReplaceWith(test);
551 } else {
552 // We've statically determined the result of the instanceof.
553 instruction->ReplaceWith(graph->GetIntConstant(outcome));
554 }
555 RecordSimplification();
556 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700557 if (outcome && !load_class->HasUses()) {
558 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
559 // However, here we know that it cannot because the instanceof check was successfull, hence
560 // the class was already loaded.
561 load_class->GetBlock()->RemoveInstruction(load_class);
562 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100563 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100564}
565
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100566void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
567 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100568 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100569 instruction->ClearValueCanBeNull();
570 }
571}
572
573void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
574 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100575 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100576 instruction->ClearValueCanBeNull();
577 }
578}
579
Anton Shaminbdd79352016-02-15 12:48:36 +0600580static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
581 HInstruction *lhs = cond->InputAt(0);
582 HInstruction *rhs = cond->InputAt(1);
583 switch (cond->GetKind()) {
584 case HInstruction::kEqual:
585 return new (arena) HEqual(rhs, lhs);
586 case HInstruction::kNotEqual:
587 return new (arena) HNotEqual(rhs, lhs);
588 case HInstruction::kLessThan:
589 return new (arena) HGreaterThan(rhs, lhs);
590 case HInstruction::kLessThanOrEqual:
591 return new (arena) HGreaterThanOrEqual(rhs, lhs);
592 case HInstruction::kGreaterThan:
593 return new (arena) HLessThan(rhs, lhs);
594 case HInstruction::kGreaterThanOrEqual:
595 return new (arena) HLessThanOrEqual(rhs, lhs);
596 case HInstruction::kBelow:
597 return new (arena) HAbove(rhs, lhs);
598 case HInstruction::kBelowOrEqual:
599 return new (arena) HAboveOrEqual(rhs, lhs);
600 case HInstruction::kAbove:
601 return new (arena) HBelow(rhs, lhs);
602 case HInstruction::kAboveOrEqual:
603 return new (arena) HBelowOrEqual(rhs, lhs);
604 default:
605 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
606 }
607 return nullptr;
608}
609
Aart Bik2767f4b2016-10-28 15:03:53 -0700610static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
611 if (input->GetType() == Primitive::kPrimBoolean) {
612 return true; // input has direct boolean type
613 } else if (cmp->GetUses().HasExactlyOneElement()) {
614 // Comparison also has boolean type if both its input and the instruction
615 // itself feed into the same phi node.
616 HInstruction* user = cmp->GetUses().front().GetUser();
617 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
618 }
619 return false;
620}
621
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000622void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100623 HInstruction* input_const = equal->GetConstantRight();
624 if (input_const != nullptr) {
625 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700626 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100627 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100628 // We are comparing the boolean to a constant which is of type int and can
629 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000630 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100631 // Replace (bool_value == true) with bool_value
632 equal->ReplaceWith(input_value);
633 block->RemoveInstruction(equal);
634 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000635 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700636 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500637 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
638 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100639 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100640 } else {
641 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
642 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
643 block->RemoveInstruction(equal);
644 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100645 }
Mark Mendellc4701932015-04-10 13:18:51 -0400646 } else {
647 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100648 }
Mark Mendellc4701932015-04-10 13:18:51 -0400649 } else {
650 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100651 }
652}
653
David Brazdil0d13fee2015-04-17 14:52:19 +0100654void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
655 HInstruction* input_const = not_equal->GetConstantRight();
656 if (input_const != nullptr) {
657 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700658 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100659 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100660 // We are comparing the boolean to a constant which is of type int and can
661 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000662 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700663 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500664 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
665 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100666 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000667 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100668 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100669 not_equal->ReplaceWith(input_value);
670 block->RemoveInstruction(not_equal);
671 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100672 } else {
673 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
674 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
675 block->RemoveInstruction(not_equal);
676 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100677 }
Mark Mendellc4701932015-04-10 13:18:51 -0400678 } else {
679 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100680 }
Mark Mendellc4701932015-04-10 13:18:51 -0400681 } else {
682 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100683 }
684}
685
686void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000687 HInstruction* input = bool_not->InputAt(0);
688 HInstruction* replace_with = nullptr;
689
690 if (input->IsIntConstant()) {
691 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000692 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000693 replace_with = GetGraph()->GetIntConstant(0);
694 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000695 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000696 replace_with = GetGraph()->GetIntConstant(1);
697 }
698 } else if (input->IsBooleanNot()) {
699 // Replace (!(!bool_value)) with bool_value.
700 replace_with = input->InputAt(0);
701 } else if (input->IsCondition() &&
702 // Don't change FP compares. The definition of compares involving
703 // NaNs forces the compares to be done as written by the user.
704 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
705 // Replace condition with its opposite.
706 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
707 }
708
709 if (replace_with != nullptr) {
710 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100711 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000712 RecordSimplification();
713 }
714}
715
716void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
717 HInstruction* replace_with = nullptr;
718 HInstruction* condition = select->GetCondition();
719 HInstruction* true_value = select->GetTrueValue();
720 HInstruction* false_value = select->GetFalseValue();
721
722 if (condition->IsBooleanNot()) {
723 // Change ((!cond) ? x : y) to (cond ? y : x).
724 condition = condition->InputAt(0);
725 std::swap(true_value, false_value);
726 select->ReplaceInput(false_value, 0);
727 select->ReplaceInput(true_value, 1);
728 select->ReplaceInput(condition, 2);
729 RecordSimplification();
730 }
731
732 if (true_value == false_value) {
733 // Replace (cond ? x : x) with (x).
734 replace_with = true_value;
735 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000736 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000737 // Replace (true ? x : y) with (x).
738 replace_with = true_value;
739 } else {
740 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000741 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000742 replace_with = false_value;
743 }
744 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000745 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000746 // Replace (cond ? true : false) with (cond).
747 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000748 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000749 // Replace (cond ? false : true) with (!cond).
750 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
751 }
752 }
753
754 if (replace_with != nullptr) {
755 select->ReplaceWith(replace_with);
756 select->GetBlock()->RemoveInstruction(select);
757 RecordSimplification();
758 }
759}
760
761void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
762 HInstruction* condition = instruction->InputAt(0);
763 if (condition->IsBooleanNot()) {
764 // Swap successors if input is negated.
765 instruction->ReplaceInput(condition->InputAt(0), 0);
766 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100767 RecordSimplification();
768 }
769}
770
Mingyao Yang0304e182015-01-30 16:41:29 -0800771void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
772 HInstruction* input = instruction->InputAt(0);
773 // If the array is a NewArray with constant size, replace the array length
774 // with the constant instruction. This helps the bounds check elimination phase.
775 if (input->IsNewArray()) {
776 input = input->InputAt(0);
777 if (input->IsIntConstant()) {
778 instruction->ReplaceWith(input);
779 }
780 }
781}
782
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000783void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000784 HInstruction* value = instruction->GetValue();
785 if (value->GetType() != Primitive::kPrimNot) return;
786
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100787 if (CanEnsureNotNullAt(value, instruction)) {
788 instruction->ClearValueCanBeNull();
789 }
790
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000791 if (value->IsArrayGet()) {
792 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
793 // If the code is just swapping elements in the array, no need for a type check.
794 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100795 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000796 }
797 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100798
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100799 if (value->IsNullConstant()) {
800 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100801 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100802 }
803
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100804 ScopedObjectAccess soa(Thread::Current());
805 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
806 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
807 if (!array_rti.IsValid()) {
808 return;
809 }
810
811 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
812 instruction->ClearNeedsTypeCheck();
813 return;
814 }
815
816 if (array_rti.IsObjectArray()) {
817 if (array_rti.IsExact()) {
818 instruction->ClearNeedsTypeCheck();
819 return;
820 }
821 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100822 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000823}
824
Vladimir Markob52bbde2016-02-12 12:06:05 +0000825static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100826 // Invariant: We should never generate a conversion to a Boolean value.
827 DCHECK_NE(Primitive::kPrimBoolean, result_type);
828
Vladimir Markob52bbde2016-02-12 12:06:05 +0000829 // Besides conversion to the same type, widening integral conversions are implicit,
830 // excluding conversions to long and the byte->char conversion where we need to
831 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
832 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100833 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
834 input_type == Primitive::kPrimByte ||
835 input_type == Primitive::kPrimShort ||
836 input_type == Primitive::kPrimChar)) ||
837 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
838 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
839 input_type == Primitive::kPrimByte)) ||
840 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000841}
842
843static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
844 // The conversion to a larger type is loss-less with the exception of two cases,
845 // - conversion to char, the only unsigned type, where we may lose some bits, and
846 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
847 // For integral to FP conversions this holds because the FP mantissa is large enough.
848 DCHECK_NE(input_type, result_type);
849 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
850 result_type != Primitive::kPrimChar &&
851 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
852}
853
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000854void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000855 HInstruction* input = instruction->GetInput();
856 Primitive::Type input_type = input->GetType();
857 Primitive::Type result_type = instruction->GetResultType();
858 if (IsTypeConversionImplicit(input_type, result_type)) {
859 // Remove the implicit conversion; this includes conversion to the same type.
860 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000861 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000862 RecordSimplification();
863 return;
864 }
865
866 if (input->IsTypeConversion()) {
867 HTypeConversion* input_conversion = input->AsTypeConversion();
868 HInstruction* original_input = input_conversion->GetInput();
869 Primitive::Type original_type = original_input->GetType();
870
871 // When the first conversion is lossless, a direct conversion from the original type
872 // to the final type yields the same result, even for a lossy second conversion, for
873 // example float->double->int or int->double->float.
874 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
875
876 // For integral conversions, see if the first conversion loses only bits that the second
877 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
878 // conversion yields the same result, for example long->int->short or int->char->short.
879 bool integral_conversions_with_non_widening_second =
880 Primitive::IsIntegralType(input_type) &&
881 Primitive::IsIntegralType(original_type) &&
882 Primitive::IsIntegralType(result_type) &&
883 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
884
885 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
886 // If the merged conversion is implicit, do the simplification unconditionally.
887 if (IsTypeConversionImplicit(original_type, result_type)) {
888 instruction->ReplaceWith(original_input);
889 instruction->GetBlock()->RemoveInstruction(instruction);
890 if (!input_conversion->HasUses()) {
891 // Don't wait for DCE.
892 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
893 }
894 RecordSimplification();
895 return;
896 }
897 // Otherwise simplify only if the first conversion has no other use.
898 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
899 input_conversion->ReplaceWith(original_input);
900 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
901 RecordSimplification();
902 return;
903 }
904 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000905 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000906 DCHECK(Primitive::IsIntegralType(input_type));
907 HAnd* input_and = input->AsAnd();
908 HConstant* constant = input_and->GetConstantRight();
909 if (constant != nullptr) {
910 int64_t value = Int64FromConstant(constant);
911 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
912 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
913 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
914 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000915 HInstruction* original_input = input_and->GetLeastConstantLeft();
916 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
917 instruction->ReplaceWith(original_input);
918 instruction->GetBlock()->RemoveInstruction(instruction);
919 RecordSimplification();
920 return;
921 } else if (input->HasOnlyOneNonEnvironmentUse()) {
922 input_and->ReplaceWith(original_input);
923 input_and->GetBlock()->RemoveInstruction(input_and);
924 RecordSimplification();
925 return;
926 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000927 }
928 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000929 }
930}
931
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000932void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
933 HConstant* input_cst = instruction->GetConstantRight();
934 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600935 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +0000936 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000937 // Replace code looking like
938 // ADD dst, src, 0
939 // with
940 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600941 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
942 // `x` is `-0.0`, the former expression yields `0.0`, while the later
943 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600944 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +0600945 instruction->ReplaceWith(input_other);
946 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100947 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +0600948 return;
949 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100950 }
951
952 HInstruction* left = instruction->GetLeft();
953 HInstruction* right = instruction->GetRight();
954 bool left_is_neg = left->IsNeg();
955 bool right_is_neg = right->IsNeg();
956
957 if (left_is_neg && right_is_neg) {
958 if (TryMoveNegOnInputsAfterBinop(instruction)) {
959 return;
960 }
961 }
962
963 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
964 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
965 // Replace code looking like
966 // NEG tmp, b
967 // ADD dst, a, tmp
968 // with
969 // SUB dst, a, b
970 // We do not perform the optimization if the input negation has environment
971 // uses or multiple non-environment uses as it could lead to worse code. In
972 // particular, we do not want the live range of `b` to be extended if we are
973 // not sure the initial 'NEG' instruction can be removed.
974 HInstruction* other = left_is_neg ? right : left;
975 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
976 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
977 RecordSimplification();
978 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000979 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000980 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000981
Anton Kirilove14dc862016-05-13 17:56:15 +0100982 if (TryReplaceWithRotate(instruction)) {
983 return;
984 }
985
986 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
987 // so no need to return.
988 TryHandleAssociativeAndCommutativeOperation(instruction);
989
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600990 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +0100991 TrySubtractionChainSimplification(instruction)) {
992 return;
993 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600994
995 if (integral_type) {
996 // Replace code patterns looking like
997 // SUB dst1, x, y SUB dst1, x, y
998 // ADD dst2, dst1, y ADD dst2, y, dst1
999 // with
1000 // SUB dst1, x, y
1001 // ADD instruction is not needed in this case, we may use
1002 // one of inputs of SUB instead.
1003 if (left->IsSub() && left->InputAt(1) == right) {
1004 instruction->ReplaceWith(left->InputAt(0));
1005 RecordSimplification();
1006 instruction->GetBlock()->RemoveInstruction(instruction);
1007 return;
1008 } else if (right->IsSub() && right->InputAt(1) == left) {
1009 instruction->ReplaceWith(right->InputAt(0));
1010 RecordSimplification();
1011 instruction->GetBlock()->RemoveInstruction(instruction);
1012 return;
1013 }
1014 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001015}
1016
1017void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1018 HConstant* input_cst = instruction->GetConstantRight();
1019 HInstruction* input_other = instruction->GetLeastConstantLeft();
1020
Vladimir Marko452c1b62015-09-25 14:44:17 +01001021 if (input_cst != nullptr) {
1022 int64_t value = Int64FromConstant(input_cst);
1023 if (value == -1) {
1024 // Replace code looking like
1025 // AND dst, src, 0xFFF...FF
1026 // with
1027 // src
1028 instruction->ReplaceWith(input_other);
1029 instruction->GetBlock()->RemoveInstruction(instruction);
1030 RecordSimplification();
1031 return;
1032 }
1033 // Eliminate And from UShr+And if the And-mask contains all the bits that
1034 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1035 // precisely clears the shifted-in sign bits.
1036 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1037 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1038 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1039 size_t num_tail_bits_set = CTZ(value + 1);
1040 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1041 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1042 instruction->ReplaceWith(input_other);
1043 instruction->GetBlock()->RemoveInstruction(instruction);
1044 RecordSimplification();
1045 return;
1046 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1047 input_other->HasOnlyOneNonEnvironmentUse()) {
1048 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1049 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1050 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1051 input_other->InputAt(0),
1052 input_other->InputAt(1),
1053 input_other->GetDexPc());
1054 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1055 input_other->GetBlock()->RemoveInstruction(input_other);
1056 RecordSimplification();
1057 return;
1058 }
1059 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001060 }
1061
1062 // We assume that GVN has run before, so we only perform a pointer comparison.
1063 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001064 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001065 if (instruction->GetLeft() == instruction->GetRight()) {
1066 // Replace code looking like
1067 // AND dst, src, src
1068 // with
1069 // src
1070 instruction->ReplaceWith(instruction->GetLeft());
1071 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001072 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001073 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001074 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001075
Anton Kirilove14dc862016-05-13 17:56:15 +01001076 if (TryDeMorganNegationFactoring(instruction)) {
1077 return;
1078 }
1079
1080 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1081 // so no need to return.
1082 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001083}
1084
Mark Mendellc4701932015-04-10 13:18:51 -04001085void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1086 VisitCondition(condition);
1087}
1088
1089void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1090 VisitCondition(condition);
1091}
1092
1093void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1094 VisitCondition(condition);
1095}
1096
1097void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1098 VisitCondition(condition);
1099}
1100
Anton Shaminbdd79352016-02-15 12:48:36 +06001101void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1102 VisitCondition(condition);
1103}
1104
1105void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1106 VisitCondition(condition);
1107}
1108
1109void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1110 VisitCondition(condition);
1111}
1112
1113void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1114 VisitCondition(condition);
1115}
Aart Bike9f37602015-10-09 11:15:55 -07001116
Mark Mendellc4701932015-04-10 13:18:51 -04001117void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001118 // Reverse condition if left is constant. Our code generators prefer constant
1119 // on the right hand side.
1120 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1121 HBasicBlock* block = condition->GetBlock();
1122 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1123 // If it is a fp we must set the opposite bias.
1124 if (replacement != nullptr) {
1125 if (condition->IsLtBias()) {
1126 replacement->SetBias(ComparisonBias::kGtBias);
1127 } else if (condition->IsGtBias()) {
1128 replacement->SetBias(ComparisonBias::kLtBias);
1129 }
1130 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1131 RecordSimplification();
1132
1133 condition = replacement;
1134 }
1135 }
Mark Mendellc4701932015-04-10 13:18:51 -04001136
Mark Mendellc4701932015-04-10 13:18:51 -04001137 HInstruction* left = condition->GetLeft();
1138 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001139
1140 // Try to fold an HCompare into this HCondition.
1141
Mark Mendellc4701932015-04-10 13:18:51 -04001142 // We can only replace an HCondition which compares a Compare to 0.
1143 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1144 // condition with a long, float or double comparison as input.
1145 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1146 // Conversion is not possible.
1147 return;
1148 }
1149
1150 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001151 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001152 // Someone else also wants the result of the compare.
1153 return;
1154 }
1155
Vladimir Marko46817b82016-03-29 12:21:58 +01001156 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001157 // There is a reference to the compare result in an environment. Do we really need it?
1158 if (GetGraph()->IsDebuggable()) {
1159 return;
1160 }
1161
1162 // We have to ensure that there are no deopt points in the sequence.
1163 if (left->HasAnyEnvironmentUseBefore(condition)) {
1164 return;
1165 }
1166 }
1167
1168 // Clean up any environment uses from the HCompare, if any.
1169 left->RemoveEnvironmentUsers();
1170
1171 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1172 condition->SetBias(left->AsCompare()->GetBias());
1173
1174 // Replace the operands of the HCondition.
1175 condition->ReplaceInput(left->InputAt(0), 0);
1176 condition->ReplaceInput(left->InputAt(1), 1);
1177
1178 // Remove the HCompare.
1179 left->GetBlock()->RemoveInstruction(left);
1180
1181 RecordSimplification();
1182}
1183
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001184void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1185 HConstant* input_cst = instruction->GetConstantRight();
1186 HInstruction* input_other = instruction->GetLeastConstantLeft();
1187 Primitive::Type type = instruction->GetType();
1188
1189 if ((input_cst != nullptr) && input_cst->IsOne()) {
1190 // Replace code looking like
1191 // DIV dst, src, 1
1192 // with
1193 // src
1194 instruction->ReplaceWith(input_other);
1195 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001196 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001197 return;
1198 }
1199
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001200 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001201 // Replace code looking like
1202 // DIV dst, src, -1
1203 // with
1204 // NEG dst, src
1205 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001206 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001207 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001208 return;
1209 }
1210
1211 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1212 // Try replacing code looking like
1213 // DIV dst, src, constant
1214 // with
1215 // MUL dst, src, 1 / constant
1216 HConstant* reciprocal = nullptr;
1217 if (type == Primitive::Primitive::kPrimDouble) {
1218 double value = input_cst->AsDoubleConstant()->GetValue();
1219 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1220 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1221 }
1222 } else {
1223 DCHECK_EQ(type, Primitive::kPrimFloat);
1224 float value = input_cst->AsFloatConstant()->GetValue();
1225 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1226 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1227 }
1228 }
1229
1230 if (reciprocal != nullptr) {
1231 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1232 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1233 RecordSimplification();
1234 return;
1235 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001236 }
1237}
1238
1239void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1240 HConstant* input_cst = instruction->GetConstantRight();
1241 HInstruction* input_other = instruction->GetLeastConstantLeft();
1242 Primitive::Type type = instruction->GetType();
1243 HBasicBlock* block = instruction->GetBlock();
1244 ArenaAllocator* allocator = GetGraph()->GetArena();
1245
1246 if (input_cst == nullptr) {
1247 return;
1248 }
1249
1250 if (input_cst->IsOne()) {
1251 // Replace code looking like
1252 // MUL dst, src, 1
1253 // with
1254 // src
1255 instruction->ReplaceWith(input_other);
1256 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001257 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001258 return;
1259 }
1260
1261 if (input_cst->IsMinusOne() &&
1262 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1263 // Replace code looking like
1264 // MUL dst, src, -1
1265 // with
1266 // NEG dst, src
1267 HNeg* neg = new (allocator) HNeg(type, input_other);
1268 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001269 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001270 return;
1271 }
1272
1273 if (Primitive::IsFloatingPointType(type) &&
1274 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1275 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1276 // Replace code looking like
1277 // FP_MUL dst, src, 2.0
1278 // with
1279 // FP_ADD dst, src, src
1280 // The 'int' and 'long' cases are handled below.
1281 block->ReplaceAndRemoveInstructionWith(instruction,
1282 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001283 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001284 return;
1285 }
1286
1287 if (Primitive::IsIntOrLongType(type)) {
1288 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001289 // Even though constant propagation also takes care of the zero case, other
1290 // optimizations can lead to having a zero multiplication.
1291 if (factor == 0) {
1292 // Replace code looking like
1293 // MUL dst, src, 0
1294 // with
1295 // 0
1296 instruction->ReplaceWith(input_cst);
1297 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001298 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001299 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001300 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001301 // Replace code looking like
1302 // MUL dst, src, pow_of_2
1303 // with
1304 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001305 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001306 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001307 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001308 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001309 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001310 } else if (IsPowerOfTwo(factor - 1)) {
1311 // Transform code looking like
1312 // MUL dst, src, (2^n + 1)
1313 // into
1314 // SHL tmp, src, n
1315 // ADD dst, src, tmp
1316 HShl* shl = new (allocator) HShl(type,
1317 input_other,
1318 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1319 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1320
1321 block->InsertInstructionBefore(shl, instruction);
1322 block->ReplaceAndRemoveInstructionWith(instruction, add);
1323 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001324 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001325 } else if (IsPowerOfTwo(factor + 1)) {
1326 // Transform code looking like
1327 // MUL dst, src, (2^n - 1)
1328 // into
1329 // SHL tmp, src, n
1330 // SUB dst, tmp, src
1331 HShl* shl = new (allocator) HShl(type,
1332 input_other,
1333 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1334 HSub* sub = new (allocator) HSub(type, shl, input_other);
1335
1336 block->InsertInstructionBefore(shl, instruction);
1337 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1338 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001339 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001340 }
1341 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001342
1343 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1344 // so no need to return.
1345 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001346}
1347
Alexandre Rames188d4312015-04-09 18:30:21 +01001348void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1349 HInstruction* input = instruction->GetInput();
1350 if (input->IsNeg()) {
1351 // Replace code looking like
1352 // NEG tmp, src
1353 // NEG dst, tmp
1354 // with
1355 // src
1356 HNeg* previous_neg = input->AsNeg();
1357 instruction->ReplaceWith(previous_neg->GetInput());
1358 instruction->GetBlock()->RemoveInstruction(instruction);
1359 // We perform the optimization even if the input negation has environment
1360 // uses since it allows removing the current instruction. But we only delete
1361 // the input negation only if it is does not have any uses left.
1362 if (!previous_neg->HasUses()) {
1363 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1364 }
1365 RecordSimplification();
1366 return;
1367 }
1368
Serguei Katkov339dfc22015-04-20 12:29:32 +06001369 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1370 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001371 // Replace code looking like
1372 // SUB tmp, a, b
1373 // NEG dst, tmp
1374 // with
1375 // SUB dst, b, a
1376 // We do not perform the optimization if the input subtraction has
1377 // environment uses or multiple non-environment uses as it could lead to
1378 // worse code. In particular, we do not want the live ranges of `a` and `b`
1379 // to be extended if we are not sure the initial 'SUB' instruction can be
1380 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001381 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001382 HSub* sub = input->AsSub();
1383 HSub* new_sub =
1384 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1385 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1386 if (!sub->HasUses()) {
1387 sub->GetBlock()->RemoveInstruction(sub);
1388 }
1389 RecordSimplification();
1390 }
1391}
1392
1393void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1394 HInstruction* input = instruction->GetInput();
1395 if (input->IsNot()) {
1396 // Replace code looking like
1397 // NOT tmp, src
1398 // NOT dst, tmp
1399 // with
1400 // src
1401 // We perform the optimization even if the input negation has environment
1402 // uses since it allows removing the current instruction. But we only delete
1403 // the input negation only if it is does not have any uses left.
1404 HNot* previous_not = input->AsNot();
1405 instruction->ReplaceWith(previous_not->GetInput());
1406 instruction->GetBlock()->RemoveInstruction(instruction);
1407 if (!previous_not->HasUses()) {
1408 previous_not->GetBlock()->RemoveInstruction(previous_not);
1409 }
1410 RecordSimplification();
1411 }
1412}
1413
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001414void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1415 HConstant* input_cst = instruction->GetConstantRight();
1416 HInstruction* input_other = instruction->GetLeastConstantLeft();
1417
Roland Levillain1a653882016-03-18 18:05:57 +00001418 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001419 // Replace code looking like
1420 // OR dst, src, 0
1421 // with
1422 // src
1423 instruction->ReplaceWith(input_other);
1424 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001425 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001426 return;
1427 }
1428
1429 // We assume that GVN has run before, so we only perform a pointer comparison.
1430 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001431 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001432 if (instruction->GetLeft() == instruction->GetRight()) {
1433 // Replace code looking like
1434 // OR dst, src, src
1435 // with
1436 // src
1437 instruction->ReplaceWith(instruction->GetLeft());
1438 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001439 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001440 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001441 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001442
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001443 if (TryDeMorganNegationFactoring(instruction)) return;
1444
Anton Kirilove14dc862016-05-13 17:56:15 +01001445 if (TryReplaceWithRotate(instruction)) {
1446 return;
1447 }
1448
1449 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1450 // so no need to return.
1451 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001452}
1453
1454void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1455 VisitShift(instruction);
1456}
1457
1458void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1459 VisitShift(instruction);
1460}
1461
1462void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1463 HConstant* input_cst = instruction->GetConstantRight();
1464 HInstruction* input_other = instruction->GetLeastConstantLeft();
1465
Serguei Katkov115b53f2015-08-05 17:03:30 +06001466 Primitive::Type type = instruction->GetType();
1467 if (Primitive::IsFloatingPointType(type)) {
1468 return;
1469 }
1470
Roland Levillain1a653882016-03-18 18:05:57 +00001471 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001472 // Replace code looking like
1473 // SUB dst, src, 0
1474 // with
1475 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001476 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1477 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1478 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001479 instruction->ReplaceWith(input_other);
1480 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001481 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001482 return;
1483 }
1484
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001485 HBasicBlock* block = instruction->GetBlock();
1486 ArenaAllocator* allocator = GetGraph()->GetArena();
1487
Alexandre Rames188d4312015-04-09 18:30:21 +01001488 HInstruction* left = instruction->GetLeft();
1489 HInstruction* right = instruction->GetRight();
1490 if (left->IsConstant()) {
1491 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001492 // Replace code looking like
1493 // SUB dst, 0, src
1494 // with
1495 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001496 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001497 // `x` is `0.0`, the former expression yields `0.0`, while the later
1498 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001499 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001500 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001501 RecordSimplification();
1502 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001503 }
1504 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001505
1506 if (left->IsNeg() && right->IsNeg()) {
1507 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1508 return;
1509 }
1510 }
1511
1512 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1513 // Replace code looking like
1514 // NEG tmp, b
1515 // SUB dst, a, tmp
1516 // with
1517 // ADD dst, a, b
1518 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1519 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1520 RecordSimplification();
1521 right->GetBlock()->RemoveInstruction(right);
1522 return;
1523 }
1524
1525 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1526 // Replace code looking like
1527 // NEG tmp, a
1528 // SUB dst, tmp, b
1529 // with
1530 // ADD tmp, a, b
1531 // NEG dst, tmp
1532 // The second version is not intrinsically better, but enables more
1533 // transformations.
1534 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1535 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1536 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1537 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1538 instruction->ReplaceWith(neg);
1539 instruction->GetBlock()->RemoveInstruction(instruction);
1540 RecordSimplification();
1541 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001542 return;
1543 }
1544
1545 if (TrySubtractionChainSimplification(instruction)) {
1546 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001547 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001548
1549 if (left->IsAdd()) {
1550 // Replace code patterns looking like
1551 // ADD dst1, x, y ADD dst1, x, y
1552 // SUB dst2, dst1, y SUB dst2, dst1, x
1553 // with
1554 // ADD dst1, x, y
1555 // SUB instruction is not needed in this case, we may use
1556 // one of inputs of ADD instead.
1557 // It is applicable to integral types only.
1558 DCHECK(Primitive::IsIntegralType(type));
1559 if (left->InputAt(1) == right) {
1560 instruction->ReplaceWith(left->InputAt(0));
1561 RecordSimplification();
1562 instruction->GetBlock()->RemoveInstruction(instruction);
1563 return;
1564 } else if (left->InputAt(0) == right) {
1565 instruction->ReplaceWith(left->InputAt(1));
1566 RecordSimplification();
1567 instruction->GetBlock()->RemoveInstruction(instruction);
1568 return;
1569 }
1570 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001571}
1572
1573void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1574 VisitShift(instruction);
1575}
1576
1577void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1578 HConstant* input_cst = instruction->GetConstantRight();
1579 HInstruction* input_other = instruction->GetLeastConstantLeft();
1580
Roland Levillain1a653882016-03-18 18:05:57 +00001581 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001582 // Replace code looking like
1583 // XOR dst, src, 0
1584 // with
1585 // src
1586 instruction->ReplaceWith(input_other);
1587 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001588 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001589 return;
1590 }
1591
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001592 if ((input_cst != nullptr) && input_cst->IsOne()
1593 && input_other->GetType() == Primitive::kPrimBoolean) {
1594 // Replace code looking like
1595 // XOR dst, src, 1
1596 // with
1597 // BOOLEAN_NOT dst, src
1598 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1599 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1600 RecordSimplification();
1601 return;
1602 }
1603
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001604 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1605 // Replace code looking like
1606 // XOR dst, src, 0xFFF...FF
1607 // with
1608 // NOT dst, src
1609 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1610 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001611 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001612 return;
1613 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001614
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001615 HInstruction* left = instruction->GetLeft();
1616 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001617 if (((left->IsNot() && right->IsNot()) ||
1618 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001619 left->HasOnlyOneNonEnvironmentUse() &&
1620 right->HasOnlyOneNonEnvironmentUse()) {
1621 // Replace code looking like
1622 // NOT nota, a
1623 // NOT notb, b
1624 // XOR dst, nota, notb
1625 // with
1626 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001627 instruction->ReplaceInput(left->InputAt(0), 0);
1628 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001629 left->GetBlock()->RemoveInstruction(left);
1630 right->GetBlock()->RemoveInstruction(right);
1631 RecordSimplification();
1632 return;
1633 }
1634
Anton Kirilove14dc862016-05-13 17:56:15 +01001635 if (TryReplaceWithRotate(instruction)) {
1636 return;
1637 }
1638
1639 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1640 // so no need to return.
1641 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001642}
1643
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001644void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1645 HInstruction* argument = instruction->InputAt(1);
1646 HInstruction* receiver = instruction->InputAt(0);
1647 if (receiver == argument) {
1648 // Because String.equals is an instance call, the receiver is
1649 // a null check if we don't know it's null. The argument however, will
1650 // be the actual object. So we cannot end up in a situation where both
1651 // are equal but could be null.
1652 DCHECK(CanEnsureNotNullAt(argument, instruction));
1653 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1654 instruction->GetBlock()->RemoveInstruction(instruction);
1655 } else {
1656 StringEqualsOptimizations optimizations(instruction);
1657 if (CanEnsureNotNullAt(argument, instruction)) {
1658 optimizations.SetArgumentNotNull();
1659 }
1660 ScopedObjectAccess soa(Thread::Current());
1661 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1662 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1663 optimizations.SetArgumentIsString();
1664 }
1665 }
1666}
1667
Roland Levillain22c49222016-03-18 14:04:28 +00001668void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1669 bool is_left,
1670 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001671 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001672 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001673 HInstruction* value = invoke->InputAt(0);
1674 HInstruction* distance = invoke->InputAt(1);
1675 // Replace the invoke with an HRor.
1676 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001677 // Unconditionally set the type of the negated distance to `int`,
1678 // as shift and rotate operations expect a 32-bit (or narrower)
1679 // value for their distance input.
1680 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001681 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1682 }
Roland Levillain22c49222016-03-18 14:04:28 +00001683 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001684 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1685 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001686 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001687 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1688 clinit->GetBlock()->RemoveInstruction(clinit);
1689 HInstruction* ldclass = clinit->InputAt(0);
1690 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1691 ldclass->GetBlock()->RemoveInstruction(ldclass);
1692 }
1693 }
1694}
1695
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001696static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1697 if (potential_length->IsArrayLength()) {
1698 return potential_length->InputAt(0) == potential_array;
1699 }
1700
1701 if (potential_array->IsNewArray()) {
1702 return potential_array->InputAt(0) == potential_length;
1703 }
1704
1705 return false;
1706}
1707
1708void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1709 HInstruction* source = instruction->InputAt(0);
1710 HInstruction* destination = instruction->InputAt(2);
1711 HInstruction* count = instruction->InputAt(4);
1712 SystemArrayCopyOptimizations optimizations(instruction);
1713 if (CanEnsureNotNullAt(source, instruction)) {
1714 optimizations.SetSourceIsNotNull();
1715 }
1716 if (CanEnsureNotNullAt(destination, instruction)) {
1717 optimizations.SetDestinationIsNotNull();
1718 }
1719 if (destination == source) {
1720 optimizations.SetDestinationIsSource();
1721 }
1722
1723 if (IsArrayLengthOf(count, source)) {
1724 optimizations.SetCountIsSourceLength();
1725 }
1726
1727 if (IsArrayLengthOf(count, destination)) {
1728 optimizations.SetCountIsDestinationLength();
1729 }
1730
1731 {
1732 ScopedObjectAccess soa(Thread::Current());
1733 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1734 if (destination_rti.IsValid()) {
1735 if (destination_rti.IsObjectArray()) {
1736 if (destination_rti.IsExact()) {
1737 optimizations.SetDoesNotNeedTypeCheck();
1738 }
1739 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001740 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001741 if (destination_rti.IsPrimitiveArrayClass()) {
1742 optimizations.SetDestinationIsPrimitiveArray();
1743 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1744 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001745 }
1746 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001747 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1748 if (source_rti.IsValid()) {
1749 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1750 optimizations.SetDoesNotNeedTypeCheck();
1751 }
1752 if (source_rti.IsPrimitiveArrayClass()) {
1753 optimizations.SetSourceIsPrimitiveArray();
1754 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1755 optimizations.SetSourceIsNonPrimitiveArray();
1756 }
1757 }
1758 }
1759}
1760
Roland Levillaina5c4a402016-03-15 15:02:50 +00001761void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1762 bool is_signum,
1763 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001764 DCHECK(invoke->IsInvokeStaticOrDirect());
1765 uint32_t dex_pc = invoke->GetDexPc();
1766 HInstruction* left = invoke->InputAt(0);
1767 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001768 if (!is_signum) {
1769 right = invoke->InputAt(1);
1770 } else if (type == Primitive::kPrimLong) {
1771 right = GetGraph()->GetLongConstant(0);
1772 } else {
1773 right = GetGraph()->GetIntConstant(0);
1774 }
1775 HCompare* compare = new (GetGraph()->GetArena())
1776 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1777 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1778}
1779
Aart Bik75a38b22016-02-17 10:41:50 -08001780void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1781 DCHECK(invoke->IsInvokeStaticOrDirect());
1782 uint32_t dex_pc = invoke->GetDexPc();
1783 // IsNaN(x) is the same as x != x.
1784 HInstruction* x = invoke->InputAt(0);
1785 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001786 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001787 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1788}
1789
Aart Bik2a6aad92016-02-25 11:32:32 -08001790void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1791 DCHECK(invoke->IsInvokeStaticOrDirect());
1792 uint32_t dex_pc = invoke->GetDexPc();
1793 HInstruction* x = invoke->InputAt(0);
1794 Primitive::Type type = x->GetType();
1795 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1796 HInstruction* nan;
1797 if (type == Primitive::kPrimDouble) {
1798 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1799 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1800 kNeedsEnvironmentOrCache,
1801 kNoSideEffects,
1802 kNoThrow);
1803 } else {
1804 DCHECK_EQ(type, Primitive::kPrimFloat);
1805 nan = GetGraph()->GetIntConstant(0x7fc00000);
1806 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1807 kNeedsEnvironmentOrCache,
1808 kNoSideEffects,
1809 kNoThrow);
1810 }
1811 // Test IsNaN(x), which is the same as x != x.
1812 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1813 condition->SetBias(ComparisonBias::kLtBias);
1814 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1815 // Select between the two.
1816 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1817 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1818 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1819}
1820
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001821void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1822 HInstruction* str = invoke->InputAt(0);
1823 HInstruction* index = invoke->InputAt(1);
1824 uint32_t dex_pc = invoke->GetDexPc();
1825 ArenaAllocator* arena = GetGraph()->GetArena();
1826 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1827 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1828 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1829 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1830 HBoundsCheck* bounds_check =
1831 new (arena) HBoundsCheck(index, length, dex_pc, invoke->GetDexMethodIndex());
1832 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
1833 HArrayGet* array_get =
1834 new (arena) HArrayGet(str, index, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
1835 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1836 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1837 GetGraph()->SetHasBoundsChecks(true);
1838}
1839
Vladimir Markodce016e2016-04-28 13:10:02 +01001840void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1841 HInstruction* str = invoke->InputAt(0);
1842 uint32_t dex_pc = invoke->GetDexPc();
1843 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1844 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001845 HArrayLength* length =
1846 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001847 HInstruction* replacement;
1848 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1849 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1850 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1851 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1852 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1853 replacement = equal;
1854 } else {
1855 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1856 replacement = length;
1857 }
1858 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
1859}
1860
Aart Bik11932592016-03-08 12:42:25 -08001861void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1862 uint32_t dex_pc = invoke->GetDexPc();
1863 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1864 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1865}
1866
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001867void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001868 switch (instruction->GetIntrinsic()) {
1869 case Intrinsics::kStringEquals:
1870 SimplifyStringEquals(instruction);
1871 break;
1872 case Intrinsics::kSystemArrayCopy:
1873 SimplifySystemArrayCopy(instruction);
1874 break;
1875 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001876 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1877 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001878 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001879 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001880 break;
1881 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001882 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1883 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001884 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001885 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001886 break;
1887 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001888 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1889 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001890 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001891 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001892 break;
1893 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001894 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1895 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001896 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001897 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001898 break;
1899 case Intrinsics::kFloatIsNaN:
1900 case Intrinsics::kDoubleIsNaN:
1901 SimplifyIsNaN(instruction);
1902 break;
1903 case Intrinsics::kFloatFloatToIntBits:
1904 case Intrinsics::kDoubleDoubleToLongBits:
1905 SimplifyFP2Int(instruction);
1906 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001907 case Intrinsics::kStringCharAt:
1908 SimplifyStringCharAt(instruction);
1909 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01001910 case Intrinsics::kStringIsEmpty:
1911 case Intrinsics::kStringLength:
1912 SimplifyStringIsEmptyOrLength(instruction);
1913 break;
Aart Bik11932592016-03-08 12:42:25 -08001914 case Intrinsics::kUnsafeLoadFence:
1915 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1916 break;
1917 case Intrinsics::kUnsafeStoreFence:
1918 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1919 break;
1920 case Intrinsics::kUnsafeFullFence:
1921 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1922 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001923 default:
1924 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001925 }
1926}
1927
Aart Bikbb245d12015-10-19 11:05:03 -07001928void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1929 HInstruction* cond = deoptimize->InputAt(0);
1930 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001931 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001932 // Never deopt: instruction can be removed.
1933 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1934 } else {
1935 // Always deopt.
1936 }
1937 }
1938}
1939
Anton Kirilove14dc862016-05-13 17:56:15 +01001940// Replace code looking like
1941// OP y, x, const1
1942// OP z, y, const2
1943// with
1944// OP z, x, const3
1945// where OP is both an associative and a commutative operation.
1946bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
1947 HBinaryOperation* instruction) {
1948 DCHECK(instruction->IsCommutative());
1949
1950 if (!Primitive::IsIntegralType(instruction->GetType())) {
1951 return false;
1952 }
1953
1954 HInstruction* left = instruction->GetLeft();
1955 HInstruction* right = instruction->GetRight();
1956 // Variable names as described above.
1957 HConstant* const2;
1958 HBinaryOperation* y;
1959
1960 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
1961 const2 = right->AsConstant();
1962 y = left->AsBinaryOperation();
1963 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
1964 const2 = left->AsConstant();
1965 y = right->AsBinaryOperation();
1966 } else {
1967 // The node does not match the pattern.
1968 return false;
1969 }
1970
1971 // If `y` has more than one use, we do not perform the optimization
1972 // because it might increase code size (e.g. if the new constant is
1973 // no longer encodable as an immediate operand in the target ISA).
1974 if (!y->HasOnlyOneNonEnvironmentUse()) {
1975 return false;
1976 }
1977
1978 // GetConstantRight() can return both left and right constants
1979 // for commutative operations.
1980 HConstant* const1 = y->GetConstantRight();
1981 if (const1 == nullptr) {
1982 return false;
1983 }
1984
1985 instruction->ReplaceInput(const1, 0);
1986 instruction->ReplaceInput(const2, 1);
1987 HConstant* const3 = instruction->TryStaticEvaluation();
1988 DCHECK(const3 != nullptr);
1989 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
1990 instruction->ReplaceInput(const3, 1);
1991 RecordSimplification();
1992 return true;
1993}
1994
1995static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
1996 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
1997}
1998
1999// Helper function that performs addition statically, considering the result type.
2000static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
2001 // Use the Compute() method for consistency with TryStaticEvaluation().
2002 if (type == Primitive::kPrimInt) {
2003 return HAdd::Compute<int32_t>(x, y);
2004 } else {
2005 DCHECK_EQ(type, Primitive::kPrimLong);
2006 return HAdd::Compute<int64_t>(x, y);
2007 }
2008}
2009
2010// Helper function that handles the child classes of HConstant
2011// and returns an integer with the appropriate sign.
2012static int64_t GetValue(HConstant* constant, bool is_negated) {
2013 int64_t ret = Int64FromConstant(constant);
2014 return is_negated ? -ret : ret;
2015}
2016
2017// Replace code looking like
2018// OP1 y, x, const1
2019// OP2 z, y, const2
2020// with
2021// OP3 z, x, const3
2022// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2023bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2024 HBinaryOperation* instruction) {
2025 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2026
2027 Primitive::Type type = instruction->GetType();
2028 if (!Primitive::IsIntegralType(type)) {
2029 return false;
2030 }
2031
2032 HInstruction* left = instruction->GetLeft();
2033 HInstruction* right = instruction->GetRight();
2034 // Variable names as described above.
2035 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2036 if (const2 == nullptr) {
2037 return false;
2038 }
2039
2040 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2041 ? left->AsBinaryOperation()
2042 : AsAddOrSub(right);
2043 // If y has more than one use, we do not perform the optimization because
2044 // it might increase code size (e.g. if the new constant is no longer
2045 // encodable as an immediate operand in the target ISA).
2046 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2047 return false;
2048 }
2049
2050 left = y->GetLeft();
2051 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2052 if (const1 == nullptr) {
2053 return false;
2054 }
2055
2056 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2057 // If both inputs are constants, let the constant folding pass deal with it.
2058 if (x->IsConstant()) {
2059 return false;
2060 }
2061
2062 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2063 int64_t const2_val = GetValue(const2, is_const2_negated);
2064 bool is_y_negated = (y == right) && instruction->IsSub();
2065 right = y->GetRight();
2066 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2067 int64_t const1_val = GetValue(const1, is_const1_negated);
2068 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2069 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2070 HBasicBlock* block = instruction->GetBlock();
2071 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2072 ArenaAllocator* arena = instruction->GetArena();
2073 HInstruction* z;
2074
2075 if (is_x_negated) {
2076 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2077 } else {
2078 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2079 }
2080
2081 block->ReplaceAndRemoveInstructionWith(instruction, z);
2082 RecordSimplification();
2083 return true;
2084}
2085
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002086} // namespace art