blob: 2ef19b92a16e5f4a4f8824fbfe6b0c57682e1bfe [file] [log] [blame]
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "instruction_simplifier.h"
#include "mirror/class-inl.h"
#include "scoped_thread_state_change.h"
namespace art {
class InstructionSimplifierVisitor : public HGraphVisitor {
public:
InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
: HGraphVisitor(graph), stats_(stats) {}
private:
void VisitShift(HBinaryOperation* shift);
void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
void VisitEqual(HEqual* equal) OVERRIDE;
void VisitArraySet(HArraySet* equal) OVERRIDE;
void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
void VisitAdd(HAdd* instruction) OVERRIDE;
void VisitAnd(HAnd* instruction) OVERRIDE;
void VisitDiv(HDiv* instruction) OVERRIDE;
void VisitMul(HMul* instruction) OVERRIDE;
void VisitOr(HOr* instruction) OVERRIDE;
void VisitShl(HShl* instruction) OVERRIDE;
void VisitShr(HShr* instruction) OVERRIDE;
void VisitSub(HSub* instruction) OVERRIDE;
void VisitUShr(HUShr* instruction) OVERRIDE;
void VisitXor(HXor* instruction) OVERRIDE;
OptimizingCompilerStats* stats_;
};
void InstructionSimplifier::Run() {
InstructionSimplifierVisitor visitor(graph_, stats_);
visitor.VisitInsertionOrder();
}
namespace {
bool AreAllBitsSet(HConstant* constant) {
return Int64FromConstant(constant) == -1;
}
} // namespace
void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// SHL dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
HInstruction* obj = null_check->InputAt(0);
if (!obj->CanBeNull()) {
null_check->ReplaceWith(obj);
null_check->GetBlock()->RemoveInstruction(null_check);
if (stats_ != nullptr) {
stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
}
}
}
void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
if (!load_class->IsResolved()) {
// If the class couldn't be resolve it's not safe to compare against it. It's
// default type would be Top which might be wider that the actual class type
// and thus producing wrong results.
return;
}
ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
ScopedObjectAccess soa(Thread::Current());
if (class_rti.IsSupertypeOf(obj_rti)) {
check_cast->GetBlock()->RemoveInstruction(check_cast);
if (stats_ != nullptr) {
stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
}
}
}
void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
HBasicBlock* block = check->GetBlock();
// Currently always keep the suspend check at entry.
if (block->IsEntryBlock()) return;
// Currently always keep suspend checks at loop entry.
if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
return;
}
// Remove the suspend check that was added at build time for the baseline
// compiler.
block->RemoveInstruction(check);
}
void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
HInstruction* input1 = equal->InputAt(0);
HInstruction* input2 = equal->InputAt(1);
if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) {
if (input2->AsIntConstant()->GetValue() == 1) {
// Replace (bool_value == 1) with bool_value
equal->ReplaceWith(equal->InputAt(0));
equal->GetBlock()->RemoveInstruction(equal);
} else {
// We should replace (bool_value == 0) with !bool_value, but we unfortunately
// do not have such instruction.
DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0);
}
}
}
void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
HInstruction* input = instruction->InputAt(0);
// If the array is a NewArray with constant size, replace the array length
// with the constant instruction. This helps the bounds check elimination phase.
if (input->IsNewArray()) {
input = input->InputAt(0);
if (input->IsIntConstant()) {
instruction->ReplaceWith(input);
}
}
}
void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
HInstruction* value = instruction->GetValue();
if (value->GetType() != Primitive::kPrimNot) return;
if (value->IsArrayGet()) {
if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
// If the code is just swapping elements in the array, no need for a type check.
instruction->ClearNeedsTypeCheck();
}
}
}
void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
if (instruction->GetResultType() == instruction->GetInputType()) {
// Remove the instruction if it's converting to the same type.
instruction->ReplaceWith(instruction->GetInput());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// ADD dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
// Replace code looking like
// AND dst, src, 0xFFF...FF
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
// We assume that GVN has run before, so we only perform a pointer comparison.
// If for some reason the values are equal but the pointers are different, we
// are still correct and only miss an optimisation opportunity.
if (instruction->GetLeft() == instruction->GetRight()) {
// Replace code looking like
// AND dst, src, src
// with
// src
instruction->ReplaceWith(instruction->GetLeft());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
Primitive::Type type = instruction->GetType();
if ((input_cst != nullptr) && input_cst->IsOne()) {
// Replace code looking like
// DIV dst, src, 1
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if ((input_cst != nullptr) && input_cst->IsMinusOne() &&
(Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
// Replace code looking like
// DIV dst, src, -1
// with
// NEG dst, src
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
instruction, (new (GetGraph()->GetArena()) HNeg(type, input_other)));
}
}
void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
Primitive::Type type = instruction->GetType();
HBasicBlock* block = instruction->GetBlock();
ArenaAllocator* allocator = GetGraph()->GetArena();
if (input_cst == nullptr) {
return;
}
if (input_cst->IsOne()) {
// Replace code looking like
// MUL dst, src, 1
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if (input_cst->IsMinusOne() &&
(Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
// Replace code looking like
// MUL dst, src, -1
// with
// NEG dst, src
HNeg* neg = new (allocator) HNeg(type, input_other);
block->ReplaceAndRemoveInstructionWith(instruction, neg);
return;
}
if (Primitive::IsFloatingPointType(type) &&
((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
(input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
// Replace code looking like
// FP_MUL dst, src, 2.0
// with
// FP_ADD dst, src, src
// The 'int' and 'long' cases are handled below.
block->ReplaceAndRemoveInstructionWith(instruction,
new (allocator) HAdd(type, input_other, input_other));
return;
}
if (Primitive::IsIntOrLongType(type)) {
int64_t factor = Int64FromConstant(input_cst);
// We expect the `0` case to have been handled in the constant folding pass.
DCHECK_NE(factor, 0);
if (IsPowerOfTwo(factor)) {
// Replace code looking like
// MUL dst, src, pow_of_2
// with
// SHL dst, src, log2(pow_of_2)
HIntConstant* shift = new (allocator) HIntConstant(WhichPowerOf2(factor));
block->InsertInstructionBefore(shift, instruction);
HShl* shl = new(allocator) HShl(type, input_other, shift);
block->ReplaceAndRemoveInstructionWith(instruction, shl);
}
}
}
void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// OR dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
// We assume that GVN has run before, so we only perform a pointer comparison.
// If for some reason the values are equal but the pointers are different, we
// are still correct and only miss an optimisation opportunity.
if (instruction->GetLeft() == instruction->GetRight()) {
// Replace code looking like
// OR dst, src, src
// with
// src
instruction->ReplaceWith(instruction->GetLeft());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// SUB dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
Primitive::Type type = instruction->GetType();
if (!Primitive::IsIntegralType(type)) {
return;
}
HBasicBlock* block = instruction->GetBlock();
ArenaAllocator* allocator = GetGraph()->GetArena();
if (instruction->GetLeft()->IsConstant()) {
int64_t left = Int64FromConstant(instruction->GetLeft()->AsConstant());
if (left == 0) {
// Replace code looking like
// SUB dst, 0, src
// with
// NEG dst, src
// Note that we cannot optimise `0.0 - x` to `-x` for floating-point. When
// `x` is `0.0`, the former expression yields `0.0`, while the later
// yields `-0.0`.
HNeg* neg = new (allocator) HNeg(type, instruction->GetRight());
block->ReplaceAndRemoveInstructionWith(instruction, neg);
}
}
}
void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// XOR dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
// Replace code looking like
// XOR dst, src, 0xFFF...FF
// with
// NOT dst, src
HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
return;
}
}
} // namespace art