blob: 1249b48e1eaa4ade86acfa4f2df996fe9f2d3859 [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);
Aart Bik11932592016-03-08 12:42:25 -0800104 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100105
Calin Juravleacf735c2015-02-12 15:25:22 +0000106 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100107 bool simplification_occurred_ = false;
108 int simplifications_at_current_position_ = 0;
109 // We ensure we do not loop infinitely. The value is a finger in the air guess
110 // that should allow enough simplification.
111 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000112};
113
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100114void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000115 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100116 visitor.Run();
117}
118
119void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100120 // Iterate in reverse post order to open up more simplifications to users
121 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100122 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
123 // The simplification of an instruction to another instruction may yield
124 // possibilities for other simplifications. So although we perform a reverse
125 // post order visit, we sometimes need to revisit an instruction index.
126 simplification_occurred_ = false;
127 VisitBasicBlock(it.Current());
128 if (simplification_occurred_ &&
129 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
130 // New simplifications may be applicable to the instruction at the
131 // current index, so don't advance the iterator.
132 continue;
133 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100134 simplifications_at_current_position_ = 0;
135 it.Advance();
136 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100137}
138
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000139namespace {
140
141bool AreAllBitsSet(HConstant* constant) {
142 return Int64FromConstant(constant) == -1;
143}
144
145} // namespace
146
Alexandre Rames188d4312015-04-09 18:30:21 +0100147// Returns true if the code was simplified to use only one negation operation
148// after the binary operation instead of one on each of the inputs.
149bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
150 DCHECK(binop->IsAdd() || binop->IsSub());
151 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
152 HNeg* left_neg = binop->GetLeft()->AsNeg();
153 HNeg* right_neg = binop->GetRight()->AsNeg();
154 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
155 !right_neg->HasOnlyOneNonEnvironmentUse()) {
156 return false;
157 }
158 // Replace code looking like
159 // NEG tmp1, a
160 // NEG tmp2, b
161 // ADD dst, tmp1, tmp2
162 // with
163 // ADD tmp, a, b
164 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600165 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
166 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
167 // while the later yields `-0.0`.
168 if (!Primitive::IsIntegralType(binop->GetType())) {
169 return false;
170 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100171 binop->ReplaceInput(left_neg->GetInput(), 0);
172 binop->ReplaceInput(right_neg->GetInput(), 1);
173 left_neg->GetBlock()->RemoveInstruction(left_neg);
174 right_neg->GetBlock()->RemoveInstruction(right_neg);
175 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
176 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
177 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
178 RecordSimplification();
179 return true;
180}
181
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000182bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
183 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
184 Primitive::Type type = op->GetType();
185 HInstruction* left = op->GetLeft();
186 HInstruction* right = op->GetRight();
187
188 // We can apply De Morgan's laws if both inputs are Not's and are only used
189 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000190 if (((left->IsNot() && right->IsNot()) ||
191 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000192 left->HasOnlyOneNonEnvironmentUse() &&
193 right->HasOnlyOneNonEnvironmentUse()) {
194 // Replace code looking like
195 // NOT nota, a
196 // NOT notb, b
197 // AND dst, nota, notb (respectively OR)
198 // with
199 // OR or, a, b (respectively AND)
200 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000201 HInstruction* src_left = left->InputAt(0);
202 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000203 uint32_t dex_pc = op->GetDexPc();
204
205 // Remove the negations on the inputs.
206 left->ReplaceWith(src_left);
207 right->ReplaceWith(src_right);
208 left->GetBlock()->RemoveInstruction(left);
209 right->GetBlock()->RemoveInstruction(right);
210
211 // Replace the `HAnd` or `HOr`.
212 HBinaryOperation* hbin;
213 if (op->IsAnd()) {
214 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
215 } else {
216 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
217 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000218 HInstruction* hnot;
219 if (left->IsBooleanNot()) {
220 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
221 } else {
222 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
223 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000224
225 op->GetBlock()->InsertInstructionBefore(hbin, op);
226 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
227
228 RecordSimplification();
229 return true;
230 }
231
232 return false;
233}
234
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000235void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
236 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
237 HConstant* input_cst = instruction->GetConstantRight();
238 HInstruction* input_other = instruction->GetLeastConstantLeft();
239
Mark Mendellba56d062015-05-05 21:34:03 -0400240 if (input_cst != nullptr) {
Vladimir Marko164306e2016-03-15 14:57:32 +0000241 int64_t cst = Int64FromConstant(input_cst);
Roland Levillain5b5b9312016-03-22 14:57:31 +0000242 int64_t mask = (input_other->GetType() == Primitive::kPrimLong)
243 ? kMaxLongShiftDistance
244 : kMaxIntShiftDistance;
Vladimir Marko164306e2016-03-15 14:57:32 +0000245 if ((cst & mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400246 // Replace code looking like
247 // SHL dst, src, 0
248 // with
249 // src
250 instruction->ReplaceWith(input_other);
251 instruction->GetBlock()->RemoveInstruction(instruction);
Mark Mendellba56d062015-05-05 21:34:03 -0400252 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000253 }
254}
255
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000256static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
257 return (sub->GetRight() == other &&
258 sub->GetLeft()->IsConstant() &&
259 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
260}
261
262bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
263 HUShr* ushr,
264 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000265 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
266 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000267 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
268 if (!ushr->HasUses()) {
269 ushr->GetBlock()->RemoveInstruction(ushr);
270 }
271 if (!ushr->GetRight()->HasUses()) {
272 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
273 }
274 if (!shl->HasUses()) {
275 shl->GetBlock()->RemoveInstruction(shl);
276 }
277 if (!shl->GetRight()->HasUses()) {
278 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
279 }
280 return true;
281}
282
283// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
284bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000285 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
286 HInstruction* left = op->GetLeft();
287 HInstruction* right = op->GetRight();
288 // If we have an UShr and a Shl (in either order).
289 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
290 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
291 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
292 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
293 if (ushr->GetType() == shl->GetType() &&
294 ushr->GetLeft() == shl->GetLeft()) {
295 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
296 // Shift distances are both constant, try replacing with Ror if they
297 // add up to the register size.
298 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
299 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
300 // Shift distances are potentially of the form x and (reg_size - x).
301 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
302 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
303 // Shift distances are potentially of the form d and -d.
304 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
305 }
306 }
307 }
308 return false;
309}
310
311// Try replacing code looking like (x >>> #rdist OP x << #ldist):
312// UShr dst, x, #rdist
313// Shl tmp, x, #ldist
314// OP dst, dst, tmp
315// or like (x >>> #rdist OP x << #-ldist):
316// UShr dst, x, #rdist
317// Shl tmp, x, #-ldist
318// OP dst, dst, tmp
319// with
320// Ror dst, x, #rdist
321bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
322 HUShr* ushr,
323 HShl* shl) {
324 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
325 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
326 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
327 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
328 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
329 ReplaceRotateWithRor(op, ushr, shl);
330 return true;
331 }
332 return false;
333}
334
335// Replace code looking like (x >>> -d OP x << d):
336// Neg neg, d
337// UShr dst, x, neg
338// Shl tmp, x, d
339// OP dst, dst, tmp
340// with
341// Neg neg, d
342// Ror dst, x, neg
343// *** OR ***
344// Replace code looking like (x >>> d OP x << -d):
345// UShr dst, x, d
346// Neg neg, d
347// Shl tmp, x, neg
348// OP dst, dst, tmp
349// with
350// Ror dst, x, d
351bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
352 HUShr* ushr,
353 HShl* shl) {
354 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
355 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
356 bool neg_is_left = shl->GetRight()->IsNeg();
357 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
358 // And the shift distance being negated is the distance being shifted the other way.
359 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
360 ReplaceRotateWithRor(op, ushr, shl);
361 }
362 return false;
363}
364
365// Try replacing code looking like (x >>> d OP x << (#bits - d)):
366// UShr dst, x, d
367// Sub ld, #bits, d
368// Shl tmp, x, ld
369// OP dst, dst, tmp
370// with
371// Ror dst, x, d
372// *** OR ***
373// Replace code looking like (x >>> (#bits - d) OP x << d):
374// Sub rd, #bits, d
375// UShr dst, x, rd
376// Shl tmp, x, d
377// OP dst, dst, tmp
378// with
379// Neg neg, d
380// Ror dst, x, neg
381bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
382 HUShr* ushr,
383 HShl* shl) {
384 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
385 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
386 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
387 HInstruction* shl_shift = shl->GetRight();
388 HInstruction* ushr_shift = ushr->GetRight();
389 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
390 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
391 return ReplaceRotateWithRor(op, ushr, shl);
392 }
393 return false;
394}
395
Calin Juravle10e244f2015-01-26 18:54:32 +0000396void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
397 HInstruction* obj = null_check->InputAt(0);
398 if (!obj->CanBeNull()) {
399 null_check->ReplaceWith(obj);
400 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000401 if (stats_ != nullptr) {
402 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
403 }
404 }
405}
406
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100407bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
408 if (!input->CanBeNull()) {
409 return true;
410 }
411
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100412 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
413 HInstruction* use = it.Current()->GetUser();
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100414 if (use->IsNullCheck() && use->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100415 return true;
416 }
417 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100418
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100419 return false;
420}
421
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100422// Returns whether doing a type test between the class of `object` against `klass` has
423// a statically known outcome. The result of the test is stored in `outcome`.
424static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000425 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
426 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
427 ScopedObjectAccess soa(Thread::Current());
428 if (!obj_rti.IsValid()) {
429 // We run the simplifier before the reference type propagation so type info might not be
430 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100431 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000432 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100433
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100434 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100435 if (!class_rti.IsValid()) {
436 // Happens when the loaded class is unresolved.
437 return false;
438 }
439 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000440 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100441 *outcome = true;
442 return true;
443 } else if (obj_rti.IsExact()) {
444 // The test failed at compile time so will also fail at runtime.
445 *outcome = false;
446 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100447 } else if (!class_rti.IsInterface()
448 && !obj_rti.IsInterface()
449 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100450 // Different type hierarchy. The test will fail.
451 *outcome = false;
452 return true;
453 }
454 return false;
455}
456
457void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
458 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100459 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
460 if (load_class->NeedsAccessCheck()) {
461 // If we need to perform an access check we cannot remove the instruction.
462 return;
463 }
464
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100465 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100466 check_cast->ClearMustDoNullCheck();
467 }
468
469 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000470 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000471 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100472 return;
473 }
474
Vladimir Markoa65ed302016-03-14 21:21:29 +0000475 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
476 // the return value check with the `outcome` check, b/27651442 .
477 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700478 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100479 if (outcome) {
480 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000481 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700482 if (!load_class->HasUses()) {
483 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
484 // However, here we know that it cannot because the checkcast was successfull, hence
485 // the class was already loaded.
486 load_class->GetBlock()->RemoveInstruction(load_class);
487 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100488 } else {
489 // Don't do anything for exceptional cases for now. Ideally we should remove
490 // all instructions and blocks this instruction dominates.
491 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000492 }
493}
494
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100495void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100496 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100497 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
498 if (load_class->NeedsAccessCheck()) {
499 // If we need to perform an access check we cannot remove the instruction.
500 return;
501 }
502
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100503 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100504 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100505 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100506 instruction->ClearMustDoNullCheck();
507 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100508
509 HGraph* graph = GetGraph();
510 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000511 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100512 instruction->ReplaceWith(graph->GetIntConstant(0));
513 instruction->GetBlock()->RemoveInstruction(instruction);
514 RecordSimplification();
515 return;
516 }
517
Vladimir Marko24bd8952016-03-15 10:40:33 +0000518 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
519 // the return value check with the `outcome` check, b/27651442 .
520 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700521 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000522 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100523 if (outcome && can_be_null) {
524 // Type test will succeed, we just need a null test.
525 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
526 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
527 instruction->ReplaceWith(test);
528 } else {
529 // We've statically determined the result of the instanceof.
530 instruction->ReplaceWith(graph->GetIntConstant(outcome));
531 }
532 RecordSimplification();
533 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700534 if (outcome && !load_class->HasUses()) {
535 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
536 // However, here we know that it cannot because the instanceof check was successfull, hence
537 // the class was already loaded.
538 load_class->GetBlock()->RemoveInstruction(load_class);
539 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100540 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100541}
542
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100543void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
544 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100545 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100546 instruction->ClearValueCanBeNull();
547 }
548}
549
550void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
551 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100552 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100553 instruction->ClearValueCanBeNull();
554 }
555}
556
Anton Shaminbdd79352016-02-15 12:48:36 +0600557static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
558 HInstruction *lhs = cond->InputAt(0);
559 HInstruction *rhs = cond->InputAt(1);
560 switch (cond->GetKind()) {
561 case HInstruction::kEqual:
562 return new (arena) HEqual(rhs, lhs);
563 case HInstruction::kNotEqual:
564 return new (arena) HNotEqual(rhs, lhs);
565 case HInstruction::kLessThan:
566 return new (arena) HGreaterThan(rhs, lhs);
567 case HInstruction::kLessThanOrEqual:
568 return new (arena) HGreaterThanOrEqual(rhs, lhs);
569 case HInstruction::kGreaterThan:
570 return new (arena) HLessThan(rhs, lhs);
571 case HInstruction::kGreaterThanOrEqual:
572 return new (arena) HLessThanOrEqual(rhs, lhs);
573 case HInstruction::kBelow:
574 return new (arena) HAbove(rhs, lhs);
575 case HInstruction::kBelowOrEqual:
576 return new (arena) HAboveOrEqual(rhs, lhs);
577 case HInstruction::kAbove:
578 return new (arena) HBelow(rhs, lhs);
579 case HInstruction::kAboveOrEqual:
580 return new (arena) HBelowOrEqual(rhs, lhs);
581 default:
582 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
583 }
584 return nullptr;
585}
586
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000587void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100588 HInstruction* input_const = equal->GetConstantRight();
589 if (input_const != nullptr) {
590 HInstruction* input_value = equal->GetLeastConstantLeft();
591 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
592 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100593 // We are comparing the boolean to a constant which is of type int and can
594 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000595 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100596 // Replace (bool_value == true) with bool_value
597 equal->ReplaceWith(input_value);
598 block->RemoveInstruction(equal);
599 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000600 } else if (input_const->AsIntConstant()->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500601 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
602 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100603 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100604 } else {
605 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
606 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
607 block->RemoveInstruction(equal);
608 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100609 }
Mark Mendellc4701932015-04-10 13:18:51 -0400610 } else {
611 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100612 }
Mark Mendellc4701932015-04-10 13:18:51 -0400613 } else {
614 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100615 }
616}
617
David Brazdil0d13fee2015-04-17 14:52:19 +0100618void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
619 HInstruction* input_const = not_equal->GetConstantRight();
620 if (input_const != nullptr) {
621 HInstruction* input_value = not_equal->GetLeastConstantLeft();
622 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
623 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100624 // We are comparing the boolean to a constant which is of type int and can
625 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000626 if (input_const->AsIntConstant()->IsTrue()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500627 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
628 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100629 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000630 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100631 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100632 not_equal->ReplaceWith(input_value);
633 block->RemoveInstruction(not_equal);
634 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100635 } else {
636 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
637 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
638 block->RemoveInstruction(not_equal);
639 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100640 }
Mark Mendellc4701932015-04-10 13:18:51 -0400641 } else {
642 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100643 }
Mark Mendellc4701932015-04-10 13:18:51 -0400644 } else {
645 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100646 }
647}
648
649void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000650 HInstruction* input = bool_not->InputAt(0);
651 HInstruction* replace_with = nullptr;
652
653 if (input->IsIntConstant()) {
654 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000655 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000656 replace_with = GetGraph()->GetIntConstant(0);
657 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000658 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000659 replace_with = GetGraph()->GetIntConstant(1);
660 }
661 } else if (input->IsBooleanNot()) {
662 // Replace (!(!bool_value)) with bool_value.
663 replace_with = input->InputAt(0);
664 } else if (input->IsCondition() &&
665 // Don't change FP compares. The definition of compares involving
666 // NaNs forces the compares to be done as written by the user.
667 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
668 // Replace condition with its opposite.
669 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
670 }
671
672 if (replace_with != nullptr) {
673 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100674 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000675 RecordSimplification();
676 }
677}
678
679void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
680 HInstruction* replace_with = nullptr;
681 HInstruction* condition = select->GetCondition();
682 HInstruction* true_value = select->GetTrueValue();
683 HInstruction* false_value = select->GetFalseValue();
684
685 if (condition->IsBooleanNot()) {
686 // Change ((!cond) ? x : y) to (cond ? y : x).
687 condition = condition->InputAt(0);
688 std::swap(true_value, false_value);
689 select->ReplaceInput(false_value, 0);
690 select->ReplaceInput(true_value, 1);
691 select->ReplaceInput(condition, 2);
692 RecordSimplification();
693 }
694
695 if (true_value == false_value) {
696 // Replace (cond ? x : x) with (x).
697 replace_with = true_value;
698 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000699 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000700 // Replace (true ? x : y) with (x).
701 replace_with = true_value;
702 } else {
703 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000704 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000705 replace_with = false_value;
706 }
707 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000708 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000709 // Replace (cond ? true : false) with (cond).
710 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000711 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000712 // Replace (cond ? false : true) with (!cond).
713 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
714 }
715 }
716
717 if (replace_with != nullptr) {
718 select->ReplaceWith(replace_with);
719 select->GetBlock()->RemoveInstruction(select);
720 RecordSimplification();
721 }
722}
723
724void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
725 HInstruction* condition = instruction->InputAt(0);
726 if (condition->IsBooleanNot()) {
727 // Swap successors if input is negated.
728 instruction->ReplaceInput(condition->InputAt(0), 0);
729 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100730 RecordSimplification();
731 }
732}
733
Mingyao Yang0304e182015-01-30 16:41:29 -0800734void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
735 HInstruction* input = instruction->InputAt(0);
736 // If the array is a NewArray with constant size, replace the array length
737 // with the constant instruction. This helps the bounds check elimination phase.
738 if (input->IsNewArray()) {
739 input = input->InputAt(0);
740 if (input->IsIntConstant()) {
741 instruction->ReplaceWith(input);
742 }
743 }
744}
745
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000746void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000747 HInstruction* value = instruction->GetValue();
748 if (value->GetType() != Primitive::kPrimNot) return;
749
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100750 if (CanEnsureNotNullAt(value, instruction)) {
751 instruction->ClearValueCanBeNull();
752 }
753
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000754 if (value->IsArrayGet()) {
755 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
756 // If the code is just swapping elements in the array, no need for a type check.
757 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100758 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000759 }
760 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100761
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100762 if (value->IsNullConstant()) {
763 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100764 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100765 }
766
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100767 ScopedObjectAccess soa(Thread::Current());
768 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
769 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
770 if (!array_rti.IsValid()) {
771 return;
772 }
773
774 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
775 instruction->ClearNeedsTypeCheck();
776 return;
777 }
778
779 if (array_rti.IsObjectArray()) {
780 if (array_rti.IsExact()) {
781 instruction->ClearNeedsTypeCheck();
782 return;
783 }
784 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100785 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000786}
787
Vladimir Markob52bbde2016-02-12 12:06:05 +0000788static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
789 // Besides conversion to the same type, widening integral conversions are implicit,
790 // excluding conversions to long and the byte->char conversion where we need to
791 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
792 return result_type == input_type ||
793 (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimByte) ||
794 (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimShort) ||
795 (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimChar) ||
796 (result_type == Primitive::kPrimShort && input_type == Primitive::kPrimByte);
797}
798
799static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
800 // The conversion to a larger type is loss-less with the exception of two cases,
801 // - conversion to char, the only unsigned type, where we may lose some bits, and
802 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
803 // For integral to FP conversions this holds because the FP mantissa is large enough.
804 DCHECK_NE(input_type, result_type);
805 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
806 result_type != Primitive::kPrimChar &&
807 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
808}
809
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000810void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000811 HInstruction* input = instruction->GetInput();
812 Primitive::Type input_type = input->GetType();
813 Primitive::Type result_type = instruction->GetResultType();
814 if (IsTypeConversionImplicit(input_type, result_type)) {
815 // Remove the implicit conversion; this includes conversion to the same type.
816 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000817 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000818 RecordSimplification();
819 return;
820 }
821
822 if (input->IsTypeConversion()) {
823 HTypeConversion* input_conversion = input->AsTypeConversion();
824 HInstruction* original_input = input_conversion->GetInput();
825 Primitive::Type original_type = original_input->GetType();
826
827 // When the first conversion is lossless, a direct conversion from the original type
828 // to the final type yields the same result, even for a lossy second conversion, for
829 // example float->double->int or int->double->float.
830 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
831
832 // For integral conversions, see if the first conversion loses only bits that the second
833 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
834 // conversion yields the same result, for example long->int->short or int->char->short.
835 bool integral_conversions_with_non_widening_second =
836 Primitive::IsIntegralType(input_type) &&
837 Primitive::IsIntegralType(original_type) &&
838 Primitive::IsIntegralType(result_type) &&
839 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
840
841 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
842 // If the merged conversion is implicit, do the simplification unconditionally.
843 if (IsTypeConversionImplicit(original_type, result_type)) {
844 instruction->ReplaceWith(original_input);
845 instruction->GetBlock()->RemoveInstruction(instruction);
846 if (!input_conversion->HasUses()) {
847 // Don't wait for DCE.
848 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
849 }
850 RecordSimplification();
851 return;
852 }
853 // Otherwise simplify only if the first conversion has no other use.
854 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
855 input_conversion->ReplaceWith(original_input);
856 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
857 RecordSimplification();
858 return;
859 }
860 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000861 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000862 DCHECK(Primitive::IsIntegralType(input_type));
863 HAnd* input_and = input->AsAnd();
864 HConstant* constant = input_and->GetConstantRight();
865 if (constant != nullptr) {
866 int64_t value = Int64FromConstant(constant);
867 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
868 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
869 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
870 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000871 HInstruction* original_input = input_and->GetLeastConstantLeft();
872 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
873 instruction->ReplaceWith(original_input);
874 instruction->GetBlock()->RemoveInstruction(instruction);
875 RecordSimplification();
876 return;
877 } else if (input->HasOnlyOneNonEnvironmentUse()) {
878 input_and->ReplaceWith(original_input);
879 input_and->GetBlock()->RemoveInstruction(input_and);
880 RecordSimplification();
881 return;
882 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000883 }
884 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000885 }
886}
887
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000888void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
889 HConstant* input_cst = instruction->GetConstantRight();
890 HInstruction* input_other = instruction->GetLeastConstantLeft();
Roland Levillain1a653882016-03-18 18:05:57 +0000891 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000892 // Replace code looking like
893 // ADD dst, src, 0
894 // with
895 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600896 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
897 // `x` is `-0.0`, the former expression yields `0.0`, while the later
898 // yields `-0.0`.
899 if (Primitive::IsIntegralType(instruction->GetType())) {
900 instruction->ReplaceWith(input_other);
901 instruction->GetBlock()->RemoveInstruction(instruction);
902 return;
903 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100904 }
905
906 HInstruction* left = instruction->GetLeft();
907 HInstruction* right = instruction->GetRight();
908 bool left_is_neg = left->IsNeg();
909 bool right_is_neg = right->IsNeg();
910
911 if (left_is_neg && right_is_neg) {
912 if (TryMoveNegOnInputsAfterBinop(instruction)) {
913 return;
914 }
915 }
916
917 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
918 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
919 // Replace code looking like
920 // NEG tmp, b
921 // ADD dst, a, tmp
922 // with
923 // SUB dst, a, b
924 // We do not perform the optimization if the input negation has environment
925 // uses or multiple non-environment uses as it could lead to worse code. In
926 // particular, we do not want the live range of `b` to be extended if we are
927 // not sure the initial 'NEG' instruction can be removed.
928 HInstruction* other = left_is_neg ? right : left;
929 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
930 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
931 RecordSimplification();
932 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000933 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000934 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000935
936 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000937}
938
939void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
940 HConstant* input_cst = instruction->GetConstantRight();
941 HInstruction* input_other = instruction->GetLeastConstantLeft();
942
Vladimir Marko452c1b62015-09-25 14:44:17 +0100943 if (input_cst != nullptr) {
944 int64_t value = Int64FromConstant(input_cst);
945 if (value == -1) {
946 // Replace code looking like
947 // AND dst, src, 0xFFF...FF
948 // with
949 // src
950 instruction->ReplaceWith(input_other);
951 instruction->GetBlock()->RemoveInstruction(instruction);
952 RecordSimplification();
953 return;
954 }
955 // Eliminate And from UShr+And if the And-mask contains all the bits that
956 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
957 // precisely clears the shifted-in sign bits.
958 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
959 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
960 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
961 size_t num_tail_bits_set = CTZ(value + 1);
962 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
963 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
964 instruction->ReplaceWith(input_other);
965 instruction->GetBlock()->RemoveInstruction(instruction);
966 RecordSimplification();
967 return;
968 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
969 input_other->HasOnlyOneNonEnvironmentUse()) {
970 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
971 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
972 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
973 input_other->InputAt(0),
974 input_other->InputAt(1),
975 input_other->GetDexPc());
976 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
977 input_other->GetBlock()->RemoveInstruction(input_other);
978 RecordSimplification();
979 return;
980 }
981 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000982 }
983
984 // We assume that GVN has run before, so we only perform a pointer comparison.
985 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100986 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000987 if (instruction->GetLeft() == instruction->GetRight()) {
988 // Replace code looking like
989 // AND dst, src, src
990 // with
991 // src
992 instruction->ReplaceWith(instruction->GetLeft());
993 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000994 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000995 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000996
997 TryDeMorganNegationFactoring(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000998}
999
Mark Mendellc4701932015-04-10 13:18:51 -04001000void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1001 VisitCondition(condition);
1002}
1003
1004void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1005 VisitCondition(condition);
1006}
1007
1008void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1009 VisitCondition(condition);
1010}
1011
1012void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1013 VisitCondition(condition);
1014}
1015
Anton Shaminbdd79352016-02-15 12:48:36 +06001016void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1017 VisitCondition(condition);
1018}
1019
1020void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1021 VisitCondition(condition);
1022}
1023
1024void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1025 VisitCondition(condition);
1026}
1027
1028void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1029 VisitCondition(condition);
1030}
Aart Bike9f37602015-10-09 11:15:55 -07001031
Mark Mendellc4701932015-04-10 13:18:51 -04001032void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001033 // Reverse condition if left is constant. Our code generators prefer constant
1034 // on the right hand side.
1035 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1036 HBasicBlock* block = condition->GetBlock();
1037 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1038 // If it is a fp we must set the opposite bias.
1039 if (replacement != nullptr) {
1040 if (condition->IsLtBias()) {
1041 replacement->SetBias(ComparisonBias::kGtBias);
1042 } else if (condition->IsGtBias()) {
1043 replacement->SetBias(ComparisonBias::kLtBias);
1044 }
1045 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1046 RecordSimplification();
1047
1048 condition = replacement;
1049 }
1050 }
Mark Mendellc4701932015-04-10 13:18:51 -04001051
Mark Mendellc4701932015-04-10 13:18:51 -04001052 HInstruction* left = condition->GetLeft();
1053 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001054
1055 // Try to fold an HCompare into this HCondition.
1056
Mark Mendellc4701932015-04-10 13:18:51 -04001057 // We can only replace an HCondition which compares a Compare to 0.
1058 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1059 // condition with a long, float or double comparison as input.
1060 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1061 // Conversion is not possible.
1062 return;
1063 }
1064
1065 // Is the Compare only used for this purpose?
1066 if (!left->GetUses().HasOnlyOneUse()) {
1067 // Someone else also wants the result of the compare.
1068 return;
1069 }
1070
1071 if (!left->GetEnvUses().IsEmpty()) {
1072 // There is a reference to the compare result in an environment. Do we really need it?
1073 if (GetGraph()->IsDebuggable()) {
1074 return;
1075 }
1076
1077 // We have to ensure that there are no deopt points in the sequence.
1078 if (left->HasAnyEnvironmentUseBefore(condition)) {
1079 return;
1080 }
1081 }
1082
1083 // Clean up any environment uses from the HCompare, if any.
1084 left->RemoveEnvironmentUsers();
1085
1086 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1087 condition->SetBias(left->AsCompare()->GetBias());
1088
1089 // Replace the operands of the HCondition.
1090 condition->ReplaceInput(left->InputAt(0), 0);
1091 condition->ReplaceInput(left->InputAt(1), 1);
1092
1093 // Remove the HCompare.
1094 left->GetBlock()->RemoveInstruction(left);
1095
1096 RecordSimplification();
1097}
1098
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001099void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1100 HConstant* input_cst = instruction->GetConstantRight();
1101 HInstruction* input_other = instruction->GetLeastConstantLeft();
1102 Primitive::Type type = instruction->GetType();
1103
1104 if ((input_cst != nullptr) && input_cst->IsOne()) {
1105 // Replace code looking like
1106 // DIV dst, src, 1
1107 // with
1108 // src
1109 instruction->ReplaceWith(input_other);
1110 instruction->GetBlock()->RemoveInstruction(instruction);
1111 return;
1112 }
1113
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001114 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001115 // Replace code looking like
1116 // DIV dst, src, -1
1117 // with
1118 // NEG dst, src
1119 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001120 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001121 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001122 return;
1123 }
1124
1125 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1126 // Try replacing code looking like
1127 // DIV dst, src, constant
1128 // with
1129 // MUL dst, src, 1 / constant
1130 HConstant* reciprocal = nullptr;
1131 if (type == Primitive::Primitive::kPrimDouble) {
1132 double value = input_cst->AsDoubleConstant()->GetValue();
1133 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1134 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1135 }
1136 } else {
1137 DCHECK_EQ(type, Primitive::kPrimFloat);
1138 float value = input_cst->AsFloatConstant()->GetValue();
1139 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1140 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1141 }
1142 }
1143
1144 if (reciprocal != nullptr) {
1145 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1146 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1147 RecordSimplification();
1148 return;
1149 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001150 }
1151}
1152
1153void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1154 HConstant* input_cst = instruction->GetConstantRight();
1155 HInstruction* input_other = instruction->GetLeastConstantLeft();
1156 Primitive::Type type = instruction->GetType();
1157 HBasicBlock* block = instruction->GetBlock();
1158 ArenaAllocator* allocator = GetGraph()->GetArena();
1159
1160 if (input_cst == nullptr) {
1161 return;
1162 }
1163
1164 if (input_cst->IsOne()) {
1165 // Replace code looking like
1166 // MUL dst, src, 1
1167 // with
1168 // src
1169 instruction->ReplaceWith(input_other);
1170 instruction->GetBlock()->RemoveInstruction(instruction);
1171 return;
1172 }
1173
1174 if (input_cst->IsMinusOne() &&
1175 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1176 // Replace code looking like
1177 // MUL dst, src, -1
1178 // with
1179 // NEG dst, src
1180 HNeg* neg = new (allocator) HNeg(type, input_other);
1181 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001182 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001183 return;
1184 }
1185
1186 if (Primitive::IsFloatingPointType(type) &&
1187 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1188 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1189 // Replace code looking like
1190 // FP_MUL dst, src, 2.0
1191 // with
1192 // FP_ADD dst, src, src
1193 // The 'int' and 'long' cases are handled below.
1194 block->ReplaceAndRemoveInstructionWith(instruction,
1195 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001196 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001197 return;
1198 }
1199
1200 if (Primitive::IsIntOrLongType(type)) {
1201 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001202 // Even though constant propagation also takes care of the zero case, other
1203 // optimizations can lead to having a zero multiplication.
1204 if (factor == 0) {
1205 // Replace code looking like
1206 // MUL dst, src, 0
1207 // with
1208 // 0
1209 instruction->ReplaceWith(input_cst);
1210 instruction->GetBlock()->RemoveInstruction(instruction);
1211 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001212 // Replace code looking like
1213 // MUL dst, src, pow_of_2
1214 // with
1215 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001216 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001217 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001218 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001219 RecordSimplification();
Alexandre Rames38db7852015-11-20 15:02:45 +00001220 } else if (IsPowerOfTwo(factor - 1)) {
1221 // Transform code looking like
1222 // MUL dst, src, (2^n + 1)
1223 // into
1224 // SHL tmp, src, n
1225 // ADD dst, src, tmp
1226 HShl* shl = new (allocator) HShl(type,
1227 input_other,
1228 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1229 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1230
1231 block->InsertInstructionBefore(shl, instruction);
1232 block->ReplaceAndRemoveInstructionWith(instruction, add);
1233 RecordSimplification();
1234 } else if (IsPowerOfTwo(factor + 1)) {
1235 // Transform code looking like
1236 // MUL dst, src, (2^n - 1)
1237 // into
1238 // SHL tmp, src, n
1239 // SUB dst, tmp, src
1240 HShl* shl = new (allocator) HShl(type,
1241 input_other,
1242 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1243 HSub* sub = new (allocator) HSub(type, shl, input_other);
1244
1245 block->InsertInstructionBefore(shl, instruction);
1246 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1247 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001248 }
1249 }
1250}
1251
Alexandre Rames188d4312015-04-09 18:30:21 +01001252void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1253 HInstruction* input = instruction->GetInput();
1254 if (input->IsNeg()) {
1255 // Replace code looking like
1256 // NEG tmp, src
1257 // NEG dst, tmp
1258 // with
1259 // src
1260 HNeg* previous_neg = input->AsNeg();
1261 instruction->ReplaceWith(previous_neg->GetInput());
1262 instruction->GetBlock()->RemoveInstruction(instruction);
1263 // We perform the optimization even if the input negation has environment
1264 // uses since it allows removing the current instruction. But we only delete
1265 // the input negation only if it is does not have any uses left.
1266 if (!previous_neg->HasUses()) {
1267 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1268 }
1269 RecordSimplification();
1270 return;
1271 }
1272
Serguei Katkov339dfc22015-04-20 12:29:32 +06001273 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1274 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001275 // Replace code looking like
1276 // SUB tmp, a, b
1277 // NEG dst, tmp
1278 // with
1279 // SUB dst, b, a
1280 // We do not perform the optimization if the input subtraction has
1281 // environment uses or multiple non-environment uses as it could lead to
1282 // worse code. In particular, we do not want the live ranges of `a` and `b`
1283 // to be extended if we are not sure the initial 'SUB' instruction can be
1284 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001285 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001286 HSub* sub = input->AsSub();
1287 HSub* new_sub =
1288 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1289 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1290 if (!sub->HasUses()) {
1291 sub->GetBlock()->RemoveInstruction(sub);
1292 }
1293 RecordSimplification();
1294 }
1295}
1296
1297void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1298 HInstruction* input = instruction->GetInput();
1299 if (input->IsNot()) {
1300 // Replace code looking like
1301 // NOT tmp, src
1302 // NOT dst, tmp
1303 // with
1304 // src
1305 // We perform the optimization even if the input negation has environment
1306 // uses since it allows removing the current instruction. But we only delete
1307 // the input negation only if it is does not have any uses left.
1308 HNot* previous_not = input->AsNot();
1309 instruction->ReplaceWith(previous_not->GetInput());
1310 instruction->GetBlock()->RemoveInstruction(instruction);
1311 if (!previous_not->HasUses()) {
1312 previous_not->GetBlock()->RemoveInstruction(previous_not);
1313 }
1314 RecordSimplification();
1315 }
1316}
1317
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001318void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1319 HConstant* input_cst = instruction->GetConstantRight();
1320 HInstruction* input_other = instruction->GetLeastConstantLeft();
1321
Roland Levillain1a653882016-03-18 18:05:57 +00001322 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001323 // Replace code looking like
1324 // OR dst, src, 0
1325 // with
1326 // src
1327 instruction->ReplaceWith(input_other);
1328 instruction->GetBlock()->RemoveInstruction(instruction);
1329 return;
1330 }
1331
1332 // We assume that GVN has run before, so we only perform a pointer comparison.
1333 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001334 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001335 if (instruction->GetLeft() == instruction->GetRight()) {
1336 // Replace code looking like
1337 // OR dst, src, src
1338 // with
1339 // src
1340 instruction->ReplaceWith(instruction->GetLeft());
1341 instruction->GetBlock()->RemoveInstruction(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001342 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001343 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001344
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001345 if (TryDeMorganNegationFactoring(instruction)) return;
1346
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001347 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001348}
1349
1350void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1351 VisitShift(instruction);
1352}
1353
1354void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1355 VisitShift(instruction);
1356}
1357
1358void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1359 HConstant* input_cst = instruction->GetConstantRight();
1360 HInstruction* input_other = instruction->GetLeastConstantLeft();
1361
Serguei Katkov115b53f2015-08-05 17:03:30 +06001362 Primitive::Type type = instruction->GetType();
1363 if (Primitive::IsFloatingPointType(type)) {
1364 return;
1365 }
1366
Roland Levillain1a653882016-03-18 18:05:57 +00001367 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001368 // Replace code looking like
1369 // SUB dst, src, 0
1370 // with
1371 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001372 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1373 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1374 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001375 instruction->ReplaceWith(input_other);
1376 instruction->GetBlock()->RemoveInstruction(instruction);
1377 return;
1378 }
1379
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001380 HBasicBlock* block = instruction->GetBlock();
1381 ArenaAllocator* allocator = GetGraph()->GetArena();
1382
Alexandre Rames188d4312015-04-09 18:30:21 +01001383 HInstruction* left = instruction->GetLeft();
1384 HInstruction* right = instruction->GetRight();
1385 if (left->IsConstant()) {
1386 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001387 // Replace code looking like
1388 // SUB dst, 0, src
1389 // with
1390 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001391 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001392 // `x` is `0.0`, the former expression yields `0.0`, while the later
1393 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001394 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001395 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001396 RecordSimplification();
1397 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001398 }
1399 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001400
1401 if (left->IsNeg() && right->IsNeg()) {
1402 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1403 return;
1404 }
1405 }
1406
1407 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1408 // Replace code looking like
1409 // NEG tmp, b
1410 // SUB dst, a, tmp
1411 // with
1412 // ADD dst, a, b
1413 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1414 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1415 RecordSimplification();
1416 right->GetBlock()->RemoveInstruction(right);
1417 return;
1418 }
1419
1420 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1421 // Replace code looking like
1422 // NEG tmp, a
1423 // SUB dst, tmp, b
1424 // with
1425 // ADD tmp, a, b
1426 // NEG dst, tmp
1427 // The second version is not intrinsically better, but enables more
1428 // transformations.
1429 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1430 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1431 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1432 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1433 instruction->ReplaceWith(neg);
1434 instruction->GetBlock()->RemoveInstruction(instruction);
1435 RecordSimplification();
1436 left->GetBlock()->RemoveInstruction(left);
1437 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001438}
1439
1440void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1441 VisitShift(instruction);
1442}
1443
1444void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1445 HConstant* input_cst = instruction->GetConstantRight();
1446 HInstruction* input_other = instruction->GetLeastConstantLeft();
1447
Roland Levillain1a653882016-03-18 18:05:57 +00001448 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001449 // Replace code looking like
1450 // XOR dst, src, 0
1451 // with
1452 // src
1453 instruction->ReplaceWith(input_other);
1454 instruction->GetBlock()->RemoveInstruction(instruction);
1455 return;
1456 }
1457
1458 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1459 // Replace code looking like
1460 // XOR dst, src, 0xFFF...FF
1461 // with
1462 // NOT dst, src
1463 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1464 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001465 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001466 return;
1467 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001468
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001469 HInstruction* left = instruction->GetLeft();
1470 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001471 if (((left->IsNot() && right->IsNot()) ||
1472 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001473 left->HasOnlyOneNonEnvironmentUse() &&
1474 right->HasOnlyOneNonEnvironmentUse()) {
1475 // Replace code looking like
1476 // NOT nota, a
1477 // NOT notb, b
1478 // XOR dst, nota, notb
1479 // with
1480 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001481 instruction->ReplaceInput(left->InputAt(0), 0);
1482 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001483 left->GetBlock()->RemoveInstruction(left);
1484 right->GetBlock()->RemoveInstruction(right);
1485 RecordSimplification();
1486 return;
1487 }
1488
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001489 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001490}
1491
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001492void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1493 HInstruction* argument = instruction->InputAt(1);
1494 HInstruction* receiver = instruction->InputAt(0);
1495 if (receiver == argument) {
1496 // Because String.equals is an instance call, the receiver is
1497 // a null check if we don't know it's null. The argument however, will
1498 // be the actual object. So we cannot end up in a situation where both
1499 // are equal but could be null.
1500 DCHECK(CanEnsureNotNullAt(argument, instruction));
1501 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1502 instruction->GetBlock()->RemoveInstruction(instruction);
1503 } else {
1504 StringEqualsOptimizations optimizations(instruction);
1505 if (CanEnsureNotNullAt(argument, instruction)) {
1506 optimizations.SetArgumentNotNull();
1507 }
1508 ScopedObjectAccess soa(Thread::Current());
1509 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1510 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1511 optimizations.SetArgumentIsString();
1512 }
1513 }
1514}
1515
Roland Levillain22c49222016-03-18 14:04:28 +00001516void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1517 bool is_left,
1518 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001519 DCHECK(invoke->IsInvokeStaticOrDirect());
1520 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001521 HInstruction* value = invoke->InputAt(0);
1522 HInstruction* distance = invoke->InputAt(1);
1523 // Replace the invoke with an HRor.
1524 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001525 // Unconditionally set the type of the negated distance to `int`,
1526 // as shift and rotate operations expect a 32-bit (or narrower)
1527 // value for their distance input.
1528 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001529 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1530 }
Roland Levillain22c49222016-03-18 14:04:28 +00001531 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001532 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1533 // Remove ClinitCheck and LoadClass, if possible.
1534 HInstruction* clinit = invoke->InputAt(invoke->InputCount() - 1);
1535 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1536 clinit->GetBlock()->RemoveInstruction(clinit);
1537 HInstruction* ldclass = clinit->InputAt(0);
1538 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1539 ldclass->GetBlock()->RemoveInstruction(ldclass);
1540 }
1541 }
1542}
1543
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001544static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1545 if (potential_length->IsArrayLength()) {
1546 return potential_length->InputAt(0) == potential_array;
1547 }
1548
1549 if (potential_array->IsNewArray()) {
1550 return potential_array->InputAt(0) == potential_length;
1551 }
1552
1553 return false;
1554}
1555
1556void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1557 HInstruction* source = instruction->InputAt(0);
1558 HInstruction* destination = instruction->InputAt(2);
1559 HInstruction* count = instruction->InputAt(4);
1560 SystemArrayCopyOptimizations optimizations(instruction);
1561 if (CanEnsureNotNullAt(source, instruction)) {
1562 optimizations.SetSourceIsNotNull();
1563 }
1564 if (CanEnsureNotNullAt(destination, instruction)) {
1565 optimizations.SetDestinationIsNotNull();
1566 }
1567 if (destination == source) {
1568 optimizations.SetDestinationIsSource();
1569 }
1570
1571 if (IsArrayLengthOf(count, source)) {
1572 optimizations.SetCountIsSourceLength();
1573 }
1574
1575 if (IsArrayLengthOf(count, destination)) {
1576 optimizations.SetCountIsDestinationLength();
1577 }
1578
1579 {
1580 ScopedObjectAccess soa(Thread::Current());
1581 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1582 if (destination_rti.IsValid()) {
1583 if (destination_rti.IsObjectArray()) {
1584 if (destination_rti.IsExact()) {
1585 optimizations.SetDoesNotNeedTypeCheck();
1586 }
1587 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001588 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001589 if (destination_rti.IsPrimitiveArrayClass()) {
1590 optimizations.SetDestinationIsPrimitiveArray();
1591 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1592 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001593 }
1594 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001595 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1596 if (source_rti.IsValid()) {
1597 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1598 optimizations.SetDoesNotNeedTypeCheck();
1599 }
1600 if (source_rti.IsPrimitiveArrayClass()) {
1601 optimizations.SetSourceIsPrimitiveArray();
1602 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1603 optimizations.SetSourceIsNonPrimitiveArray();
1604 }
1605 }
1606 }
1607}
1608
Roland Levillaina5c4a402016-03-15 15:02:50 +00001609void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1610 bool is_signum,
1611 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001612 DCHECK(invoke->IsInvokeStaticOrDirect());
1613 uint32_t dex_pc = invoke->GetDexPc();
1614 HInstruction* left = invoke->InputAt(0);
1615 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001616 if (!is_signum) {
1617 right = invoke->InputAt(1);
1618 } else if (type == Primitive::kPrimLong) {
1619 right = GetGraph()->GetLongConstant(0);
1620 } else {
1621 right = GetGraph()->GetIntConstant(0);
1622 }
1623 HCompare* compare = new (GetGraph()->GetArena())
1624 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1625 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1626}
1627
Aart Bik75a38b22016-02-17 10:41:50 -08001628void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1629 DCHECK(invoke->IsInvokeStaticOrDirect());
1630 uint32_t dex_pc = invoke->GetDexPc();
1631 // IsNaN(x) is the same as x != x.
1632 HInstruction* x = invoke->InputAt(0);
1633 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001634 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001635 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1636}
1637
Aart Bik2a6aad92016-02-25 11:32:32 -08001638void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1639 DCHECK(invoke->IsInvokeStaticOrDirect());
1640 uint32_t dex_pc = invoke->GetDexPc();
1641 HInstruction* x = invoke->InputAt(0);
1642 Primitive::Type type = x->GetType();
1643 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1644 HInstruction* nan;
1645 if (type == Primitive::kPrimDouble) {
1646 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1647 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1648 kNeedsEnvironmentOrCache,
1649 kNoSideEffects,
1650 kNoThrow);
1651 } else {
1652 DCHECK_EQ(type, Primitive::kPrimFloat);
1653 nan = GetGraph()->GetIntConstant(0x7fc00000);
1654 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1655 kNeedsEnvironmentOrCache,
1656 kNoSideEffects,
1657 kNoThrow);
1658 }
1659 // Test IsNaN(x), which is the same as x != x.
1660 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1661 condition->SetBias(ComparisonBias::kLtBias);
1662 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1663 // Select between the two.
1664 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1665 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1666 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1667}
1668
Aart Bik11932592016-03-08 12:42:25 -08001669void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1670 uint32_t dex_pc = invoke->GetDexPc();
1671 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1672 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1673}
1674
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001675void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001676 switch (instruction->GetIntrinsic()) {
1677 case Intrinsics::kStringEquals:
1678 SimplifyStringEquals(instruction);
1679 break;
1680 case Intrinsics::kSystemArrayCopy:
1681 SimplifySystemArrayCopy(instruction);
1682 break;
1683 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001684 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1685 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001686 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001687 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001688 break;
1689 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001690 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1691 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001692 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001693 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001694 break;
1695 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001696 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1697 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001698 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001699 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001700 break;
1701 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001702 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1703 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001704 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001705 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001706 break;
1707 case Intrinsics::kFloatIsNaN:
1708 case Intrinsics::kDoubleIsNaN:
1709 SimplifyIsNaN(instruction);
1710 break;
1711 case Intrinsics::kFloatFloatToIntBits:
1712 case Intrinsics::kDoubleDoubleToLongBits:
1713 SimplifyFP2Int(instruction);
1714 break;
Aart Bik11932592016-03-08 12:42:25 -08001715 case Intrinsics::kUnsafeLoadFence:
1716 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1717 break;
1718 case Intrinsics::kUnsafeStoreFence:
1719 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1720 break;
1721 case Intrinsics::kUnsafeFullFence:
1722 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1723 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001724 default:
1725 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001726 }
1727}
1728
Aart Bikbb245d12015-10-19 11:05:03 -07001729void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1730 HInstruction* cond = deoptimize->InputAt(0);
1731 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001732 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001733 // Never deopt: instruction can be removed.
1734 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1735 } else {
1736 // Always deopt.
1737 }
1738 }
1739}
1740
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001741} // namespace art