blob: fcb3471821fab6b33ba40d403fc959883ea3ed4e [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
Calin Juravleacf735c2015-02-12 15:25:22 +000019#include "mirror/class-inl.h"
20#include "scoped_thread_state_change.h"
21
Nicolas Geoffray3c049742014-09-24 18:10:46 +010022namespace art {
23
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000024class InstructionSimplifierVisitor : public HGraphVisitor {
25 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000026 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Alexandre Rames188d4312015-04-09 18:30:21 +010027 : HGraphVisitor(graph),
28 stats_(stats) {}
29
30 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000031
32 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010033 void RecordSimplification() {
34 simplification_occurred_ = true;
35 simplifications_at_current_position_++;
36 if (stats_) {
37 stats_->RecordStat(kInstructionSimplifications);
38 }
39 }
40
41 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000042 void VisitShift(HBinaryOperation* shift);
43
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000044 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
45 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010046 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
47 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010048 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
49 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000050 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000051 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000052 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080053 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000054 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000055 void VisitAdd(HAdd* instruction) OVERRIDE;
56 void VisitAnd(HAnd* instruction) OVERRIDE;
57 void VisitDiv(HDiv* instruction) OVERRIDE;
58 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010059 void VisitNeg(HNeg* instruction) OVERRIDE;
60 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000061 void VisitOr(HOr* instruction) OVERRIDE;
62 void VisitShl(HShl* instruction) OVERRIDE;
63 void VisitShr(HShr* instruction) OVERRIDE;
64 void VisitSub(HSub* instruction) OVERRIDE;
65 void VisitUShr(HUShr* instruction) OVERRIDE;
66 void VisitXor(HXor* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010067 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +010068 bool IsDominatedByInputNullCheck(HInstruction* instr);
Calin Juravleacf735c2015-02-12 15:25:22 +000069
70 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010071 bool simplification_occurred_ = false;
72 int simplifications_at_current_position_ = 0;
73 // We ensure we do not loop infinitely. The value is a finger in the air guess
74 // that should allow enough simplification.
75 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000076};
77
Nicolas Geoffray3c049742014-09-24 18:10:46 +010078void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000079 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010080 visitor.Run();
81}
82
83void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +010084 // Iterate in reverse post order to open up more simplifications to users
85 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +010086 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
87 // The simplification of an instruction to another instruction may yield
88 // possibilities for other simplifications. So although we perform a reverse
89 // post order visit, we sometimes need to revisit an instruction index.
90 simplification_occurred_ = false;
91 VisitBasicBlock(it.Current());
92 if (simplification_occurred_ &&
93 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
94 // New simplifications may be applicable to the instruction at the
95 // current index, so don't advance the iterator.
96 continue;
97 }
Alexandre Rames188d4312015-04-09 18:30:21 +010098 simplifications_at_current_position_ = 0;
99 it.Advance();
100 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100101}
102
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000103namespace {
104
105bool AreAllBitsSet(HConstant* constant) {
106 return Int64FromConstant(constant) == -1;
107}
108
109} // namespace
110
Alexandre Rames188d4312015-04-09 18:30:21 +0100111// Returns true if the code was simplified to use only one negation operation
112// after the binary operation instead of one on each of the inputs.
113bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
114 DCHECK(binop->IsAdd() || binop->IsSub());
115 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
116 HNeg* left_neg = binop->GetLeft()->AsNeg();
117 HNeg* right_neg = binop->GetRight()->AsNeg();
118 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
119 !right_neg->HasOnlyOneNonEnvironmentUse()) {
120 return false;
121 }
122 // Replace code looking like
123 // NEG tmp1, a
124 // NEG tmp2, b
125 // ADD dst, tmp1, tmp2
126 // with
127 // ADD tmp, a, b
128 // NEG dst, tmp
129 binop->ReplaceInput(left_neg->GetInput(), 0);
130 binop->ReplaceInput(right_neg->GetInput(), 1);
131 left_neg->GetBlock()->RemoveInstruction(left_neg);
132 right_neg->GetBlock()->RemoveInstruction(right_neg);
133 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
134 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
135 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
136 RecordSimplification();
137 return true;
138}
139
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000140void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
141 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
142 HConstant* input_cst = instruction->GetConstantRight();
143 HInstruction* input_other = instruction->GetLeastConstantLeft();
144
Mark Mendellba56d062015-05-05 21:34:03 -0400145 if (input_cst != nullptr) {
146 if (input_cst->IsZero()) {
147 // Replace code looking like
148 // SHL dst, src, 0
149 // with
150 // src
151 instruction->ReplaceWith(input_other);
152 instruction->GetBlock()->RemoveInstruction(instruction);
153 } else if (instruction->IsShl() && input_cst->IsOne()) {
154 // Replace Shl looking like
155 // SHL dst, src, 1
156 // with
157 // ADD dst, src, src
158 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
159 input_other,
160 input_other);
161 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
162 RecordSimplification();
163 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000164 }
165}
166
Calin Juravle10e244f2015-01-26 18:54:32 +0000167void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
168 HInstruction* obj = null_check->InputAt(0);
169 if (!obj->CanBeNull()) {
170 null_check->ReplaceWith(obj);
171 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000172 if (stats_ != nullptr) {
173 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
174 }
175 }
176}
177
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100178bool InstructionSimplifierVisitor::IsDominatedByInputNullCheck(HInstruction* instr) {
179 HInstruction* input = instr->InputAt(0);
180 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
181 HInstruction* use = it.Current()->GetUser();
182 if (use->IsNullCheck() && use->StrictlyDominates(instr)) {
183 return true;
184 }
185 }
186 return false;
187}
188
Calin Juravleacf735c2015-02-12 15:25:22 +0000189void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
190 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100191 if (!check_cast->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(check_cast)) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100192 check_cast->ClearMustDoNullCheck();
193 }
194
Calin Juravleacf735c2015-02-12 15:25:22 +0000195 if (!load_class->IsResolved()) {
196 // If the class couldn't be resolve it's not safe to compare against it. It's
197 // default type would be Top which might be wider that the actual class type
198 // and thus producing wrong results.
199 return;
200 }
201 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
202 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
203 ScopedObjectAccess soa(Thread::Current());
204 if (class_rti.IsSupertypeOf(obj_rti)) {
205 check_cast->GetBlock()->RemoveInstruction(check_cast);
206 if (stats_ != nullptr) {
207 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
208 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000209 }
210}
211
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100212void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100213 if (!instruction->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(instruction)) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100214 instruction->ClearMustDoNullCheck();
215 }
216}
217
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100218void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
219 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
220 && !instruction->GetValue()->CanBeNull()) {
221 instruction->ClearValueCanBeNull();
222 }
223}
224
225void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
226 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
227 && !instruction->GetValue()->CanBeNull()) {
228 instruction->ClearValueCanBeNull();
229 }
230}
231
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000232void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 HBasicBlock* block = check->GetBlock();
234 // Currently always keep the suspend check at entry.
235 if (block->IsEntryBlock()) return;
236
237 // Currently always keep suspend checks at loop entry.
238 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
239 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
240 return;
241 }
242
243 // Remove the suspend check that was added at build time for the baseline
244 // compiler.
245 block->RemoveInstruction(check);
246}
247
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000248void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100249 HInstruction* input_const = equal->GetConstantRight();
250 if (input_const != nullptr) {
251 HInstruction* input_value = equal->GetLeastConstantLeft();
252 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
253 HBasicBlock* block = equal->GetBlock();
254 if (input_const->AsIntConstant()->IsOne()) {
255 // Replace (bool_value == true) with bool_value
256 equal->ReplaceWith(input_value);
257 block->RemoveInstruction(equal);
258 RecordSimplification();
259 } else {
260 // Replace (bool_value == false) with !bool_value
261 DCHECK(input_const->AsIntConstant()->IsZero());
262 block->ReplaceAndRemoveInstructionWith(
263 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
264 RecordSimplification();
265 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100266 }
267 }
268}
269
David Brazdil0d13fee2015-04-17 14:52:19 +0100270void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
271 HInstruction* input_const = not_equal->GetConstantRight();
272 if (input_const != nullptr) {
273 HInstruction* input_value = not_equal->GetLeastConstantLeft();
274 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
275 HBasicBlock* block = not_equal->GetBlock();
276 if (input_const->AsIntConstant()->IsOne()) {
277 // Replace (bool_value != true) with !bool_value
278 block->ReplaceAndRemoveInstructionWith(
279 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
280 RecordSimplification();
281 } else {
282 // Replace (bool_value != false) with bool_value
283 DCHECK(input_const->AsIntConstant()->IsZero());
284 not_equal->ReplaceWith(input_value);
285 block->RemoveInstruction(not_equal);
286 RecordSimplification();
287 }
288 }
289 }
290}
291
292void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
293 HInstruction* parent = bool_not->InputAt(0);
294 if (parent->IsBooleanNot()) {
295 HInstruction* value = parent->InputAt(0);
296 // Replace (!(!bool_value)) with bool_value
297 bool_not->ReplaceWith(value);
298 bool_not->GetBlock()->RemoveInstruction(bool_not);
299 // It is possible that `parent` is dead at this point but we leave
300 // its removal to DCE for simplicity.
301 RecordSimplification();
302 }
303}
304
Mingyao Yang0304e182015-01-30 16:41:29 -0800305void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
306 HInstruction* input = instruction->InputAt(0);
307 // If the array is a NewArray with constant size, replace the array length
308 // with the constant instruction. This helps the bounds check elimination phase.
309 if (input->IsNewArray()) {
310 input = input->InputAt(0);
311 if (input->IsIntConstant()) {
312 instruction->ReplaceWith(input);
313 }
314 }
315}
316
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000317void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000318 HInstruction* value = instruction->GetValue();
319 if (value->GetType() != Primitive::kPrimNot) return;
320
321 if (value->IsArrayGet()) {
322 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
323 // If the code is just swapping elements in the array, no need for a type check.
324 instruction->ClearNeedsTypeCheck();
325 }
326 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100327
328 if (!value->CanBeNull()) {
329 instruction->ClearValueCanBeNull();
330 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000331}
332
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000333void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
334 if (instruction->GetResultType() == instruction->GetInputType()) {
335 // Remove the instruction if it's converting to the same type.
336 instruction->ReplaceWith(instruction->GetInput());
337 instruction->GetBlock()->RemoveInstruction(instruction);
338 }
339}
340
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000341void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
342 HConstant* input_cst = instruction->GetConstantRight();
343 HInstruction* input_other = instruction->GetLeastConstantLeft();
344 if ((input_cst != nullptr) && input_cst->IsZero()) {
345 // Replace code looking like
346 // ADD dst, src, 0
347 // with
348 // src
349 instruction->ReplaceWith(input_other);
350 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100351 return;
352 }
353
354 HInstruction* left = instruction->GetLeft();
355 HInstruction* right = instruction->GetRight();
356 bool left_is_neg = left->IsNeg();
357 bool right_is_neg = right->IsNeg();
358
359 if (left_is_neg && right_is_neg) {
360 if (TryMoveNegOnInputsAfterBinop(instruction)) {
361 return;
362 }
363 }
364
365 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
366 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
367 // Replace code looking like
368 // NEG tmp, b
369 // ADD dst, a, tmp
370 // with
371 // SUB dst, a, b
372 // We do not perform the optimization if the input negation has environment
373 // uses or multiple non-environment uses as it could lead to worse code. In
374 // particular, we do not want the live range of `b` to be extended if we are
375 // not sure the initial 'NEG' instruction can be removed.
376 HInstruction* other = left_is_neg ? right : left;
377 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
378 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
379 RecordSimplification();
380 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000381 }
382}
383
384void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
385 HConstant* input_cst = instruction->GetConstantRight();
386 HInstruction* input_other = instruction->GetLeastConstantLeft();
387
388 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
389 // Replace code looking like
390 // AND dst, src, 0xFFF...FF
391 // with
392 // src
393 instruction->ReplaceWith(input_other);
394 instruction->GetBlock()->RemoveInstruction(instruction);
395 return;
396 }
397
398 // We assume that GVN has run before, so we only perform a pointer comparison.
399 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100400 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000401 if (instruction->GetLeft() == instruction->GetRight()) {
402 // Replace code looking like
403 // AND dst, src, src
404 // with
405 // src
406 instruction->ReplaceWith(instruction->GetLeft());
407 instruction->GetBlock()->RemoveInstruction(instruction);
408 }
409}
410
411void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
412 HConstant* input_cst = instruction->GetConstantRight();
413 HInstruction* input_other = instruction->GetLeastConstantLeft();
414 Primitive::Type type = instruction->GetType();
415
416 if ((input_cst != nullptr) && input_cst->IsOne()) {
417 // Replace code looking like
418 // DIV dst, src, 1
419 // with
420 // src
421 instruction->ReplaceWith(input_other);
422 instruction->GetBlock()->RemoveInstruction(instruction);
423 return;
424 }
425
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000426 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000427 // Replace code looking like
428 // DIV dst, src, -1
429 // with
430 // NEG dst, src
431 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000432 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100433 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000434 return;
435 }
436
437 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
438 // Try replacing code looking like
439 // DIV dst, src, constant
440 // with
441 // MUL dst, src, 1 / constant
442 HConstant* reciprocal = nullptr;
443 if (type == Primitive::Primitive::kPrimDouble) {
444 double value = input_cst->AsDoubleConstant()->GetValue();
445 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
446 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
447 }
448 } else {
449 DCHECK_EQ(type, Primitive::kPrimFloat);
450 float value = input_cst->AsFloatConstant()->GetValue();
451 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
452 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
453 }
454 }
455
456 if (reciprocal != nullptr) {
457 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
458 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
459 RecordSimplification();
460 return;
461 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000462 }
463}
464
465void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
466 HConstant* input_cst = instruction->GetConstantRight();
467 HInstruction* input_other = instruction->GetLeastConstantLeft();
468 Primitive::Type type = instruction->GetType();
469 HBasicBlock* block = instruction->GetBlock();
470 ArenaAllocator* allocator = GetGraph()->GetArena();
471
472 if (input_cst == nullptr) {
473 return;
474 }
475
476 if (input_cst->IsOne()) {
477 // Replace code looking like
478 // MUL dst, src, 1
479 // with
480 // src
481 instruction->ReplaceWith(input_other);
482 instruction->GetBlock()->RemoveInstruction(instruction);
483 return;
484 }
485
486 if (input_cst->IsMinusOne() &&
487 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
488 // Replace code looking like
489 // MUL dst, src, -1
490 // with
491 // NEG dst, src
492 HNeg* neg = new (allocator) HNeg(type, input_other);
493 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100494 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000495 return;
496 }
497
498 if (Primitive::IsFloatingPointType(type) &&
499 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
500 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
501 // Replace code looking like
502 // FP_MUL dst, src, 2.0
503 // with
504 // FP_ADD dst, src, src
505 // The 'int' and 'long' cases are handled below.
506 block->ReplaceAndRemoveInstructionWith(instruction,
507 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100508 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000509 return;
510 }
511
512 if (Primitive::IsIntOrLongType(type)) {
513 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600514 // Even though constant propagation also takes care of the zero case, other
515 // optimizations can lead to having a zero multiplication.
516 if (factor == 0) {
517 // Replace code looking like
518 // MUL dst, src, 0
519 // with
520 // 0
521 instruction->ReplaceWith(input_cst);
522 instruction->GetBlock()->RemoveInstruction(instruction);
523 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000524 // Replace code looking like
525 // MUL dst, src, pow_of_2
526 // with
527 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000529 HShl* shl = new(allocator) HShl(type, input_other, shift);
530 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100531 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000532 }
533 }
534}
535
Alexandre Rames188d4312015-04-09 18:30:21 +0100536void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
537 HInstruction* input = instruction->GetInput();
538 if (input->IsNeg()) {
539 // Replace code looking like
540 // NEG tmp, src
541 // NEG dst, tmp
542 // with
543 // src
544 HNeg* previous_neg = input->AsNeg();
545 instruction->ReplaceWith(previous_neg->GetInput());
546 instruction->GetBlock()->RemoveInstruction(instruction);
547 // We perform the optimization even if the input negation has environment
548 // uses since it allows removing the current instruction. But we only delete
549 // the input negation only if it is does not have any uses left.
550 if (!previous_neg->HasUses()) {
551 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
552 }
553 RecordSimplification();
554 return;
555 }
556
Serguei Katkov339dfc22015-04-20 12:29:32 +0600557 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
558 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100559 // Replace code looking like
560 // SUB tmp, a, b
561 // NEG dst, tmp
562 // with
563 // SUB dst, b, a
564 // We do not perform the optimization if the input subtraction has
565 // environment uses or multiple non-environment uses as it could lead to
566 // worse code. In particular, we do not want the live ranges of `a` and `b`
567 // to be extended if we are not sure the initial 'SUB' instruction can be
568 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600569 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100570 HSub* sub = input->AsSub();
571 HSub* new_sub =
572 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
573 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
574 if (!sub->HasUses()) {
575 sub->GetBlock()->RemoveInstruction(sub);
576 }
577 RecordSimplification();
578 }
579}
580
581void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
582 HInstruction* input = instruction->GetInput();
583 if (input->IsNot()) {
584 // Replace code looking like
585 // NOT tmp, src
586 // NOT dst, tmp
587 // with
588 // src
589 // We perform the optimization even if the input negation has environment
590 // uses since it allows removing the current instruction. But we only delete
591 // the input negation only if it is does not have any uses left.
592 HNot* previous_not = input->AsNot();
593 instruction->ReplaceWith(previous_not->GetInput());
594 instruction->GetBlock()->RemoveInstruction(instruction);
595 if (!previous_not->HasUses()) {
596 previous_not->GetBlock()->RemoveInstruction(previous_not);
597 }
598 RecordSimplification();
599 }
600}
601
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000602void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
603 HConstant* input_cst = instruction->GetConstantRight();
604 HInstruction* input_other = instruction->GetLeastConstantLeft();
605
606 if ((input_cst != nullptr) && input_cst->IsZero()) {
607 // Replace code looking like
608 // OR dst, src, 0
609 // with
610 // src
611 instruction->ReplaceWith(input_other);
612 instruction->GetBlock()->RemoveInstruction(instruction);
613 return;
614 }
615
616 // We assume that GVN has run before, so we only perform a pointer comparison.
617 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100618 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000619 if (instruction->GetLeft() == instruction->GetRight()) {
620 // Replace code looking like
621 // OR dst, src, src
622 // with
623 // src
624 instruction->ReplaceWith(instruction->GetLeft());
625 instruction->GetBlock()->RemoveInstruction(instruction);
626 }
627}
628
629void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
630 VisitShift(instruction);
631}
632
633void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
634 VisitShift(instruction);
635}
636
637void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
638 HConstant* input_cst = instruction->GetConstantRight();
639 HInstruction* input_other = instruction->GetLeastConstantLeft();
640
641 if ((input_cst != nullptr) && input_cst->IsZero()) {
642 // Replace code looking like
643 // SUB dst, src, 0
644 // with
645 // src
646 instruction->ReplaceWith(input_other);
647 instruction->GetBlock()->RemoveInstruction(instruction);
648 return;
649 }
650
651 Primitive::Type type = instruction->GetType();
652 if (!Primitive::IsIntegralType(type)) {
653 return;
654 }
655
656 HBasicBlock* block = instruction->GetBlock();
657 ArenaAllocator* allocator = GetGraph()->GetArena();
658
Alexandre Rames188d4312015-04-09 18:30:21 +0100659 HInstruction* left = instruction->GetLeft();
660 HInstruction* right = instruction->GetRight();
661 if (left->IsConstant()) {
662 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000663 // Replace code looking like
664 // SUB dst, 0, src
665 // with
666 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100667 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000668 // `x` is `0.0`, the former expression yields `0.0`, while the later
669 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100670 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000671 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100672 RecordSimplification();
673 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000674 }
675 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100676
677 if (left->IsNeg() && right->IsNeg()) {
678 if (TryMoveNegOnInputsAfterBinop(instruction)) {
679 return;
680 }
681 }
682
683 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
684 // Replace code looking like
685 // NEG tmp, b
686 // SUB dst, a, tmp
687 // with
688 // ADD dst, a, b
689 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
690 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
691 RecordSimplification();
692 right->GetBlock()->RemoveInstruction(right);
693 return;
694 }
695
696 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
697 // Replace code looking like
698 // NEG tmp, a
699 // SUB dst, tmp, b
700 // with
701 // ADD tmp, a, b
702 // NEG dst, tmp
703 // The second version is not intrinsically better, but enables more
704 // transformations.
705 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
706 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
707 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
708 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
709 instruction->ReplaceWith(neg);
710 instruction->GetBlock()->RemoveInstruction(instruction);
711 RecordSimplification();
712 left->GetBlock()->RemoveInstruction(left);
713 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000714}
715
716void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
717 VisitShift(instruction);
718}
719
720void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
721 HConstant* input_cst = instruction->GetConstantRight();
722 HInstruction* input_other = instruction->GetLeastConstantLeft();
723
724 if ((input_cst != nullptr) && input_cst->IsZero()) {
725 // Replace code looking like
726 // XOR dst, src, 0
727 // with
728 // src
729 instruction->ReplaceWith(input_other);
730 instruction->GetBlock()->RemoveInstruction(instruction);
731 return;
732 }
733
734 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
735 // Replace code looking like
736 // XOR dst, src, 0xFFF...FF
737 // with
738 // NOT dst, src
739 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
740 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100741 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000742 return;
743 }
744}
745
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100746} // namespace art