blob: 37fcdb9d5c70ab1f97ec32f7509f8d0f78e93738 [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
26namespace art {
Anton Kirilov74234da2017-01-13 14:42:47 +000027
28using helpers::CanFitInShifterOperand;
29using helpers::HasShifterOperand;
30
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030031namespace arm {
32
Vladimir Marko0f689e72017-10-02 12:38:21 +010033class InstructionSimplifierArmVisitor : public HGraphVisitor {
34 public:
35 InstructionSimplifierArmVisitor(HGraph* graph, OptimizingCompilerStats* stats)
36 : HGraphVisitor(graph), stats_(stats) {}
37
38 private:
39 void RecordSimplification() {
Vladimir Markocd09e1f2017-11-24 15:02:40 +000040 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
Vladimir Marko0f689e72017-10-02 12:38:21 +010041 }
42
43 bool TryMergeIntoUsersShifterOperand(HInstruction* instruction);
44 bool TryMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op, bool do_merge);
45 bool CanMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
46 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge */ false);
47 }
48 bool MergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
49 DCHECK(CanMergeIntoShifterOperand(use, bitfield_op));
50 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge */ true);
51 }
52
53 /**
54 * This simplifier uses a special-purpose BB visitor.
55 * (1) No need to visit Phi nodes.
56 * (2) Since statements can be removed in a "forward" fashion,
57 * the visitor should test if each statement is still there.
58 */
59 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
60 // TODO: fragile iteration, provide more robust iterators?
61 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
62 HInstruction* instruction = it.Current();
63 if (instruction->IsInBlock()) {
64 instruction->Accept(this);
65 }
66 }
67 }
68
69 void VisitAnd(HAnd* instruction) OVERRIDE;
70 void VisitArrayGet(HArrayGet* instruction) OVERRIDE;
71 void VisitArraySet(HArraySet* instruction) OVERRIDE;
72 void VisitMul(HMul* instruction) OVERRIDE;
73 void VisitOr(HOr* instruction) OVERRIDE;
74 void VisitShl(HShl* instruction) OVERRIDE;
75 void VisitShr(HShr* instruction) OVERRIDE;
76 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
77 void VisitUShr(HUShr* instruction) OVERRIDE;
78
79 OptimizingCompilerStats* stats_;
80};
81
Anton Kirilov74234da2017-01-13 14:42:47 +000082bool InstructionSimplifierArmVisitor::TryMergeIntoShifterOperand(HInstruction* use,
83 HInstruction* bitfield_op,
84 bool do_merge) {
Vladimir Marko33bff252017-11-01 14:35:42 +000085 DCHECK(HasShifterOperand(use, InstructionSet::kArm));
Anton Kirilov74234da2017-01-13 14:42:47 +000086 DCHECK(use->IsBinaryOperation());
87 DCHECK(CanFitInShifterOperand(bitfield_op));
88 DCHECK(!bitfield_op->HasEnvironmentUses());
89
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 DataType::Type type = use->GetType();
91 if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) {
Anton Kirilov74234da2017-01-13 14:42:47 +000092 return false;
93 }
94
95 HInstruction* left = use->InputAt(0);
96 HInstruction* right = use->InputAt(1);
97 DCHECK(left == bitfield_op || right == bitfield_op);
98
99 if (left == right) {
100 // TODO: Handle special transformations in this situation?
101 // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
102 // Or should this be part of a separate transformation logic?
103 return false;
104 }
105
106 bool is_commutative = use->AsBinaryOperation()->IsCommutative();
107 HInstruction* other_input;
108 if (bitfield_op == right) {
109 other_input = left;
110 } else {
111 if (is_commutative) {
112 other_input = right;
113 } else {
114 return false;
115 }
116 }
117
118 HDataProcWithShifterOp::OpKind op_kind;
119 int shift_amount = 0;
120
121 HDataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 shift_amount &= use->GetType() == DataType::Type::kInt32
Anton Kirilov74234da2017-01-13 14:42:47 +0000123 ? kMaxIntShiftDistance
124 : kMaxLongShiftDistance;
125
126 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100127 if (!use->IsAdd() && (!use->IsSub() || use->GetType() != DataType::Type::kInt64)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000128 return false;
129 }
130 // Shift by 1 is a special case that results in the same number and type of instructions
131 // as this simplification, but potentially shorter code.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 } else if (type == DataType::Type::kInt64 && shift_amount == 1) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000133 return false;
134 }
135
136 if (do_merge) {
137 HDataProcWithShifterOp* alu_with_op =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100138 new (GetGraph()->GetAllocator()) HDataProcWithShifterOp(use,
139 other_input,
140 bitfield_op->InputAt(0),
141 op_kind,
142 shift_amount,
143 use->GetDexPc());
Anton Kirilov74234da2017-01-13 14:42:47 +0000144 use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
145 if (bitfield_op->GetUses().empty()) {
146 bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
147 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300148 RecordSimplification();
149 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000150
151 return true;
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300152}
153
Anton Kirilov74234da2017-01-13 14:42:47 +0000154// Merge a bitfield move instruction into its uses if it can be merged in all of them.
155bool InstructionSimplifierArmVisitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
156 DCHECK(CanFitInShifterOperand(bitfield_op));
157
158 if (bitfield_op->HasEnvironmentUses()) {
159 return false;
Artem Serov7fc63502016-02-09 17:15:29 +0000160 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000161
162 const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
163
164 // Check whether we can merge the instruction in all its users' shifter operand.
165 for (const HUseListNode<HInstruction*>& use : uses) {
166 HInstruction* user = use.GetUser();
Vladimir Marko33bff252017-11-01 14:35:42 +0000167 if (!HasShifterOperand(user, InstructionSet::kArm)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000168 return false;
169 }
170 if (!CanMergeIntoShifterOperand(user, bitfield_op)) {
171 return false;
172 }
173 }
174
175 // Merge the instruction into its uses.
176 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
177 HInstruction* user = it->GetUser();
178 // Increment `it` now because `*it` will disappear thanks to MergeIntoShifterOperand().
179 ++it;
180 bool merged = MergeIntoShifterOperand(user, bitfield_op);
181 DCHECK(merged);
182 }
183
184 return true;
Artem Serov7fc63502016-02-09 17:15:29 +0000185}
186
187void InstructionSimplifierArmVisitor::VisitAnd(HAnd* instruction) {
188 if (TryMergeNegatedInput(instruction)) {
189 RecordSimplification();
190 }
191}
192
Artem Serov328429f2016-07-06 16:23:04 +0100193void InstructionSimplifierArmVisitor::VisitArrayGet(HArrayGet* instruction) {
194 size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100195 DataType::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +0100196
jessicahandojo05765752016-09-09 19:01:32 -0700197 // TODO: Implement reading (length + compression) for String compression feature from
Roland Levillainc043d002017-07-14 16:39:16 +0100198 // negative offset (count_offset - data_offset). Thumb2Assembler (now removed) did
199 // not support T4 encoding of "LDR (immediate)", but ArmVIXLMacroAssembler might.
jessicahandojo05765752016-09-09 19:01:32 -0700200 // Don't move array pointer if it is charAt because we need to take the count first.
201 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
202 return;
203 }
204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100205 if (type == DataType::Type::kInt64
206 || type == DataType::Type::kFloat32
207 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100208 // T32 doesn't support ShiftedRegOffset mem address mode for these types
209 // to enable optimization.
210 return;
211 }
212
213 if (TryExtractArrayAccessAddress(instruction,
214 instruction->GetArray(),
215 instruction->GetIndex(),
216 data_offset)) {
217 RecordSimplification();
218 }
219}
220
221void InstructionSimplifierArmVisitor::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 size_t access_size = DataType::Size(instruction->GetComponentType());
Artem Serov328429f2016-07-06 16:23:04 +0100223 size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100224 DataType::Type type = instruction->GetComponentType();
Artem Serov328429f2016-07-06 16:23:04 +0100225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100226 if (type == DataType::Type::kInt64
227 || type == DataType::Type::kFloat32
228 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100229 // T32 doesn't support ShiftedRegOffset mem address mode for these types
230 // to enable optimization.
231 return;
232 }
233
234 if (TryExtractArrayAccessAddress(instruction,
235 instruction->GetArray(),
236 instruction->GetIndex(),
237 data_offset)) {
238 RecordSimplification();
239 }
240}
Artem Serov7fc63502016-02-09 17:15:29 +0000241
Anton Kirilov74234da2017-01-13 14:42:47 +0000242void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000243 if (TryCombineMultiplyAccumulate(instruction, InstructionSet::kArm)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000244 RecordSimplification();
245 }
246}
247
248void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) {
249 if (TryMergeNegatedInput(instruction)) {
250 RecordSimplification();
251 }
252}
253
254void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) {
255 if (instruction->InputAt(1)->IsConstant()) {
256 TryMergeIntoUsersShifterOperand(instruction);
257 }
258}
259
260void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) {
261 if (instruction->InputAt(1)->IsConstant()) {
262 TryMergeIntoUsersShifterOperand(instruction);
263 }
264}
265
266void InstructionSimplifierArmVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100267 DataType::Type result_type = instruction->GetResultType();
268 DataType::Type input_type = instruction->GetInputType();
Anton Kirilov74234da2017-01-13 14:42:47 +0000269
270 if (input_type == result_type) {
271 // We let the arch-independent code handle this.
272 return;
273 }
274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100275 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000276 TryMergeIntoUsersShifterOperand(instruction);
277 }
278}
279
280void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) {
281 if (instruction->InputAt(1)->IsConstant()) {
282 TryMergeIntoUsersShifterOperand(instruction);
283 }
284}
285
Aart Bik24773202018-04-26 10:28:51 -0700286bool InstructionSimplifierArm::Run() {
Vladimir Marko0f689e72017-10-02 12:38:21 +0100287 InstructionSimplifierArmVisitor visitor(graph_, stats_);
288 visitor.VisitReversePostOrder();
Aart Bik24773202018-04-26 10:28:51 -0700289 return true;
Vladimir Marko0f689e72017-10-02 12:38:21 +0100290}
291
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300292} // namespace arm
293} // namespace art