blob: 1371ea7781e925e28ae7d5a7910c84c4c58256b2 [file] [log] [blame]
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001/*
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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "instruction_simplifier_arm.h"
18
Artem Serov328429f2016-07-06 16:23:04 +010019#include "code_generator.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000020#include "common_arm.h"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030021#include "instruction_simplifier_shared.h"
Artem Serov328429f2016-07-06 16:23:04 +010022#include "mirror/array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "mirror/string.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000024#include "nodes.h"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030025
Vladimir Marko0a516052019-10-14 13:00:44 +000026namespace art {
Anton Kirilov74234da2017-01-13 14:42:47 +000027
28using helpers::CanFitInShifterOperand;
29using helpers::HasShifterOperand;
Evgeny Astigeevich3d190c02020-06-17 15:37:02 +010030using helpers::IsSubRightSubLeftShl;
Anton Kirilov74234da2017-01-13 14:42:47 +000031
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030032namespace arm {
33
Vladimir Marko0f689e72017-10-02 12:38:21 +010034class InstructionSimplifierArmVisitor : public HGraphVisitor {
35 public:
36 InstructionSimplifierArmVisitor(HGraph* graph, OptimizingCompilerStats* stats)
37 : HGraphVisitor(graph), stats_(stats) {}
38
39 private:
40 void RecordSimplification() {
Vladimir Markocd09e1f2017-11-24 15:02:40 +000041 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
Vladimir Marko0f689e72017-10-02 12:38:21 +010042 }
43
44 bool TryMergeIntoUsersShifterOperand(HInstruction* instruction);
45 bool TryMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op, bool do_merge);
46 bool CanMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
Andreas Gampe3db70682018-12-26 15:12:03 -080047 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ false);
Vladimir Marko0f689e72017-10-02 12:38:21 +010048 }
49 bool MergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
50 DCHECK(CanMergeIntoShifterOperand(use, bitfield_op));
Andreas Gampe3db70682018-12-26 15:12:03 -080051 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ true);
Vladimir Marko0f689e72017-10-02 12:38:21 +010052 }
53
54 /**
55 * This simplifier uses a special-purpose BB visitor.
56 * (1) No need to visit Phi nodes.
57 * (2) Since statements can be removed in a "forward" fashion,
58 * the visitor should test if each statement is still there.
59 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010060 void VisitBasicBlock(HBasicBlock* block) override {
Vladimir Marko0f689e72017-10-02 12:38:21 +010061 // TODO: fragile iteration, provide more robust iterators?
62 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
63 HInstruction* instruction = it.Current();
64 if (instruction->IsInBlock()) {
65 instruction->Accept(this);
66 }
67 }
68 }
69
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010070 void VisitAnd(HAnd* instruction) override;
71 void VisitArrayGet(HArrayGet* instruction) override;
72 void VisitArraySet(HArraySet* instruction) override;
73 void VisitMul(HMul* instruction) override;
74 void VisitOr(HOr* instruction) override;
75 void VisitShl(HShl* instruction) override;
76 void VisitShr(HShr* instruction) override;
Evgeny Astigeevich3d190c02020-06-17 15:37:02 +010077 void VisitSub(HSub* instruction) override;
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010078 void VisitTypeConversion(HTypeConversion* instruction) override;
79 void VisitUShr(HUShr* instruction) override;
Vladimir Marko0f689e72017-10-02 12:38:21 +010080
81 OptimizingCompilerStats* stats_;
82};
83
Anton Kirilov74234da2017-01-13 14:42:47 +000084bool InstructionSimplifierArmVisitor::TryMergeIntoShifterOperand(HInstruction* use,
85 HInstruction* bitfield_op,
86 bool do_merge) {
Vladimir Marko33bff252017-11-01 14:35:42 +000087 DCHECK(HasShifterOperand(use, InstructionSet::kArm));
Anton Kirilov74234da2017-01-13 14:42:47 +000088 DCHECK(use->IsBinaryOperation());
89 DCHECK(CanFitInShifterOperand(bitfield_op));
90 DCHECK(!bitfield_op->HasEnvironmentUses());
91
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 DataType::Type type = use->GetType();
93 if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) {
Anton Kirilov74234da2017-01-13 14:42:47 +000094 return false;
95 }
96
97 HInstruction* left = use->InputAt(0);
98 HInstruction* right = use->InputAt(1);
99 DCHECK(left == bitfield_op || right == bitfield_op);
100
101 if (left == right) {
102 // TODO: Handle special transformations in this situation?
103 // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
104 // Or should this be part of a separate transformation logic?
105 return false;
106 }
107
108 bool is_commutative = use->AsBinaryOperation()->IsCommutative();
109 HInstruction* other_input;
110 if (bitfield_op == right) {
111 other_input = left;
112 } else {
113 if (is_commutative) {
114 other_input = right;
115 } else {
116 return false;
117 }
118 }
119
120 HDataProcWithShifterOp::OpKind op_kind;
121 int shift_amount = 0;
122
123 HDataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100124 shift_amount &= use->GetType() == DataType::Type::kInt32
Anton Kirilov74234da2017-01-13 14:42:47 +0000125 ? kMaxIntShiftDistance
126 : kMaxLongShiftDistance;
127
128 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 if (!use->IsAdd() && (!use->IsSub() || use->GetType() != DataType::Type::kInt64)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000130 return false;
131 }
132 // Shift by 1 is a special case that results in the same number and type of instructions
133 // as this simplification, but potentially shorter code.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 } else if (type == DataType::Type::kInt64 && shift_amount == 1) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000135 return false;
136 }
137
138 if (do_merge) {
139 HDataProcWithShifterOp* alu_with_op =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100140 new (GetGraph()->GetAllocator()) HDataProcWithShifterOp(use,
141 other_input,
142 bitfield_op->InputAt(0),
143 op_kind,
144 shift_amount,
145 use->GetDexPc());
Anton Kirilov74234da2017-01-13 14:42:47 +0000146 use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
147 if (bitfield_op->GetUses().empty()) {
148 bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
149 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300150 RecordSimplification();
151 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000152
153 return true;
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300154}
155
Anton Kirilov74234da2017-01-13 14:42:47 +0000156// Merge a bitfield move instruction into its uses if it can be merged in all of them.
157bool InstructionSimplifierArmVisitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
158 DCHECK(CanFitInShifterOperand(bitfield_op));
159
160 if (bitfield_op->HasEnvironmentUses()) {
161 return false;
Artem Serov7fc63502016-02-09 17:15:29 +0000162 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000163
164 const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
165
166 // Check whether we can merge the instruction in all its users' shifter operand.
167 for (const HUseListNode<HInstruction*>& use : uses) {
168 HInstruction* user = use.GetUser();
Vladimir Marko33bff252017-11-01 14:35:42 +0000169 if (!HasShifterOperand(user, InstructionSet::kArm)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000170 return false;
171 }
172 if (!CanMergeIntoShifterOperand(user, bitfield_op)) {
173 return false;
174 }
175 }
176
177 // Merge the instruction into its uses.
178 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
179 HInstruction* user = it->GetUser();
180 // Increment `it` now because `*it` will disappear thanks to MergeIntoShifterOperand().
181 ++it;
182 bool merged = MergeIntoShifterOperand(user, bitfield_op);
183 DCHECK(merged);
184 }
185
186 return true;
Artem Serov7fc63502016-02-09 17:15:29 +0000187}
188
189void InstructionSimplifierArmVisitor::VisitAnd(HAnd* instruction) {
190 if (TryMergeNegatedInput(instruction)) {
191 RecordSimplification();
192 }
193}
194
Artem Serov328429f2016-07-06 16:23:04 +0100195void InstructionSimplifierArmVisitor::VisitArrayGet(HArrayGet* instruction) {
196 size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100197 DataType::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +0100198
jessicahandojo05765752016-09-09 19:01:32 -0700199 // TODO: Implement reading (length + compression) for String compression feature from
Roland Levillainc043d002017-07-14 16:39:16 +0100200 // negative offset (count_offset - data_offset). Thumb2Assembler (now removed) did
201 // not support T4 encoding of "LDR (immediate)", but ArmVIXLMacroAssembler might.
jessicahandojo05765752016-09-09 19:01:32 -0700202 // Don't move array pointer if it is charAt because we need to take the count first.
203 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
204 return;
205 }
206
Artem Serov0806f582018-10-11 20:14:20 +0100207 // TODO: Support intermediate address for object arrays on arm.
208 if (type == DataType::Type::kReference) {
209 return;
210 }
211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100212 if (type == DataType::Type::kInt64
213 || type == DataType::Type::kFloat32
214 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100215 // T32 doesn't support ShiftedRegOffset mem address mode for these types
216 // to enable optimization.
217 return;
218 }
219
220 if (TryExtractArrayAccessAddress(instruction,
221 instruction->GetArray(),
222 instruction->GetIndex(),
223 data_offset)) {
224 RecordSimplification();
225 }
226}
227
228void InstructionSimplifierArmVisitor::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100229 size_t access_size = DataType::Size(instruction->GetComponentType());
Artem Serov328429f2016-07-06 16:23:04 +0100230 size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100231 DataType::Type type = instruction->GetComponentType();
Artem Serov328429f2016-07-06 16:23:04 +0100232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100233 if (type == DataType::Type::kInt64
234 || type == DataType::Type::kFloat32
235 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100236 // T32 doesn't support ShiftedRegOffset mem address mode for these types
237 // to enable optimization.
238 return;
239 }
240
241 if (TryExtractArrayAccessAddress(instruction,
242 instruction->GetArray(),
243 instruction->GetIndex(),
244 data_offset)) {
245 RecordSimplification();
246 }
247}
Artem Serov7fc63502016-02-09 17:15:29 +0000248
Anton Kirilov74234da2017-01-13 14:42:47 +0000249void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000250 if (TryCombineMultiplyAccumulate(instruction, InstructionSet::kArm)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000251 RecordSimplification();
252 }
253}
254
255void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) {
256 if (TryMergeNegatedInput(instruction)) {
257 RecordSimplification();
258 }
259}
260
261void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) {
262 if (instruction->InputAt(1)->IsConstant()) {
263 TryMergeIntoUsersShifterOperand(instruction);
264 }
265}
266
267void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) {
268 if (instruction->InputAt(1)->IsConstant()) {
269 TryMergeIntoUsersShifterOperand(instruction);
270 }
271}
272
Evgeny Astigeevich3d190c02020-06-17 15:37:02 +0100273void InstructionSimplifierArmVisitor::VisitSub(HSub* instruction) {
274 if (IsSubRightSubLeftShl(instruction)) {
275 HInstruction* shl = instruction->GetRight()->InputAt(0);
276 if (shl->InputAt(1)->IsConstant() && TryReplaceSubSubWithSubAdd(instruction)) {
277 TryMergeIntoUsersShifterOperand(shl);
278 }
279 }
280}
281
Anton Kirilov74234da2017-01-13 14:42:47 +0000282void InstructionSimplifierArmVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100283 DataType::Type result_type = instruction->GetResultType();
284 DataType::Type input_type = instruction->GetInputType();
Anton Kirilov74234da2017-01-13 14:42:47 +0000285
286 if (input_type == result_type) {
287 // We let the arch-independent code handle this.
288 return;
289 }
290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100291 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000292 TryMergeIntoUsersShifterOperand(instruction);
293 }
294}
295
296void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) {
297 if (instruction->InputAt(1)->IsConstant()) {
298 TryMergeIntoUsersShifterOperand(instruction);
299 }
300}
301
Aart Bik24773202018-04-26 10:28:51 -0700302bool InstructionSimplifierArm::Run() {
Vladimir Marko0f689e72017-10-02 12:38:21 +0100303 InstructionSimplifierArmVisitor visitor(graph_, stats_);
304 visitor.VisitReversePostOrder();
Aart Bik24773202018-04-26 10:28:51 -0700305 return true;
Vladimir Marko0f689e72017-10-02 12:38:21 +0100306}
307
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300308} // namespace arm
309} // namespace art