Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "x86_memory_gen.h" |
| 18 | #include "code_generator.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 19 | #include "driver/compiler_options.h" |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | namespace x86 { |
| 23 | |
| 24 | /** |
| 25 | * Replace instructions with memory operand forms. |
| 26 | */ |
| 27 | class MemoryOperandVisitor : public HGraphVisitor { |
| 28 | public: |
| 29 | MemoryOperandVisitor(HGraph* graph, bool do_implicit_null_checks) |
| 30 | : HGraphVisitor(graph), |
| 31 | do_implicit_null_checks_(do_implicit_null_checks) {} |
| 32 | |
| 33 | private: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 34 | void VisitBoundsCheck(HBoundsCheck* check) override { |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 35 | // Replace the length by the array itself, so that we can do compares to memory. |
| 36 | HArrayLength* array_len = check->InputAt(1)->AsArrayLength(); |
| 37 | |
| 38 | // We only want to replace an ArrayLength. |
| 39 | if (array_len == nullptr) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | HInstruction* array = array_len->InputAt(0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 44 | DCHECK_EQ(array->GetType(), DataType::Type::kReference); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 45 | |
| 46 | // Don't apply this optimization when the array is nullptr. |
| 47 | if (array->IsConstant() || (array->IsNullCheck() && array->InputAt(0)->IsConstant())) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Is there a null check that could be an implicit check? |
| 52 | if (array->IsNullCheck() && do_implicit_null_checks_) { |
| 53 | // The ArrayLen may generate the implicit null check. Can the |
| 54 | // bounds check do so as well? |
| 55 | if (array_len->GetNextDisregardingMoves() != check) { |
| 56 | // No, it won't. Leave as is. |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Can we suppress the ArrayLength and generate at BoundCheck? |
| 62 | if (array_len->HasOnlyOneNonEnvironmentUse()) { |
| 63 | array_len->MarkEmittedAtUseSite(); |
| 64 | // We need the ArrayLength just before the BoundsCheck. |
| 65 | array_len->MoveBefore(check); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool do_implicit_null_checks_; |
| 70 | }; |
| 71 | |
| 72 | X86MemoryOperandGeneration::X86MemoryOperandGeneration(HGraph* graph, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 73 | CodeGenerator* codegen, |
| 74 | OptimizingCompilerStats* stats) |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 75 | : HOptimization(graph, kX86MemoryOperandGenerationPassName, stats), |
| 76 | do_implicit_null_checks_(codegen->GetCompilerOptions().GetImplicitNullChecks()) { |
| 77 | } |
| 78 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 79 | bool X86MemoryOperandGeneration::Run() { |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 80 | MemoryOperandVisitor visitor(graph_, do_implicit_null_checks_); |
| 81 | visitor.VisitInsertionOrder(); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 82 | return true; |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace x86 |
| 86 | } // namespace art |