blob: 1a2494a992f9e88f407e1553be38dafadc772270 [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"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010021#include "data_type-inl.h"
Aart Bik71bf7b42016-11-16 10:17:46 -080022#include "escape.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010023#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "sharpening.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000027
Nicolas Geoffray3c049742014-09-24 18:10:46 +010028namespace art {
29
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010030class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000031 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000032 InstructionSimplifierVisitor(HGraph* graph,
33 CodeGenerator* codegen,
Vladimir Marko65979462017-05-19 17:25:12 +010034 CompilerDriver* compiler_driver,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000035 OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010036 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000037 codegen_(codegen),
Vladimir Marko65979462017-05-19 17:25:12 +010038 compiler_driver_(compiler_driver),
Alexandre Rames188d4312015-04-09 18:30:21 +010039 stats_(stats) {}
40
41 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000042
43 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010044 void RecordSimplification() {
45 simplification_occurred_ = true;
46 simplifications_at_current_position_++;
Igor Murashkin1e065a52017-08-09 13:20:34 -070047 MaybeRecordStat(stats_, kInstructionSimplifications);
Alexandre Rames188d4312015-04-09 18:30:21 +010048 }
49
Scott Wakeling40a04bf2015-12-11 09:50:36 +000050 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
51 bool TryReplaceWithRotate(HBinaryOperation* instruction);
52 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
53 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
54 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
55
Alexandre Rames188d4312015-04-09 18:30:21 +010056 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000057 // `op` should be either HOr or HAnd.
58 // De Morgan's laws:
59 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
60 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010061 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
62 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
Lena Djokicbc5460b2017-07-20 16:07:36 +020063 bool TryCombineVecMultiplyAccumulate(HVecMul* mul);
Anton Kirilove14dc862016-05-13 17:56:15 +010064
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000065 void VisitShift(HBinaryOperation* shift);
66
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000067 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010068 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
69 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010070 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
71 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000072 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000073 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000074 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080075 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000076 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000077 void VisitAdd(HAdd* instruction) OVERRIDE;
78 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040079 void VisitCondition(HCondition* instruction) OVERRIDE;
80 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
81 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
82 void VisitLessThan(HLessThan* condition) OVERRIDE;
83 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060084 void VisitBelow(HBelow* condition) OVERRIDE;
85 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
86 void VisitAbove(HAbove* condition) OVERRIDE;
87 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000088 void VisitDiv(HDiv* instruction) OVERRIDE;
89 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010090 void VisitNeg(HNeg* instruction) OVERRIDE;
91 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000092 void VisitOr(HOr* instruction) OVERRIDE;
93 void VisitShl(HShl* instruction) OVERRIDE;
94 void VisitShr(HShr* instruction) OVERRIDE;
95 void VisitSub(HSub* instruction) OVERRIDE;
96 void VisitUShr(HUShr* instruction) OVERRIDE;
97 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000098 void VisitSelect(HSelect* select) OVERRIDE;
99 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100100 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100101 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -0700102 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Lena Djokicbc5460b2017-07-20 16:07:36 +0200103 void VisitVecMul(HVecMul* instruction) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100104
105 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100107 void SimplifyRotate(HInvoke* invoke, bool is_left, DataType::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100108 void SimplifySystemArrayCopy(HInvoke* invoke);
109 void SimplifyStringEquals(HInvoke* invoke);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110 void SimplifyCompare(HInvoke* invoke, bool is_signum, DataType::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800111 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800112 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100113 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100114 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800115 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800116 void SimplifyReturnThis(HInvoke* invoke);
117 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800118 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100119
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000120 CodeGenerator* codegen_;
Vladimir Marko65979462017-05-19 17:25:12 +0100121 CompilerDriver* compiler_driver_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000122 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100123 bool simplification_occurred_ = false;
124 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700125 // We ensure we do not loop infinitely. The value should not be too high, since that
126 // would allow looping around the same basic block too many times. The value should
127 // not be too low either, however, since we want to allow revisiting a basic block
128 // with many statements and simplifications at least once.
129 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000130};
131
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132void InstructionSimplifier::Run() {
Vladimir Marko65979462017-05-19 17:25:12 +0100133 InstructionSimplifierVisitor visitor(graph_, codegen_, compiler_driver_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100134 visitor.Run();
135}
136
137void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100138 // Iterate in reverse post order to open up more simplifications to users
139 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100140 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100141 // The simplification of an instruction to another instruction may yield
142 // possibilities for other simplifications. So although we perform a reverse
143 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100144 do {
145 simplification_occurred_ = false;
146 VisitBasicBlock(block);
147 } while (simplification_occurred_ &&
148 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100149 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100150 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151}
152
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000153namespace {
154
155bool AreAllBitsSet(HConstant* constant) {
156 return Int64FromConstant(constant) == -1;
157}
158
159} // namespace
160
Alexandre Rames188d4312015-04-09 18:30:21 +0100161// Returns true if the code was simplified to use only one negation operation
162// after the binary operation instead of one on each of the inputs.
163bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
164 DCHECK(binop->IsAdd() || binop->IsSub());
165 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
166 HNeg* left_neg = binop->GetLeft()->AsNeg();
167 HNeg* right_neg = binop->GetRight()->AsNeg();
168 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
169 !right_neg->HasOnlyOneNonEnvironmentUse()) {
170 return false;
171 }
172 // Replace code looking like
173 // NEG tmp1, a
174 // NEG tmp2, b
175 // ADD dst, tmp1, tmp2
176 // with
177 // ADD tmp, a, b
178 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600179 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
180 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
181 // while the later yields `-0.0`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 if (!DataType::IsIntegralType(binop->GetType())) {
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600183 return false;
184 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100185 binop->ReplaceInput(left_neg->GetInput(), 0);
186 binop->ReplaceInput(right_neg->GetInput(), 1);
187 left_neg->GetBlock()->RemoveInstruction(left_neg);
188 right_neg->GetBlock()->RemoveInstruction(right_neg);
189 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
190 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
191 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
192 RecordSimplification();
193 return true;
194}
195
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000196bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
197 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100198 DataType::Type type = op->GetType();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000199 HInstruction* left = op->GetLeft();
200 HInstruction* right = op->GetRight();
201
202 // We can apply De Morgan's laws if both inputs are Not's and are only used
203 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000204 if (((left->IsNot() && right->IsNot()) ||
205 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000206 left->HasOnlyOneNonEnvironmentUse() &&
207 right->HasOnlyOneNonEnvironmentUse()) {
208 // Replace code looking like
209 // NOT nota, a
210 // NOT notb, b
211 // AND dst, nota, notb (respectively OR)
212 // with
213 // OR or, a, b (respectively AND)
214 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000215 HInstruction* src_left = left->InputAt(0);
216 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000217 uint32_t dex_pc = op->GetDexPc();
218
219 // Remove the negations on the inputs.
220 left->ReplaceWith(src_left);
221 right->ReplaceWith(src_right);
222 left->GetBlock()->RemoveInstruction(left);
223 right->GetBlock()->RemoveInstruction(right);
224
225 // Replace the `HAnd` or `HOr`.
226 HBinaryOperation* hbin;
227 if (op->IsAnd()) {
228 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
229 } else {
230 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
231 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000232 HInstruction* hnot;
233 if (left->IsBooleanNot()) {
234 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
235 } else {
236 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
237 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000238
239 op->GetBlock()->InsertInstructionBefore(hbin, op);
240 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
241
242 RecordSimplification();
243 return true;
244 }
245
246 return false;
247}
248
Lena Djokicbc5460b2017-07-20 16:07:36 +0200249bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100250 DataType::Type type = mul->GetPackedType();
Lena Djokicbc5460b2017-07-20 16:07:36 +0200251 InstructionSet isa = codegen_->GetInstructionSet();
252 switch (isa) {
253 case kArm64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100254 if (!(type == DataType::Type::kInt8 ||
255 type == DataType::Type::kUint16 ||
256 type == DataType::Type::kInt16 ||
257 type == DataType::Type::kInt32)) {
Lena Djokicbc5460b2017-07-20 16:07:36 +0200258 return false;
259 }
260 break;
261 case kMips:
262 case kMips64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100263 if (!(type == DataType::Type::kInt8 ||
264 type == DataType::Type::kUint16 ||
265 type == DataType::Type::kInt16 ||
266 type == DataType::Type::kInt32 ||
267 type == DataType::Type::kInt64)) {
Lena Djokicbc5460b2017-07-20 16:07:36 +0200268 return false;
269 }
270 break;
271 default:
272 return false;
273 }
274
275 ArenaAllocator* arena = mul->GetBlock()->GetGraph()->GetArena();
276
277 if (mul->HasOnlyOneNonEnvironmentUse()) {
278 HInstruction* use = mul->GetUses().front().GetUser();
279 if (use->IsVecAdd() || use->IsVecSub()) {
280 // Replace code looking like
281 // VECMUL tmp, x, y
282 // VECADD/SUB dst, acc, tmp
283 // with
284 // VECMULACC dst, acc, x, y
285 // Note that we do not want to (unconditionally) perform the merge when the
286 // multiplication has multiple uses and it can be merged in all of them.
287 // Multiple uses could happen on the same control-flow path, and we would
288 // then increase the amount of work. In the future we could try to evaluate
289 // whether all uses are on different control-flow paths (using dominance and
290 // reverse-dominance information) and only perform the merge when they are.
291 HInstruction* accumulator = nullptr;
292 HVecBinaryOperation* binop = use->AsVecBinaryOperation();
293 HInstruction* binop_left = binop->GetLeft();
294 HInstruction* binop_right = binop->GetRight();
295 // This is always true since the `HVecMul` has only one use (which is checked above).
296 DCHECK_NE(binop_left, binop_right);
297 if (binop_right == mul) {
298 accumulator = binop_left;
299 } else if (use->IsVecAdd()) {
300 DCHECK_EQ(binop_left, mul);
301 accumulator = binop_right;
302 }
303
304 HInstruction::InstructionKind kind =
305 use->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub;
306 if (accumulator != nullptr) {
307 HVecMultiplyAccumulate* mulacc =
308 new (arena) HVecMultiplyAccumulate(arena,
309 kind,
310 accumulator,
311 mul->GetLeft(),
312 mul->GetRight(),
313 binop->GetPackedType(),
314 binop->GetVectorLength());
315
316 binop->GetBlock()->ReplaceAndRemoveInstructionWith(binop, mulacc);
317 DCHECK(!mul->HasUses());
318 mul->GetBlock()->RemoveInstruction(mul);
319 return true;
320 }
321 }
322 }
323
324 return false;
325}
326
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000327void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
328 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100329 HInstruction* shift_amount = instruction->GetRight();
330 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100332 int64_t implicit_mask = (value->GetType() == DataType::Type::kInt64)
Alexandre Rames50518442016-06-27 11:39:19 +0100333 ? kMaxLongShiftDistance
334 : kMaxIntShiftDistance;
335
336 if (shift_amount->IsConstant()) {
337 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700338 int64_t masked_cst = cst & implicit_mask;
339 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400340 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100341 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400342 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100343 // value
344 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400345 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100346 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100347 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700348 } else if (masked_cst != cst) {
349 // Replace code looking like
350 // SHL dst, value, cst
351 // where cst exceeds maximum distance with the equivalent
352 // SHL dst, value, cst & implicit_mask
353 // (as defined by shift semantics). This ensures other
354 // optimizations do not need to special case for such situations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DCHECK_EQ(shift_amount->GetType(), DataType::Type::kInt32);
Aart Bik50e20d52017-05-05 14:07:29 -0700356 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index */ 1);
357 RecordSimplification();
358 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100359 }
360 }
361
362 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
363 // unnecessary explicit masking operations on the shift amount.
364 // Replace code looking like
365 // AND masked_shift, shift, <superset of implicit mask>
366 // SHL dst, value, masked_shift
367 // with
368 // SHL dst, value, shift
369 if (shift_amount->IsAnd()) {
370 HAnd* and_insn = shift_amount->AsAnd();
371 HConstant* mask = and_insn->GetConstantRight();
372 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
373 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
374 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400375 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000376 }
377}
378
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000379static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
380 return (sub->GetRight() == other &&
381 sub->GetLeft()->IsConstant() &&
382 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
383}
384
385bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
386 HUShr* ushr,
387 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000388 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
389 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000390 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
391 if (!ushr->HasUses()) {
392 ushr->GetBlock()->RemoveInstruction(ushr);
393 }
394 if (!ushr->GetRight()->HasUses()) {
395 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
396 }
397 if (!shl->HasUses()) {
398 shl->GetBlock()->RemoveInstruction(shl);
399 }
400 if (!shl->GetRight()->HasUses()) {
401 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
402 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100403 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000404 return true;
405}
406
407// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
408bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000409 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
410 HInstruction* left = op->GetLeft();
411 HInstruction* right = op->GetRight();
412 // If we have an UShr and a Shl (in either order).
413 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
414 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
415 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100416 DCHECK(DataType::IsIntOrLongType(ushr->GetType()));
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000417 if (ushr->GetType() == shl->GetType() &&
418 ushr->GetLeft() == shl->GetLeft()) {
419 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
420 // Shift distances are both constant, try replacing with Ror if they
421 // add up to the register size.
422 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
423 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
424 // Shift distances are potentially of the form x and (reg_size - x).
425 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
426 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
427 // Shift distances are potentially of the form d and -d.
428 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
429 }
430 }
431 }
432 return false;
433}
434
435// Try replacing code looking like (x >>> #rdist OP x << #ldist):
436// UShr dst, x, #rdist
437// Shl tmp, x, #ldist
438// OP dst, dst, tmp
439// or like (x >>> #rdist OP x << #-ldist):
440// UShr dst, x, #rdist
441// Shl tmp, x, #-ldist
442// OP dst, dst, tmp
443// with
444// Ror dst, x, #rdist
445bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
446 HUShr* ushr,
447 HShl* shl) {
448 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100449 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000450 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
451 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
452 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
453 ReplaceRotateWithRor(op, ushr, shl);
454 return true;
455 }
456 return false;
457}
458
459// Replace code looking like (x >>> -d OP x << d):
460// Neg neg, d
461// UShr dst, x, neg
462// Shl tmp, x, d
463// OP dst, dst, tmp
464// with
465// Neg neg, d
466// Ror dst, x, neg
467// *** OR ***
468// Replace code looking like (x >>> d OP x << -d):
469// UShr dst, x, d
470// Neg neg, d
471// Shl tmp, x, neg
472// OP dst, dst, tmp
473// with
474// Ror dst, x, d
475bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
476 HUShr* ushr,
477 HShl* shl) {
478 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
479 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
480 bool neg_is_left = shl->GetRight()->IsNeg();
481 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
482 // And the shift distance being negated is the distance being shifted the other way.
483 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
484 ReplaceRotateWithRor(op, ushr, shl);
485 }
486 return false;
487}
488
489// Try replacing code looking like (x >>> d OP x << (#bits - d)):
490// UShr dst, x, d
491// Sub ld, #bits, d
492// Shl tmp, x, ld
493// OP dst, dst, tmp
494// with
495// Ror dst, x, d
496// *** OR ***
497// Replace code looking like (x >>> (#bits - d) OP x << d):
498// Sub rd, #bits, d
499// UShr dst, x, rd
500// Shl tmp, x, d
501// OP dst, dst, tmp
502// with
503// Neg neg, d
504// Ror dst, x, neg
505bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
506 HUShr* ushr,
507 HShl* shl) {
508 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
509 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100510 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000511 HInstruction* shl_shift = shl->GetRight();
512 HInstruction* ushr_shift = ushr->GetRight();
513 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
514 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
515 return ReplaceRotateWithRor(op, ushr, shl);
516 }
517 return false;
518}
519
Calin Juravle10e244f2015-01-26 18:54:32 +0000520void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
521 HInstruction* obj = null_check->InputAt(0);
522 if (!obj->CanBeNull()) {
523 null_check->ReplaceWith(obj);
524 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000525 if (stats_ != nullptr) {
526 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
527 }
528 }
529}
530
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100531bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
532 if (!input->CanBeNull()) {
533 return true;
534 }
535
Vladimir Marko46817b82016-03-29 12:21:58 +0100536 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
537 HInstruction* user = use.GetUser();
538 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100539 return true;
540 }
541 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100542
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100543 return false;
544}
545
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100546// Returns whether doing a type test between the class of `object` against `klass` has
547// a statically known outcome. The result of the test is stored in `outcome`.
548static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000549 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
550 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
551 ScopedObjectAccess soa(Thread::Current());
552 if (!obj_rti.IsValid()) {
553 // We run the simplifier before the reference type propagation so type info might not be
554 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100555 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000556 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100557
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100558 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100559 if (!class_rti.IsValid()) {
560 // Happens when the loaded class is unresolved.
561 return false;
562 }
563 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000564 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100565 *outcome = true;
566 return true;
567 } else if (obj_rti.IsExact()) {
568 // The test failed at compile time so will also fail at runtime.
569 *outcome = false;
570 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100571 } else if (!class_rti.IsInterface()
572 && !obj_rti.IsInterface()
573 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100574 // Different type hierarchy. The test will fail.
575 *outcome = false;
576 return true;
577 }
578 return false;
579}
580
581void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
582 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100583 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
584 if (load_class->NeedsAccessCheck()) {
585 // If we need to perform an access check we cannot remove the instruction.
586 return;
587 }
588
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100589 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100590 check_cast->ClearMustDoNullCheck();
591 }
592
593 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000594 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700595 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100596 return;
597 }
598
Vladimir Markoa65ed302016-03-14 21:21:29 +0000599 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
600 // the return value check with the `outcome` check, b/27651442 .
601 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700602 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100603 if (outcome) {
604 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700605 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700606 if (!load_class->HasUses()) {
607 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
608 // However, here we know that it cannot because the checkcast was successfull, hence
609 // the class was already loaded.
610 load_class->GetBlock()->RemoveInstruction(load_class);
611 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100612 } else {
613 // Don't do anything for exceptional cases for now. Ideally we should remove
614 // all instructions and blocks this instruction dominates.
615 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000616 }
617}
618
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100619void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100620 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100621 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
622 if (load_class->NeedsAccessCheck()) {
623 // If we need to perform an access check we cannot remove the instruction.
624 return;
625 }
626
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100627 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100628 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100629 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100630 instruction->ClearMustDoNullCheck();
631 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100632
633 HGraph* graph = GetGraph();
634 if (object->IsNullConstant()) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700635 MaybeRecordStat(stats_, kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100636 instruction->ReplaceWith(graph->GetIntConstant(0));
637 instruction->GetBlock()->RemoveInstruction(instruction);
638 RecordSimplification();
639 return;
640 }
641
Vladimir Marko24bd8952016-03-15 10:40:33 +0000642 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
643 // the return value check with the `outcome` check, b/27651442 .
644 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700645 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700646 MaybeRecordStat(stats_, kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100647 if (outcome && can_be_null) {
648 // Type test will succeed, we just need a null test.
649 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
650 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
651 instruction->ReplaceWith(test);
652 } else {
653 // We've statically determined the result of the instanceof.
654 instruction->ReplaceWith(graph->GetIntConstant(outcome));
655 }
656 RecordSimplification();
657 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700658 if (outcome && !load_class->HasUses()) {
659 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
660 // However, here we know that it cannot because the instanceof check was successfull, hence
661 // the class was already loaded.
662 load_class->GetBlock()->RemoveInstruction(load_class);
663 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100664 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100665}
666
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100667void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100668 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100669 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100670 instruction->ClearValueCanBeNull();
671 }
672}
673
674void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100675 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100676 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100677 instruction->ClearValueCanBeNull();
678 }
679}
680
Anton Shaminbdd79352016-02-15 12:48:36 +0600681static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
682 HInstruction *lhs = cond->InputAt(0);
683 HInstruction *rhs = cond->InputAt(1);
684 switch (cond->GetKind()) {
685 case HInstruction::kEqual:
686 return new (arena) HEqual(rhs, lhs);
687 case HInstruction::kNotEqual:
688 return new (arena) HNotEqual(rhs, lhs);
689 case HInstruction::kLessThan:
690 return new (arena) HGreaterThan(rhs, lhs);
691 case HInstruction::kLessThanOrEqual:
692 return new (arena) HGreaterThanOrEqual(rhs, lhs);
693 case HInstruction::kGreaterThan:
694 return new (arena) HLessThan(rhs, lhs);
695 case HInstruction::kGreaterThanOrEqual:
696 return new (arena) HLessThanOrEqual(rhs, lhs);
697 case HInstruction::kBelow:
698 return new (arena) HAbove(rhs, lhs);
699 case HInstruction::kBelowOrEqual:
700 return new (arena) HAboveOrEqual(rhs, lhs);
701 case HInstruction::kAbove:
702 return new (arena) HBelow(rhs, lhs);
703 case HInstruction::kAboveOrEqual:
704 return new (arena) HBelowOrEqual(rhs, lhs);
705 default:
706 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
707 }
708 return nullptr;
709}
710
Aart Bik2767f4b2016-10-28 15:03:53 -0700711static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712 if (input->GetType() == DataType::Type::kBool) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700713 return true; // input has direct boolean type
714 } else if (cmp->GetUses().HasExactlyOneElement()) {
715 // Comparison also has boolean type if both its input and the instruction
716 // itself feed into the same phi node.
717 HInstruction* user = cmp->GetUses().front().GetUser();
718 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
719 }
720 return false;
721}
722
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000723void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100724 HInstruction* input_const = equal->GetConstantRight();
725 if (input_const != nullptr) {
726 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700727 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100728 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100729 // We are comparing the boolean to a constant which is of type int and can
730 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000731 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100732 // Replace (bool_value == true) with bool_value
733 equal->ReplaceWith(input_value);
734 block->RemoveInstruction(equal);
735 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000736 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700737 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500738 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
739 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100740 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100741 } else {
742 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
743 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
744 block->RemoveInstruction(equal);
745 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100746 }
Mark Mendellc4701932015-04-10 13:18:51 -0400747 } else {
748 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100749 }
Mark Mendellc4701932015-04-10 13:18:51 -0400750 } else {
751 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100752 }
753}
754
David Brazdil0d13fee2015-04-17 14:52:19 +0100755void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
756 HInstruction* input_const = not_equal->GetConstantRight();
757 if (input_const != nullptr) {
758 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700759 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100760 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100761 // We are comparing the boolean to a constant which is of type int and can
762 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000763 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700764 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500765 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
766 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100767 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000768 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100769 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100770 not_equal->ReplaceWith(input_value);
771 block->RemoveInstruction(not_equal);
772 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100773 } else {
774 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
775 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
776 block->RemoveInstruction(not_equal);
777 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100778 }
Mark Mendellc4701932015-04-10 13:18:51 -0400779 } else {
780 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100781 }
Mark Mendellc4701932015-04-10 13:18:51 -0400782 } else {
783 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100784 }
785}
786
787void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000788 HInstruction* input = bool_not->InputAt(0);
789 HInstruction* replace_with = nullptr;
790
791 if (input->IsIntConstant()) {
792 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000793 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000794 replace_with = GetGraph()->GetIntConstant(0);
795 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000796 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000797 replace_with = GetGraph()->GetIntConstant(1);
798 }
799 } else if (input->IsBooleanNot()) {
800 // Replace (!(!bool_value)) with bool_value.
801 replace_with = input->InputAt(0);
802 } else if (input->IsCondition() &&
803 // Don't change FP compares. The definition of compares involving
804 // NaNs forces the compares to be done as written by the user.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100805 !DataType::IsFloatingPointType(input->InputAt(0)->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000806 // Replace condition with its opposite.
807 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
808 }
809
810 if (replace_with != nullptr) {
811 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100812 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000813 RecordSimplification();
814 }
815}
816
Aart Bik4f7dd342017-09-12 13:12:57 -0700817// Constructs a new ABS(x) node in the HIR.
818static HInstruction* NewIntegralAbs(ArenaAllocator* arena, HInstruction* x, HInstruction* cursor) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 DataType::Type type = x->GetType();
820 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Aart Bik4f7dd342017-09-12 13:12:57 -0700821 // Construct a fake intrinsic with as much context as is needed to allocate one.
822 // The intrinsic will always be lowered into code later anyway.
823 // TODO: b/65164101 : moving towards a real HAbs node makes more sense.
824 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
825 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress,
826 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
827 0u
828 };
829 HInvokeStaticOrDirect* invoke = new (arena) HInvokeStaticOrDirect(
830 arena,
831 1,
832 type,
833 x->GetDexPc(),
834 /*method_idx*/ -1,
835 /*resolved_method*/ nullptr,
836 dispatch_info,
837 kStatic,
838 MethodReference(nullptr, dex::kDexNoIndex),
839 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
840 invoke->SetArgumentAt(0, x);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100841 invoke->SetIntrinsic(type == DataType::Type::kInt32 ? Intrinsics::kMathAbsInt
842 : Intrinsics::kMathAbsLong,
Aart Bik4f7dd342017-09-12 13:12:57 -0700843 kNoEnvironmentOrCache,
844 kNoSideEffects,
845 kNoThrow);
846 cursor->GetBlock()->InsertInstructionBefore(invoke, cursor);
847 return invoke;
848}
849
850// Returns true if operands a and b consists of widening type conversions
851// (either explicit or implicit) to the given to_type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100852static bool AreLowerPrecisionArgs(DataType::Type to_type, HInstruction* a, HInstruction* b) {
Aart Bik4f7dd342017-09-12 13:12:57 -0700853 if (a->IsTypeConversion() && a->GetType() == to_type) {
854 a = a->InputAt(0);
855 }
856 if (b->IsTypeConversion() && b->GetType() == to_type) {
857 b = b->InputAt(0);
858 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100859 DataType::Type type1 = a->GetType();
860 DataType::Type type2 = b->GetType();
861 return (type1 == DataType::Type::kInt8 && type2 == DataType::Type::kInt8) ||
862 (type1 == DataType::Type::kInt16 && type2 == DataType::Type::kInt16) ||
863 (type1 == DataType::Type::kUint16 && type2 == DataType::Type::kUint16) ||
864 (type1 == DataType::Type::kInt32 && type2 == DataType::Type::kInt32 &&
865 to_type == DataType::Type::kInt64);
Aart Bik4f7dd342017-09-12 13:12:57 -0700866}
867
David Brazdil74eb1b22015-12-14 11:44:01 +0000868void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
869 HInstruction* replace_with = nullptr;
870 HInstruction* condition = select->GetCondition();
871 HInstruction* true_value = select->GetTrueValue();
872 HInstruction* false_value = select->GetFalseValue();
873
874 if (condition->IsBooleanNot()) {
875 // Change ((!cond) ? x : y) to (cond ? y : x).
876 condition = condition->InputAt(0);
877 std::swap(true_value, false_value);
878 select->ReplaceInput(false_value, 0);
879 select->ReplaceInput(true_value, 1);
880 select->ReplaceInput(condition, 2);
881 RecordSimplification();
882 }
883
884 if (true_value == false_value) {
885 // Replace (cond ? x : x) with (x).
886 replace_with = true_value;
887 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000888 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000889 // Replace (true ? x : y) with (x).
890 replace_with = true_value;
891 } else {
892 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000893 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000894 replace_with = false_value;
895 }
896 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000897 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000898 // Replace (cond ? true : false) with (cond).
899 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000900 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000901 // Replace (cond ? false : true) with (!cond).
902 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
903 }
Aart Bik4f7dd342017-09-12 13:12:57 -0700904 } else if (condition->IsCondition()) {
905 IfCondition cmp = condition->AsCondition()->GetCondition();
906 HInstruction* a = condition->InputAt(0);
907 HInstruction* b = condition->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100908 DataType::Type t_type = true_value->GetType();
909 DataType::Type f_type = false_value->GetType();
Aart Bik4f7dd342017-09-12 13:12:57 -0700910 // Here we have a <cmp> b ? true_value : false_value.
911 // Test if both values are same-typed int or long.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100912 if (t_type == f_type &&
913 (t_type == DataType::Type::kInt32 || t_type == DataType::Type::kInt64)) {
Aart Bik4f7dd342017-09-12 13:12:57 -0700914 // Try to replace typical integral ABS constructs.
915 if (true_value->IsNeg()) {
916 HInstruction* negated = true_value->InputAt(0);
917 if ((cmp == kCondLT || cmp == kCondLE) &&
918 (a == negated && a == false_value && IsInt64Value(b, 0))) {
919 // Found a < 0 ? -a : a which can be replaced by ABS(a).
920 replace_with = NewIntegralAbs(GetGraph()->GetArena(), false_value, select);
921 }
922 } else if (false_value->IsNeg()) {
923 HInstruction* negated = false_value->InputAt(0);
924 if ((cmp == kCondGT || cmp == kCondGE) &&
925 (a == true_value && a == negated && IsInt64Value(b, 0))) {
926 // Found a > 0 ? a : -a which can be replaced by ABS(a).
927 replace_with = NewIntegralAbs(GetGraph()->GetArena(), true_value, select);
928 }
929 } else if (true_value->IsSub() && false_value->IsSub()) {
930 HInstruction* true_sub1 = true_value->InputAt(0);
931 HInstruction* true_sub2 = true_value->InputAt(1);
932 HInstruction* false_sub1 = false_value->InputAt(0);
933 HInstruction* false_sub2 = false_value->InputAt(1);
934 if ((((cmp == kCondGT || cmp == kCondGE) &&
935 (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) ||
936 ((cmp == kCondLT || cmp == kCondLE) &&
937 (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) &&
938 AreLowerPrecisionArgs(t_type, a, b)) {
939 // Found a > b ? a - b : b - a or
940 // a < b ? b - a : a - b
941 // which can be replaced by ABS(a - b) for lower precision operands a, b.
942 replace_with = NewIntegralAbs(GetGraph()->GetArena(), true_value, select);
943 }
944 }
945 }
David Brazdil74eb1b22015-12-14 11:44:01 +0000946 }
947
948 if (replace_with != nullptr) {
949 select->ReplaceWith(replace_with);
950 select->GetBlock()->RemoveInstruction(select);
951 RecordSimplification();
952 }
953}
954
955void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
956 HInstruction* condition = instruction->InputAt(0);
957 if (condition->IsBooleanNot()) {
958 // Swap successors if input is negated.
959 instruction->ReplaceInput(condition->InputAt(0), 0);
960 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100961 RecordSimplification();
962 }
963}
964
Mingyao Yang0304e182015-01-30 16:41:29 -0800965void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
966 HInstruction* input = instruction->InputAt(0);
967 // If the array is a NewArray with constant size, replace the array length
968 // with the constant instruction. This helps the bounds check elimination phase.
969 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000970 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -0800971 if (input->IsIntConstant()) {
972 instruction->ReplaceWith(input);
973 }
974 }
975}
976
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000977void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000978 HInstruction* value = instruction->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100979 if (value->GetType() != DataType::Type::kReference) {
980 return;
981 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000982
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100983 if (CanEnsureNotNullAt(value, instruction)) {
984 instruction->ClearValueCanBeNull();
985 }
986
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000987 if (value->IsArrayGet()) {
988 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
989 // If the code is just swapping elements in the array, no need for a type check.
990 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100991 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000992 }
993 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100994
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100995 if (value->IsNullConstant()) {
996 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100997 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100998 }
999
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001000 ScopedObjectAccess soa(Thread::Current());
1001 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
1002 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
1003 if (!array_rti.IsValid()) {
1004 return;
1005 }
1006
1007 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
1008 instruction->ClearNeedsTypeCheck();
1009 return;
1010 }
1011
1012 if (array_rti.IsObjectArray()) {
1013 if (array_rti.IsExact()) {
1014 instruction->ClearNeedsTypeCheck();
1015 return;
1016 }
1017 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001018 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001019}
1020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001021static bool IsTypeConversionImplicit(DataType::Type input_type, DataType::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +01001022 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001023 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillainf355c3f2016-03-30 19:09:03 +01001024
Vladimir Markob52bbde2016-02-12 12:06:05 +00001025 // Besides conversion to the same type, widening integral conversions are implicit,
1026 // excluding conversions to long and the byte->char conversion where we need to
1027 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
1028 return result_type == input_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001029 (result_type == DataType::Type::kInt32 && (input_type == DataType::Type::kBool ||
1030 input_type == DataType::Type::kInt8 ||
1031 input_type == DataType::Type::kInt16 ||
1032 input_type == DataType::Type::kUint16)) ||
1033 (result_type == DataType::Type::kUint16 && input_type == DataType::Type::kBool) ||
1034 (result_type == DataType::Type::kInt16 && (input_type == DataType::Type::kBool ||
1035 input_type == DataType::Type::kInt8)) ||
1036 (result_type == DataType::Type::kInt8 && input_type == DataType::Type::kBool);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001037}
1038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001039static bool IsTypeConversionLossless(DataType::Type input_type, DataType::Type result_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001040 // The conversion to a larger type is loss-less with the exception of two cases,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001041 // - conversion to Uint16, the only unsigned type, where we may lose some bits, and
Vladimir Markob52bbde2016-02-12 12:06:05 +00001042 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
1043 // For integral to FP conversions this holds because the FP mantissa is large enough.
1044 DCHECK_NE(input_type, result_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001045 return DataType::Size(result_type) > DataType::Size(input_type) &&
1046 result_type != DataType::Type::kUint16 &&
1047 !(result_type == DataType::Type::kInt64 && input_type == DataType::Type::kFloat32);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001048}
1049
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001050void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001051 HInstruction* input = instruction->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001052 DataType::Type input_type = input->GetType();
1053 DataType::Type result_type = instruction->GetResultType();
Vladimir Markob52bbde2016-02-12 12:06:05 +00001054 if (IsTypeConversionImplicit(input_type, result_type)) {
1055 // Remove the implicit conversion; this includes conversion to the same type.
1056 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001057 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001058 RecordSimplification();
1059 return;
1060 }
1061
1062 if (input->IsTypeConversion()) {
1063 HTypeConversion* input_conversion = input->AsTypeConversion();
1064 HInstruction* original_input = input_conversion->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001065 DataType::Type original_type = original_input->GetType();
Vladimir Markob52bbde2016-02-12 12:06:05 +00001066
1067 // When the first conversion is lossless, a direct conversion from the original type
1068 // to the final type yields the same result, even for a lossy second conversion, for
1069 // example float->double->int or int->double->float.
1070 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
1071
1072 // For integral conversions, see if the first conversion loses only bits that the second
1073 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
1074 // conversion yields the same result, for example long->int->short or int->char->short.
1075 bool integral_conversions_with_non_widening_second =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001076 DataType::IsIntegralType(input_type) &&
1077 DataType::IsIntegralType(original_type) &&
1078 DataType::IsIntegralType(result_type) &&
1079 DataType::Size(result_type) <= DataType::Size(input_type);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001080
1081 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
1082 // If the merged conversion is implicit, do the simplification unconditionally.
1083 if (IsTypeConversionImplicit(original_type, result_type)) {
1084 instruction->ReplaceWith(original_input);
1085 instruction->GetBlock()->RemoveInstruction(instruction);
1086 if (!input_conversion->HasUses()) {
1087 // Don't wait for DCE.
1088 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1089 }
1090 RecordSimplification();
1091 return;
1092 }
1093 // Otherwise simplify only if the first conversion has no other use.
1094 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
1095 input_conversion->ReplaceWith(original_input);
1096 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1097 RecordSimplification();
1098 return;
1099 }
1100 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001101 } else if (input->IsAnd() && DataType::IsIntegralType(result_type)) {
1102 DCHECK(DataType::IsIntegralType(input_type));
Vladimir Marko8428bd32016-02-12 16:53:57 +00001103 HAnd* input_and = input->AsAnd();
1104 HConstant* constant = input_and->GetConstantRight();
1105 if (constant != nullptr) {
1106 int64_t value = Int64FromConstant(constant);
1107 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
1108 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001109 if (trailing_ones >= kBitsPerByte * DataType::Size(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +00001110 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +00001111 HInstruction* original_input = input_and->GetLeastConstantLeft();
1112 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
1113 instruction->ReplaceWith(original_input);
1114 instruction->GetBlock()->RemoveInstruction(instruction);
1115 RecordSimplification();
1116 return;
1117 } else if (input->HasOnlyOneNonEnvironmentUse()) {
1118 input_and->ReplaceWith(original_input);
1119 input_and->GetBlock()->RemoveInstruction(input_and);
1120 RecordSimplification();
1121 return;
1122 }
Vladimir Marko8428bd32016-02-12 16:53:57 +00001123 }
1124 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001125 }
1126}
1127
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001128void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
1129 HConstant* input_cst = instruction->GetConstantRight();
1130 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001131 bool integral_type = DataType::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +00001132 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001133 // Replace code looking like
1134 // ADD dst, src, 0
1135 // with
1136 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001137 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
1138 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1139 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001140 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001141 instruction->ReplaceWith(input_other);
1142 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001143 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +06001144 return;
1145 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001146 }
1147
1148 HInstruction* left = instruction->GetLeft();
1149 HInstruction* right = instruction->GetRight();
1150 bool left_is_neg = left->IsNeg();
1151 bool right_is_neg = right->IsNeg();
1152
1153 if (left_is_neg && right_is_neg) {
1154 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1155 return;
1156 }
1157 }
1158
1159 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
1160 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
1161 // Replace code looking like
1162 // NEG tmp, b
1163 // ADD dst, a, tmp
1164 // with
1165 // SUB dst, a, b
1166 // We do not perform the optimization if the input negation has environment
1167 // uses or multiple non-environment uses as it could lead to worse code. In
1168 // particular, we do not want the live range of `b` to be extended if we are
1169 // not sure the initial 'NEG' instruction can be removed.
1170 HInstruction* other = left_is_neg ? right : left;
1171 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
1172 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1173 RecordSimplification();
1174 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001175 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001176 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001177
Anton Kirilove14dc862016-05-13 17:56:15 +01001178 if (TryReplaceWithRotate(instruction)) {
1179 return;
1180 }
1181
1182 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1183 // so no need to return.
1184 TryHandleAssociativeAndCommutativeOperation(instruction);
1185
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001186 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001187 TrySubtractionChainSimplification(instruction)) {
1188 return;
1189 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001190
1191 if (integral_type) {
1192 // Replace code patterns looking like
1193 // SUB dst1, x, y SUB dst1, x, y
1194 // ADD dst2, dst1, y ADD dst2, y, dst1
1195 // with
1196 // SUB dst1, x, y
1197 // ADD instruction is not needed in this case, we may use
1198 // one of inputs of SUB instead.
1199 if (left->IsSub() && left->InputAt(1) == right) {
1200 instruction->ReplaceWith(left->InputAt(0));
1201 RecordSimplification();
1202 instruction->GetBlock()->RemoveInstruction(instruction);
1203 return;
1204 } else if (right->IsSub() && right->InputAt(1) == left) {
1205 instruction->ReplaceWith(right->InputAt(0));
1206 RecordSimplification();
1207 instruction->GetBlock()->RemoveInstruction(instruction);
1208 return;
1209 }
1210 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001211}
1212
1213void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1214 HConstant* input_cst = instruction->GetConstantRight();
1215 HInstruction* input_other = instruction->GetLeastConstantLeft();
1216
Vladimir Marko452c1b62015-09-25 14:44:17 +01001217 if (input_cst != nullptr) {
1218 int64_t value = Int64FromConstant(input_cst);
1219 if (value == -1) {
1220 // Replace code looking like
1221 // AND dst, src, 0xFFF...FF
1222 // with
1223 // src
1224 instruction->ReplaceWith(input_other);
1225 instruction->GetBlock()->RemoveInstruction(instruction);
1226 RecordSimplification();
1227 return;
1228 }
1229 // Eliminate And from UShr+And if the And-mask contains all the bits that
1230 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1231 // precisely clears the shifted-in sign bits.
1232 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001233 size_t reg_bits = (instruction->GetResultType() == DataType::Type::kInt64) ? 64 : 32;
Vladimir Marko452c1b62015-09-25 14:44:17 +01001234 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1235 size_t num_tail_bits_set = CTZ(value + 1);
1236 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1237 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1238 instruction->ReplaceWith(input_other);
1239 instruction->GetBlock()->RemoveInstruction(instruction);
1240 RecordSimplification();
1241 return;
1242 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1243 input_other->HasOnlyOneNonEnvironmentUse()) {
1244 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1245 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1246 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1247 input_other->InputAt(0),
1248 input_other->InputAt(1),
1249 input_other->GetDexPc());
1250 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1251 input_other->GetBlock()->RemoveInstruction(input_other);
1252 RecordSimplification();
1253 return;
1254 }
1255 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001256 }
1257
1258 // We assume that GVN has run before, so we only perform a pointer comparison.
1259 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001260 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001261 if (instruction->GetLeft() == instruction->GetRight()) {
1262 // Replace code looking like
1263 // AND dst, src, src
1264 // with
1265 // src
1266 instruction->ReplaceWith(instruction->GetLeft());
1267 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001268 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001269 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001270 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001271
Anton Kirilove14dc862016-05-13 17:56:15 +01001272 if (TryDeMorganNegationFactoring(instruction)) {
1273 return;
1274 }
1275
1276 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1277 // so no need to return.
1278 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001279}
1280
Mark Mendellc4701932015-04-10 13:18:51 -04001281void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1282 VisitCondition(condition);
1283}
1284
1285void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1286 VisitCondition(condition);
1287}
1288
1289void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1290 VisitCondition(condition);
1291}
1292
1293void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1294 VisitCondition(condition);
1295}
1296
Anton Shaminbdd79352016-02-15 12:48:36 +06001297void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1298 VisitCondition(condition);
1299}
1300
1301void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1302 VisitCondition(condition);
1303}
1304
1305void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1306 VisitCondition(condition);
1307}
1308
1309void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1310 VisitCondition(condition);
1311}
Aart Bike9f37602015-10-09 11:15:55 -07001312
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001313// Recognize the following pattern:
1314// obj.getClass() ==/!= Foo.class
1315// And replace it with a constant value if the type of `obj` is statically known.
1316static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1317 HInstruction* input_one = condition->InputAt(0);
1318 HInstruction* input_two = condition->InputAt(1);
1319 HLoadClass* load_class = input_one->IsLoadClass()
1320 ? input_one->AsLoadClass()
1321 : input_two->AsLoadClass();
1322 if (load_class == nullptr) {
1323 return false;
1324 }
1325
1326 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1327 if (!class_rti.IsValid()) {
1328 // Unresolved class.
1329 return false;
1330 }
1331
1332 HInstanceFieldGet* field_get = (load_class == input_one)
1333 ? input_two->AsInstanceFieldGet()
1334 : input_one->AsInstanceFieldGet();
1335 if (field_get == nullptr) {
1336 return false;
1337 }
1338
1339 HInstruction* receiver = field_get->InputAt(0);
1340 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1341 if (!receiver_type.IsExact()) {
1342 return false;
1343 }
1344
1345 {
1346 ScopedObjectAccess soa(Thread::Current());
1347 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1348 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
1349 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1350 if (field_get->GetFieldInfo().GetField() != field) {
1351 return false;
1352 }
1353
1354 // We can replace the compare.
1355 int value = 0;
1356 if (receiver_type.IsEqual(class_rti)) {
1357 value = condition->IsEqual() ? 1 : 0;
1358 } else {
1359 value = condition->IsNotEqual() ? 1 : 0;
1360 }
1361 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1362 return true;
1363 }
1364}
1365
Mark Mendellc4701932015-04-10 13:18:51 -04001366void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001367 if (condition->IsEqual() || condition->IsNotEqual()) {
1368 if (RecognizeAndSimplifyClassCheck(condition)) {
1369 return;
1370 }
1371 }
1372
Anton Shaminbdd79352016-02-15 12:48:36 +06001373 // Reverse condition if left is constant. Our code generators prefer constant
1374 // on the right hand side.
1375 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1376 HBasicBlock* block = condition->GetBlock();
1377 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1378 // If it is a fp we must set the opposite bias.
1379 if (replacement != nullptr) {
1380 if (condition->IsLtBias()) {
1381 replacement->SetBias(ComparisonBias::kGtBias);
1382 } else if (condition->IsGtBias()) {
1383 replacement->SetBias(ComparisonBias::kLtBias);
1384 }
1385 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1386 RecordSimplification();
1387
1388 condition = replacement;
1389 }
1390 }
Mark Mendellc4701932015-04-10 13:18:51 -04001391
Mark Mendellc4701932015-04-10 13:18:51 -04001392 HInstruction* left = condition->GetLeft();
1393 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001394
1395 // Try to fold an HCompare into this HCondition.
1396
Mark Mendellc4701932015-04-10 13:18:51 -04001397 // We can only replace an HCondition which compares a Compare to 0.
1398 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1399 // condition with a long, float or double comparison as input.
1400 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1401 // Conversion is not possible.
1402 return;
1403 }
1404
1405 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001406 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001407 // Someone else also wants the result of the compare.
1408 return;
1409 }
1410
Vladimir Marko46817b82016-03-29 12:21:58 +01001411 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001412 // There is a reference to the compare result in an environment. Do we really need it?
1413 if (GetGraph()->IsDebuggable()) {
1414 return;
1415 }
1416
1417 // We have to ensure that there are no deopt points in the sequence.
1418 if (left->HasAnyEnvironmentUseBefore(condition)) {
1419 return;
1420 }
1421 }
1422
1423 // Clean up any environment uses from the HCompare, if any.
1424 left->RemoveEnvironmentUsers();
1425
1426 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1427 condition->SetBias(left->AsCompare()->GetBias());
1428
1429 // Replace the operands of the HCondition.
1430 condition->ReplaceInput(left->InputAt(0), 0);
1431 condition->ReplaceInput(left->InputAt(1), 1);
1432
1433 // Remove the HCompare.
1434 left->GetBlock()->RemoveInstruction(left);
1435
1436 RecordSimplification();
1437}
1438
Andreas Gampe9186ced2016-12-12 14:28:21 -08001439// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1440static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1441 // True, if the most significant bits of divisor are 0.
1442 return ((divisor & 0x7fffff) == 0);
1443}
1444
1445// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1446static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1447 // True, if the most significant bits of divisor are 0.
1448 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1449}
1450
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001451void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1452 HConstant* input_cst = instruction->GetConstantRight();
1453 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001455
1456 if ((input_cst != nullptr) && input_cst->IsOne()) {
1457 // Replace code looking like
1458 // DIV dst, src, 1
1459 // with
1460 // src
1461 instruction->ReplaceWith(input_other);
1462 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001463 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001464 return;
1465 }
1466
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001467 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001468 // Replace code looking like
1469 // DIV dst, src, -1
1470 // with
1471 // NEG dst, src
1472 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001473 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001474 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001475 return;
1476 }
1477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001478 if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001479 // Try replacing code looking like
1480 // DIV dst, src, constant
1481 // with
1482 // MUL dst, src, 1 / constant
1483 HConstant* reciprocal = nullptr;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001484 if (type == DataType::Type::kFloat64) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001485 double value = input_cst->AsDoubleConstant()->GetValue();
1486 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1487 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1488 }
1489 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001490 DCHECK_EQ(type, DataType::Type::kFloat32);
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001491 float value = input_cst->AsFloatConstant()->GetValue();
1492 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1493 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1494 }
1495 }
1496
1497 if (reciprocal != nullptr) {
1498 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1499 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1500 RecordSimplification();
1501 return;
1502 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001503 }
1504}
1505
1506void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1507 HConstant* input_cst = instruction->GetConstantRight();
1508 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001509 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001510 HBasicBlock* block = instruction->GetBlock();
1511 ArenaAllocator* allocator = GetGraph()->GetArena();
1512
1513 if (input_cst == nullptr) {
1514 return;
1515 }
1516
1517 if (input_cst->IsOne()) {
1518 // Replace code looking like
1519 // MUL dst, src, 1
1520 // with
1521 // src
1522 instruction->ReplaceWith(input_other);
1523 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001524 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001525 return;
1526 }
1527
1528 if (input_cst->IsMinusOne() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001530 // Replace code looking like
1531 // MUL dst, src, -1
1532 // with
1533 // NEG dst, src
1534 HNeg* neg = new (allocator) HNeg(type, input_other);
1535 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001536 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001537 return;
1538 }
1539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 if (DataType::IsFloatingPointType(type) &&
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001541 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1542 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1543 // Replace code looking like
1544 // FP_MUL dst, src, 2.0
1545 // with
1546 // FP_ADD dst, src, src
1547 // The 'int' and 'long' cases are handled below.
1548 block->ReplaceAndRemoveInstructionWith(instruction,
1549 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001550 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001551 return;
1552 }
1553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001554 if (DataType::IsIntOrLongType(type)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001555 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001556 // Even though constant propagation also takes care of the zero case, other
1557 // optimizations can lead to having a zero multiplication.
1558 if (factor == 0) {
1559 // Replace code looking like
1560 // MUL dst, src, 0
1561 // with
1562 // 0
1563 instruction->ReplaceWith(input_cst);
1564 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001565 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001566 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001567 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001568 // Replace code looking like
1569 // MUL dst, src, pow_of_2
1570 // with
1571 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001572 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001573 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001574 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001575 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001576 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001577 } else if (IsPowerOfTwo(factor - 1)) {
1578 // Transform code looking like
1579 // MUL dst, src, (2^n + 1)
1580 // into
1581 // SHL tmp, src, n
1582 // ADD dst, src, tmp
1583 HShl* shl = new (allocator) HShl(type,
1584 input_other,
1585 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1586 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1587
1588 block->InsertInstructionBefore(shl, instruction);
1589 block->ReplaceAndRemoveInstructionWith(instruction, add);
1590 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001591 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001592 } else if (IsPowerOfTwo(factor + 1)) {
1593 // Transform code looking like
1594 // MUL dst, src, (2^n - 1)
1595 // into
1596 // SHL tmp, src, n
1597 // SUB dst, tmp, src
1598 HShl* shl = new (allocator) HShl(type,
1599 input_other,
1600 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1601 HSub* sub = new (allocator) HSub(type, shl, input_other);
1602
1603 block->InsertInstructionBefore(shl, instruction);
1604 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1605 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001606 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001607 }
1608 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001609
1610 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1611 // so no need to return.
1612 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001613}
1614
Alexandre Rames188d4312015-04-09 18:30:21 +01001615void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1616 HInstruction* input = instruction->GetInput();
1617 if (input->IsNeg()) {
1618 // Replace code looking like
1619 // NEG tmp, src
1620 // NEG dst, tmp
1621 // with
1622 // src
1623 HNeg* previous_neg = input->AsNeg();
1624 instruction->ReplaceWith(previous_neg->GetInput());
1625 instruction->GetBlock()->RemoveInstruction(instruction);
1626 // We perform the optimization even if the input negation has environment
1627 // uses since it allows removing the current instruction. But we only delete
1628 // the input negation only if it is does not have any uses left.
1629 if (!previous_neg->HasUses()) {
1630 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1631 }
1632 RecordSimplification();
1633 return;
1634 }
1635
Serguei Katkov339dfc22015-04-20 12:29:32 +06001636 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001637 !DataType::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001638 // Replace code looking like
1639 // SUB tmp, a, b
1640 // NEG dst, tmp
1641 // with
1642 // SUB dst, b, a
1643 // We do not perform the optimization if the input subtraction has
1644 // environment uses or multiple non-environment uses as it could lead to
1645 // worse code. In particular, we do not want the live ranges of `a` and `b`
1646 // to be extended if we are not sure the initial 'SUB' instruction can be
1647 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001648 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001649 HSub* sub = input->AsSub();
1650 HSub* new_sub =
1651 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1652 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1653 if (!sub->HasUses()) {
1654 sub->GetBlock()->RemoveInstruction(sub);
1655 }
1656 RecordSimplification();
1657 }
1658}
1659
1660void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1661 HInstruction* input = instruction->GetInput();
1662 if (input->IsNot()) {
1663 // Replace code looking like
1664 // NOT tmp, src
1665 // NOT dst, tmp
1666 // with
1667 // src
1668 // We perform the optimization even if the input negation has environment
1669 // uses since it allows removing the current instruction. But we only delete
1670 // the input negation only if it is does not have any uses left.
1671 HNot* previous_not = input->AsNot();
1672 instruction->ReplaceWith(previous_not->GetInput());
1673 instruction->GetBlock()->RemoveInstruction(instruction);
1674 if (!previous_not->HasUses()) {
1675 previous_not->GetBlock()->RemoveInstruction(previous_not);
1676 }
1677 RecordSimplification();
1678 }
1679}
1680
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001681void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1682 HConstant* input_cst = instruction->GetConstantRight();
1683 HInstruction* input_other = instruction->GetLeastConstantLeft();
1684
Roland Levillain1a653882016-03-18 18:05:57 +00001685 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001686 // Replace code looking like
1687 // OR dst, src, 0
1688 // with
1689 // src
1690 instruction->ReplaceWith(input_other);
1691 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001692 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001693 return;
1694 }
1695
1696 // We assume that GVN has run before, so we only perform a pointer comparison.
1697 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001698 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001699 if (instruction->GetLeft() == instruction->GetRight()) {
1700 // Replace code looking like
1701 // OR dst, src, src
1702 // with
1703 // src
1704 instruction->ReplaceWith(instruction->GetLeft());
1705 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001706 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001707 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001708 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001709
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001710 if (TryDeMorganNegationFactoring(instruction)) return;
1711
Anton Kirilove14dc862016-05-13 17:56:15 +01001712 if (TryReplaceWithRotate(instruction)) {
1713 return;
1714 }
1715
1716 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1717 // so no need to return.
1718 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001719}
1720
1721void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1722 VisitShift(instruction);
1723}
1724
1725void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1726 VisitShift(instruction);
1727}
1728
1729void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1730 HConstant* input_cst = instruction->GetConstantRight();
1731 HInstruction* input_other = instruction->GetLeastConstantLeft();
1732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001733 DataType::Type type = instruction->GetType();
1734 if (DataType::IsFloatingPointType(type)) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001735 return;
1736 }
1737
Roland Levillain1a653882016-03-18 18:05:57 +00001738 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001739 // Replace code looking like
1740 // SUB dst, src, 0
1741 // with
1742 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001743 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1744 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1745 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001746 instruction->ReplaceWith(input_other);
1747 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001748 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001749 return;
1750 }
1751
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001752 HBasicBlock* block = instruction->GetBlock();
1753 ArenaAllocator* allocator = GetGraph()->GetArena();
1754
Alexandre Rames188d4312015-04-09 18:30:21 +01001755 HInstruction* left = instruction->GetLeft();
1756 HInstruction* right = instruction->GetRight();
1757 if (left->IsConstant()) {
1758 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001759 // Replace code looking like
1760 // SUB dst, 0, src
1761 // with
1762 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001763 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001764 // `x` is `0.0`, the former expression yields `0.0`, while the later
1765 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001766 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001767 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001768 RecordSimplification();
1769 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001770 }
1771 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001772
1773 if (left->IsNeg() && right->IsNeg()) {
1774 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1775 return;
1776 }
1777 }
1778
1779 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1780 // Replace code looking like
1781 // NEG tmp, b
1782 // SUB dst, a, tmp
1783 // with
1784 // ADD dst, a, b
1785 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1786 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1787 RecordSimplification();
1788 right->GetBlock()->RemoveInstruction(right);
1789 return;
1790 }
1791
1792 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1793 // Replace code looking like
1794 // NEG tmp, a
1795 // SUB dst, tmp, b
1796 // with
1797 // ADD tmp, a, b
1798 // NEG dst, tmp
1799 // The second version is not intrinsically better, but enables more
1800 // transformations.
1801 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1802 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1803 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1804 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1805 instruction->ReplaceWith(neg);
1806 instruction->GetBlock()->RemoveInstruction(instruction);
1807 RecordSimplification();
1808 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001809 return;
1810 }
1811
1812 if (TrySubtractionChainSimplification(instruction)) {
1813 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001814 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001815
1816 if (left->IsAdd()) {
1817 // Replace code patterns looking like
1818 // ADD dst1, x, y ADD dst1, x, y
1819 // SUB dst2, dst1, y SUB dst2, dst1, x
1820 // with
1821 // ADD dst1, x, y
1822 // SUB instruction is not needed in this case, we may use
1823 // one of inputs of ADD instead.
1824 // It is applicable to integral types only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 DCHECK(DataType::IsIntegralType(type));
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001826 if (left->InputAt(1) == right) {
1827 instruction->ReplaceWith(left->InputAt(0));
1828 RecordSimplification();
1829 instruction->GetBlock()->RemoveInstruction(instruction);
1830 return;
1831 } else if (left->InputAt(0) == right) {
1832 instruction->ReplaceWith(left->InputAt(1));
1833 RecordSimplification();
1834 instruction->GetBlock()->RemoveInstruction(instruction);
1835 return;
1836 }
1837 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001838}
1839
1840void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1841 VisitShift(instruction);
1842}
1843
1844void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1845 HConstant* input_cst = instruction->GetConstantRight();
1846 HInstruction* input_other = instruction->GetLeastConstantLeft();
1847
Roland Levillain1a653882016-03-18 18:05:57 +00001848 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001849 // Replace code looking like
1850 // XOR dst, src, 0
1851 // with
1852 // src
1853 instruction->ReplaceWith(input_other);
1854 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001855 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001856 return;
1857 }
1858
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001859 if ((input_cst != nullptr) && input_cst->IsOne()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 && input_other->GetType() == DataType::Type::kBool) {
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001861 // Replace code looking like
1862 // XOR dst, src, 1
1863 // with
1864 // BOOLEAN_NOT dst, src
1865 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1866 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1867 RecordSimplification();
1868 return;
1869 }
1870
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001871 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1872 // Replace code looking like
1873 // XOR dst, src, 0xFFF...FF
1874 // with
1875 // NOT dst, src
1876 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1877 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001878 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001879 return;
1880 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001881
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001882 HInstruction* left = instruction->GetLeft();
1883 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001884 if (((left->IsNot() && right->IsNot()) ||
1885 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001886 left->HasOnlyOneNonEnvironmentUse() &&
1887 right->HasOnlyOneNonEnvironmentUse()) {
1888 // Replace code looking like
1889 // NOT nota, a
1890 // NOT notb, b
1891 // XOR dst, nota, notb
1892 // with
1893 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001894 instruction->ReplaceInput(left->InputAt(0), 0);
1895 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001896 left->GetBlock()->RemoveInstruction(left);
1897 right->GetBlock()->RemoveInstruction(right);
1898 RecordSimplification();
1899 return;
1900 }
1901
Anton Kirilove14dc862016-05-13 17:56:15 +01001902 if (TryReplaceWithRotate(instruction)) {
1903 return;
1904 }
1905
1906 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1907 // so no need to return.
1908 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001909}
1910
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001911void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1912 HInstruction* argument = instruction->InputAt(1);
1913 HInstruction* receiver = instruction->InputAt(0);
1914 if (receiver == argument) {
1915 // Because String.equals is an instance call, the receiver is
1916 // a null check if we don't know it's null. The argument however, will
1917 // be the actual object. So we cannot end up in a situation where both
1918 // are equal but could be null.
1919 DCHECK(CanEnsureNotNullAt(argument, instruction));
1920 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1921 instruction->GetBlock()->RemoveInstruction(instruction);
1922 } else {
1923 StringEqualsOptimizations optimizations(instruction);
1924 if (CanEnsureNotNullAt(argument, instruction)) {
1925 optimizations.SetArgumentNotNull();
1926 }
1927 ScopedObjectAccess soa(Thread::Current());
1928 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1929 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1930 optimizations.SetArgumentIsString();
1931 }
1932 }
1933}
1934
Roland Levillain22c49222016-03-18 14:04:28 +00001935void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1936 bool is_left,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001937 DataType::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001938 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001939 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001940 HInstruction* value = invoke->InputAt(0);
1941 HInstruction* distance = invoke->InputAt(1);
1942 // Replace the invoke with an HRor.
1943 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001944 // Unconditionally set the type of the negated distance to `int`,
1945 // as shift and rotate operations expect a 32-bit (or narrower)
1946 // value for their distance input.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 distance = new (GetGraph()->GetArena()) HNeg(DataType::Type::kInt32, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001948 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1949 }
Roland Levillain22c49222016-03-18 14:04:28 +00001950 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001951 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1952 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001953 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001954 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1955 clinit->GetBlock()->RemoveInstruction(clinit);
1956 HInstruction* ldclass = clinit->InputAt(0);
1957 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1958 ldclass->GetBlock()->RemoveInstruction(ldclass);
1959 }
1960 }
1961}
1962
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001963static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1964 if (potential_length->IsArrayLength()) {
1965 return potential_length->InputAt(0) == potential_array;
1966 }
1967
1968 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001969 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001970 }
1971
1972 return false;
1973}
1974
1975void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1976 HInstruction* source = instruction->InputAt(0);
1977 HInstruction* destination = instruction->InputAt(2);
1978 HInstruction* count = instruction->InputAt(4);
1979 SystemArrayCopyOptimizations optimizations(instruction);
1980 if (CanEnsureNotNullAt(source, instruction)) {
1981 optimizations.SetSourceIsNotNull();
1982 }
1983 if (CanEnsureNotNullAt(destination, instruction)) {
1984 optimizations.SetDestinationIsNotNull();
1985 }
1986 if (destination == source) {
1987 optimizations.SetDestinationIsSource();
1988 }
1989
1990 if (IsArrayLengthOf(count, source)) {
1991 optimizations.SetCountIsSourceLength();
1992 }
1993
1994 if (IsArrayLengthOf(count, destination)) {
1995 optimizations.SetCountIsDestinationLength();
1996 }
1997
1998 {
1999 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 DataType::Type source_component_type = DataType::Type::kVoid;
2001 DataType::Type destination_component_type = DataType::Type::kVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002002 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
2003 if (destination_rti.IsValid()) {
2004 if (destination_rti.IsObjectArray()) {
2005 if (destination_rti.IsExact()) {
2006 optimizations.SetDoesNotNeedTypeCheck();
2007 }
2008 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002009 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002010 if (destination_rti.IsPrimitiveArrayClass()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 destination_component_type = DataTypeFromPrimitive(
2012 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002013 optimizations.SetDestinationIsPrimitiveArray();
2014 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2015 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002016 }
2017 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002018 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2019 if (source_rti.IsValid()) {
2020 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2021 optimizations.SetDoesNotNeedTypeCheck();
2022 }
2023 if (source_rti.IsPrimitiveArrayClass()) {
2024 optimizations.SetSourceIsPrimitiveArray();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 source_component_type = DataTypeFromPrimitive(
2026 source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002027 } else if (source_rti.IsNonPrimitiveArrayClass()) {
2028 optimizations.SetSourceIsNonPrimitiveArray();
2029 }
2030 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002031 // For primitive arrays, use their optimized ArtMethod implementations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002032 if ((source_component_type != DataType::Type::kVoid) &&
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002033 (source_component_type == destination_component_type)) {
2034 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2035 PointerSize image_size = class_linker->GetImagePointerSize();
2036 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
2037 mirror::Class* system = invoke->GetResolvedMethod()->GetDeclaringClass();
2038 ArtMethod* method = nullptr;
2039 switch (source_component_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kBool:
Vladimir Markoba118822017-06-12 15:41:56 +01002041 method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002042 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 case DataType::Type::kInt8:
Vladimir Markoba118822017-06-12 15:41:56 +01002044 method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002045 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 case DataType::Type::kUint16:
Vladimir Markoba118822017-06-12 15:41:56 +01002047 method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002048 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 case DataType::Type::kInt16:
Vladimir Markoba118822017-06-12 15:41:56 +01002050 method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002051 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 case DataType::Type::kInt32:
Vladimir Markoba118822017-06-12 15:41:56 +01002053 method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002054 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 case DataType::Type::kFloat32:
Vladimir Markoba118822017-06-12 15:41:56 +01002056 method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002057 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 case DataType::Type::kInt64:
Vladimir Markoba118822017-06-12 15:41:56 +01002059 method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002060 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 case DataType::Type::kFloat64:
Vladimir Markoba118822017-06-12 15:41:56 +01002062 method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002063 break;
2064 default:
2065 LOG(FATAL) << "Unreachable";
2066 }
2067 DCHECK(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01002068 DCHECK(method->IsStatic());
2069 DCHECK(method->GetDeclaringClass() == system);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002070 invoke->SetResolvedMethod(method);
2071 // Sharpen the new invoke. Note that we do not update the dex method index of
2072 // the invoke, as we would need to look it up in the current dex file, and it
2073 // is unlikely that it exists. The most usual situation for such typed
2074 // arraycopy methods is a direct pointer to the boot image.
Vladimir Marko65979462017-05-19 17:25:12 +01002075 HSharpening::SharpenInvokeStaticOrDirect(invoke, codegen_, compiler_driver_);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002076 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002077 }
2078}
2079
Roland Levillaina5c4a402016-03-15 15:02:50 +00002080void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
2081 bool is_signum,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 DataType::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08002083 DCHECK(invoke->IsInvokeStaticOrDirect());
2084 uint32_t dex_pc = invoke->GetDexPc();
2085 HInstruction* left = invoke->InputAt(0);
2086 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08002087 if (!is_signum) {
2088 right = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 } else if (type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08002090 right = GetGraph()->GetLongConstant(0);
2091 } else {
2092 right = GetGraph()->GetIntConstant(0);
2093 }
2094 HCompare* compare = new (GetGraph()->GetArena())
2095 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
2096 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
2097}
2098
Aart Bik75a38b22016-02-17 10:41:50 -08002099void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
2100 DCHECK(invoke->IsInvokeStaticOrDirect());
2101 uint32_t dex_pc = invoke->GetDexPc();
2102 // IsNaN(x) is the same as x != x.
2103 HInstruction* x = invoke->InputAt(0);
2104 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08002105 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08002106 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
2107}
2108
Aart Bik2a6aad92016-02-25 11:32:32 -08002109void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2110 DCHECK(invoke->IsInvokeStaticOrDirect());
2111 uint32_t dex_pc = invoke->GetDexPc();
2112 HInstruction* x = invoke->InputAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 DataType::Type type = x->GetType();
Aart Bik2a6aad92016-02-25 11:32:32 -08002114 // Set proper bit pattern for NaN and replace intrinsic with raw version.
2115 HInstruction* nan;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 if (type == DataType::Type::kFloat64) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002117 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2118 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
2119 kNeedsEnvironmentOrCache,
2120 kNoSideEffects,
2121 kNoThrow);
2122 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 DCHECK_EQ(type, DataType::Type::kFloat32);
Aart Bik2a6aad92016-02-25 11:32:32 -08002124 nan = GetGraph()->GetIntConstant(0x7fc00000);
2125 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
2126 kNeedsEnvironmentOrCache,
2127 kNoSideEffects,
2128 kNoThrow);
2129 }
2130 // Test IsNaN(x), which is the same as x != x.
2131 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
2132 condition->SetBias(ComparisonBias::kLtBias);
2133 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2134 // Select between the two.
2135 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
2136 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2137 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
2138}
2139
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002140void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2141 HInstruction* str = invoke->InputAt(0);
2142 HInstruction* index = invoke->InputAt(1);
2143 uint32_t dex_pc = invoke->GetDexPc();
2144 ArenaAllocator* arena = GetGraph()->GetArena();
2145 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2146 // so create the HArrayLength, HBoundsCheck and HArrayGet.
2147 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
2148 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00002149 HBoundsCheck* bounds_check = new (arena) HBoundsCheck(
2150 index, length, dex_pc, invoke->GetDexMethodIndex());
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002151 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00002152 HArrayGet* array_get = new (arena) HArrayGet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 str, bounds_check, DataType::Type::kUint16, dex_pc, /* is_string_char_at */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002154 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2155 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2156 GetGraph()->SetHasBoundsChecks(true);
2157}
2158
Vladimir Markodce016e2016-04-28 13:10:02 +01002159void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
2160 HInstruction* str = invoke->InputAt(0);
2161 uint32_t dex_pc = invoke->GetDexPc();
2162 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2163 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002164 HArrayLength* length =
2165 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01002166 HInstruction* replacement;
2167 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
2168 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
2169 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
2170 HIntConstant* zero = GetGraph()->GetIntConstant(0);
2171 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
2172 replacement = equal;
2173 } else {
2174 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
2175 replacement = length;
2176 }
2177 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
2178}
2179
Aart Bikff7d89c2016-11-07 08:49:28 -08002180// This method should only be used on intrinsics whose sole way of throwing an
2181// exception is raising a NPE when the nth argument is null. If that argument
2182// is provably non-null, we can clear the flag.
2183void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2184 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002185 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002186 invoke->SetCanThrow(false);
2187 }
2188}
2189
Aart Bik71bf7b42016-11-16 10:17:46 -08002190// Methods that return "this" can replace the returned value with the receiver.
2191void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2192 if (invoke->HasUses()) {
2193 HInstruction* receiver = invoke->InputAt(0);
2194 invoke->ReplaceWith(receiver);
2195 RecordSimplification();
2196 }
2197}
2198
2199// Helper method for StringBuffer escape analysis.
2200static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2201 if (user->IsInvokeStaticOrDirect()) {
2202 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002203 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2204 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002205 user->InputAt(0) == reference;
2206 } else if (user->IsInvokeVirtual()) {
2207 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2208 case Intrinsics::kStringBufferLength:
2209 case Intrinsics::kStringBufferToString:
2210 DCHECK_EQ(user->InputAt(0), reference);
2211 return true;
2212 case Intrinsics::kStringBufferAppend:
2213 // Returns "this", so only okay if no further uses.
2214 DCHECK_EQ(user->InputAt(0), reference);
2215 DCHECK_NE(user->InputAt(1), reference);
2216 return !user->HasUses();
2217 default:
2218 break;
2219 }
2220 }
2221 return false;
2222}
2223
2224// Certain allocation intrinsics are not removed by dead code elimination
2225// because of potentially throwing an OOM exception or other side effects.
2226// This method removes such intrinsics when special circumstances allow.
2227void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2228 if (!invoke->HasUses()) {
2229 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2230 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2231 // call does not escape since only thread-local synchronization may be removed.
2232 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2233 HInstruction* receiver = invoke->InputAt(0);
2234 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2235 invoke->GetBlock()->RemoveInstruction(invoke);
2236 RecordSimplification();
2237 }
2238 }
2239}
2240
Aart Bik11932592016-03-08 12:42:25 -08002241void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
2242 uint32_t dex_pc = invoke->GetDexPc();
2243 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
2244 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
2245}
2246
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002247void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002248 switch (instruction->GetIntrinsic()) {
2249 case Intrinsics::kStringEquals:
2250 SimplifyStringEquals(instruction);
2251 break;
2252 case Intrinsics::kSystemArrayCopy:
2253 SimplifySystemArrayCopy(instruction);
2254 break;
2255 case Intrinsics::kIntegerRotateRight:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 SimplifyRotate(instruction, /* is_left */ false, DataType::Type::kInt32);
Roland Levillain22c49222016-03-18 14:04:28 +00002257 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002258 case Intrinsics::kLongRotateRight:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 SimplifyRotate(instruction, /* is_left */ false, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002260 break;
2261 case Intrinsics::kIntegerRotateLeft:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 SimplifyRotate(instruction, /* is_left */ true, DataType::Type::kInt32);
Roland Levillain22c49222016-03-18 14:04:28 +00002263 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002264 case Intrinsics::kLongRotateLeft:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002265 SimplifyRotate(instruction, /* is_left */ true, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002266 break;
2267 case Intrinsics::kIntegerCompare:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 SimplifyCompare(instruction, /* is_signum */ false, DataType::Type::kInt32);
Roland Levillaina5c4a402016-03-15 15:02:50 +00002269 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002270 case Intrinsics::kLongCompare:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 SimplifyCompare(instruction, /* is_signum */ false, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002272 break;
2273 case Intrinsics::kIntegerSignum:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 SimplifyCompare(instruction, /* is_signum */ true, DataType::Type::kInt32);
Roland Levillaina5c4a402016-03-15 15:02:50 +00002275 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002276 case Intrinsics::kLongSignum:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 SimplifyCompare(instruction, /* is_signum */ true, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002278 break;
2279 case Intrinsics::kFloatIsNaN:
2280 case Intrinsics::kDoubleIsNaN:
2281 SimplifyIsNaN(instruction);
2282 break;
2283 case Intrinsics::kFloatFloatToIntBits:
2284 case Intrinsics::kDoubleDoubleToLongBits:
2285 SimplifyFP2Int(instruction);
2286 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002287 case Intrinsics::kStringCharAt:
2288 SimplifyStringCharAt(instruction);
2289 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002290 case Intrinsics::kStringIsEmpty:
2291 case Intrinsics::kStringLength:
2292 SimplifyStringIsEmptyOrLength(instruction);
2293 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002294 case Intrinsics::kStringStringIndexOf:
2295 case Intrinsics::kStringStringIndexOfAfter:
2296 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2297 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002298 case Intrinsics::kStringBufferAppend:
2299 case Intrinsics::kStringBuilderAppend:
2300 SimplifyReturnThis(instruction);
2301 break;
2302 case Intrinsics::kStringBufferToString:
2303 case Intrinsics::kStringBuilderToString:
2304 SimplifyAllocationIntrinsic(instruction);
2305 break;
Aart Bik11932592016-03-08 12:42:25 -08002306 case Intrinsics::kUnsafeLoadFence:
2307 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2308 break;
2309 case Intrinsics::kUnsafeStoreFence:
2310 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2311 break;
2312 case Intrinsics::kUnsafeFullFence:
2313 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2314 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002315 default:
2316 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002317 }
2318}
2319
Aart Bikbb245d12015-10-19 11:05:03 -07002320void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2321 HInstruction* cond = deoptimize->InputAt(0);
2322 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002323 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002324 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002325 if (deoptimize->GuardsAnInput()) {
2326 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2327 }
Aart Bikbb245d12015-10-19 11:05:03 -07002328 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2329 } else {
2330 // Always deopt.
2331 }
2332 }
2333}
2334
Anton Kirilove14dc862016-05-13 17:56:15 +01002335// Replace code looking like
2336// OP y, x, const1
2337// OP z, y, const2
2338// with
2339// OP z, x, const3
2340// where OP is both an associative and a commutative operation.
2341bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2342 HBinaryOperation* instruction) {
2343 DCHECK(instruction->IsCommutative());
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 if (!DataType::IsIntegralType(instruction->GetType())) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002346 return false;
2347 }
2348
2349 HInstruction* left = instruction->GetLeft();
2350 HInstruction* right = instruction->GetRight();
2351 // Variable names as described above.
2352 HConstant* const2;
2353 HBinaryOperation* y;
2354
2355 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
2356 const2 = right->AsConstant();
2357 y = left->AsBinaryOperation();
2358 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
2359 const2 = left->AsConstant();
2360 y = right->AsBinaryOperation();
2361 } else {
2362 // The node does not match the pattern.
2363 return false;
2364 }
2365
2366 // If `y` has more than one use, we do not perform the optimization
2367 // because it might increase code size (e.g. if the new constant is
2368 // no longer encodable as an immediate operand in the target ISA).
2369 if (!y->HasOnlyOneNonEnvironmentUse()) {
2370 return false;
2371 }
2372
2373 // GetConstantRight() can return both left and right constants
2374 // for commutative operations.
2375 HConstant* const1 = y->GetConstantRight();
2376 if (const1 == nullptr) {
2377 return false;
2378 }
2379
2380 instruction->ReplaceInput(const1, 0);
2381 instruction->ReplaceInput(const2, 1);
2382 HConstant* const3 = instruction->TryStaticEvaluation();
2383 DCHECK(const3 != nullptr);
2384 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2385 instruction->ReplaceInput(const3, 1);
2386 RecordSimplification();
2387 return true;
2388}
2389
2390static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2391 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2392}
2393
2394// Helper function that performs addition statically, considering the result type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002396 // Use the Compute() method for consistency with TryStaticEvaluation().
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 if (type == DataType::Type::kInt32) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002398 return HAdd::Compute<int32_t>(x, y);
2399 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002400 DCHECK_EQ(type, DataType::Type::kInt64);
Anton Kirilove14dc862016-05-13 17:56:15 +01002401 return HAdd::Compute<int64_t>(x, y);
2402 }
2403}
2404
2405// Helper function that handles the child classes of HConstant
2406// and returns an integer with the appropriate sign.
2407static int64_t GetValue(HConstant* constant, bool is_negated) {
2408 int64_t ret = Int64FromConstant(constant);
2409 return is_negated ? -ret : ret;
2410}
2411
2412// Replace code looking like
2413// OP1 y, x, const1
2414// OP2 z, y, const2
2415// with
2416// OP3 z, x, const3
2417// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2418bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2419 HBinaryOperation* instruction) {
2420 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 DataType::Type type = instruction->GetType();
2423 if (!DataType::IsIntegralType(type)) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002424 return false;
2425 }
2426
2427 HInstruction* left = instruction->GetLeft();
2428 HInstruction* right = instruction->GetRight();
2429 // Variable names as described above.
2430 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2431 if (const2 == nullptr) {
2432 return false;
2433 }
2434
2435 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2436 ? left->AsBinaryOperation()
2437 : AsAddOrSub(right);
2438 // If y has more than one use, we do not perform the optimization because
2439 // it might increase code size (e.g. if the new constant is no longer
2440 // encodable as an immediate operand in the target ISA).
2441 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2442 return false;
2443 }
2444
2445 left = y->GetLeft();
2446 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2447 if (const1 == nullptr) {
2448 return false;
2449 }
2450
2451 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2452 // If both inputs are constants, let the constant folding pass deal with it.
2453 if (x->IsConstant()) {
2454 return false;
2455 }
2456
2457 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2458 int64_t const2_val = GetValue(const2, is_const2_negated);
2459 bool is_y_negated = (y == right) && instruction->IsSub();
2460 right = y->GetRight();
2461 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2462 int64_t const1_val = GetValue(const1, is_const1_negated);
2463 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2464 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2465 HBasicBlock* block = instruction->GetBlock();
2466 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2467 ArenaAllocator* arena = instruction->GetArena();
2468 HInstruction* z;
2469
2470 if (is_x_negated) {
2471 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2472 } else {
2473 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2474 }
2475
2476 block->ReplaceAndRemoveInstructionWith(instruction, z);
2477 RecordSimplification();
2478 return true;
2479}
2480
Lena Djokicbc5460b2017-07-20 16:07:36 +02002481void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
2482 if (TryCombineVecMultiplyAccumulate(instruction)) {
2483 RecordSimplification();
2484 }
2485}
2486
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002487} // namespace art