blob: 8aa315a7e349df933e8d7c3141208028c90965b8 [file] [log] [blame]
Mark Mendellee8d9712016-07-12 11:13:15 -04001/*
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"
19
20namespace art {
21namespace x86 {
22
23/**
24 * Replace instructions with memory operand forms.
25 */
26class MemoryOperandVisitor : public HGraphVisitor {
27 public:
28 MemoryOperandVisitor(HGraph* graph, bool do_implicit_null_checks)
29 : HGraphVisitor(graph),
30 do_implicit_null_checks_(do_implicit_null_checks) {}
31
32 private:
33 void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE {
34 // Replace the length by the array itself, so that we can do compares to memory.
35 HArrayLength* array_len = check->InputAt(1)->AsArrayLength();
36
37 // We only want to replace an ArrayLength.
38 if (array_len == nullptr) {
39 return;
40 }
41
42 HInstruction* array = array_len->InputAt(0);
43 DCHECK_EQ(array->GetType(), Primitive::kPrimNot);
44
45 // Don't apply this optimization when the array is nullptr.
46 if (array->IsConstant() || (array->IsNullCheck() && array->InputAt(0)->IsConstant())) {
47 return;
48 }
49
50 // Is there a null check that could be an implicit check?
51 if (array->IsNullCheck() && do_implicit_null_checks_) {
52 // The ArrayLen may generate the implicit null check. Can the
53 // bounds check do so as well?
54 if (array_len->GetNextDisregardingMoves() != check) {
55 // No, it won't. Leave as is.
56 return;
57 }
58 }
59
60 // Can we suppress the ArrayLength and generate at BoundCheck?
61 if (array_len->HasOnlyOneNonEnvironmentUse()) {
62 array_len->MarkEmittedAtUseSite();
63 // We need the ArrayLength just before the BoundsCheck.
64 array_len->MoveBefore(check);
65 }
66 }
67
68 bool do_implicit_null_checks_;
69};
70
71X86MemoryOperandGeneration::X86MemoryOperandGeneration(HGraph* graph,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070072 CodeGenerator* codegen,
73 OptimizingCompilerStats* stats)
Mark Mendellee8d9712016-07-12 11:13:15 -040074 : HOptimization(graph, kX86MemoryOperandGenerationPassName, stats),
75 do_implicit_null_checks_(codegen->GetCompilerOptions().GetImplicitNullChecks()) {
76}
77
78void X86MemoryOperandGeneration::Run() {
79 MemoryOperandVisitor visitor(graph_, do_implicit_null_checks_);
80 visitor.VisitInsertionOrder();
81}
82
83} // namespace x86
84} // namespace art