blob: 6a34b13320ae244517e7a5c4816d2b262f5f2338 [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 Rames8626b742015-11-25 16:28:08 +000019#include "common_arm64.h"
Alexandre Ramese6dbf482015-10-19 10:10:41 +010020#include "mirror/array-inl.h"
21
Alexandre Rames44b9cf92015-08-19 15:39:06 +010022namespace art {
23namespace arm64 {
24
Alexandre Rames8626b742015-11-25 16:28:08 +000025using helpers::CanFitInShifterOperand;
26using helpers::HasShifterOperand;
27using helpers::ShifterOperandSupportsExtension;
28
Alexandre Ramese6dbf482015-10-19 10:10:41 +010029void InstructionSimplifierArm64Visitor::TryExtractArrayAccessAddress(HInstruction* access,
30 HInstruction* array,
31 HInstruction* index,
32 int access_size) {
33 if (index->IsConstant() ||
34 (index->IsBoundsCheck() && index->AsBoundsCheck()->GetIndex()->IsConstant())) {
35 // When the index is a constant all the addressing can be fitted in the
36 // memory access instruction, so do not split the access.
37 return;
38 }
39 if (access->IsArraySet() &&
40 access->AsArraySet()->GetValue()->GetType() == Primitive::kPrimNot) {
41 // The access may require a runtime call or the original array pointer.
42 return;
43 }
44
45 // Proceed to extract the base address computation.
46 ArenaAllocator* arena = GetGraph()->GetArena();
47
48 HIntConstant* offset =
49 GetGraph()->GetIntConstant(mirror::Array::DataOffset(access_size).Uint32Value());
50 HArm64IntermediateAddress* address =
51 new (arena) HArm64IntermediateAddress(array, offset, kNoDexPc);
52 access->GetBlock()->InsertInstructionBefore(address, access);
53 access->ReplaceInput(address, 0);
54 // Both instructions must depend on GC to prevent any instruction that can
55 // trigger GC to be inserted between the two.
56 access->AddSideEffects(SideEffects::DependsOnGC());
57 DCHECK(address->GetSideEffects().Includes(SideEffects::DependsOnGC()));
58 DCHECK(access->GetSideEffects().Includes(SideEffects::DependsOnGC()));
59 // TODO: Code generation for HArrayGet and HArraySet will check whether the input address
60 // is an HArm64IntermediateAddress and generate appropriate code.
61 // We would like to replace the `HArrayGet` and `HArraySet` with custom instructions (maybe
62 // `HArm64Load` and `HArm64Store`). We defer these changes because these new instructions would
63 // not bring any advantages yet.
64 // Also see the comments in
65 // `InstructionCodeGeneratorARM64::VisitArrayGet()` and
66 // `InstructionCodeGeneratorARM64::VisitArraySet()`.
67 RecordSimplification();
68}
69
Alexandre Rames8626b742015-11-25 16:28:08 +000070bool InstructionSimplifierArm64Visitor::TryMergeIntoShifterOperand(HInstruction* use,
71 HInstruction* bitfield_op,
72 bool do_merge) {
73 DCHECK(HasShifterOperand(use));
74 DCHECK(use->IsBinaryOperation() || use->IsNeg());
75 DCHECK(CanFitInShifterOperand(bitfield_op));
76 DCHECK(!bitfield_op->HasEnvironmentUses());
77
78 Primitive::Type type = use->GetType();
79 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
80 return false;
81 }
82
83 HInstruction* left;
84 HInstruction* right;
85 if (use->IsBinaryOperation()) {
86 left = use->InputAt(0);
87 right = use->InputAt(1);
88 } else {
89 DCHECK(use->IsNeg());
90 right = use->AsNeg()->InputAt(0);
91 left = GetGraph()->GetConstant(right->GetType(), 0);
92 }
93 DCHECK(left == bitfield_op || right == bitfield_op);
94
95 if (left == right) {
96 // TODO: Handle special transformations in this situation?
97 // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
98 // Or should this be part of a separate transformation logic?
99 return false;
100 }
101
102 bool is_commutative = use->IsBinaryOperation() && use->AsBinaryOperation()->IsCommutative();
103 HInstruction* other_input;
104 if (bitfield_op == right) {
105 other_input = left;
106 } else {
107 if (is_commutative) {
108 other_input = right;
109 } else {
110 return false;
111 }
112 }
113
114 HArm64DataProcWithShifterOp::OpKind op_kind;
115 int shift_amount = 0;
116 HArm64DataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
117
118 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind) &&
119 !ShifterOperandSupportsExtension(use)) {
120 return false;
121 }
122
123 if (do_merge) {
124 HArm64DataProcWithShifterOp* alu_with_op =
125 new (GetGraph()->GetArena()) HArm64DataProcWithShifterOp(use,
126 other_input,
127 bitfield_op->InputAt(0),
128 op_kind,
129 shift_amount,
130 use->GetDexPc());
131 use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
132 if (bitfield_op->GetUses().IsEmpty()) {
133 bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
134 }
135 RecordSimplification();
136 }
137
138 return true;
139}
140
141// Merge a bitfield move instruction into its uses if it can be merged in all of them.
142bool InstructionSimplifierArm64Visitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
143 DCHECK(CanFitInShifterOperand(bitfield_op));
144
145 if (bitfield_op->HasEnvironmentUses()) {
146 return false;
147 }
148
149 const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
150
151 // Check whether we can merge the instruction in all its users' shifter operand.
152 for (HUseIterator<HInstruction*> it_use(uses); !it_use.Done(); it_use.Advance()) {
153 HInstruction* use = it_use.Current()->GetUser();
154 if (!HasShifterOperand(use)) {
155 return false;
156 }
157 if (!CanMergeIntoShifterOperand(use, bitfield_op)) {
158 return false;
159 }
160 }
161
162 // Merge the instruction into its uses.
163 for (HUseIterator<HInstruction*> it_use(uses); !it_use.Done(); it_use.Advance()) {
164 HInstruction* use = it_use.Current()->GetUser();
165 bool merged = MergeIntoShifterOperand(use, bitfield_op);
166 DCHECK(merged);
167 }
168
169 return true;
170}
171
Alexandre Rames418318f2015-11-20 15:55:47 +0000172bool InstructionSimplifierArm64Visitor::TrySimpleMultiplyAccumulatePatterns(
173 HMul* mul, HBinaryOperation* input_binop, HInstruction* input_other) {
174 DCHECK(Primitive::IsIntOrLongType(mul->GetType()));
175 DCHECK(input_binop->IsAdd() || input_binop->IsSub());
176 DCHECK_NE(input_binop, input_other);
177 if (!input_binop->HasOnlyOneNonEnvironmentUse()) {
178 return false;
179 }
180
181 // Try to interpret patterns like
182 // a * (b <+/-> 1)
183 // as
184 // (a * b) <+/-> a
185 HInstruction* input_a = input_other;
186 HInstruction* input_b = nullptr; // Set to a non-null value if we found a pattern to optimize.
187 HInstruction::InstructionKind op_kind;
188
189 if (input_binop->IsAdd()) {
190 if ((input_binop->GetConstantRight() != nullptr) && input_binop->GetConstantRight()->IsOne()) {
191 // Interpret
192 // a * (b + 1)
193 // as
194 // (a * b) + a
195 input_b = input_binop->GetLeastConstantLeft();
196 op_kind = HInstruction::kAdd;
197 }
198 } else {
199 DCHECK(input_binop->IsSub());
200 if (input_binop->GetRight()->IsConstant() &&
201 input_binop->GetRight()->AsConstant()->IsMinusOne()) {
202 // Interpret
203 // a * (b - (-1))
204 // as
205 // a + (a * b)
206 input_b = input_binop->GetLeft();
207 op_kind = HInstruction::kAdd;
208 } else if (input_binop->GetLeft()->IsConstant() &&
209 input_binop->GetLeft()->AsConstant()->IsOne()) {
210 // Interpret
211 // a * (1 - b)
212 // as
213 // a - (a * b)
214 input_b = input_binop->GetRight();
215 op_kind = HInstruction::kSub;
216 }
217 }
218
219 if (input_b == nullptr) {
220 // We did not find a pattern we can optimize.
221 return false;
222 }
223
224 HArm64MultiplyAccumulate* mulacc = new(GetGraph()->GetArena()) HArm64MultiplyAccumulate(
225 mul->GetType(), op_kind, input_a, input_a, input_b, mul->GetDexPc());
226
227 mul->GetBlock()->ReplaceAndRemoveInstructionWith(mul, mulacc);
228 input_binop->GetBlock()->RemoveInstruction(input_binop);
229
230 return false;
231}
232
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100233void InstructionSimplifierArm64Visitor::VisitArrayGet(HArrayGet* instruction) {
234 TryExtractArrayAccessAddress(instruction,
235 instruction->GetArray(),
236 instruction->GetIndex(),
237 Primitive::ComponentSize(instruction->GetType()));
238}
239
240void InstructionSimplifierArm64Visitor::VisitArraySet(HArraySet* instruction) {
241 TryExtractArrayAccessAddress(instruction,
242 instruction->GetArray(),
243 instruction->GetIndex(),
244 Primitive::ComponentSize(instruction->GetComponentType()));
245}
246
Alexandre Rames418318f2015-11-20 15:55:47 +0000247void InstructionSimplifierArm64Visitor::VisitMul(HMul* instruction) {
248 Primitive::Type type = instruction->GetType();
249 if (!Primitive::IsIntOrLongType(type)) {
250 return;
251 }
252
253 HInstruction* use = instruction->HasNonEnvironmentUses()
254 ? instruction->GetUses().GetFirst()->GetUser()
255 : nullptr;
256
257 if (instruction->HasOnlyOneNonEnvironmentUse() && (use->IsAdd() || use->IsSub())) {
258 // Replace code looking like
259 // MUL tmp, x, y
260 // SUB dst, acc, tmp
261 // with
262 // MULSUB dst, acc, x, y
263 // Note that we do not want to (unconditionally) perform the merge when the
264 // multiplication has multiple uses and it can be merged in all of them.
265 // Multiple uses could happen on the same control-flow path, and we would
266 // then increase the amount of work. In the future we could try to evaluate
267 // whether all uses are on different control-flow paths (using dominance and
268 // reverse-dominance information) and only perform the merge when they are.
269 HInstruction* accumulator = nullptr;
270 HBinaryOperation* binop = use->AsBinaryOperation();
271 HInstruction* binop_left = binop->GetLeft();
272 HInstruction* binop_right = binop->GetRight();
273 // Be careful after GVN. This should not happen since the `HMul` has only
274 // one use.
275 DCHECK_NE(binop_left, binop_right);
276 if (binop_right == instruction) {
277 accumulator = binop_left;
278 } else if (use->IsAdd()) {
279 DCHECK_EQ(binop_left, instruction);
280 accumulator = binop_right;
281 }
282
283 if (accumulator != nullptr) {
284 HArm64MultiplyAccumulate* mulacc =
285 new (GetGraph()->GetArena()) HArm64MultiplyAccumulate(type,
286 binop->GetKind(),
287 accumulator,
288 instruction->GetLeft(),
289 instruction->GetRight());
290
291 binop->GetBlock()->ReplaceAndRemoveInstructionWith(binop, mulacc);
292 DCHECK(!instruction->HasUses());
293 instruction->GetBlock()->RemoveInstruction(instruction);
294 RecordSimplification();
295 return;
296 }
297 }
298
299 // Use multiply accumulate instruction for a few simple patterns.
300 // We prefer not applying the following transformations if the left and
301 // right inputs perform the same operation.
302 // We rely on GVN having squashed the inputs if appropriate. However the
303 // results are still correct even if that did not happen.
304 if (instruction->GetLeft() == instruction->GetRight()) {
305 return;
306 }
307
308 HInstruction* left = instruction->GetLeft();
309 HInstruction* right = instruction->GetRight();
310 if ((right->IsAdd() || right->IsSub()) &&
311 TrySimpleMultiplyAccumulatePatterns(instruction, right->AsBinaryOperation(), left)) {
312 return;
313 }
314 if ((left->IsAdd() || left->IsSub()) &&
315 TrySimpleMultiplyAccumulatePatterns(instruction, left->AsBinaryOperation(), right)) {
316 return;
317 }
318}
319
Alexandre Rames8626b742015-11-25 16:28:08 +0000320void InstructionSimplifierArm64Visitor::VisitShl(HShl* instruction) {
321 if (instruction->InputAt(1)->IsConstant()) {
322 TryMergeIntoUsersShifterOperand(instruction);
323 }
324}
325
326void InstructionSimplifierArm64Visitor::VisitShr(HShr* instruction) {
327 if (instruction->InputAt(1)->IsConstant()) {
328 TryMergeIntoUsersShifterOperand(instruction);
329 }
330}
331
332void InstructionSimplifierArm64Visitor::VisitTypeConversion(HTypeConversion* instruction) {
333 Primitive::Type result_type = instruction->GetResultType();
334 Primitive::Type input_type = instruction->GetInputType();
335
336 if (input_type == result_type) {
337 // We let the arch-independent code handle this.
338 return;
339 }
340
341 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
342 TryMergeIntoUsersShifterOperand(instruction);
343 }
344}
345
346void InstructionSimplifierArm64Visitor::VisitUShr(HUShr* instruction) {
347 if (instruction->InputAt(1)->IsConstant()) {
348 TryMergeIntoUsersShifterOperand(instruction);
349 }
350}
351
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100352} // namespace arm64
353} // namespace art