summaryrefslogtreecommitdiff
path: root/compiler/optimizing/constant_folding.cc
diff options
context:
space:
mode:
author Vladimir Marko <vmarko@google.com> 2023-04-25 16:40:06 +0000
committer Vladimir Marko <vmarko@google.com> 2023-04-27 10:53:55 +0000
commitcde6497d286337de2ed21c71c85157e2745b742b (patch)
tree087d790efb6987f5aab1da7cd91b89bedcdc5725 /compiler/optimizing/constant_folding.cc
parent79dc217688a774fc532584f6551a0aec8b45bc4a (diff)
Optimizing: Add `HInstruction::As##type()`.
After the old implementation was renamed in https://android-review.googlesource.com/2526708 , we introduce a new function with the old name but new behavior, just `DCHECK()`-ing the instruction kind before casting down the pointer. We change appropriate calls from `As##type##OrNull()` to `As##type()` to avoid unncessary run-time checks and reduce the size of libart-compiler.so. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Test: run-gtests.sh Test: testrunner.py --target --optimizing Bug: 181943478 Change-Id: I025681612a77ca2157fed4886ca47f2053975d4e
Diffstat (limited to 'compiler/optimizing/constant_folding.cc')
-rw-r--r--compiler/optimizing/constant_folding.cc53
1 files changed, 18 insertions, 35 deletions
diff --git a/compiler/optimizing/constant_folding.cc b/compiler/optimizing/constant_folding.cc
index c27d22c8d6..2163c007e6 100644
--- a/compiler/optimizing/constant_folding.cc
+++ b/compiler/optimizing/constant_folding.cc
@@ -132,8 +132,7 @@ void HConstantFoldingVisitor::VisitBinaryOperation(HBinaryOperation* inst) {
void HConstantFoldingVisitor::VisitDivZeroCheck(HDivZeroCheck* inst) {
// We can safely remove the check if the input is a non-null constant.
HInstruction* check_input = inst->InputAt(0);
- // TODO: Remove "OrNull".
- if (check_input->IsConstant() && !check_input->AsConstantOrNull()->IsArithmeticZero()) {
+ if (check_input->IsConstant() && !check_input->AsConstant()->IsArithmeticZero()) {
inst->ReplaceWith(check_input);
inst->GetBlock()->RemoveInstruction(inst);
}
@@ -195,8 +194,7 @@ void HConstantFoldingVisitor::VisitIf(HIf* inst) {
if (!if_input->IsCondition()) {
return;
}
- // TODO: Remove "OrNull".
- HCondition* condition = if_input->AsConditionOrNull();
+ HCondition* condition = if_input->AsCondition();
// We want either `==` or `!=`, since we cannot make assumptions for other conditions e.g. `>`
if (!condition->IsEqual() && !condition->IsNotEqual()) {
@@ -219,8 +217,7 @@ void HConstantFoldingVisitor::VisitIf(HIf* inst) {
// }
// Similarly with variable != constant, except that we can make guarantees in the else case.
- // TODO: Remove "OrNull".
- HConstant* constant = left->IsConstant() ? left->AsConstantOrNull() : right->AsConstantOrNull();
+ HConstant* constant = left->IsConstant() ? left->AsConstant() : right->AsConstant();
HInstruction* variable = left->IsConstant() ? right : left;
// Don't deal with floats/doubles since they bring a lot of edge cases e.g.
@@ -241,18 +238,15 @@ void HConstantFoldingVisitor::VisitIf(HIf* inst) {
}
// Update left and right to be the ones from the HCompare.
- // TODO: Remove "OrNull".
- left = variable->AsCompareOrNull()->GetLeft();
- // TODO: Remove "OrNull".
- right = variable->AsCompareOrNull()->GetRight();
+ left = variable->AsCompare()->GetLeft();
+ right = variable->AsCompare()->GetRight();
// Re-check that one of them to be a constant and not the other.
if (left->IsConstant() == right->IsConstant()) {
return;
}
- // TODO: Remove "OrNull".
- constant = left->IsConstant() ? left->AsConstantOrNull() : right->AsConstantOrNull();
+ constant = left->IsConstant() ? left->AsConstant() : right->AsConstant();
variable = left->IsConstant() ? right : left;
// Re-check floating point values.
@@ -274,14 +268,12 @@ void HConstantFoldingVisitor::VisitIf(HIf* inst) {
// we cannot make an assumption for the `else` branch.
if (variable->GetType() == DataType::Type::kBool &&
constant->IsIntConstant() &&
- // TODO: Remove "OrNull".
- (constant->AsIntConstantOrNull()->IsTrue() || constant->AsIntConstantOrNull()->IsFalse())) {
+ (constant->AsIntConstant()->IsTrue() || constant->AsIntConstant()->IsFalse())) {
HBasicBlock* other_starting_block =
condition->IsEqual() ? inst->IfFalseSuccessor() : inst->IfTrueSuccessor();
DCHECK_NE(other_starting_block, starting_block);
- // TODO: Remove "OrNull".
- HConstant* other_constant = constant->AsIntConstantOrNull()->IsTrue() ?
+ HConstant* other_constant = constant->AsIntConstant()->IsTrue() ?
GetGraph()->GetIntConstant(0) :
GetGraph()->GetIntConstant(1);
DCHECK_NE(other_constant, constant);
@@ -293,8 +285,7 @@ void HConstantFoldingVisitor::VisitArrayLength(HArrayLength* inst) {
HInstruction* input = inst->InputAt(0);
if (input->IsLoadString()) {
DCHECK(inst->IsStringLength());
- // TODO: Remove "OrNull".
- HLoadString* load_string = input->AsLoadStringOrNull();
+ HLoadString* load_string = input->AsLoadString();
const DexFile& dex_file = load_string->GetDexFile();
const dex::StringId& string_id = dex_file.GetStringId(load_string->GetStringIndex());
inst->ReplaceWith(GetGraph()->GetIntConstant(dex_file.GetStringLength(string_id)));
@@ -314,8 +305,7 @@ void HConstantFoldingVisitor::VisitTypeConversion(HTypeConversion* inst) {
void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) {
DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
HInstruction* left = instruction->GetLeft();
- // TODO: Remove "OrNull".
- if (left->IsConstant() && left->AsConstantOrNull()->IsArithmeticZero()) {
+ if (left->IsConstant() && left->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// SHL dst, 0, shift_amount
// with
@@ -375,8 +365,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitAbove(HAbove* instruction) {
instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0));
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (instruction->GetLeft()->IsConstant() &&
- // TODO: Remove "OrNull".
- instruction->GetLeft()->AsConstantOrNull()->IsArithmeticZero()) {
+ instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// ABOVE dst, 0, src // unsigned 0 > src is always false
// with
@@ -394,8 +383,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitAboveOrEqual(HAboveOrEqual* i
instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1));
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (instruction->GetRight()->IsConstant() &&
- // TODO: Remove "OrNull".
- instruction->GetRight()->AsConstantOrNull()->IsArithmeticZero()) {
+ instruction->GetRight()->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// ABOVE_OR_EQUAL dst, src, 0 // unsigned src >= 0 is always true
// with
@@ -413,8 +401,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitBelow(HBelow* instruction) {
instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0));
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (instruction->GetRight()->IsConstant() &&
- // TODO: Remove "OrNull".
- instruction->GetRight()->AsConstantOrNull()->IsArithmeticZero()) {
+ instruction->GetRight()->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// BELOW dst, src, 0 // unsigned src < 0 is always false
// with
@@ -432,8 +419,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitBelowOrEqual(HBelowOrEqual* i
instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1));
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (instruction->GetLeft()->IsConstant() &&
- // TODO: Remove "OrNull".
- instruction->GetLeft()->AsConstantOrNull()->IsArithmeticZero()) {
+ instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// BELOW_OR_EQUAL dst, 0, src // unsigned 0 <= src is always true
// with
@@ -515,8 +501,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) {
// CONSTANT 0
HInstruction* hnot = (left->IsNot() ? left : right);
HInstruction* hother = (left->IsNot() ? right : left);
- // TODO: Remove "OrNull".
- HInstruction* src = hnot->AsNotOrNull()->GetInput();
+ HInstruction* src = hnot->AsNot()->GetInput();
if (src == hother) {
instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
@@ -529,10 +514,9 @@ void InstructionWithAbsorbingInputSimplifier::VisitCompare(HCompare* instruction
HConstant* input_cst = instruction->GetConstantRight();
if (input_cst != nullptr) {
HInstruction* input_value = instruction->GetLeastConstantLeft();
- // TODO: Remove "OrNull".
if (DataType::IsFloatingPointType(input_value->GetType()) &&
- ((input_cst->IsFloatConstant() && input_cst->AsFloatConstantOrNull()->IsNaN()) ||
- (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstantOrNull()->IsNaN()))) {
+ ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->IsNaN()) ||
+ (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->IsNaN()))) {
// Replace code looking like
// CMP{G,L}-{FLOAT,DOUBLE} dst, src, NaN
// with
@@ -590,8 +574,7 @@ void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) {
HBasicBlock* block = instruction->GetBlock();
if (instruction->GetLeft()->IsConstant() &&
- // TODO: Remove "OrNull".
- instruction->GetLeft()->AsConstantOrNull()->IsArithmeticZero()) {
+ instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
// Replace code looking like
// REM dst, 0, src
// with