blob: d14716601c60021af951ee704dc6153657eb8fee [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_simplifier.h"
18
Andreas Gampec6ea7d02017-02-01 16:46:28 -080019#include "art_method-inl.h"
20#include "class_linker-inl.h"
Aart Bik71bf7b42016-11-16 10:17:46 -080021#include "escape.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010022#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "mirror/class-inl.h"
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000024#include "sharpening.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026
Nicolas Geoffray3c049742014-09-24 18:10:46 +010027namespace art {
28
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010029class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000030 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000031 InstructionSimplifierVisitor(HGraph* graph,
32 CodeGenerator* codegen,
Vladimir Marko65979462017-05-19 17:25:12 +010033 CompilerDriver* compiler_driver,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000034 OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010035 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000036 codegen_(codegen),
Vladimir Marko65979462017-05-19 17:25:12 +010037 compiler_driver_(compiler_driver),
Alexandre Rames188d4312015-04-09 18:30:21 +010038 stats_(stats) {}
39
40 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000041
42 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010043 void RecordSimplification() {
44 simplification_occurred_ = true;
45 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000046 MaybeRecordStat(kInstructionSimplifications);
47 }
48
49 void MaybeRecordStat(MethodCompilationStat stat) {
50 if (stats_ != nullptr) {
51 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010052 }
53 }
54
Scott Wakeling40a04bf2015-12-11 09:50:36 +000055 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
56 bool TryReplaceWithRotate(HBinaryOperation* instruction);
57 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
58 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
59 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
60
Alexandre Rames188d4312015-04-09 18:30:21 +010061 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000062 // `op` should be either HOr or HAnd.
63 // De Morgan's laws:
64 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
65 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010066 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
67 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
68
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000069 void VisitShift(HBinaryOperation* shift);
70
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000071 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010072 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
73 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010074 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
75 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000076 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000077 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000078 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080079 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000080 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000081 void VisitAdd(HAdd* instruction) OVERRIDE;
82 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040083 void VisitCondition(HCondition* instruction) OVERRIDE;
84 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
85 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
86 void VisitLessThan(HLessThan* condition) OVERRIDE;
87 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060088 void VisitBelow(HBelow* condition) OVERRIDE;
89 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
90 void VisitAbove(HAbove* condition) OVERRIDE;
91 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000092 void VisitDiv(HDiv* instruction) OVERRIDE;
93 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010094 void VisitNeg(HNeg* instruction) OVERRIDE;
95 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000096 void VisitOr(HOr* instruction) OVERRIDE;
97 void VisitShl(HShl* instruction) OVERRIDE;
98 void VisitShr(HShr* instruction) OVERRIDE;
99 void VisitSub(HSub* instruction) OVERRIDE;
100 void VisitUShr(HUShr* instruction) OVERRIDE;
101 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +0000102 void VisitSelect(HSelect* select) OVERRIDE;
103 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100104 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100105 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -0700106 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100107
108 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000109
Roland Levillain22c49222016-03-18 14:04:28 +0000110 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100111 void SimplifySystemArrayCopy(HInvoke* invoke);
112 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000113 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800114 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800115 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100116 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100117 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800118 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800119 void SimplifyReturnThis(HInvoke* invoke);
120 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800121 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100122
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000123 CodeGenerator* codegen_;
Vladimir Marko65979462017-05-19 17:25:12 +0100124 CompilerDriver* compiler_driver_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000125 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100126 bool simplification_occurred_ = false;
127 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700128 // We ensure we do not loop infinitely. The value should not be too high, since that
129 // would allow looping around the same basic block too many times. The value should
130 // not be too low either, however, since we want to allow revisiting a basic block
131 // with many statements and simplifications at least once.
132 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000133};
134
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135void InstructionSimplifier::Run() {
Vladimir Marko65979462017-05-19 17:25:12 +0100136 InstructionSimplifierVisitor visitor(graph_, codegen_, compiler_driver_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100137 visitor.Run();
138}
139
140void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100141 // Iterate in reverse post order to open up more simplifications to users
142 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100143 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100144 // The simplification of an instruction to another instruction may yield
145 // possibilities for other simplifications. So although we perform a reverse
146 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100147 do {
148 simplification_occurred_ = false;
149 VisitBasicBlock(block);
150 } while (simplification_occurred_ &&
151 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100152 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100153 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154}
155
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000156namespace {
157
158bool AreAllBitsSet(HConstant* constant) {
159 return Int64FromConstant(constant) == -1;
160}
161
162} // namespace
163
Alexandre Rames188d4312015-04-09 18:30:21 +0100164// Returns true if the code was simplified to use only one negation operation
165// after the binary operation instead of one on each of the inputs.
166bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
167 DCHECK(binop->IsAdd() || binop->IsSub());
168 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
169 HNeg* left_neg = binop->GetLeft()->AsNeg();
170 HNeg* right_neg = binop->GetRight()->AsNeg();
171 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
172 !right_neg->HasOnlyOneNonEnvironmentUse()) {
173 return false;
174 }
175 // Replace code looking like
176 // NEG tmp1, a
177 // NEG tmp2, b
178 // ADD dst, tmp1, tmp2
179 // with
180 // ADD tmp, a, b
181 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600182 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
183 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
184 // while the later yields `-0.0`.
185 if (!Primitive::IsIntegralType(binop->GetType())) {
186 return false;
187 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100188 binop->ReplaceInput(left_neg->GetInput(), 0);
189 binop->ReplaceInput(right_neg->GetInput(), 1);
190 left_neg->GetBlock()->RemoveInstruction(left_neg);
191 right_neg->GetBlock()->RemoveInstruction(right_neg);
192 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
193 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
194 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
195 RecordSimplification();
196 return true;
197}
198
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000199bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
200 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
201 Primitive::Type type = op->GetType();
202 HInstruction* left = op->GetLeft();
203 HInstruction* right = op->GetRight();
204
205 // We can apply De Morgan's laws if both inputs are Not's and are only used
206 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000207 if (((left->IsNot() && right->IsNot()) ||
208 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000209 left->HasOnlyOneNonEnvironmentUse() &&
210 right->HasOnlyOneNonEnvironmentUse()) {
211 // Replace code looking like
212 // NOT nota, a
213 // NOT notb, b
214 // AND dst, nota, notb (respectively OR)
215 // with
216 // OR or, a, b (respectively AND)
217 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000218 HInstruction* src_left = left->InputAt(0);
219 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000220 uint32_t dex_pc = op->GetDexPc();
221
222 // Remove the negations on the inputs.
223 left->ReplaceWith(src_left);
224 right->ReplaceWith(src_right);
225 left->GetBlock()->RemoveInstruction(left);
226 right->GetBlock()->RemoveInstruction(right);
227
228 // Replace the `HAnd` or `HOr`.
229 HBinaryOperation* hbin;
230 if (op->IsAnd()) {
231 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
232 } else {
233 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
234 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000235 HInstruction* hnot;
236 if (left->IsBooleanNot()) {
237 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
238 } else {
239 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
240 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000241
242 op->GetBlock()->InsertInstructionBefore(hbin, op);
243 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
244
245 RecordSimplification();
246 return true;
247 }
248
249 return false;
250}
251
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000252void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
253 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100254 HInstruction* shift_amount = instruction->GetRight();
255 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000256
Alexandre Rames50518442016-06-27 11:39:19 +0100257 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
258 ? kMaxLongShiftDistance
259 : kMaxIntShiftDistance;
260
261 if (shift_amount->IsConstant()) {
262 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700263 int64_t masked_cst = cst & implicit_mask;
264 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400265 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100266 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400267 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100268 // value
269 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400270 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100271 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100272 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700273 } else if (masked_cst != cst) {
274 // Replace code looking like
275 // SHL dst, value, cst
276 // where cst exceeds maximum distance with the equivalent
277 // SHL dst, value, cst & implicit_mask
278 // (as defined by shift semantics). This ensures other
279 // optimizations do not need to special case for such situations.
280 DCHECK_EQ(shift_amount->GetType(), Primitive::kPrimInt);
281 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index */ 1);
282 RecordSimplification();
283 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100284 }
285 }
286
287 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
288 // unnecessary explicit masking operations on the shift amount.
289 // Replace code looking like
290 // AND masked_shift, shift, <superset of implicit mask>
291 // SHL dst, value, masked_shift
292 // with
293 // SHL dst, value, shift
294 if (shift_amount->IsAnd()) {
295 HAnd* and_insn = shift_amount->AsAnd();
296 HConstant* mask = and_insn->GetConstantRight();
297 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
298 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
299 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400300 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000301 }
302}
303
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000304static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
305 return (sub->GetRight() == other &&
306 sub->GetLeft()->IsConstant() &&
307 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
308}
309
310bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
311 HUShr* ushr,
312 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000313 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
314 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000315 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
316 if (!ushr->HasUses()) {
317 ushr->GetBlock()->RemoveInstruction(ushr);
318 }
319 if (!ushr->GetRight()->HasUses()) {
320 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
321 }
322 if (!shl->HasUses()) {
323 shl->GetBlock()->RemoveInstruction(shl);
324 }
325 if (!shl->GetRight()->HasUses()) {
326 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
327 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100328 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000329 return true;
330}
331
332// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
333bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000334 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
335 HInstruction* left = op->GetLeft();
336 HInstruction* right = op->GetRight();
337 // If we have an UShr and a Shl (in either order).
338 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
339 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
340 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
341 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
342 if (ushr->GetType() == shl->GetType() &&
343 ushr->GetLeft() == shl->GetLeft()) {
344 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
345 // Shift distances are both constant, try replacing with Ror if they
346 // add up to the register size.
347 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
348 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
349 // Shift distances are potentially of the form x and (reg_size - x).
350 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
351 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
352 // Shift distances are potentially of the form d and -d.
353 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
354 }
355 }
356 }
357 return false;
358}
359
360// Try replacing code looking like (x >>> #rdist OP x << #ldist):
361// UShr dst, x, #rdist
362// Shl tmp, x, #ldist
363// OP dst, dst, tmp
364// or like (x >>> #rdist OP x << #-ldist):
365// UShr dst, x, #rdist
366// Shl tmp, x, #-ldist
367// OP dst, dst, tmp
368// with
369// Ror dst, x, #rdist
370bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
371 HUShr* ushr,
372 HShl* shl) {
373 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
374 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
375 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
376 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
377 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
378 ReplaceRotateWithRor(op, ushr, shl);
379 return true;
380 }
381 return false;
382}
383
384// Replace code looking like (x >>> -d OP x << d):
385// Neg neg, d
386// UShr dst, x, neg
387// Shl tmp, x, d
388// OP dst, dst, tmp
389// with
390// Neg neg, d
391// Ror dst, x, neg
392// *** OR ***
393// Replace code looking like (x >>> d OP x << -d):
394// UShr dst, x, d
395// Neg neg, d
396// Shl tmp, x, neg
397// OP dst, dst, tmp
398// with
399// Ror dst, x, d
400bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
401 HUShr* ushr,
402 HShl* shl) {
403 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
404 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
405 bool neg_is_left = shl->GetRight()->IsNeg();
406 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
407 // And the shift distance being negated is the distance being shifted the other way.
408 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
409 ReplaceRotateWithRor(op, ushr, shl);
410 }
411 return false;
412}
413
414// Try replacing code looking like (x >>> d OP x << (#bits - d)):
415// UShr dst, x, d
416// Sub ld, #bits, d
417// Shl tmp, x, ld
418// OP dst, dst, tmp
419// with
420// Ror dst, x, d
421// *** OR ***
422// Replace code looking like (x >>> (#bits - d) OP x << d):
423// Sub rd, #bits, d
424// UShr dst, x, rd
425// Shl tmp, x, d
426// OP dst, dst, tmp
427// with
428// Neg neg, d
429// Ror dst, x, neg
430bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
431 HUShr* ushr,
432 HShl* shl) {
433 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
434 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
435 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
436 HInstruction* shl_shift = shl->GetRight();
437 HInstruction* ushr_shift = ushr->GetRight();
438 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
439 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
440 return ReplaceRotateWithRor(op, ushr, shl);
441 }
442 return false;
443}
444
Calin Juravle10e244f2015-01-26 18:54:32 +0000445void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
446 HInstruction* obj = null_check->InputAt(0);
447 if (!obj->CanBeNull()) {
448 null_check->ReplaceWith(obj);
449 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000450 if (stats_ != nullptr) {
451 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
452 }
453 }
454}
455
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100456bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
457 if (!input->CanBeNull()) {
458 return true;
459 }
460
Vladimir Marko46817b82016-03-29 12:21:58 +0100461 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
462 HInstruction* user = use.GetUser();
463 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100464 return true;
465 }
466 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100467
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100468 return false;
469}
470
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100471// Returns whether doing a type test between the class of `object` against `klass` has
472// a statically known outcome. The result of the test is stored in `outcome`.
473static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000474 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
475 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
476 ScopedObjectAccess soa(Thread::Current());
477 if (!obj_rti.IsValid()) {
478 // We run the simplifier before the reference type propagation so type info might not be
479 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100480 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000481 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100482
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100483 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100484 if (!class_rti.IsValid()) {
485 // Happens when the loaded class is unresolved.
486 return false;
487 }
488 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000489 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100490 *outcome = true;
491 return true;
492 } else if (obj_rti.IsExact()) {
493 // The test failed at compile time so will also fail at runtime.
494 *outcome = false;
495 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100496 } else if (!class_rti.IsInterface()
497 && !obj_rti.IsInterface()
498 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100499 // Different type hierarchy. The test will fail.
500 *outcome = false;
501 return true;
502 }
503 return false;
504}
505
506void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
507 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100508 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
509 if (load_class->NeedsAccessCheck()) {
510 // If we need to perform an access check we cannot remove the instruction.
511 return;
512 }
513
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100514 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100515 check_cast->ClearMustDoNullCheck();
516 }
517
518 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000519 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000520 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100521 return;
522 }
523
Vladimir Markoa65ed302016-03-14 21:21:29 +0000524 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
525 // the return value check with the `outcome` check, b/27651442 .
526 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700527 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100528 if (outcome) {
529 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000530 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700531 if (!load_class->HasUses()) {
532 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
533 // However, here we know that it cannot because the checkcast was successfull, hence
534 // the class was already loaded.
535 load_class->GetBlock()->RemoveInstruction(load_class);
536 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100537 } else {
538 // Don't do anything for exceptional cases for now. Ideally we should remove
539 // all instructions and blocks this instruction dominates.
540 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000541 }
542}
543
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100544void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100545 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100546 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
547 if (load_class->NeedsAccessCheck()) {
548 // If we need to perform an access check we cannot remove the instruction.
549 return;
550 }
551
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100552 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100553 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100554 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100555 instruction->ClearMustDoNullCheck();
556 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100557
558 HGraph* graph = GetGraph();
559 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000560 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100561 instruction->ReplaceWith(graph->GetIntConstant(0));
562 instruction->GetBlock()->RemoveInstruction(instruction);
563 RecordSimplification();
564 return;
565 }
566
Vladimir Marko24bd8952016-03-15 10:40:33 +0000567 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
568 // the return value check with the `outcome` check, b/27651442 .
569 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700570 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000571 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100572 if (outcome && can_be_null) {
573 // Type test will succeed, we just need a null test.
574 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
575 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
576 instruction->ReplaceWith(test);
577 } else {
578 // We've statically determined the result of the instanceof.
579 instruction->ReplaceWith(graph->GetIntConstant(outcome));
580 }
581 RecordSimplification();
582 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700583 if (outcome && !load_class->HasUses()) {
584 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
585 // However, here we know that it cannot because the instanceof check was successfull, hence
586 // the class was already loaded.
587 load_class->GetBlock()->RemoveInstruction(load_class);
588 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100589 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100590}
591
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100592void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
593 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100594 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100595 instruction->ClearValueCanBeNull();
596 }
597}
598
599void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
600 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100601 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100602 instruction->ClearValueCanBeNull();
603 }
604}
605
Anton Shaminbdd79352016-02-15 12:48:36 +0600606static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
607 HInstruction *lhs = cond->InputAt(0);
608 HInstruction *rhs = cond->InputAt(1);
609 switch (cond->GetKind()) {
610 case HInstruction::kEqual:
611 return new (arena) HEqual(rhs, lhs);
612 case HInstruction::kNotEqual:
613 return new (arena) HNotEqual(rhs, lhs);
614 case HInstruction::kLessThan:
615 return new (arena) HGreaterThan(rhs, lhs);
616 case HInstruction::kLessThanOrEqual:
617 return new (arena) HGreaterThanOrEqual(rhs, lhs);
618 case HInstruction::kGreaterThan:
619 return new (arena) HLessThan(rhs, lhs);
620 case HInstruction::kGreaterThanOrEqual:
621 return new (arena) HLessThanOrEqual(rhs, lhs);
622 case HInstruction::kBelow:
623 return new (arena) HAbove(rhs, lhs);
624 case HInstruction::kBelowOrEqual:
625 return new (arena) HAboveOrEqual(rhs, lhs);
626 case HInstruction::kAbove:
627 return new (arena) HBelow(rhs, lhs);
628 case HInstruction::kAboveOrEqual:
629 return new (arena) HBelowOrEqual(rhs, lhs);
630 default:
631 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
632 }
633 return nullptr;
634}
635
Aart Bik2767f4b2016-10-28 15:03:53 -0700636static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
637 if (input->GetType() == Primitive::kPrimBoolean) {
638 return true; // input has direct boolean type
639 } else if (cmp->GetUses().HasExactlyOneElement()) {
640 // Comparison also has boolean type if both its input and the instruction
641 // itself feed into the same phi node.
642 HInstruction* user = cmp->GetUses().front().GetUser();
643 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
644 }
645 return false;
646}
647
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000648void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100649 HInstruction* input_const = equal->GetConstantRight();
650 if (input_const != nullptr) {
651 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700652 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100653 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100654 // We are comparing the boolean to a constant which is of type int and can
655 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000656 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100657 // Replace (bool_value == true) with bool_value
658 equal->ReplaceWith(input_value);
659 block->RemoveInstruction(equal);
660 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000661 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700662 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500663 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
664 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100665 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100666 } else {
667 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
668 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
669 block->RemoveInstruction(equal);
670 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100671 }
Mark Mendellc4701932015-04-10 13:18:51 -0400672 } else {
673 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100674 }
Mark Mendellc4701932015-04-10 13:18:51 -0400675 } else {
676 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100677 }
678}
679
David Brazdil0d13fee2015-04-17 14:52:19 +0100680void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
681 HInstruction* input_const = not_equal->GetConstantRight();
682 if (input_const != nullptr) {
683 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700684 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100685 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100686 // We are comparing the boolean to a constant which is of type int and can
687 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000688 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700689 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500690 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
691 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100692 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000693 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100694 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100695 not_equal->ReplaceWith(input_value);
696 block->RemoveInstruction(not_equal);
697 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100698 } else {
699 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
700 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
701 block->RemoveInstruction(not_equal);
702 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100703 }
Mark Mendellc4701932015-04-10 13:18:51 -0400704 } else {
705 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100706 }
Mark Mendellc4701932015-04-10 13:18:51 -0400707 } else {
708 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100709 }
710}
711
712void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000713 HInstruction* input = bool_not->InputAt(0);
714 HInstruction* replace_with = nullptr;
715
716 if (input->IsIntConstant()) {
717 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000718 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000719 replace_with = GetGraph()->GetIntConstant(0);
720 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000721 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000722 replace_with = GetGraph()->GetIntConstant(1);
723 }
724 } else if (input->IsBooleanNot()) {
725 // Replace (!(!bool_value)) with bool_value.
726 replace_with = input->InputAt(0);
727 } else if (input->IsCondition() &&
728 // Don't change FP compares. The definition of compares involving
729 // NaNs forces the compares to be done as written by the user.
730 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
731 // Replace condition with its opposite.
732 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
733 }
734
735 if (replace_with != nullptr) {
736 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100737 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000738 RecordSimplification();
739 }
740}
741
742void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
743 HInstruction* replace_with = nullptr;
744 HInstruction* condition = select->GetCondition();
745 HInstruction* true_value = select->GetTrueValue();
746 HInstruction* false_value = select->GetFalseValue();
747
748 if (condition->IsBooleanNot()) {
749 // Change ((!cond) ? x : y) to (cond ? y : x).
750 condition = condition->InputAt(0);
751 std::swap(true_value, false_value);
752 select->ReplaceInput(false_value, 0);
753 select->ReplaceInput(true_value, 1);
754 select->ReplaceInput(condition, 2);
755 RecordSimplification();
756 }
757
758 if (true_value == false_value) {
759 // Replace (cond ? x : x) with (x).
760 replace_with = true_value;
761 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000762 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000763 // Replace (true ? x : y) with (x).
764 replace_with = true_value;
765 } else {
766 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000767 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000768 replace_with = false_value;
769 }
770 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000771 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000772 // Replace (cond ? true : false) with (cond).
773 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000774 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000775 // Replace (cond ? false : true) with (!cond).
776 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
777 }
778 }
779
780 if (replace_with != nullptr) {
781 select->ReplaceWith(replace_with);
782 select->GetBlock()->RemoveInstruction(select);
783 RecordSimplification();
784 }
785}
786
787void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
788 HInstruction* condition = instruction->InputAt(0);
789 if (condition->IsBooleanNot()) {
790 // Swap successors if input is negated.
791 instruction->ReplaceInput(condition->InputAt(0), 0);
792 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100793 RecordSimplification();
794 }
795}
796
Mingyao Yang0304e182015-01-30 16:41:29 -0800797void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
798 HInstruction* input = instruction->InputAt(0);
799 // If the array is a NewArray with constant size, replace the array length
800 // with the constant instruction. This helps the bounds check elimination phase.
801 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000802 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -0800803 if (input->IsIntConstant()) {
804 instruction->ReplaceWith(input);
805 }
806 }
807}
808
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000809void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000810 HInstruction* value = instruction->GetValue();
811 if (value->GetType() != Primitive::kPrimNot) return;
812
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100813 if (CanEnsureNotNullAt(value, instruction)) {
814 instruction->ClearValueCanBeNull();
815 }
816
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000817 if (value->IsArrayGet()) {
818 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
819 // If the code is just swapping elements in the array, no need for a type check.
820 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100821 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000822 }
823 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100824
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100825 if (value->IsNullConstant()) {
826 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100827 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100828 }
829
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100830 ScopedObjectAccess soa(Thread::Current());
831 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
832 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
833 if (!array_rti.IsValid()) {
834 return;
835 }
836
837 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
838 instruction->ClearNeedsTypeCheck();
839 return;
840 }
841
842 if (array_rti.IsObjectArray()) {
843 if (array_rti.IsExact()) {
844 instruction->ClearNeedsTypeCheck();
845 return;
846 }
847 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100848 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000849}
850
Vladimir Markob52bbde2016-02-12 12:06:05 +0000851static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100852 // Invariant: We should never generate a conversion to a Boolean value.
853 DCHECK_NE(Primitive::kPrimBoolean, result_type);
854
Vladimir Markob52bbde2016-02-12 12:06:05 +0000855 // Besides conversion to the same type, widening integral conversions are implicit,
856 // excluding conversions to long and the byte->char conversion where we need to
857 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
858 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100859 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
860 input_type == Primitive::kPrimByte ||
861 input_type == Primitive::kPrimShort ||
862 input_type == Primitive::kPrimChar)) ||
863 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
864 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
865 input_type == Primitive::kPrimByte)) ||
866 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000867}
868
869static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
870 // The conversion to a larger type is loss-less with the exception of two cases,
871 // - conversion to char, the only unsigned type, where we may lose some bits, and
872 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
873 // For integral to FP conversions this holds because the FP mantissa is large enough.
874 DCHECK_NE(input_type, result_type);
875 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
876 result_type != Primitive::kPrimChar &&
877 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
878}
879
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000880void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000881 HInstruction* input = instruction->GetInput();
882 Primitive::Type input_type = input->GetType();
883 Primitive::Type result_type = instruction->GetResultType();
884 if (IsTypeConversionImplicit(input_type, result_type)) {
885 // Remove the implicit conversion; this includes conversion to the same type.
886 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000887 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000888 RecordSimplification();
889 return;
890 }
891
892 if (input->IsTypeConversion()) {
893 HTypeConversion* input_conversion = input->AsTypeConversion();
894 HInstruction* original_input = input_conversion->GetInput();
895 Primitive::Type original_type = original_input->GetType();
896
897 // When the first conversion is lossless, a direct conversion from the original type
898 // to the final type yields the same result, even for a lossy second conversion, for
899 // example float->double->int or int->double->float.
900 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
901
902 // For integral conversions, see if the first conversion loses only bits that the second
903 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
904 // conversion yields the same result, for example long->int->short or int->char->short.
905 bool integral_conversions_with_non_widening_second =
906 Primitive::IsIntegralType(input_type) &&
907 Primitive::IsIntegralType(original_type) &&
908 Primitive::IsIntegralType(result_type) &&
909 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
910
911 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
912 // If the merged conversion is implicit, do the simplification unconditionally.
913 if (IsTypeConversionImplicit(original_type, result_type)) {
914 instruction->ReplaceWith(original_input);
915 instruction->GetBlock()->RemoveInstruction(instruction);
916 if (!input_conversion->HasUses()) {
917 // Don't wait for DCE.
918 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
919 }
920 RecordSimplification();
921 return;
922 }
923 // Otherwise simplify only if the first conversion has no other use.
924 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
925 input_conversion->ReplaceWith(original_input);
926 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
927 RecordSimplification();
928 return;
929 }
930 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000931 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000932 DCHECK(Primitive::IsIntegralType(input_type));
933 HAnd* input_and = input->AsAnd();
934 HConstant* constant = input_and->GetConstantRight();
935 if (constant != nullptr) {
936 int64_t value = Int64FromConstant(constant);
937 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
938 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
939 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
940 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000941 HInstruction* original_input = input_and->GetLeastConstantLeft();
942 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
943 instruction->ReplaceWith(original_input);
944 instruction->GetBlock()->RemoveInstruction(instruction);
945 RecordSimplification();
946 return;
947 } else if (input->HasOnlyOneNonEnvironmentUse()) {
948 input_and->ReplaceWith(original_input);
949 input_and->GetBlock()->RemoveInstruction(input_and);
950 RecordSimplification();
951 return;
952 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000953 }
954 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000955 }
956}
957
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000958void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
959 HConstant* input_cst = instruction->GetConstantRight();
960 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600961 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +0000962 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000963 // Replace code looking like
964 // ADD dst, src, 0
965 // with
966 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600967 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
968 // `x` is `-0.0`, the former expression yields `0.0`, while the later
969 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600970 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +0600971 instruction->ReplaceWith(input_other);
972 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100973 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +0600974 return;
975 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100976 }
977
978 HInstruction* left = instruction->GetLeft();
979 HInstruction* right = instruction->GetRight();
980 bool left_is_neg = left->IsNeg();
981 bool right_is_neg = right->IsNeg();
982
983 if (left_is_neg && right_is_neg) {
984 if (TryMoveNegOnInputsAfterBinop(instruction)) {
985 return;
986 }
987 }
988
989 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
990 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
991 // Replace code looking like
992 // NEG tmp, b
993 // ADD dst, a, tmp
994 // with
995 // SUB dst, a, b
996 // We do not perform the optimization if the input negation has environment
997 // uses or multiple non-environment uses as it could lead to worse code. In
998 // particular, we do not want the live range of `b` to be extended if we are
999 // not sure the initial 'NEG' instruction can be removed.
1000 HInstruction* other = left_is_neg ? right : left;
1001 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
1002 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1003 RecordSimplification();
1004 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001005 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001006 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001007
Anton Kirilove14dc862016-05-13 17:56:15 +01001008 if (TryReplaceWithRotate(instruction)) {
1009 return;
1010 }
1011
1012 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1013 // so no need to return.
1014 TryHandleAssociativeAndCommutativeOperation(instruction);
1015
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001016 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001017 TrySubtractionChainSimplification(instruction)) {
1018 return;
1019 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001020
1021 if (integral_type) {
1022 // Replace code patterns looking like
1023 // SUB dst1, x, y SUB dst1, x, y
1024 // ADD dst2, dst1, y ADD dst2, y, dst1
1025 // with
1026 // SUB dst1, x, y
1027 // ADD instruction is not needed in this case, we may use
1028 // one of inputs of SUB instead.
1029 if (left->IsSub() && left->InputAt(1) == right) {
1030 instruction->ReplaceWith(left->InputAt(0));
1031 RecordSimplification();
1032 instruction->GetBlock()->RemoveInstruction(instruction);
1033 return;
1034 } else if (right->IsSub() && right->InputAt(1) == left) {
1035 instruction->ReplaceWith(right->InputAt(0));
1036 RecordSimplification();
1037 instruction->GetBlock()->RemoveInstruction(instruction);
1038 return;
1039 }
1040 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001041}
1042
1043void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1044 HConstant* input_cst = instruction->GetConstantRight();
1045 HInstruction* input_other = instruction->GetLeastConstantLeft();
1046
Vladimir Marko452c1b62015-09-25 14:44:17 +01001047 if (input_cst != nullptr) {
1048 int64_t value = Int64FromConstant(input_cst);
1049 if (value == -1) {
1050 // Replace code looking like
1051 // AND dst, src, 0xFFF...FF
1052 // with
1053 // src
1054 instruction->ReplaceWith(input_other);
1055 instruction->GetBlock()->RemoveInstruction(instruction);
1056 RecordSimplification();
1057 return;
1058 }
1059 // Eliminate And from UShr+And if the And-mask contains all the bits that
1060 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1061 // precisely clears the shifted-in sign bits.
1062 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1063 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1064 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1065 size_t num_tail_bits_set = CTZ(value + 1);
1066 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1067 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1068 instruction->ReplaceWith(input_other);
1069 instruction->GetBlock()->RemoveInstruction(instruction);
1070 RecordSimplification();
1071 return;
1072 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1073 input_other->HasOnlyOneNonEnvironmentUse()) {
1074 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1075 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1076 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1077 input_other->InputAt(0),
1078 input_other->InputAt(1),
1079 input_other->GetDexPc());
1080 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1081 input_other->GetBlock()->RemoveInstruction(input_other);
1082 RecordSimplification();
1083 return;
1084 }
1085 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001086 }
1087
1088 // We assume that GVN has run before, so we only perform a pointer comparison.
1089 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001090 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001091 if (instruction->GetLeft() == instruction->GetRight()) {
1092 // Replace code looking like
1093 // AND dst, src, src
1094 // with
1095 // src
1096 instruction->ReplaceWith(instruction->GetLeft());
1097 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001098 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001099 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001100 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001101
Anton Kirilove14dc862016-05-13 17:56:15 +01001102 if (TryDeMorganNegationFactoring(instruction)) {
1103 return;
1104 }
1105
1106 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1107 // so no need to return.
1108 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001109}
1110
Mark Mendellc4701932015-04-10 13:18:51 -04001111void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1112 VisitCondition(condition);
1113}
1114
1115void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1116 VisitCondition(condition);
1117}
1118
1119void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1120 VisitCondition(condition);
1121}
1122
1123void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1124 VisitCondition(condition);
1125}
1126
Anton Shaminbdd79352016-02-15 12:48:36 +06001127void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1128 VisitCondition(condition);
1129}
1130
1131void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1132 VisitCondition(condition);
1133}
1134
1135void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1136 VisitCondition(condition);
1137}
1138
1139void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1140 VisitCondition(condition);
1141}
Aart Bike9f37602015-10-09 11:15:55 -07001142
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001143// Recognize the following pattern:
1144// obj.getClass() ==/!= Foo.class
1145// And replace it with a constant value if the type of `obj` is statically known.
1146static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1147 HInstruction* input_one = condition->InputAt(0);
1148 HInstruction* input_two = condition->InputAt(1);
1149 HLoadClass* load_class = input_one->IsLoadClass()
1150 ? input_one->AsLoadClass()
1151 : input_two->AsLoadClass();
1152 if (load_class == nullptr) {
1153 return false;
1154 }
1155
1156 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1157 if (!class_rti.IsValid()) {
1158 // Unresolved class.
1159 return false;
1160 }
1161
1162 HInstanceFieldGet* field_get = (load_class == input_one)
1163 ? input_two->AsInstanceFieldGet()
1164 : input_one->AsInstanceFieldGet();
1165 if (field_get == nullptr) {
1166 return false;
1167 }
1168
1169 HInstruction* receiver = field_get->InputAt(0);
1170 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1171 if (!receiver_type.IsExact()) {
1172 return false;
1173 }
1174
1175 {
1176 ScopedObjectAccess soa(Thread::Current());
1177 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1178 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
1179 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1180 if (field_get->GetFieldInfo().GetField() != field) {
1181 return false;
1182 }
1183
1184 // We can replace the compare.
1185 int value = 0;
1186 if (receiver_type.IsEqual(class_rti)) {
1187 value = condition->IsEqual() ? 1 : 0;
1188 } else {
1189 value = condition->IsNotEqual() ? 1 : 0;
1190 }
1191 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1192 return true;
1193 }
1194}
1195
Mark Mendellc4701932015-04-10 13:18:51 -04001196void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001197 if (condition->IsEqual() || condition->IsNotEqual()) {
1198 if (RecognizeAndSimplifyClassCheck(condition)) {
1199 return;
1200 }
1201 }
1202
Anton Shaminbdd79352016-02-15 12:48:36 +06001203 // Reverse condition if left is constant. Our code generators prefer constant
1204 // on the right hand side.
1205 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1206 HBasicBlock* block = condition->GetBlock();
1207 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1208 // If it is a fp we must set the opposite bias.
1209 if (replacement != nullptr) {
1210 if (condition->IsLtBias()) {
1211 replacement->SetBias(ComparisonBias::kGtBias);
1212 } else if (condition->IsGtBias()) {
1213 replacement->SetBias(ComparisonBias::kLtBias);
1214 }
1215 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1216 RecordSimplification();
1217
1218 condition = replacement;
1219 }
1220 }
Mark Mendellc4701932015-04-10 13:18:51 -04001221
Mark Mendellc4701932015-04-10 13:18:51 -04001222 HInstruction* left = condition->GetLeft();
1223 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001224
1225 // Try to fold an HCompare into this HCondition.
1226
Mark Mendellc4701932015-04-10 13:18:51 -04001227 // We can only replace an HCondition which compares a Compare to 0.
1228 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1229 // condition with a long, float or double comparison as input.
1230 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1231 // Conversion is not possible.
1232 return;
1233 }
1234
1235 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001236 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001237 // Someone else also wants the result of the compare.
1238 return;
1239 }
1240
Vladimir Marko46817b82016-03-29 12:21:58 +01001241 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001242 // There is a reference to the compare result in an environment. Do we really need it?
1243 if (GetGraph()->IsDebuggable()) {
1244 return;
1245 }
1246
1247 // We have to ensure that there are no deopt points in the sequence.
1248 if (left->HasAnyEnvironmentUseBefore(condition)) {
1249 return;
1250 }
1251 }
1252
1253 // Clean up any environment uses from the HCompare, if any.
1254 left->RemoveEnvironmentUsers();
1255
1256 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1257 condition->SetBias(left->AsCompare()->GetBias());
1258
1259 // Replace the operands of the HCondition.
1260 condition->ReplaceInput(left->InputAt(0), 0);
1261 condition->ReplaceInput(left->InputAt(1), 1);
1262
1263 // Remove the HCompare.
1264 left->GetBlock()->RemoveInstruction(left);
1265
1266 RecordSimplification();
1267}
1268
Andreas Gampe9186ced2016-12-12 14:28:21 -08001269// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1270static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1271 // True, if the most significant bits of divisor are 0.
1272 return ((divisor & 0x7fffff) == 0);
1273}
1274
1275// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1276static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1277 // True, if the most significant bits of divisor are 0.
1278 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1279}
1280
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001281void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1282 HConstant* input_cst = instruction->GetConstantRight();
1283 HInstruction* input_other = instruction->GetLeastConstantLeft();
1284 Primitive::Type type = instruction->GetType();
1285
1286 if ((input_cst != nullptr) && input_cst->IsOne()) {
1287 // Replace code looking like
1288 // DIV dst, src, 1
1289 // with
1290 // src
1291 instruction->ReplaceWith(input_other);
1292 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001293 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001294 return;
1295 }
1296
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001297 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001298 // Replace code looking like
1299 // DIV dst, src, -1
1300 // with
1301 // NEG dst, src
1302 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001303 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001304 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001305 return;
1306 }
1307
1308 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1309 // Try replacing code looking like
1310 // DIV dst, src, constant
1311 // with
1312 // MUL dst, src, 1 / constant
1313 HConstant* reciprocal = nullptr;
1314 if (type == Primitive::Primitive::kPrimDouble) {
1315 double value = input_cst->AsDoubleConstant()->GetValue();
1316 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1317 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1318 }
1319 } else {
1320 DCHECK_EQ(type, Primitive::kPrimFloat);
1321 float value = input_cst->AsFloatConstant()->GetValue();
1322 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1323 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1324 }
1325 }
1326
1327 if (reciprocal != nullptr) {
1328 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1329 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1330 RecordSimplification();
1331 return;
1332 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001333 }
1334}
1335
1336void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1337 HConstant* input_cst = instruction->GetConstantRight();
1338 HInstruction* input_other = instruction->GetLeastConstantLeft();
1339 Primitive::Type type = instruction->GetType();
1340 HBasicBlock* block = instruction->GetBlock();
1341 ArenaAllocator* allocator = GetGraph()->GetArena();
1342
1343 if (input_cst == nullptr) {
1344 return;
1345 }
1346
1347 if (input_cst->IsOne()) {
1348 // Replace code looking like
1349 // MUL dst, src, 1
1350 // with
1351 // src
1352 instruction->ReplaceWith(input_other);
1353 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001354 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001355 return;
1356 }
1357
1358 if (input_cst->IsMinusOne() &&
1359 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1360 // Replace code looking like
1361 // MUL dst, src, -1
1362 // with
1363 // NEG dst, src
1364 HNeg* neg = new (allocator) HNeg(type, input_other);
1365 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001366 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001367 return;
1368 }
1369
1370 if (Primitive::IsFloatingPointType(type) &&
1371 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1372 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1373 // Replace code looking like
1374 // FP_MUL dst, src, 2.0
1375 // with
1376 // FP_ADD dst, src, src
1377 // The 'int' and 'long' cases are handled below.
1378 block->ReplaceAndRemoveInstructionWith(instruction,
1379 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001380 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001381 return;
1382 }
1383
1384 if (Primitive::IsIntOrLongType(type)) {
1385 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001386 // Even though constant propagation also takes care of the zero case, other
1387 // optimizations can lead to having a zero multiplication.
1388 if (factor == 0) {
1389 // Replace code looking like
1390 // MUL dst, src, 0
1391 // with
1392 // 0
1393 instruction->ReplaceWith(input_cst);
1394 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001395 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001396 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001397 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001398 // Replace code looking like
1399 // MUL dst, src, pow_of_2
1400 // with
1401 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001402 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001403 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001404 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001405 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001406 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001407 } else if (IsPowerOfTwo(factor - 1)) {
1408 // Transform code looking like
1409 // MUL dst, src, (2^n + 1)
1410 // into
1411 // SHL tmp, src, n
1412 // ADD dst, src, tmp
1413 HShl* shl = new (allocator) HShl(type,
1414 input_other,
1415 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1416 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1417
1418 block->InsertInstructionBefore(shl, instruction);
1419 block->ReplaceAndRemoveInstructionWith(instruction, add);
1420 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001421 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001422 } else if (IsPowerOfTwo(factor + 1)) {
1423 // Transform code looking like
1424 // MUL dst, src, (2^n - 1)
1425 // into
1426 // SHL tmp, src, n
1427 // SUB dst, tmp, src
1428 HShl* shl = new (allocator) HShl(type,
1429 input_other,
1430 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1431 HSub* sub = new (allocator) HSub(type, shl, input_other);
1432
1433 block->InsertInstructionBefore(shl, instruction);
1434 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1435 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001436 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001437 }
1438 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001439
1440 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1441 // so no need to return.
1442 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001443}
1444
Alexandre Rames188d4312015-04-09 18:30:21 +01001445void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1446 HInstruction* input = instruction->GetInput();
1447 if (input->IsNeg()) {
1448 // Replace code looking like
1449 // NEG tmp, src
1450 // NEG dst, tmp
1451 // with
1452 // src
1453 HNeg* previous_neg = input->AsNeg();
1454 instruction->ReplaceWith(previous_neg->GetInput());
1455 instruction->GetBlock()->RemoveInstruction(instruction);
1456 // We perform the optimization even if the input negation has environment
1457 // uses since it allows removing the current instruction. But we only delete
1458 // the input negation only if it is does not have any uses left.
1459 if (!previous_neg->HasUses()) {
1460 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1461 }
1462 RecordSimplification();
1463 return;
1464 }
1465
Serguei Katkov339dfc22015-04-20 12:29:32 +06001466 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1467 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001468 // Replace code looking like
1469 // SUB tmp, a, b
1470 // NEG dst, tmp
1471 // with
1472 // SUB dst, b, a
1473 // We do not perform the optimization if the input subtraction has
1474 // environment uses or multiple non-environment uses as it could lead to
1475 // worse code. In particular, we do not want the live ranges of `a` and `b`
1476 // to be extended if we are not sure the initial 'SUB' instruction can be
1477 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001478 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001479 HSub* sub = input->AsSub();
1480 HSub* new_sub =
1481 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1482 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1483 if (!sub->HasUses()) {
1484 sub->GetBlock()->RemoveInstruction(sub);
1485 }
1486 RecordSimplification();
1487 }
1488}
1489
1490void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1491 HInstruction* input = instruction->GetInput();
1492 if (input->IsNot()) {
1493 // Replace code looking like
1494 // NOT tmp, src
1495 // NOT dst, tmp
1496 // with
1497 // src
1498 // We perform the optimization even if the input negation has environment
1499 // uses since it allows removing the current instruction. But we only delete
1500 // the input negation only if it is does not have any uses left.
1501 HNot* previous_not = input->AsNot();
1502 instruction->ReplaceWith(previous_not->GetInput());
1503 instruction->GetBlock()->RemoveInstruction(instruction);
1504 if (!previous_not->HasUses()) {
1505 previous_not->GetBlock()->RemoveInstruction(previous_not);
1506 }
1507 RecordSimplification();
1508 }
1509}
1510
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001511void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1512 HConstant* input_cst = instruction->GetConstantRight();
1513 HInstruction* input_other = instruction->GetLeastConstantLeft();
1514
Roland Levillain1a653882016-03-18 18:05:57 +00001515 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001516 // Replace code looking like
1517 // OR dst, src, 0
1518 // with
1519 // src
1520 instruction->ReplaceWith(input_other);
1521 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001522 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001523 return;
1524 }
1525
1526 // We assume that GVN has run before, so we only perform a pointer comparison.
1527 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001528 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001529 if (instruction->GetLeft() == instruction->GetRight()) {
1530 // Replace code looking like
1531 // OR dst, src, src
1532 // with
1533 // src
1534 instruction->ReplaceWith(instruction->GetLeft());
1535 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001536 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001537 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001538 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001539
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001540 if (TryDeMorganNegationFactoring(instruction)) return;
1541
Anton Kirilove14dc862016-05-13 17:56:15 +01001542 if (TryReplaceWithRotate(instruction)) {
1543 return;
1544 }
1545
1546 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1547 // so no need to return.
1548 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001549}
1550
1551void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1552 VisitShift(instruction);
1553}
1554
1555void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1556 VisitShift(instruction);
1557}
1558
1559void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1560 HConstant* input_cst = instruction->GetConstantRight();
1561 HInstruction* input_other = instruction->GetLeastConstantLeft();
1562
Serguei Katkov115b53f2015-08-05 17:03:30 +06001563 Primitive::Type type = instruction->GetType();
1564 if (Primitive::IsFloatingPointType(type)) {
1565 return;
1566 }
1567
Roland Levillain1a653882016-03-18 18:05:57 +00001568 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001569 // Replace code looking like
1570 // SUB dst, src, 0
1571 // with
1572 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001573 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1574 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1575 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001576 instruction->ReplaceWith(input_other);
1577 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001578 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001579 return;
1580 }
1581
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001582 HBasicBlock* block = instruction->GetBlock();
1583 ArenaAllocator* allocator = GetGraph()->GetArena();
1584
Alexandre Rames188d4312015-04-09 18:30:21 +01001585 HInstruction* left = instruction->GetLeft();
1586 HInstruction* right = instruction->GetRight();
1587 if (left->IsConstant()) {
1588 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001589 // Replace code looking like
1590 // SUB dst, 0, src
1591 // with
1592 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001593 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001594 // `x` is `0.0`, the former expression yields `0.0`, while the later
1595 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001596 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001597 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001598 RecordSimplification();
1599 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001600 }
1601 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001602
1603 if (left->IsNeg() && right->IsNeg()) {
1604 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1605 return;
1606 }
1607 }
1608
1609 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1610 // Replace code looking like
1611 // NEG tmp, b
1612 // SUB dst, a, tmp
1613 // with
1614 // ADD dst, a, b
1615 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1616 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1617 RecordSimplification();
1618 right->GetBlock()->RemoveInstruction(right);
1619 return;
1620 }
1621
1622 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1623 // Replace code looking like
1624 // NEG tmp, a
1625 // SUB dst, tmp, b
1626 // with
1627 // ADD tmp, a, b
1628 // NEG dst, tmp
1629 // The second version is not intrinsically better, but enables more
1630 // transformations.
1631 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1632 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1633 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1634 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1635 instruction->ReplaceWith(neg);
1636 instruction->GetBlock()->RemoveInstruction(instruction);
1637 RecordSimplification();
1638 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001639 return;
1640 }
1641
1642 if (TrySubtractionChainSimplification(instruction)) {
1643 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001644 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001645
1646 if (left->IsAdd()) {
1647 // Replace code patterns looking like
1648 // ADD dst1, x, y ADD dst1, x, y
1649 // SUB dst2, dst1, y SUB dst2, dst1, x
1650 // with
1651 // ADD dst1, x, y
1652 // SUB instruction is not needed in this case, we may use
1653 // one of inputs of ADD instead.
1654 // It is applicable to integral types only.
1655 DCHECK(Primitive::IsIntegralType(type));
1656 if (left->InputAt(1) == right) {
1657 instruction->ReplaceWith(left->InputAt(0));
1658 RecordSimplification();
1659 instruction->GetBlock()->RemoveInstruction(instruction);
1660 return;
1661 } else if (left->InputAt(0) == right) {
1662 instruction->ReplaceWith(left->InputAt(1));
1663 RecordSimplification();
1664 instruction->GetBlock()->RemoveInstruction(instruction);
1665 return;
1666 }
1667 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001668}
1669
1670void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1671 VisitShift(instruction);
1672}
1673
1674void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1675 HConstant* input_cst = instruction->GetConstantRight();
1676 HInstruction* input_other = instruction->GetLeastConstantLeft();
1677
Roland Levillain1a653882016-03-18 18:05:57 +00001678 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001679 // Replace code looking like
1680 // XOR dst, src, 0
1681 // with
1682 // src
1683 instruction->ReplaceWith(input_other);
1684 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001685 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001686 return;
1687 }
1688
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001689 if ((input_cst != nullptr) && input_cst->IsOne()
1690 && input_other->GetType() == Primitive::kPrimBoolean) {
1691 // Replace code looking like
1692 // XOR dst, src, 1
1693 // with
1694 // BOOLEAN_NOT dst, src
1695 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1696 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1697 RecordSimplification();
1698 return;
1699 }
1700
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001701 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1702 // Replace code looking like
1703 // XOR dst, src, 0xFFF...FF
1704 // with
1705 // NOT dst, src
1706 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1707 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001708 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001709 return;
1710 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001711
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001712 HInstruction* left = instruction->GetLeft();
1713 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001714 if (((left->IsNot() && right->IsNot()) ||
1715 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001716 left->HasOnlyOneNonEnvironmentUse() &&
1717 right->HasOnlyOneNonEnvironmentUse()) {
1718 // Replace code looking like
1719 // NOT nota, a
1720 // NOT notb, b
1721 // XOR dst, nota, notb
1722 // with
1723 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001724 instruction->ReplaceInput(left->InputAt(0), 0);
1725 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001726 left->GetBlock()->RemoveInstruction(left);
1727 right->GetBlock()->RemoveInstruction(right);
1728 RecordSimplification();
1729 return;
1730 }
1731
Anton Kirilove14dc862016-05-13 17:56:15 +01001732 if (TryReplaceWithRotate(instruction)) {
1733 return;
1734 }
1735
1736 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1737 // so no need to return.
1738 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001739}
1740
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001741void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1742 HInstruction* argument = instruction->InputAt(1);
1743 HInstruction* receiver = instruction->InputAt(0);
1744 if (receiver == argument) {
1745 // Because String.equals is an instance call, the receiver is
1746 // a null check if we don't know it's null. The argument however, will
1747 // be the actual object. So we cannot end up in a situation where both
1748 // are equal but could be null.
1749 DCHECK(CanEnsureNotNullAt(argument, instruction));
1750 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1751 instruction->GetBlock()->RemoveInstruction(instruction);
1752 } else {
1753 StringEqualsOptimizations optimizations(instruction);
1754 if (CanEnsureNotNullAt(argument, instruction)) {
1755 optimizations.SetArgumentNotNull();
1756 }
1757 ScopedObjectAccess soa(Thread::Current());
1758 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1759 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1760 optimizations.SetArgumentIsString();
1761 }
1762 }
1763}
1764
Roland Levillain22c49222016-03-18 14:04:28 +00001765void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1766 bool is_left,
1767 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001768 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001769 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001770 HInstruction* value = invoke->InputAt(0);
1771 HInstruction* distance = invoke->InputAt(1);
1772 // Replace the invoke with an HRor.
1773 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001774 // Unconditionally set the type of the negated distance to `int`,
1775 // as shift and rotate operations expect a 32-bit (or narrower)
1776 // value for their distance input.
1777 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001778 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1779 }
Roland Levillain22c49222016-03-18 14:04:28 +00001780 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001781 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1782 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001783 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001784 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1785 clinit->GetBlock()->RemoveInstruction(clinit);
1786 HInstruction* ldclass = clinit->InputAt(0);
1787 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1788 ldclass->GetBlock()->RemoveInstruction(ldclass);
1789 }
1790 }
1791}
1792
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001793static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1794 if (potential_length->IsArrayLength()) {
1795 return potential_length->InputAt(0) == potential_array;
1796 }
1797
1798 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001799 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001800 }
1801
1802 return false;
1803}
1804
1805void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1806 HInstruction* source = instruction->InputAt(0);
1807 HInstruction* destination = instruction->InputAt(2);
1808 HInstruction* count = instruction->InputAt(4);
1809 SystemArrayCopyOptimizations optimizations(instruction);
1810 if (CanEnsureNotNullAt(source, instruction)) {
1811 optimizations.SetSourceIsNotNull();
1812 }
1813 if (CanEnsureNotNullAt(destination, instruction)) {
1814 optimizations.SetDestinationIsNotNull();
1815 }
1816 if (destination == source) {
1817 optimizations.SetDestinationIsSource();
1818 }
1819
1820 if (IsArrayLengthOf(count, source)) {
1821 optimizations.SetCountIsSourceLength();
1822 }
1823
1824 if (IsArrayLengthOf(count, destination)) {
1825 optimizations.SetCountIsDestinationLength();
1826 }
1827
1828 {
1829 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001830 Primitive::Type source_component_type = Primitive::kPrimVoid;
1831 Primitive::Type destination_component_type = Primitive::kPrimVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001832 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1833 if (destination_rti.IsValid()) {
1834 if (destination_rti.IsObjectArray()) {
1835 if (destination_rti.IsExact()) {
1836 optimizations.SetDoesNotNeedTypeCheck();
1837 }
1838 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001839 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001840 if (destination_rti.IsPrimitiveArrayClass()) {
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001841 destination_component_type =
1842 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001843 optimizations.SetDestinationIsPrimitiveArray();
1844 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1845 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001846 }
1847 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001848 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1849 if (source_rti.IsValid()) {
1850 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1851 optimizations.SetDoesNotNeedTypeCheck();
1852 }
1853 if (source_rti.IsPrimitiveArrayClass()) {
1854 optimizations.SetSourceIsPrimitiveArray();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001855 source_component_type = source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001856 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1857 optimizations.SetSourceIsNonPrimitiveArray();
1858 }
1859 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001860 // For primitive arrays, use their optimized ArtMethod implementations.
1861 if ((source_component_type != Primitive::kPrimVoid) &&
1862 (source_component_type == destination_component_type)) {
1863 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1864 PointerSize image_size = class_linker->GetImagePointerSize();
1865 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
1866 mirror::Class* system = invoke->GetResolvedMethod()->GetDeclaringClass();
1867 ArtMethod* method = nullptr;
1868 switch (source_component_type) {
1869 case Primitive::kPrimBoolean:
1870 method = system->FindDeclaredDirectMethod("arraycopy", "([ZI[ZII)V", image_size);
1871 break;
1872 case Primitive::kPrimByte:
1873 method = system->FindDeclaredDirectMethod("arraycopy", "([BI[BII)V", image_size);
1874 break;
1875 case Primitive::kPrimChar:
1876 method = system->FindDeclaredDirectMethod("arraycopy", "([CI[CII)V", image_size);
1877 break;
1878 case Primitive::kPrimShort:
1879 method = system->FindDeclaredDirectMethod("arraycopy", "([SI[SII)V", image_size);
1880 break;
1881 case Primitive::kPrimInt:
1882 method = system->FindDeclaredDirectMethod("arraycopy", "([II[III)V", image_size);
1883 break;
1884 case Primitive::kPrimFloat:
1885 method = system->FindDeclaredDirectMethod("arraycopy", "([FI[FII)V", image_size);
1886 break;
1887 case Primitive::kPrimLong:
1888 method = system->FindDeclaredDirectMethod("arraycopy", "([JI[JII)V", image_size);
1889 break;
1890 case Primitive::kPrimDouble:
1891 method = system->FindDeclaredDirectMethod("arraycopy", "([DI[DII)V", image_size);
1892 break;
1893 default:
1894 LOG(FATAL) << "Unreachable";
1895 }
1896 DCHECK(method != nullptr);
1897 invoke->SetResolvedMethod(method);
1898 // Sharpen the new invoke. Note that we do not update the dex method index of
1899 // the invoke, as we would need to look it up in the current dex file, and it
1900 // is unlikely that it exists. The most usual situation for such typed
1901 // arraycopy methods is a direct pointer to the boot image.
Vladimir Marko65979462017-05-19 17:25:12 +01001902 HSharpening::SharpenInvokeStaticOrDirect(invoke, codegen_, compiler_driver_);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001903 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001904 }
1905}
1906
Roland Levillaina5c4a402016-03-15 15:02:50 +00001907void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1908 bool is_signum,
1909 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001910 DCHECK(invoke->IsInvokeStaticOrDirect());
1911 uint32_t dex_pc = invoke->GetDexPc();
1912 HInstruction* left = invoke->InputAt(0);
1913 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001914 if (!is_signum) {
1915 right = invoke->InputAt(1);
1916 } else if (type == Primitive::kPrimLong) {
1917 right = GetGraph()->GetLongConstant(0);
1918 } else {
1919 right = GetGraph()->GetIntConstant(0);
1920 }
1921 HCompare* compare = new (GetGraph()->GetArena())
1922 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1923 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1924}
1925
Aart Bik75a38b22016-02-17 10:41:50 -08001926void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1927 DCHECK(invoke->IsInvokeStaticOrDirect());
1928 uint32_t dex_pc = invoke->GetDexPc();
1929 // IsNaN(x) is the same as x != x.
1930 HInstruction* x = invoke->InputAt(0);
1931 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001932 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001933 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1934}
1935
Aart Bik2a6aad92016-02-25 11:32:32 -08001936void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1937 DCHECK(invoke->IsInvokeStaticOrDirect());
1938 uint32_t dex_pc = invoke->GetDexPc();
1939 HInstruction* x = invoke->InputAt(0);
1940 Primitive::Type type = x->GetType();
1941 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1942 HInstruction* nan;
1943 if (type == Primitive::kPrimDouble) {
1944 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1945 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1946 kNeedsEnvironmentOrCache,
1947 kNoSideEffects,
1948 kNoThrow);
1949 } else {
1950 DCHECK_EQ(type, Primitive::kPrimFloat);
1951 nan = GetGraph()->GetIntConstant(0x7fc00000);
1952 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1953 kNeedsEnvironmentOrCache,
1954 kNoSideEffects,
1955 kNoThrow);
1956 }
1957 // Test IsNaN(x), which is the same as x != x.
1958 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1959 condition->SetBias(ComparisonBias::kLtBias);
1960 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1961 // Select between the two.
1962 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1963 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1964 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1965}
1966
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001967void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1968 HInstruction* str = invoke->InputAt(0);
1969 HInstruction* index = invoke->InputAt(1);
1970 uint32_t dex_pc = invoke->GetDexPc();
1971 ArenaAllocator* arena = GetGraph()->GetArena();
1972 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1973 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1974 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1975 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00001976 HBoundsCheck* bounds_check = new (arena) HBoundsCheck(
1977 index, length, dex_pc, invoke->GetDexMethodIndex());
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001978 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00001979 HArrayGet* array_get = new (arena) HArrayGet(
1980 str, bounds_check, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001981 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1982 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1983 GetGraph()->SetHasBoundsChecks(true);
1984}
1985
Vladimir Markodce016e2016-04-28 13:10:02 +01001986void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1987 HInstruction* str = invoke->InputAt(0);
1988 uint32_t dex_pc = invoke->GetDexPc();
1989 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1990 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001991 HArrayLength* length =
1992 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001993 HInstruction* replacement;
1994 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1995 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1996 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1997 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1998 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1999 replacement = equal;
2000 } else {
2001 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
2002 replacement = length;
2003 }
2004 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
2005}
2006
Aart Bikff7d89c2016-11-07 08:49:28 -08002007// This method should only be used on intrinsics whose sole way of throwing an
2008// exception is raising a NPE when the nth argument is null. If that argument
2009// is provably non-null, we can clear the flag.
2010void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2011 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002012 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002013 invoke->SetCanThrow(false);
2014 }
2015}
2016
Aart Bik71bf7b42016-11-16 10:17:46 -08002017// Methods that return "this" can replace the returned value with the receiver.
2018void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2019 if (invoke->HasUses()) {
2020 HInstruction* receiver = invoke->InputAt(0);
2021 invoke->ReplaceWith(receiver);
2022 RecordSimplification();
2023 }
2024}
2025
2026// Helper method for StringBuffer escape analysis.
2027static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2028 if (user->IsInvokeStaticOrDirect()) {
2029 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002030 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2031 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002032 user->InputAt(0) == reference;
2033 } else if (user->IsInvokeVirtual()) {
2034 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2035 case Intrinsics::kStringBufferLength:
2036 case Intrinsics::kStringBufferToString:
2037 DCHECK_EQ(user->InputAt(0), reference);
2038 return true;
2039 case Intrinsics::kStringBufferAppend:
2040 // Returns "this", so only okay if no further uses.
2041 DCHECK_EQ(user->InputAt(0), reference);
2042 DCHECK_NE(user->InputAt(1), reference);
2043 return !user->HasUses();
2044 default:
2045 break;
2046 }
2047 }
2048 return false;
2049}
2050
2051// Certain allocation intrinsics are not removed by dead code elimination
2052// because of potentially throwing an OOM exception or other side effects.
2053// This method removes such intrinsics when special circumstances allow.
2054void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2055 if (!invoke->HasUses()) {
2056 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2057 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2058 // call does not escape since only thread-local synchronization may be removed.
2059 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2060 HInstruction* receiver = invoke->InputAt(0);
2061 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2062 invoke->GetBlock()->RemoveInstruction(invoke);
2063 RecordSimplification();
2064 }
2065 }
2066}
2067
Aart Bik11932592016-03-08 12:42:25 -08002068void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
2069 uint32_t dex_pc = invoke->GetDexPc();
2070 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
2071 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
2072}
2073
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002074void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002075 switch (instruction->GetIntrinsic()) {
2076 case Intrinsics::kStringEquals:
2077 SimplifyStringEquals(instruction);
2078 break;
2079 case Intrinsics::kSystemArrayCopy:
2080 SimplifySystemArrayCopy(instruction);
2081 break;
2082 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002083 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
2084 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002085 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002086 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002087 break;
2088 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002089 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
2090 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002091 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002092 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002093 break;
2094 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002095 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
2096 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002097 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002098 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002099 break;
2100 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002101 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
2102 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002103 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002104 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002105 break;
2106 case Intrinsics::kFloatIsNaN:
2107 case Intrinsics::kDoubleIsNaN:
2108 SimplifyIsNaN(instruction);
2109 break;
2110 case Intrinsics::kFloatFloatToIntBits:
2111 case Intrinsics::kDoubleDoubleToLongBits:
2112 SimplifyFP2Int(instruction);
2113 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002114 case Intrinsics::kStringCharAt:
2115 SimplifyStringCharAt(instruction);
2116 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002117 case Intrinsics::kStringIsEmpty:
2118 case Intrinsics::kStringLength:
2119 SimplifyStringIsEmptyOrLength(instruction);
2120 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002121 case Intrinsics::kStringStringIndexOf:
2122 case Intrinsics::kStringStringIndexOfAfter:
2123 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2124 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002125 case Intrinsics::kStringBufferAppend:
2126 case Intrinsics::kStringBuilderAppend:
2127 SimplifyReturnThis(instruction);
2128 break;
2129 case Intrinsics::kStringBufferToString:
2130 case Intrinsics::kStringBuilderToString:
2131 SimplifyAllocationIntrinsic(instruction);
2132 break;
Aart Bik11932592016-03-08 12:42:25 -08002133 case Intrinsics::kUnsafeLoadFence:
2134 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2135 break;
2136 case Intrinsics::kUnsafeStoreFence:
2137 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2138 break;
2139 case Intrinsics::kUnsafeFullFence:
2140 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2141 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002142 default:
2143 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002144 }
2145}
2146
Aart Bikbb245d12015-10-19 11:05:03 -07002147void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2148 HInstruction* cond = deoptimize->InputAt(0);
2149 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002150 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002151 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002152 if (deoptimize->GuardsAnInput()) {
2153 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2154 }
Aart Bikbb245d12015-10-19 11:05:03 -07002155 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2156 } else {
2157 // Always deopt.
2158 }
2159 }
2160}
2161
Anton Kirilove14dc862016-05-13 17:56:15 +01002162// Replace code looking like
2163// OP y, x, const1
2164// OP z, y, const2
2165// with
2166// OP z, x, const3
2167// where OP is both an associative and a commutative operation.
2168bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2169 HBinaryOperation* instruction) {
2170 DCHECK(instruction->IsCommutative());
2171
2172 if (!Primitive::IsIntegralType(instruction->GetType())) {
2173 return false;
2174 }
2175
2176 HInstruction* left = instruction->GetLeft();
2177 HInstruction* right = instruction->GetRight();
2178 // Variable names as described above.
2179 HConstant* const2;
2180 HBinaryOperation* y;
2181
2182 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
2183 const2 = right->AsConstant();
2184 y = left->AsBinaryOperation();
2185 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
2186 const2 = left->AsConstant();
2187 y = right->AsBinaryOperation();
2188 } else {
2189 // The node does not match the pattern.
2190 return false;
2191 }
2192
2193 // If `y` has more than one use, we do not perform the optimization
2194 // because it might increase code size (e.g. if the new constant is
2195 // no longer encodable as an immediate operand in the target ISA).
2196 if (!y->HasOnlyOneNonEnvironmentUse()) {
2197 return false;
2198 }
2199
2200 // GetConstantRight() can return both left and right constants
2201 // for commutative operations.
2202 HConstant* const1 = y->GetConstantRight();
2203 if (const1 == nullptr) {
2204 return false;
2205 }
2206
2207 instruction->ReplaceInput(const1, 0);
2208 instruction->ReplaceInput(const2, 1);
2209 HConstant* const3 = instruction->TryStaticEvaluation();
2210 DCHECK(const3 != nullptr);
2211 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2212 instruction->ReplaceInput(const3, 1);
2213 RecordSimplification();
2214 return true;
2215}
2216
2217static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2218 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2219}
2220
2221// Helper function that performs addition statically, considering the result type.
2222static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
2223 // Use the Compute() method for consistency with TryStaticEvaluation().
2224 if (type == Primitive::kPrimInt) {
2225 return HAdd::Compute<int32_t>(x, y);
2226 } else {
2227 DCHECK_EQ(type, Primitive::kPrimLong);
2228 return HAdd::Compute<int64_t>(x, y);
2229 }
2230}
2231
2232// Helper function that handles the child classes of HConstant
2233// and returns an integer with the appropriate sign.
2234static int64_t GetValue(HConstant* constant, bool is_negated) {
2235 int64_t ret = Int64FromConstant(constant);
2236 return is_negated ? -ret : ret;
2237}
2238
2239// Replace code looking like
2240// OP1 y, x, const1
2241// OP2 z, y, const2
2242// with
2243// OP3 z, x, const3
2244// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2245bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2246 HBinaryOperation* instruction) {
2247 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2248
2249 Primitive::Type type = instruction->GetType();
2250 if (!Primitive::IsIntegralType(type)) {
2251 return false;
2252 }
2253
2254 HInstruction* left = instruction->GetLeft();
2255 HInstruction* right = instruction->GetRight();
2256 // Variable names as described above.
2257 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2258 if (const2 == nullptr) {
2259 return false;
2260 }
2261
2262 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2263 ? left->AsBinaryOperation()
2264 : AsAddOrSub(right);
2265 // If y has more than one use, we do not perform the optimization because
2266 // it might increase code size (e.g. if the new constant is no longer
2267 // encodable as an immediate operand in the target ISA).
2268 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2269 return false;
2270 }
2271
2272 left = y->GetLeft();
2273 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2274 if (const1 == nullptr) {
2275 return false;
2276 }
2277
2278 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2279 // If both inputs are constants, let the constant folding pass deal with it.
2280 if (x->IsConstant()) {
2281 return false;
2282 }
2283
2284 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2285 int64_t const2_val = GetValue(const2, is_const2_negated);
2286 bool is_y_negated = (y == right) && instruction->IsSub();
2287 right = y->GetRight();
2288 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2289 int64_t const1_val = GetValue(const1, is_const1_negated);
2290 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2291 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2292 HBasicBlock* block = instruction->GetBlock();
2293 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2294 ArenaAllocator* arena = instruction->GetArena();
2295 HInstruction* z;
2296
2297 if (is_x_negated) {
2298 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2299 } else {
2300 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2301 }
2302
2303 block->ReplaceAndRemoveInstructionWith(instruction, z);
2304 RecordSimplification();
2305 return true;
2306}
2307
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002308} // namespace art