blob: 54dd2ccaf872e217431448ef0002312c8cd74270 [file] [log] [blame]
Alexandre Rames44b9cf92015-08-19 15:39:06 +01001/*
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 "instruction_simplifier_arm64.h"
18
Alexandre Ramese6dbf482015-10-19 10:10:41 +010019#include "mirror/array-inl.h"
20
Alexandre Rames44b9cf92015-08-19 15:39:06 +010021namespace art {
22namespace arm64 {
23
Alexandre Ramese6dbf482015-10-19 10:10:41 +010024void InstructionSimplifierArm64Visitor::TryExtractArrayAccessAddress(HInstruction* access,
25 HInstruction* array,
26 HInstruction* index,
27 int access_size) {
28 if (index->IsConstant() ||
29 (index->IsBoundsCheck() && index->AsBoundsCheck()->GetIndex()->IsConstant())) {
30 // When the index is a constant all the addressing can be fitted in the
31 // memory access instruction, so do not split the access.
32 return;
33 }
34 if (access->IsArraySet() &&
35 access->AsArraySet()->GetValue()->GetType() == Primitive::kPrimNot) {
36 // The access may require a runtime call or the original array pointer.
37 return;
38 }
39
40 // Proceed to extract the base address computation.
41 ArenaAllocator* arena = GetGraph()->GetArena();
42
43 HIntConstant* offset =
44 GetGraph()->GetIntConstant(mirror::Array::DataOffset(access_size).Uint32Value());
45 HArm64IntermediateAddress* address =
46 new (arena) HArm64IntermediateAddress(array, offset, kNoDexPc);
47 access->GetBlock()->InsertInstructionBefore(address, access);
48 access->ReplaceInput(address, 0);
49 // Both instructions must depend on GC to prevent any instruction that can
50 // trigger GC to be inserted between the two.
51 access->AddSideEffects(SideEffects::DependsOnGC());
52 DCHECK(address->GetSideEffects().Includes(SideEffects::DependsOnGC()));
53 DCHECK(access->GetSideEffects().Includes(SideEffects::DependsOnGC()));
54 // TODO: Code generation for HArrayGet and HArraySet will check whether the input address
55 // is an HArm64IntermediateAddress and generate appropriate code.
56 // We would like to replace the `HArrayGet` and `HArraySet` with custom instructions (maybe
57 // `HArm64Load` and `HArm64Store`). We defer these changes because these new instructions would
58 // not bring any advantages yet.
59 // Also see the comments in
60 // `InstructionCodeGeneratorARM64::VisitArrayGet()` and
61 // `InstructionCodeGeneratorARM64::VisitArraySet()`.
62 RecordSimplification();
63}
64
Alexandre Rames418318f2015-11-20 15:55:47 +000065bool InstructionSimplifierArm64Visitor::TrySimpleMultiplyAccumulatePatterns(
66 HMul* mul, HBinaryOperation* input_binop, HInstruction* input_other) {
67 DCHECK(Primitive::IsIntOrLongType(mul->GetType()));
68 DCHECK(input_binop->IsAdd() || input_binop->IsSub());
69 DCHECK_NE(input_binop, input_other);
70 if (!input_binop->HasOnlyOneNonEnvironmentUse()) {
71 return false;
72 }
73
74 // Try to interpret patterns like
75 // a * (b <+/-> 1)
76 // as
77 // (a * b) <+/-> a
78 HInstruction* input_a = input_other;
79 HInstruction* input_b = nullptr; // Set to a non-null value if we found a pattern to optimize.
80 HInstruction::InstructionKind op_kind;
81
82 if (input_binop->IsAdd()) {
83 if ((input_binop->GetConstantRight() != nullptr) && input_binop->GetConstantRight()->IsOne()) {
84 // Interpret
85 // a * (b + 1)
86 // as
87 // (a * b) + a
88 input_b = input_binop->GetLeastConstantLeft();
89 op_kind = HInstruction::kAdd;
90 }
91 } else {
92 DCHECK(input_binop->IsSub());
93 if (input_binop->GetRight()->IsConstant() &&
94 input_binop->GetRight()->AsConstant()->IsMinusOne()) {
95 // Interpret
96 // a * (b - (-1))
97 // as
98 // a + (a * b)
99 input_b = input_binop->GetLeft();
100 op_kind = HInstruction::kAdd;
101 } else if (input_binop->GetLeft()->IsConstant() &&
102 input_binop->GetLeft()->AsConstant()->IsOne()) {
103 // Interpret
104 // a * (1 - b)
105 // as
106 // a - (a * b)
107 input_b = input_binop->GetRight();
108 op_kind = HInstruction::kSub;
109 }
110 }
111
112 if (input_b == nullptr) {
113 // We did not find a pattern we can optimize.
114 return false;
115 }
116
117 HArm64MultiplyAccumulate* mulacc = new(GetGraph()->GetArena()) HArm64MultiplyAccumulate(
118 mul->GetType(), op_kind, input_a, input_a, input_b, mul->GetDexPc());
119
120 mul->GetBlock()->ReplaceAndRemoveInstructionWith(mul, mulacc);
121 input_binop->GetBlock()->RemoveInstruction(input_binop);
122
123 return false;
124}
125
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100126void InstructionSimplifierArm64Visitor::VisitArrayGet(HArrayGet* instruction) {
127 TryExtractArrayAccessAddress(instruction,
128 instruction->GetArray(),
129 instruction->GetIndex(),
130 Primitive::ComponentSize(instruction->GetType()));
131}
132
133void InstructionSimplifierArm64Visitor::VisitArraySet(HArraySet* instruction) {
134 TryExtractArrayAccessAddress(instruction,
135 instruction->GetArray(),
136 instruction->GetIndex(),
137 Primitive::ComponentSize(instruction->GetComponentType()));
138}
139
Alexandre Rames418318f2015-11-20 15:55:47 +0000140void InstructionSimplifierArm64Visitor::VisitMul(HMul* instruction) {
141 Primitive::Type type = instruction->GetType();
142 if (!Primitive::IsIntOrLongType(type)) {
143 return;
144 }
145
146 HInstruction* use = instruction->HasNonEnvironmentUses()
147 ? instruction->GetUses().GetFirst()->GetUser()
148 : nullptr;
149
150 if (instruction->HasOnlyOneNonEnvironmentUse() && (use->IsAdd() || use->IsSub())) {
151 // Replace code looking like
152 // MUL tmp, x, y
153 // SUB dst, acc, tmp
154 // with
155 // MULSUB dst, acc, x, y
156 // Note that we do not want to (unconditionally) perform the merge when the
157 // multiplication has multiple uses and it can be merged in all of them.
158 // Multiple uses could happen on the same control-flow path, and we would
159 // then increase the amount of work. In the future we could try to evaluate
160 // whether all uses are on different control-flow paths (using dominance and
161 // reverse-dominance information) and only perform the merge when they are.
162 HInstruction* accumulator = nullptr;
163 HBinaryOperation* binop = use->AsBinaryOperation();
164 HInstruction* binop_left = binop->GetLeft();
165 HInstruction* binop_right = binop->GetRight();
166 // Be careful after GVN. This should not happen since the `HMul` has only
167 // one use.
168 DCHECK_NE(binop_left, binop_right);
169 if (binop_right == instruction) {
170 accumulator = binop_left;
171 } else if (use->IsAdd()) {
172 DCHECK_EQ(binop_left, instruction);
173 accumulator = binop_right;
174 }
175
176 if (accumulator != nullptr) {
177 HArm64MultiplyAccumulate* mulacc =
178 new (GetGraph()->GetArena()) HArm64MultiplyAccumulate(type,
179 binop->GetKind(),
180 accumulator,
181 instruction->GetLeft(),
182 instruction->GetRight());
183
184 binop->GetBlock()->ReplaceAndRemoveInstructionWith(binop, mulacc);
185 DCHECK(!instruction->HasUses());
186 instruction->GetBlock()->RemoveInstruction(instruction);
187 RecordSimplification();
188 return;
189 }
190 }
191
192 // Use multiply accumulate instruction for a few simple patterns.
193 // We prefer not applying the following transformations if the left and
194 // right inputs perform the same operation.
195 // We rely on GVN having squashed the inputs if appropriate. However the
196 // results are still correct even if that did not happen.
197 if (instruction->GetLeft() == instruction->GetRight()) {
198 return;
199 }
200
201 HInstruction* left = instruction->GetLeft();
202 HInstruction* right = instruction->GetRight();
203 if ((right->IsAdd() || right->IsSub()) &&
204 TrySimpleMultiplyAccumulatePatterns(instruction, right->AsBinaryOperation(), left)) {
205 return;
206 }
207 if ((left->IsAdd() || left->IsSub()) &&
208 TrySimpleMultiplyAccumulatePatterns(instruction, left->AsBinaryOperation(), right)) {
209 return;
210 }
211}
212
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100213} // namespace arm64
214} // namespace art