blob: c2bbdccc2952cb5521806a72ac218223df960fd3 [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"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030020#include "instruction_simplifier_shared.h"
Alexandre Ramese6dbf482015-10-19 10:10:41 +010021#include "mirror/array-inl.h"
22
Alexandre Rames44b9cf92015-08-19 15:39:06 +010023namespace art {
24namespace arm64 {
25
Alexandre Rames8626b742015-11-25 16:28:08 +000026using helpers::CanFitInShifterOperand;
27using helpers::HasShifterOperand;
28using helpers::ShifterOperandSupportsExtension;
29
Alexandre Ramese6dbf482015-10-19 10:10:41 +010030void InstructionSimplifierArm64Visitor::TryExtractArrayAccessAddress(HInstruction* access,
31 HInstruction* array,
32 HInstruction* index,
33 int access_size) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +000034 if (kEmitCompilerReadBarrier) {
35 // The read barrier instrumentation does not support the
36 // HArm64IntermediateAddress instruction yet.
37 //
38 // TODO: Handle this case properly in the ARM64 code generator and
39 // re-enable this optimization; otherwise, remove this TODO.
40 // b/26601270
41 return;
42 }
Alexandre Ramese6dbf482015-10-19 10:10:41 +010043 if (index->IsConstant() ||
44 (index->IsBoundsCheck() && index->AsBoundsCheck()->GetIndex()->IsConstant())) {
45 // When the index is a constant all the addressing can be fitted in the
46 // memory access instruction, so do not split the access.
47 return;
48 }
49 if (access->IsArraySet() &&
50 access->AsArraySet()->GetValue()->GetType() == Primitive::kPrimNot) {
51 // The access may require a runtime call or the original array pointer.
52 return;
53 }
54
55 // Proceed to extract the base address computation.
56 ArenaAllocator* arena = GetGraph()->GetArena();
57
58 HIntConstant* offset =
59 GetGraph()->GetIntConstant(mirror::Array::DataOffset(access_size).Uint32Value());
60 HArm64IntermediateAddress* address =
61 new (arena) HArm64IntermediateAddress(array, offset, kNoDexPc);
David Brazdil295abc12015-12-31 11:06:00 +000062 address->SetReferenceTypeInfo(array->GetReferenceTypeInfo());
Alexandre Ramese6dbf482015-10-19 10:10:41 +010063 access->GetBlock()->InsertInstructionBefore(address, access);
64 access->ReplaceInput(address, 0);
65 // Both instructions must depend on GC to prevent any instruction that can
66 // trigger GC to be inserted between the two.
67 access->AddSideEffects(SideEffects::DependsOnGC());
68 DCHECK(address->GetSideEffects().Includes(SideEffects::DependsOnGC()));
69 DCHECK(access->GetSideEffects().Includes(SideEffects::DependsOnGC()));
70 // TODO: Code generation for HArrayGet and HArraySet will check whether the input address
71 // is an HArm64IntermediateAddress and generate appropriate code.
72 // We would like to replace the `HArrayGet` and `HArraySet` with custom instructions (maybe
73 // `HArm64Load` and `HArm64Store`). We defer these changes because these new instructions would
74 // not bring any advantages yet.
75 // Also see the comments in
76 // `InstructionCodeGeneratorARM64::VisitArrayGet()` and
77 // `InstructionCodeGeneratorARM64::VisitArraySet()`.
78 RecordSimplification();
79}
80
Alexandre Rames8626b742015-11-25 16:28:08 +000081bool InstructionSimplifierArm64Visitor::TryMergeIntoShifterOperand(HInstruction* use,
82 HInstruction* bitfield_op,
83 bool do_merge) {
84 DCHECK(HasShifterOperand(use));
85 DCHECK(use->IsBinaryOperation() || use->IsNeg());
86 DCHECK(CanFitInShifterOperand(bitfield_op));
87 DCHECK(!bitfield_op->HasEnvironmentUses());
88
89 Primitive::Type type = use->GetType();
90 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
91 return false;
92 }
93
94 HInstruction* left;
95 HInstruction* right;
96 if (use->IsBinaryOperation()) {
97 left = use->InputAt(0);
98 right = use->InputAt(1);
99 } else {
100 DCHECK(use->IsNeg());
101 right = use->AsNeg()->InputAt(0);
102 left = GetGraph()->GetConstant(right->GetType(), 0);
103 }
104 DCHECK(left == bitfield_op || right == bitfield_op);
105
106 if (left == right) {
107 // TODO: Handle special transformations in this situation?
108 // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
109 // Or should this be part of a separate transformation logic?
110 return false;
111 }
112
113 bool is_commutative = use->IsBinaryOperation() && use->AsBinaryOperation()->IsCommutative();
114 HInstruction* other_input;
115 if (bitfield_op == right) {
116 other_input = left;
117 } else {
118 if (is_commutative) {
119 other_input = right;
120 } else {
121 return false;
122 }
123 }
124
125 HArm64DataProcWithShifterOp::OpKind op_kind;
126 int shift_amount = 0;
127 HArm64DataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
128
129 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind) &&
130 !ShifterOperandSupportsExtension(use)) {
131 return false;
132 }
133
134 if (do_merge) {
135 HArm64DataProcWithShifterOp* alu_with_op =
136 new (GetGraph()->GetArena()) HArm64DataProcWithShifterOp(use,
137 other_input,
138 bitfield_op->InputAt(0),
139 op_kind,
140 shift_amount,
141 use->GetDexPc());
142 use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
143 if (bitfield_op->GetUses().IsEmpty()) {
144 bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
145 }
146 RecordSimplification();
147 }
148
149 return true;
150}
151
152// Merge a bitfield move instruction into its uses if it can be merged in all of them.
153bool InstructionSimplifierArm64Visitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
154 DCHECK(CanFitInShifterOperand(bitfield_op));
155
156 if (bitfield_op->HasEnvironmentUses()) {
157 return false;
158 }
159
160 const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
161
162 // Check whether we can merge the instruction in all its users' shifter operand.
163 for (HUseIterator<HInstruction*> it_use(uses); !it_use.Done(); it_use.Advance()) {
164 HInstruction* use = it_use.Current()->GetUser();
165 if (!HasShifterOperand(use)) {
166 return false;
167 }
168 if (!CanMergeIntoShifterOperand(use, bitfield_op)) {
169 return false;
170 }
171 }
172
173 // Merge the instruction into its uses.
174 for (HUseIterator<HInstruction*> it_use(uses); !it_use.Done(); it_use.Advance()) {
175 HInstruction* use = it_use.Current()->GetUser();
176 bool merged = MergeIntoShifterOperand(use, bitfield_op);
177 DCHECK(merged);
178 }
179
180 return true;
181}
182
Kevin Brodsky9ff0d202016-01-11 13:43:31 +0000183bool InstructionSimplifierArm64Visitor::TryMergeNegatedInput(HBinaryOperation* op) {
184 DCHECK(op->IsAnd() || op->IsOr() || op->IsXor()) << op->DebugName();
185 HInstruction* left = op->GetLeft();
186 HInstruction* right = op->GetRight();
187
188 // Only consider the case where there is exactly one Not, with 2 Not's De
189 // Morgan's laws should be applied instead.
190 if (left->IsNot() ^ right->IsNot()) {
191 HInstruction* hnot = (left->IsNot() ? left : right);
192 HInstruction* hother = (left->IsNot() ? right : left);
193
194 // Only do the simplification if the Not has only one use and can thus be
195 // safely removed. Even though ARM64 negated bitwise operations do not have
196 // an immediate variant (only register), we still do the simplification when
197 // `hother` is a constant, because it removes an instruction if the constant
198 // cannot be encoded as an immediate:
199 // mov r0, #large_constant
200 // neg r2, r1
201 // and r0, r0, r2
202 // becomes:
203 // mov r0, #large_constant
204 // bic r0, r0, r1
205 if (hnot->HasOnlyOneNonEnvironmentUse()) {
206 // Replace code looking like
207 // NOT tmp, mask
208 // AND dst, src, tmp (respectively ORR, EOR)
209 // with
210 // BIC dst, src, mask (respectively ORN, EON)
211 HInstruction* src = hnot->AsNot()->GetInput();
212
213 HArm64BitwiseNegatedRight* neg_op = new (GetGraph()->GetArena())
214 HArm64BitwiseNegatedRight(op->GetType(), op->GetKind(), hother, src, op->GetDexPc());
215
216 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, neg_op);
217 hnot->GetBlock()->RemoveInstruction(hnot);
218 RecordSimplification();
219 return true;
220 }
221 }
222
223 return false;
224}
225
226void InstructionSimplifierArm64Visitor::VisitAnd(HAnd* instruction) {
227 TryMergeNegatedInput(instruction);
228}
229
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100230void InstructionSimplifierArm64Visitor::VisitArrayGet(HArrayGet* instruction) {
231 TryExtractArrayAccessAddress(instruction,
232 instruction->GetArray(),
233 instruction->GetIndex(),
234 Primitive::ComponentSize(instruction->GetType()));
235}
236
237void InstructionSimplifierArm64Visitor::VisitArraySet(HArraySet* instruction) {
238 TryExtractArrayAccessAddress(instruction,
239 instruction->GetArray(),
240 instruction->GetIndex(),
241 Primitive::ComponentSize(instruction->GetComponentType()));
242}
243
Alexandre Rames418318f2015-11-20 15:55:47 +0000244void InstructionSimplifierArm64Visitor::VisitMul(HMul* instruction) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300245 if (TryCombineMultiplyAccumulate(instruction, kArm64)) {
246 RecordSimplification();
Alexandre Rames418318f2015-11-20 15:55:47 +0000247 }
248}
249
Kevin Brodsky9ff0d202016-01-11 13:43:31 +0000250void InstructionSimplifierArm64Visitor::VisitOr(HOr* instruction) {
251 TryMergeNegatedInput(instruction);
252}
253
Alexandre Rames8626b742015-11-25 16:28:08 +0000254void InstructionSimplifierArm64Visitor::VisitShl(HShl* instruction) {
255 if (instruction->InputAt(1)->IsConstant()) {
256 TryMergeIntoUsersShifterOperand(instruction);
257 }
258}
259
260void InstructionSimplifierArm64Visitor::VisitShr(HShr* instruction) {
261 if (instruction->InputAt(1)->IsConstant()) {
262 TryMergeIntoUsersShifterOperand(instruction);
263 }
264}
265
266void InstructionSimplifierArm64Visitor::VisitTypeConversion(HTypeConversion* instruction) {
267 Primitive::Type result_type = instruction->GetResultType();
268 Primitive::Type input_type = instruction->GetInputType();
269
270 if (input_type == result_type) {
271 // We let the arch-independent code handle this.
272 return;
273 }
274
275 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
276 TryMergeIntoUsersShifterOperand(instruction);
277 }
278}
279
280void InstructionSimplifierArm64Visitor::VisitUShr(HUShr* instruction) {
281 if (instruction->InputAt(1)->IsConstant()) {
282 TryMergeIntoUsersShifterOperand(instruction);
283 }
284}
285
Kevin Brodsky9ff0d202016-01-11 13:43:31 +0000286void InstructionSimplifierArm64Visitor::VisitXor(HXor* instruction) {
287 TryMergeNegatedInput(instruction);
288}
289
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100290} // namespace arm64
291} // namespace art