blob: 011983fb70633bcf9a97aa7ccf85a7d5b43fce2f [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_simplifier.h"
18
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010019#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
Nicolas Geoffray3c049742014-09-24 18:10:46 +010023namespace art {
24
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010025class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000026 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000027 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010028 : HGraphDelegateVisitor(graph),
Alexandre Rames188d4312015-04-09 18:30:21 +010029 stats_(stats) {}
30
31 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000032
33 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010034 void RecordSimplification() {
35 simplification_occurred_ = true;
36 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000037 MaybeRecordStat(kInstructionSimplifications);
38 }
39
40 void MaybeRecordStat(MethodCompilationStat stat) {
41 if (stats_ != nullptr) {
42 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010043 }
44 }
45
Scott Wakeling40a04bf2015-12-11 09:50:36 +000046 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
47 bool TryReplaceWithRotate(HBinaryOperation* instruction);
48 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
49 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
50 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
51
Alexandre Rames188d4312015-04-09 18:30:21 +010052 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000053 // `op` should be either HOr or HAnd.
54 // De Morgan's laws:
55 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
56 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000057 void VisitShift(HBinaryOperation* shift);
58
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000059 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010060 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
61 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010062 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
63 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000064 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000065 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000066 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080067 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000068 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000069 void VisitAdd(HAdd* instruction) OVERRIDE;
70 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040071 void VisitCondition(HCondition* instruction) OVERRIDE;
72 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
73 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
74 void VisitLessThan(HLessThan* condition) OVERRIDE;
75 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060076 void VisitBelow(HBelow* condition) OVERRIDE;
77 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
78 void VisitAbove(HAbove* condition) OVERRIDE;
79 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000080 void VisitDiv(HDiv* instruction) OVERRIDE;
81 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010082 void VisitNeg(HNeg* instruction) OVERRIDE;
83 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000084 void VisitOr(HOr* instruction) OVERRIDE;
85 void VisitShl(HShl* instruction) OVERRIDE;
86 void VisitShr(HShr* instruction) OVERRIDE;
87 void VisitSub(HSub* instruction) OVERRIDE;
88 void VisitUShr(HUShr* instruction) OVERRIDE;
89 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000090 void VisitSelect(HSelect* select) OVERRIDE;
91 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010092 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010093 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070094 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010095
96 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +000097
Roland Levillain22c49222016-03-18 14:04:28 +000098 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010099 void SimplifySystemArrayCopy(HInvoke* invoke);
100 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000101 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800102 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800103 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100104 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800105 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100106
Calin Juravleacf735c2015-02-12 15:25:22 +0000107 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100108 bool simplification_occurred_ = false;
109 int simplifications_at_current_position_ = 0;
110 // We ensure we do not loop infinitely. The value is a finger in the air guess
111 // that should allow enough simplification.
112 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000113};
114
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100115void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000116 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100117 visitor.Run();
118}
119
120void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100121 // Iterate in reverse post order to open up more simplifications to users
122 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100123 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
124 // The simplification of an instruction to another instruction may yield
125 // possibilities for other simplifications. So although we perform a reverse
126 // post order visit, we sometimes need to revisit an instruction index.
127 simplification_occurred_ = false;
128 VisitBasicBlock(it.Current());
129 if (simplification_occurred_ &&
130 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
131 // New simplifications may be applicable to the instruction at the
132 // current index, so don't advance the iterator.
133 continue;
134 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100135 simplifications_at_current_position_ = 0;
136 it.Advance();
137 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100138}
139
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000140namespace {
141
142bool AreAllBitsSet(HConstant* constant) {
143 return Int64FromConstant(constant) == -1;
144}
145
146} // namespace
147
Alexandre Rames188d4312015-04-09 18:30:21 +0100148// Returns true if the code was simplified to use only one negation operation
149// after the binary operation instead of one on each of the inputs.
150bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
151 DCHECK(binop->IsAdd() || binop->IsSub());
152 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
153 HNeg* left_neg = binop->GetLeft()->AsNeg();
154 HNeg* right_neg = binop->GetRight()->AsNeg();
155 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
156 !right_neg->HasOnlyOneNonEnvironmentUse()) {
157 return false;
158 }
159 // Replace code looking like
160 // NEG tmp1, a
161 // NEG tmp2, b
162 // ADD dst, tmp1, tmp2
163 // with
164 // ADD tmp, a, b
165 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600166 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
167 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
168 // while the later yields `-0.0`.
169 if (!Primitive::IsIntegralType(binop->GetType())) {
170 return false;
171 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100172 binop->ReplaceInput(left_neg->GetInput(), 0);
173 binop->ReplaceInput(right_neg->GetInput(), 1);
174 left_neg->GetBlock()->RemoveInstruction(left_neg);
175 right_neg->GetBlock()->RemoveInstruction(right_neg);
176 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
177 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
178 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
179 RecordSimplification();
180 return true;
181}
182
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000183bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
184 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
185 Primitive::Type type = op->GetType();
186 HInstruction* left = op->GetLeft();
187 HInstruction* right = op->GetRight();
188
189 // We can apply De Morgan's laws if both inputs are Not's and are only used
190 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000191 if (((left->IsNot() && right->IsNot()) ||
192 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000193 left->HasOnlyOneNonEnvironmentUse() &&
194 right->HasOnlyOneNonEnvironmentUse()) {
195 // Replace code looking like
196 // NOT nota, a
197 // NOT notb, b
198 // AND dst, nota, notb (respectively OR)
199 // with
200 // OR or, a, b (respectively AND)
201 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000202 HInstruction* src_left = left->InputAt(0);
203 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000204 uint32_t dex_pc = op->GetDexPc();
205
206 // Remove the negations on the inputs.
207 left->ReplaceWith(src_left);
208 right->ReplaceWith(src_right);
209 left->GetBlock()->RemoveInstruction(left);
210 right->GetBlock()->RemoveInstruction(right);
211
212 // Replace the `HAnd` or `HOr`.
213 HBinaryOperation* hbin;
214 if (op->IsAnd()) {
215 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
216 } else {
217 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
218 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000219 HInstruction* hnot;
220 if (left->IsBooleanNot()) {
221 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
222 } else {
223 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
224 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000225
226 op->GetBlock()->InsertInstructionBefore(hbin, op);
227 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
228
229 RecordSimplification();
230 return true;
231 }
232
233 return false;
234}
235
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000236void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
237 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
238 HConstant* input_cst = instruction->GetConstantRight();
239 HInstruction* input_other = instruction->GetLeastConstantLeft();
240
Mark Mendellba56d062015-05-05 21:34:03 -0400241 if (input_cst != nullptr) {
Vladimir Marko164306e2016-03-15 14:57:32 +0000242 int64_t cst = Int64FromConstant(input_cst);
Roland Levillain5b5b9312016-03-22 14:57:31 +0000243 int64_t mask = (input_other->GetType() == Primitive::kPrimLong)
244 ? kMaxLongShiftDistance
245 : kMaxIntShiftDistance;
Vladimir Marko164306e2016-03-15 14:57:32 +0000246 if ((cst & mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400247 // Replace code looking like
248 // SHL dst, src, 0
249 // with
250 // src
251 instruction->ReplaceWith(input_other);
252 instruction->GetBlock()->RemoveInstruction(instruction);
Mark Mendellba56d062015-05-05 21:34:03 -0400253 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000254 }
255}
256
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000257static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
258 return (sub->GetRight() == other &&
259 sub->GetLeft()->IsConstant() &&
260 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
261}
262
263bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
264 HUShr* ushr,
265 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000266 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
267 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000268 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
269 if (!ushr->HasUses()) {
270 ushr->GetBlock()->RemoveInstruction(ushr);
271 }
272 if (!ushr->GetRight()->HasUses()) {
273 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
274 }
275 if (!shl->HasUses()) {
276 shl->GetBlock()->RemoveInstruction(shl);
277 }
278 if (!shl->GetRight()->HasUses()) {
279 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
280 }
281 return true;
282}
283
284// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
285bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000286 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
287 HInstruction* left = op->GetLeft();
288 HInstruction* right = op->GetRight();
289 // If we have an UShr and a Shl (in either order).
290 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
291 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
292 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
293 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
294 if (ushr->GetType() == shl->GetType() &&
295 ushr->GetLeft() == shl->GetLeft()) {
296 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
297 // Shift distances are both constant, try replacing with Ror if they
298 // add up to the register size.
299 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
300 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
301 // Shift distances are potentially of the form x and (reg_size - x).
302 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
303 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
304 // Shift distances are potentially of the form d and -d.
305 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
306 }
307 }
308 }
309 return false;
310}
311
312// Try replacing code looking like (x >>> #rdist OP x << #ldist):
313// UShr dst, x, #rdist
314// Shl tmp, x, #ldist
315// OP dst, dst, tmp
316// or like (x >>> #rdist OP x << #-ldist):
317// UShr dst, x, #rdist
318// Shl tmp, x, #-ldist
319// OP dst, dst, tmp
320// with
321// Ror dst, x, #rdist
322bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
323 HUShr* ushr,
324 HShl* shl) {
325 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
326 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
327 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
328 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
329 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
330 ReplaceRotateWithRor(op, ushr, shl);
331 return true;
332 }
333 return false;
334}
335
336// Replace code looking like (x >>> -d OP x << d):
337// Neg neg, d
338// UShr dst, x, neg
339// Shl tmp, x, d
340// OP dst, dst, tmp
341// with
342// Neg neg, d
343// Ror dst, x, neg
344// *** OR ***
345// Replace code looking like (x >>> d OP x << -d):
346// UShr dst, x, d
347// Neg neg, d
348// Shl tmp, x, neg
349// OP dst, dst, tmp
350// with
351// Ror dst, x, d
352bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
353 HUShr* ushr,
354 HShl* shl) {
355 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
356 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
357 bool neg_is_left = shl->GetRight()->IsNeg();
358 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
359 // And the shift distance being negated is the distance being shifted the other way.
360 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
361 ReplaceRotateWithRor(op, ushr, shl);
362 }
363 return false;
364}
365
366// Try replacing code looking like (x >>> d OP x << (#bits - d)):
367// UShr dst, x, d
368// Sub ld, #bits, d
369// Shl tmp, x, ld
370// OP dst, dst, tmp
371// with
372// Ror dst, x, d
373// *** OR ***
374// Replace code looking like (x >>> (#bits - d) OP x << d):
375// Sub rd, #bits, d
376// UShr dst, x, rd
377// Shl tmp, x, d
378// OP dst, dst, tmp
379// with
380// Neg neg, d
381// Ror dst, x, neg
382bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
383 HUShr* ushr,
384 HShl* shl) {
385 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
386 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
387 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
388 HInstruction* shl_shift = shl->GetRight();
389 HInstruction* ushr_shift = ushr->GetRight();
390 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
391 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
392 return ReplaceRotateWithRor(op, ushr, shl);
393 }
394 return false;
395}
396
Calin Juravle10e244f2015-01-26 18:54:32 +0000397void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
398 HInstruction* obj = null_check->InputAt(0);
399 if (!obj->CanBeNull()) {
400 null_check->ReplaceWith(obj);
401 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000402 if (stats_ != nullptr) {
403 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
404 }
405 }
406}
407
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100408bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
409 if (!input->CanBeNull()) {
410 return true;
411 }
412
Vladimir Marko46817b82016-03-29 12:21:58 +0100413 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
414 HInstruction* user = use.GetUser();
415 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100416 return true;
417 }
418 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100419
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100420 return false;
421}
422
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100423// Returns whether doing a type test between the class of `object` against `klass` has
424// a statically known outcome. The result of the test is stored in `outcome`.
425static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000426 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
427 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
428 ScopedObjectAccess soa(Thread::Current());
429 if (!obj_rti.IsValid()) {
430 // We run the simplifier before the reference type propagation so type info might not be
431 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100432 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000433 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100434
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100435 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100436 if (!class_rti.IsValid()) {
437 // Happens when the loaded class is unresolved.
438 return false;
439 }
440 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000441 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100442 *outcome = true;
443 return true;
444 } else if (obj_rti.IsExact()) {
445 // The test failed at compile time so will also fail at runtime.
446 *outcome = false;
447 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100448 } else if (!class_rti.IsInterface()
449 && !obj_rti.IsInterface()
450 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100451 // Different type hierarchy. The test will fail.
452 *outcome = false;
453 return true;
454 }
455 return false;
456}
457
458void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
459 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100460 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
461 if (load_class->NeedsAccessCheck()) {
462 // If we need to perform an access check we cannot remove the instruction.
463 return;
464 }
465
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100466 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100467 check_cast->ClearMustDoNullCheck();
468 }
469
470 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000471 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000472 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100473 return;
474 }
475
Vladimir Markoa65ed302016-03-14 21:21:29 +0000476 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
477 // the return value check with the `outcome` check, b/27651442 .
478 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700479 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100480 if (outcome) {
481 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000482 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700483 if (!load_class->HasUses()) {
484 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
485 // However, here we know that it cannot because the checkcast was successfull, hence
486 // the class was already loaded.
487 load_class->GetBlock()->RemoveInstruction(load_class);
488 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100489 } else {
490 // Don't do anything for exceptional cases for now. Ideally we should remove
491 // all instructions and blocks this instruction dominates.
492 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000493 }
494}
495
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100496void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100497 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100498 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
499 if (load_class->NeedsAccessCheck()) {
500 // If we need to perform an access check we cannot remove the instruction.
501 return;
502 }
503
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100504 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100505 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100506 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100507 instruction->ClearMustDoNullCheck();
508 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100509
510 HGraph* graph = GetGraph();
511 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000512 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100513 instruction->ReplaceWith(graph->GetIntConstant(0));
514 instruction->GetBlock()->RemoveInstruction(instruction);
515 RecordSimplification();
516 return;
517 }
518
Vladimir Marko24bd8952016-03-15 10:40:33 +0000519 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
520 // the return value check with the `outcome` check, b/27651442 .
521 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700522 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000523 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100524 if (outcome && can_be_null) {
525 // Type test will succeed, we just need a null test.
526 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
527 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
528 instruction->ReplaceWith(test);
529 } else {
530 // We've statically determined the result of the instanceof.
531 instruction->ReplaceWith(graph->GetIntConstant(outcome));
532 }
533 RecordSimplification();
534 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700535 if (outcome && !load_class->HasUses()) {
536 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
537 // However, here we know that it cannot because the instanceof check was successfull, hence
538 // the class was already loaded.
539 load_class->GetBlock()->RemoveInstruction(load_class);
540 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100541 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100542}
543
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100544void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
545 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100546 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100547 instruction->ClearValueCanBeNull();
548 }
549}
550
551void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
552 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100553 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100554 instruction->ClearValueCanBeNull();
555 }
556}
557
Anton Shaminbdd79352016-02-15 12:48:36 +0600558static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
559 HInstruction *lhs = cond->InputAt(0);
560 HInstruction *rhs = cond->InputAt(1);
561 switch (cond->GetKind()) {
562 case HInstruction::kEqual:
563 return new (arena) HEqual(rhs, lhs);
564 case HInstruction::kNotEqual:
565 return new (arena) HNotEqual(rhs, lhs);
566 case HInstruction::kLessThan:
567 return new (arena) HGreaterThan(rhs, lhs);
568 case HInstruction::kLessThanOrEqual:
569 return new (arena) HGreaterThanOrEqual(rhs, lhs);
570 case HInstruction::kGreaterThan:
571 return new (arena) HLessThan(rhs, lhs);
572 case HInstruction::kGreaterThanOrEqual:
573 return new (arena) HLessThanOrEqual(rhs, lhs);
574 case HInstruction::kBelow:
575 return new (arena) HAbove(rhs, lhs);
576 case HInstruction::kBelowOrEqual:
577 return new (arena) HAboveOrEqual(rhs, lhs);
578 case HInstruction::kAbove:
579 return new (arena) HBelow(rhs, lhs);
580 case HInstruction::kAboveOrEqual:
581 return new (arena) HBelowOrEqual(rhs, lhs);
582 default:
583 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
584 }
585 return nullptr;
586}
587
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000588void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100589 HInstruction* input_const = equal->GetConstantRight();
590 if (input_const != nullptr) {
591 HInstruction* input_value = equal->GetLeastConstantLeft();
592 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
593 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100594 // We are comparing the boolean to a constant which is of type int and can
595 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000596 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100597 // Replace (bool_value == true) with bool_value
598 equal->ReplaceWith(input_value);
599 block->RemoveInstruction(equal);
600 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000601 } else if (input_const->AsIntConstant()->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500602 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
603 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100604 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100605 } else {
606 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
607 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
608 block->RemoveInstruction(equal);
609 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100610 }
Mark Mendellc4701932015-04-10 13:18:51 -0400611 } else {
612 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100613 }
Mark Mendellc4701932015-04-10 13:18:51 -0400614 } else {
615 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100616 }
617}
618
David Brazdil0d13fee2015-04-17 14:52:19 +0100619void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
620 HInstruction* input_const = not_equal->GetConstantRight();
621 if (input_const != nullptr) {
622 HInstruction* input_value = not_equal->GetLeastConstantLeft();
623 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
624 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100625 // We are comparing the boolean to a constant which is of type int and can
626 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000627 if (input_const->AsIntConstant()->IsTrue()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500628 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
629 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100630 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000631 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100632 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100633 not_equal->ReplaceWith(input_value);
634 block->RemoveInstruction(not_equal);
635 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100636 } else {
637 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
638 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
639 block->RemoveInstruction(not_equal);
640 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100641 }
Mark Mendellc4701932015-04-10 13:18:51 -0400642 } else {
643 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100644 }
Mark Mendellc4701932015-04-10 13:18:51 -0400645 } else {
646 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100647 }
648}
649
650void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000651 HInstruction* input = bool_not->InputAt(0);
652 HInstruction* replace_with = nullptr;
653
654 if (input->IsIntConstant()) {
655 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000656 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000657 replace_with = GetGraph()->GetIntConstant(0);
658 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000659 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000660 replace_with = GetGraph()->GetIntConstant(1);
661 }
662 } else if (input->IsBooleanNot()) {
663 // Replace (!(!bool_value)) with bool_value.
664 replace_with = input->InputAt(0);
665 } else if (input->IsCondition() &&
666 // Don't change FP compares. The definition of compares involving
667 // NaNs forces the compares to be done as written by the user.
668 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
669 // Replace condition with its opposite.
670 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
671 }
672
673 if (replace_with != nullptr) {
674 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100675 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000676 RecordSimplification();
677 }
678}
679
680void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
681 HInstruction* replace_with = nullptr;
682 HInstruction* condition = select->GetCondition();
683 HInstruction* true_value = select->GetTrueValue();
684 HInstruction* false_value = select->GetFalseValue();
685
686 if (condition->IsBooleanNot()) {
687 // Change ((!cond) ? x : y) to (cond ? y : x).
688 condition = condition->InputAt(0);
689 std::swap(true_value, false_value);
690 select->ReplaceInput(false_value, 0);
691 select->ReplaceInput(true_value, 1);
692 select->ReplaceInput(condition, 2);
693 RecordSimplification();
694 }
695
696 if (true_value == false_value) {
697 // Replace (cond ? x : x) with (x).
698 replace_with = true_value;
699 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000700 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000701 // Replace (true ? x : y) with (x).
702 replace_with = true_value;
703 } else {
704 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000705 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000706 replace_with = false_value;
707 }
708 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000709 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000710 // Replace (cond ? true : false) with (cond).
711 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000712 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000713 // Replace (cond ? false : true) with (!cond).
714 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
715 }
716 }
717
718 if (replace_with != nullptr) {
719 select->ReplaceWith(replace_with);
720 select->GetBlock()->RemoveInstruction(select);
721 RecordSimplification();
722 }
723}
724
725void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
726 HInstruction* condition = instruction->InputAt(0);
727 if (condition->IsBooleanNot()) {
728 // Swap successors if input is negated.
729 instruction->ReplaceInput(condition->InputAt(0), 0);
730 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100731 RecordSimplification();
732 }
733}
734
Mingyao Yang0304e182015-01-30 16:41:29 -0800735void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
736 HInstruction* input = instruction->InputAt(0);
737 // If the array is a NewArray with constant size, replace the array length
738 // with the constant instruction. This helps the bounds check elimination phase.
739 if (input->IsNewArray()) {
740 input = input->InputAt(0);
741 if (input->IsIntConstant()) {
742 instruction->ReplaceWith(input);
743 }
744 }
745}
746
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000747void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000748 HInstruction* value = instruction->GetValue();
749 if (value->GetType() != Primitive::kPrimNot) return;
750
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100751 if (CanEnsureNotNullAt(value, instruction)) {
752 instruction->ClearValueCanBeNull();
753 }
754
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000755 if (value->IsArrayGet()) {
756 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
757 // If the code is just swapping elements in the array, no need for a type check.
758 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100759 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000760 }
761 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100762
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100763 if (value->IsNullConstant()) {
764 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100765 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100766 }
767
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100768 ScopedObjectAccess soa(Thread::Current());
769 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
770 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
771 if (!array_rti.IsValid()) {
772 return;
773 }
774
775 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
776 instruction->ClearNeedsTypeCheck();
777 return;
778 }
779
780 if (array_rti.IsObjectArray()) {
781 if (array_rti.IsExact()) {
782 instruction->ClearNeedsTypeCheck();
783 return;
784 }
785 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100786 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000787}
788
Vladimir Markob52bbde2016-02-12 12:06:05 +0000789static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100790 // Invariant: We should never generate a conversion to a Boolean value.
791 DCHECK_NE(Primitive::kPrimBoolean, result_type);
792
Vladimir Markob52bbde2016-02-12 12:06:05 +0000793 // Besides conversion to the same type, widening integral conversions are implicit,
794 // excluding conversions to long and the byte->char conversion where we need to
795 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
796 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100797 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
798 input_type == Primitive::kPrimByte ||
799 input_type == Primitive::kPrimShort ||
800 input_type == Primitive::kPrimChar)) ||
801 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
802 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
803 input_type == Primitive::kPrimByte)) ||
804 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000805}
806
807static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
808 // The conversion to a larger type is loss-less with the exception of two cases,
809 // - conversion to char, the only unsigned type, where we may lose some bits, and
810 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
811 // For integral to FP conversions this holds because the FP mantissa is large enough.
812 DCHECK_NE(input_type, result_type);
813 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
814 result_type != Primitive::kPrimChar &&
815 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
816}
817
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000818void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000819 HInstruction* input = instruction->GetInput();
820 Primitive::Type input_type = input->GetType();
821 Primitive::Type result_type = instruction->GetResultType();
822 if (IsTypeConversionImplicit(input_type, result_type)) {
823 // Remove the implicit conversion; this includes conversion to the same type.
824 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000825 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000826 RecordSimplification();
827 return;
828 }
829
830 if (input->IsTypeConversion()) {
831 HTypeConversion* input_conversion = input->AsTypeConversion();
832 HInstruction* original_input = input_conversion->GetInput();
833 Primitive::Type original_type = original_input->GetType();
834
835 // When the first conversion is lossless, a direct conversion from the original type
836 // to the final type yields the same result, even for a lossy second conversion, for
837 // example float->double->int or int->double->float.
838 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
839
840 // For integral conversions, see if the first conversion loses only bits that the second
841 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
842 // conversion yields the same result, for example long->int->short or int->char->short.
843 bool integral_conversions_with_non_widening_second =
844 Primitive::IsIntegralType(input_type) &&
845 Primitive::IsIntegralType(original_type) &&
846 Primitive::IsIntegralType(result_type) &&
847 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
848
849 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
850 // If the merged conversion is implicit, do the simplification unconditionally.
851 if (IsTypeConversionImplicit(original_type, result_type)) {
852 instruction->ReplaceWith(original_input);
853 instruction->GetBlock()->RemoveInstruction(instruction);
854 if (!input_conversion->HasUses()) {
855 // Don't wait for DCE.
856 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
857 }
858 RecordSimplification();
859 return;
860 }
861 // Otherwise simplify only if the first conversion has no other use.
862 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
863 input_conversion->ReplaceWith(original_input);
864 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
865 RecordSimplification();
866 return;
867 }
868 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000869 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000870 DCHECK(Primitive::IsIntegralType(input_type));
871 HAnd* input_and = input->AsAnd();
872 HConstant* constant = input_and->GetConstantRight();
873 if (constant != nullptr) {
874 int64_t value = Int64FromConstant(constant);
875 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
876 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
877 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
878 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000879 HInstruction* original_input = input_and->GetLeastConstantLeft();
880 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
881 instruction->ReplaceWith(original_input);
882 instruction->GetBlock()->RemoveInstruction(instruction);
883 RecordSimplification();
884 return;
885 } else if (input->HasOnlyOneNonEnvironmentUse()) {
886 input_and->ReplaceWith(original_input);
887 input_and->GetBlock()->RemoveInstruction(input_and);
888 RecordSimplification();
889 return;
890 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000891 }
892 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000893 }
894}
895
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000896void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
897 HConstant* input_cst = instruction->GetConstantRight();
898 HInstruction* input_other = instruction->GetLeastConstantLeft();
Roland Levillain1a653882016-03-18 18:05:57 +0000899 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000900 // Replace code looking like
901 // ADD dst, src, 0
902 // with
903 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600904 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
905 // `x` is `-0.0`, the former expression yields `0.0`, while the later
906 // yields `-0.0`.
907 if (Primitive::IsIntegralType(instruction->GetType())) {
908 instruction->ReplaceWith(input_other);
909 instruction->GetBlock()->RemoveInstruction(instruction);
910 return;
911 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100912 }
913
914 HInstruction* left = instruction->GetLeft();
915 HInstruction* right = instruction->GetRight();
916 bool left_is_neg = left->IsNeg();
917 bool right_is_neg = right->IsNeg();
918
919 if (left_is_neg && right_is_neg) {
920 if (TryMoveNegOnInputsAfterBinop(instruction)) {
921 return;
922 }
923 }
924
925 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
926 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
927 // Replace code looking like
928 // NEG tmp, b
929 // ADD dst, a, tmp
930 // with
931 // SUB dst, a, b
932 // We do not perform the optimization if the input negation has environment
933 // uses or multiple non-environment uses as it could lead to worse code. In
934 // particular, we do not want the live range of `b` to be extended if we are
935 // not sure the initial 'NEG' instruction can be removed.
936 HInstruction* other = left_is_neg ? right : left;
937 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
938 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
939 RecordSimplification();
940 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000941 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000942 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000943
944 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000945}
946
947void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
948 HConstant* input_cst = instruction->GetConstantRight();
949 HInstruction* input_other = instruction->GetLeastConstantLeft();
950
Vladimir Marko452c1b62015-09-25 14:44:17 +0100951 if (input_cst != nullptr) {
952 int64_t value = Int64FromConstant(input_cst);
953 if (value == -1) {
954 // Replace code looking like
955 // AND dst, src, 0xFFF...FF
956 // with
957 // src
958 instruction->ReplaceWith(input_other);
959 instruction->GetBlock()->RemoveInstruction(instruction);
960 RecordSimplification();
961 return;
962 }
963 // Eliminate And from UShr+And if the And-mask contains all the bits that
964 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
965 // precisely clears the shifted-in sign bits.
966 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
967 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
968 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
969 size_t num_tail_bits_set = CTZ(value + 1);
970 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
971 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
972 instruction->ReplaceWith(input_other);
973 instruction->GetBlock()->RemoveInstruction(instruction);
974 RecordSimplification();
975 return;
976 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
977 input_other->HasOnlyOneNonEnvironmentUse()) {
978 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
979 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
980 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
981 input_other->InputAt(0),
982 input_other->InputAt(1),
983 input_other->GetDexPc());
984 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
985 input_other->GetBlock()->RemoveInstruction(input_other);
986 RecordSimplification();
987 return;
988 }
989 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000990 }
991
992 // We assume that GVN has run before, so we only perform a pointer comparison.
993 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100994 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000995 if (instruction->GetLeft() == instruction->GetRight()) {
996 // Replace code looking like
997 // AND dst, src, src
998 // with
999 // src
1000 instruction->ReplaceWith(instruction->GetLeft());
1001 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001002 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001003 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001004
1005 TryDeMorganNegationFactoring(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001006}
1007
Mark Mendellc4701932015-04-10 13:18:51 -04001008void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1009 VisitCondition(condition);
1010}
1011
1012void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1013 VisitCondition(condition);
1014}
1015
1016void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1017 VisitCondition(condition);
1018}
1019
1020void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1021 VisitCondition(condition);
1022}
1023
Anton Shaminbdd79352016-02-15 12:48:36 +06001024void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1025 VisitCondition(condition);
1026}
1027
1028void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1029 VisitCondition(condition);
1030}
1031
1032void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1033 VisitCondition(condition);
1034}
1035
1036void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1037 VisitCondition(condition);
1038}
Aart Bike9f37602015-10-09 11:15:55 -07001039
Mark Mendellc4701932015-04-10 13:18:51 -04001040void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001041 // Reverse condition if left is constant. Our code generators prefer constant
1042 // on the right hand side.
1043 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1044 HBasicBlock* block = condition->GetBlock();
1045 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1046 // If it is a fp we must set the opposite bias.
1047 if (replacement != nullptr) {
1048 if (condition->IsLtBias()) {
1049 replacement->SetBias(ComparisonBias::kGtBias);
1050 } else if (condition->IsGtBias()) {
1051 replacement->SetBias(ComparisonBias::kLtBias);
1052 }
1053 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1054 RecordSimplification();
1055
1056 condition = replacement;
1057 }
1058 }
Mark Mendellc4701932015-04-10 13:18:51 -04001059
Mark Mendellc4701932015-04-10 13:18:51 -04001060 HInstruction* left = condition->GetLeft();
1061 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001062
1063 // Try to fold an HCompare into this HCondition.
1064
Mark Mendellc4701932015-04-10 13:18:51 -04001065 // We can only replace an HCondition which compares a Compare to 0.
1066 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1067 // condition with a long, float or double comparison as input.
1068 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1069 // Conversion is not possible.
1070 return;
1071 }
1072
1073 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001074 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001075 // Someone else also wants the result of the compare.
1076 return;
1077 }
1078
Vladimir Marko46817b82016-03-29 12:21:58 +01001079 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001080 // There is a reference to the compare result in an environment. Do we really need it?
1081 if (GetGraph()->IsDebuggable()) {
1082 return;
1083 }
1084
1085 // We have to ensure that there are no deopt points in the sequence.
1086 if (left->HasAnyEnvironmentUseBefore(condition)) {
1087 return;
1088 }
1089 }
1090
1091 // Clean up any environment uses from the HCompare, if any.
1092 left->RemoveEnvironmentUsers();
1093
1094 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1095 condition->SetBias(left->AsCompare()->GetBias());
1096
1097 // Replace the operands of the HCondition.
1098 condition->ReplaceInput(left->InputAt(0), 0);
1099 condition->ReplaceInput(left->InputAt(1), 1);
1100
1101 // Remove the HCompare.
1102 left->GetBlock()->RemoveInstruction(left);
1103
1104 RecordSimplification();
1105}
1106
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001107void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1108 HConstant* input_cst = instruction->GetConstantRight();
1109 HInstruction* input_other = instruction->GetLeastConstantLeft();
1110 Primitive::Type type = instruction->GetType();
1111
1112 if ((input_cst != nullptr) && input_cst->IsOne()) {
1113 // Replace code looking like
1114 // DIV dst, src, 1
1115 // with
1116 // src
1117 instruction->ReplaceWith(input_other);
1118 instruction->GetBlock()->RemoveInstruction(instruction);
1119 return;
1120 }
1121
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001122 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001123 // Replace code looking like
1124 // DIV dst, src, -1
1125 // with
1126 // NEG dst, src
1127 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001128 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001129 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001130 return;
1131 }
1132
1133 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1134 // Try replacing code looking like
1135 // DIV dst, src, constant
1136 // with
1137 // MUL dst, src, 1 / constant
1138 HConstant* reciprocal = nullptr;
1139 if (type == Primitive::Primitive::kPrimDouble) {
1140 double value = input_cst->AsDoubleConstant()->GetValue();
1141 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1142 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1143 }
1144 } else {
1145 DCHECK_EQ(type, Primitive::kPrimFloat);
1146 float value = input_cst->AsFloatConstant()->GetValue();
1147 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1148 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1149 }
1150 }
1151
1152 if (reciprocal != nullptr) {
1153 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1154 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1155 RecordSimplification();
1156 return;
1157 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001158 }
1159}
1160
1161void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1162 HConstant* input_cst = instruction->GetConstantRight();
1163 HInstruction* input_other = instruction->GetLeastConstantLeft();
1164 Primitive::Type type = instruction->GetType();
1165 HBasicBlock* block = instruction->GetBlock();
1166 ArenaAllocator* allocator = GetGraph()->GetArena();
1167
1168 if (input_cst == nullptr) {
1169 return;
1170 }
1171
1172 if (input_cst->IsOne()) {
1173 // Replace code looking like
1174 // MUL dst, src, 1
1175 // with
1176 // src
1177 instruction->ReplaceWith(input_other);
1178 instruction->GetBlock()->RemoveInstruction(instruction);
1179 return;
1180 }
1181
1182 if (input_cst->IsMinusOne() &&
1183 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1184 // Replace code looking like
1185 // MUL dst, src, -1
1186 // with
1187 // NEG dst, src
1188 HNeg* neg = new (allocator) HNeg(type, input_other);
1189 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001190 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001191 return;
1192 }
1193
1194 if (Primitive::IsFloatingPointType(type) &&
1195 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1196 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1197 // Replace code looking like
1198 // FP_MUL dst, src, 2.0
1199 // with
1200 // FP_ADD dst, src, src
1201 // The 'int' and 'long' cases are handled below.
1202 block->ReplaceAndRemoveInstructionWith(instruction,
1203 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001204 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001205 return;
1206 }
1207
1208 if (Primitive::IsIntOrLongType(type)) {
1209 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001210 // Even though constant propagation also takes care of the zero case, other
1211 // optimizations can lead to having a zero multiplication.
1212 if (factor == 0) {
1213 // Replace code looking like
1214 // MUL dst, src, 0
1215 // with
1216 // 0
1217 instruction->ReplaceWith(input_cst);
1218 instruction->GetBlock()->RemoveInstruction(instruction);
1219 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001220 // Replace code looking like
1221 // MUL dst, src, pow_of_2
1222 // with
1223 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001224 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001225 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001226 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001227 RecordSimplification();
Alexandre Rames38db7852015-11-20 15:02:45 +00001228 } else if (IsPowerOfTwo(factor - 1)) {
1229 // Transform code looking like
1230 // MUL dst, src, (2^n + 1)
1231 // into
1232 // SHL tmp, src, n
1233 // ADD dst, src, tmp
1234 HShl* shl = new (allocator) HShl(type,
1235 input_other,
1236 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1237 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1238
1239 block->InsertInstructionBefore(shl, instruction);
1240 block->ReplaceAndRemoveInstructionWith(instruction, add);
1241 RecordSimplification();
1242 } else if (IsPowerOfTwo(factor + 1)) {
1243 // Transform code looking like
1244 // MUL dst, src, (2^n - 1)
1245 // into
1246 // SHL tmp, src, n
1247 // SUB dst, tmp, src
1248 HShl* shl = new (allocator) HShl(type,
1249 input_other,
1250 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1251 HSub* sub = new (allocator) HSub(type, shl, input_other);
1252
1253 block->InsertInstructionBefore(shl, instruction);
1254 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1255 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001256 }
1257 }
1258}
1259
Alexandre Rames188d4312015-04-09 18:30:21 +01001260void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1261 HInstruction* input = instruction->GetInput();
1262 if (input->IsNeg()) {
1263 // Replace code looking like
1264 // NEG tmp, src
1265 // NEG dst, tmp
1266 // with
1267 // src
1268 HNeg* previous_neg = input->AsNeg();
1269 instruction->ReplaceWith(previous_neg->GetInput());
1270 instruction->GetBlock()->RemoveInstruction(instruction);
1271 // We perform the optimization even if the input negation has environment
1272 // uses since it allows removing the current instruction. But we only delete
1273 // the input negation only if it is does not have any uses left.
1274 if (!previous_neg->HasUses()) {
1275 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1276 }
1277 RecordSimplification();
1278 return;
1279 }
1280
Serguei Katkov339dfc22015-04-20 12:29:32 +06001281 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1282 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001283 // Replace code looking like
1284 // SUB tmp, a, b
1285 // NEG dst, tmp
1286 // with
1287 // SUB dst, b, a
1288 // We do not perform the optimization if the input subtraction has
1289 // environment uses or multiple non-environment uses as it could lead to
1290 // worse code. In particular, we do not want the live ranges of `a` and `b`
1291 // to be extended if we are not sure the initial 'SUB' instruction can be
1292 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001293 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001294 HSub* sub = input->AsSub();
1295 HSub* new_sub =
1296 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1297 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1298 if (!sub->HasUses()) {
1299 sub->GetBlock()->RemoveInstruction(sub);
1300 }
1301 RecordSimplification();
1302 }
1303}
1304
1305void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1306 HInstruction* input = instruction->GetInput();
1307 if (input->IsNot()) {
1308 // Replace code looking like
1309 // NOT tmp, src
1310 // NOT dst, tmp
1311 // with
1312 // src
1313 // We perform the optimization even if the input negation has environment
1314 // uses since it allows removing the current instruction. But we only delete
1315 // the input negation only if it is does not have any uses left.
1316 HNot* previous_not = input->AsNot();
1317 instruction->ReplaceWith(previous_not->GetInput());
1318 instruction->GetBlock()->RemoveInstruction(instruction);
1319 if (!previous_not->HasUses()) {
1320 previous_not->GetBlock()->RemoveInstruction(previous_not);
1321 }
1322 RecordSimplification();
1323 }
1324}
1325
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001326void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1327 HConstant* input_cst = instruction->GetConstantRight();
1328 HInstruction* input_other = instruction->GetLeastConstantLeft();
1329
Roland Levillain1a653882016-03-18 18:05:57 +00001330 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001331 // Replace code looking like
1332 // OR dst, src, 0
1333 // with
1334 // src
1335 instruction->ReplaceWith(input_other);
1336 instruction->GetBlock()->RemoveInstruction(instruction);
1337 return;
1338 }
1339
1340 // We assume that GVN has run before, so we only perform a pointer comparison.
1341 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001342 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001343 if (instruction->GetLeft() == instruction->GetRight()) {
1344 // Replace code looking like
1345 // OR dst, src, src
1346 // with
1347 // src
1348 instruction->ReplaceWith(instruction->GetLeft());
1349 instruction->GetBlock()->RemoveInstruction(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001350 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001351 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001352
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001353 if (TryDeMorganNegationFactoring(instruction)) return;
1354
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001355 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001356}
1357
1358void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1359 VisitShift(instruction);
1360}
1361
1362void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1363 VisitShift(instruction);
1364}
1365
1366void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1367 HConstant* input_cst = instruction->GetConstantRight();
1368 HInstruction* input_other = instruction->GetLeastConstantLeft();
1369
Serguei Katkov115b53f2015-08-05 17:03:30 +06001370 Primitive::Type type = instruction->GetType();
1371 if (Primitive::IsFloatingPointType(type)) {
1372 return;
1373 }
1374
Roland Levillain1a653882016-03-18 18:05:57 +00001375 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001376 // Replace code looking like
1377 // SUB dst, src, 0
1378 // with
1379 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001380 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1381 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1382 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001383 instruction->ReplaceWith(input_other);
1384 instruction->GetBlock()->RemoveInstruction(instruction);
1385 return;
1386 }
1387
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001388 HBasicBlock* block = instruction->GetBlock();
1389 ArenaAllocator* allocator = GetGraph()->GetArena();
1390
Alexandre Rames188d4312015-04-09 18:30:21 +01001391 HInstruction* left = instruction->GetLeft();
1392 HInstruction* right = instruction->GetRight();
1393 if (left->IsConstant()) {
1394 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001395 // Replace code looking like
1396 // SUB dst, 0, src
1397 // with
1398 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001399 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001400 // `x` is `0.0`, the former expression yields `0.0`, while the later
1401 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001402 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001403 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001404 RecordSimplification();
1405 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001406 }
1407 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001408
1409 if (left->IsNeg() && right->IsNeg()) {
1410 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1411 return;
1412 }
1413 }
1414
1415 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1416 // Replace code looking like
1417 // NEG tmp, b
1418 // SUB dst, a, tmp
1419 // with
1420 // ADD dst, a, b
1421 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1422 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1423 RecordSimplification();
1424 right->GetBlock()->RemoveInstruction(right);
1425 return;
1426 }
1427
1428 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1429 // Replace code looking like
1430 // NEG tmp, a
1431 // SUB dst, tmp, b
1432 // with
1433 // ADD tmp, a, b
1434 // NEG dst, tmp
1435 // The second version is not intrinsically better, but enables more
1436 // transformations.
1437 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1438 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1439 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1440 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1441 instruction->ReplaceWith(neg);
1442 instruction->GetBlock()->RemoveInstruction(instruction);
1443 RecordSimplification();
1444 left->GetBlock()->RemoveInstruction(left);
1445 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001446}
1447
1448void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1449 VisitShift(instruction);
1450}
1451
1452void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1453 HConstant* input_cst = instruction->GetConstantRight();
1454 HInstruction* input_other = instruction->GetLeastConstantLeft();
1455
Roland Levillain1a653882016-03-18 18:05:57 +00001456 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001457 // Replace code looking like
1458 // XOR dst, src, 0
1459 // with
1460 // src
1461 instruction->ReplaceWith(input_other);
1462 instruction->GetBlock()->RemoveInstruction(instruction);
1463 return;
1464 }
1465
1466 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1467 // Replace code looking like
1468 // XOR dst, src, 0xFFF...FF
1469 // with
1470 // NOT dst, src
1471 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1472 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001473 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001474 return;
1475 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001476
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001477 HInstruction* left = instruction->GetLeft();
1478 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001479 if (((left->IsNot() && right->IsNot()) ||
1480 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001481 left->HasOnlyOneNonEnvironmentUse() &&
1482 right->HasOnlyOneNonEnvironmentUse()) {
1483 // Replace code looking like
1484 // NOT nota, a
1485 // NOT notb, b
1486 // XOR dst, nota, notb
1487 // with
1488 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001489 instruction->ReplaceInput(left->InputAt(0), 0);
1490 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001491 left->GetBlock()->RemoveInstruction(left);
1492 right->GetBlock()->RemoveInstruction(right);
1493 RecordSimplification();
1494 return;
1495 }
1496
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001497 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001498}
1499
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001500void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1501 HInstruction* argument = instruction->InputAt(1);
1502 HInstruction* receiver = instruction->InputAt(0);
1503 if (receiver == argument) {
1504 // Because String.equals is an instance call, the receiver is
1505 // a null check if we don't know it's null. The argument however, will
1506 // be the actual object. So we cannot end up in a situation where both
1507 // are equal but could be null.
1508 DCHECK(CanEnsureNotNullAt(argument, instruction));
1509 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1510 instruction->GetBlock()->RemoveInstruction(instruction);
1511 } else {
1512 StringEqualsOptimizations optimizations(instruction);
1513 if (CanEnsureNotNullAt(argument, instruction)) {
1514 optimizations.SetArgumentNotNull();
1515 }
1516 ScopedObjectAccess soa(Thread::Current());
1517 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1518 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1519 optimizations.SetArgumentIsString();
1520 }
1521 }
1522}
1523
Roland Levillain22c49222016-03-18 14:04:28 +00001524void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1525 bool is_left,
1526 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001527 DCHECK(invoke->IsInvokeStaticOrDirect());
1528 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001529 HInstruction* value = invoke->InputAt(0);
1530 HInstruction* distance = invoke->InputAt(1);
1531 // Replace the invoke with an HRor.
1532 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001533 // Unconditionally set the type of the negated distance to `int`,
1534 // as shift and rotate operations expect a 32-bit (or narrower)
1535 // value for their distance input.
1536 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001537 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1538 }
Roland Levillain22c49222016-03-18 14:04:28 +00001539 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001540 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1541 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001542 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001543 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1544 clinit->GetBlock()->RemoveInstruction(clinit);
1545 HInstruction* ldclass = clinit->InputAt(0);
1546 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1547 ldclass->GetBlock()->RemoveInstruction(ldclass);
1548 }
1549 }
1550}
1551
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001552static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1553 if (potential_length->IsArrayLength()) {
1554 return potential_length->InputAt(0) == potential_array;
1555 }
1556
1557 if (potential_array->IsNewArray()) {
1558 return potential_array->InputAt(0) == potential_length;
1559 }
1560
1561 return false;
1562}
1563
1564void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1565 HInstruction* source = instruction->InputAt(0);
1566 HInstruction* destination = instruction->InputAt(2);
1567 HInstruction* count = instruction->InputAt(4);
1568 SystemArrayCopyOptimizations optimizations(instruction);
1569 if (CanEnsureNotNullAt(source, instruction)) {
1570 optimizations.SetSourceIsNotNull();
1571 }
1572 if (CanEnsureNotNullAt(destination, instruction)) {
1573 optimizations.SetDestinationIsNotNull();
1574 }
1575 if (destination == source) {
1576 optimizations.SetDestinationIsSource();
1577 }
1578
1579 if (IsArrayLengthOf(count, source)) {
1580 optimizations.SetCountIsSourceLength();
1581 }
1582
1583 if (IsArrayLengthOf(count, destination)) {
1584 optimizations.SetCountIsDestinationLength();
1585 }
1586
1587 {
1588 ScopedObjectAccess soa(Thread::Current());
1589 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1590 if (destination_rti.IsValid()) {
1591 if (destination_rti.IsObjectArray()) {
1592 if (destination_rti.IsExact()) {
1593 optimizations.SetDoesNotNeedTypeCheck();
1594 }
1595 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001596 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001597 if (destination_rti.IsPrimitiveArrayClass()) {
1598 optimizations.SetDestinationIsPrimitiveArray();
1599 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1600 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001601 }
1602 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001603 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1604 if (source_rti.IsValid()) {
1605 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1606 optimizations.SetDoesNotNeedTypeCheck();
1607 }
1608 if (source_rti.IsPrimitiveArrayClass()) {
1609 optimizations.SetSourceIsPrimitiveArray();
1610 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1611 optimizations.SetSourceIsNonPrimitiveArray();
1612 }
1613 }
1614 }
1615}
1616
Roland Levillaina5c4a402016-03-15 15:02:50 +00001617void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1618 bool is_signum,
1619 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001620 DCHECK(invoke->IsInvokeStaticOrDirect());
1621 uint32_t dex_pc = invoke->GetDexPc();
1622 HInstruction* left = invoke->InputAt(0);
1623 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001624 if (!is_signum) {
1625 right = invoke->InputAt(1);
1626 } else if (type == Primitive::kPrimLong) {
1627 right = GetGraph()->GetLongConstant(0);
1628 } else {
1629 right = GetGraph()->GetIntConstant(0);
1630 }
1631 HCompare* compare = new (GetGraph()->GetArena())
1632 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1633 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1634}
1635
Aart Bik75a38b22016-02-17 10:41:50 -08001636void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1637 DCHECK(invoke->IsInvokeStaticOrDirect());
1638 uint32_t dex_pc = invoke->GetDexPc();
1639 // IsNaN(x) is the same as x != x.
1640 HInstruction* x = invoke->InputAt(0);
1641 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001642 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001643 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1644}
1645
Aart Bik2a6aad92016-02-25 11:32:32 -08001646void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1647 DCHECK(invoke->IsInvokeStaticOrDirect());
1648 uint32_t dex_pc = invoke->GetDexPc();
1649 HInstruction* x = invoke->InputAt(0);
1650 Primitive::Type type = x->GetType();
1651 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1652 HInstruction* nan;
1653 if (type == Primitive::kPrimDouble) {
1654 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1655 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1656 kNeedsEnvironmentOrCache,
1657 kNoSideEffects,
1658 kNoThrow);
1659 } else {
1660 DCHECK_EQ(type, Primitive::kPrimFloat);
1661 nan = GetGraph()->GetIntConstant(0x7fc00000);
1662 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1663 kNeedsEnvironmentOrCache,
1664 kNoSideEffects,
1665 kNoThrow);
1666 }
1667 // Test IsNaN(x), which is the same as x != x.
1668 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1669 condition->SetBias(ComparisonBias::kLtBias);
1670 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1671 // Select between the two.
1672 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1673 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1674 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1675}
1676
Vladimir Markodce016e2016-04-28 13:10:02 +01001677void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1678 HInstruction* str = invoke->InputAt(0);
1679 uint32_t dex_pc = invoke->GetDexPc();
1680 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1681 // so create the HArrayLength.
1682 HArrayLength* length = new (GetGraph()->GetArena()) HArrayLength(str, dex_pc);
1683 length->MarkAsStringLength();
1684 HInstruction* replacement;
1685 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1686 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1687 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1688 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1689 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1690 replacement = equal;
1691 } else {
1692 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1693 replacement = length;
1694 }
1695 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
1696}
1697
Aart Bik11932592016-03-08 12:42:25 -08001698void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1699 uint32_t dex_pc = invoke->GetDexPc();
1700 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1701 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1702}
1703
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001704void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001705 switch (instruction->GetIntrinsic()) {
1706 case Intrinsics::kStringEquals:
1707 SimplifyStringEquals(instruction);
1708 break;
1709 case Intrinsics::kSystemArrayCopy:
1710 SimplifySystemArrayCopy(instruction);
1711 break;
1712 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001713 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1714 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001715 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001716 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001717 break;
1718 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001719 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1720 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001721 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001722 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001723 break;
1724 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001725 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1726 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001727 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001728 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001729 break;
1730 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001731 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1732 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001733 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001734 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001735 break;
1736 case Intrinsics::kFloatIsNaN:
1737 case Intrinsics::kDoubleIsNaN:
1738 SimplifyIsNaN(instruction);
1739 break;
1740 case Intrinsics::kFloatFloatToIntBits:
1741 case Intrinsics::kDoubleDoubleToLongBits:
1742 SimplifyFP2Int(instruction);
1743 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01001744 case Intrinsics::kStringIsEmpty:
1745 case Intrinsics::kStringLength:
1746 SimplifyStringIsEmptyOrLength(instruction);
1747 break;
Aart Bik11932592016-03-08 12:42:25 -08001748 case Intrinsics::kUnsafeLoadFence:
1749 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1750 break;
1751 case Intrinsics::kUnsafeStoreFence:
1752 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1753 break;
1754 case Intrinsics::kUnsafeFullFence:
1755 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1756 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001757 default:
1758 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001759 }
1760}
1761
Aart Bikbb245d12015-10-19 11:05:03 -07001762void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1763 HInstruction* cond = deoptimize->InputAt(0);
1764 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001765 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001766 // Never deopt: instruction can be removed.
1767 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1768 } else {
1769 // Always deopt.
1770 }
1771 }
1772}
1773
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001774} // namespace art