blob: 9422f9f30c72b4ae95b5af85f917cf297be758b3 [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() {
40 if (stats_ != nullptr) {
41 stats_->RecordStat(kInstructionSimplificationsArch);
42 }
43 }
44
45 bool TryMergeIntoUsersShifterOperand(HInstruction* instruction);
46 bool TryMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op, bool do_merge);
47 bool CanMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
48 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge */ false);
49 }
50 bool MergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
51 DCHECK(CanMergeIntoShifterOperand(use, bitfield_op));
52 return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge */ true);
53 }
54
55 /**
56 * This simplifier uses a special-purpose BB visitor.
57 * (1) No need to visit Phi nodes.
58 * (2) Since statements can be removed in a "forward" fashion,
59 * the visitor should test if each statement is still there.
60 */
61 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
62 // TODO: fragile iteration, provide more robust iterators?
63 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
64 HInstruction* instruction = it.Current();
65 if (instruction->IsInBlock()) {
66 instruction->Accept(this);
67 }
68 }
69 }
70
71 void VisitAnd(HAnd* instruction) OVERRIDE;
72 void VisitArrayGet(HArrayGet* instruction) OVERRIDE;
73 void VisitArraySet(HArraySet* instruction) OVERRIDE;
74 void VisitMul(HMul* instruction) OVERRIDE;
75 void VisitOr(HOr* instruction) OVERRIDE;
76 void VisitShl(HShl* instruction) OVERRIDE;
77 void VisitShr(HShr* instruction) OVERRIDE;
78 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
79 void VisitUShr(HUShr* instruction) OVERRIDE;
80
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) {
87 DCHECK(HasShifterOperand(use, kArm));
88 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();
169 if (!HasShifterOperand(user, kArm)) {
170 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
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100207 if (type == DataType::Type::kInt64
208 || type == DataType::Type::kFloat32
209 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100210 // T32 doesn't support ShiftedRegOffset mem address mode for these types
211 // to enable optimization.
212 return;
213 }
214
215 if (TryExtractArrayAccessAddress(instruction,
216 instruction->GetArray(),
217 instruction->GetIndex(),
218 data_offset)) {
219 RecordSimplification();
220 }
221}
222
223void InstructionSimplifierArmVisitor::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100224 size_t access_size = DataType::Size(instruction->GetComponentType());
Artem Serov328429f2016-07-06 16:23:04 +0100225 size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100226 DataType::Type type = instruction->GetComponentType();
Artem Serov328429f2016-07-06 16:23:04 +0100227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100228 if (type == DataType::Type::kInt64
229 || type == DataType::Type::kFloat32
230 || type == DataType::Type::kFloat64) {
Artem Serov328429f2016-07-06 16:23:04 +0100231 // T32 doesn't support ShiftedRegOffset mem address mode for these types
232 // to enable optimization.
233 return;
234 }
235
236 if (TryExtractArrayAccessAddress(instruction,
237 instruction->GetArray(),
238 instruction->GetIndex(),
239 data_offset)) {
240 RecordSimplification();
241 }
242}
Artem Serov7fc63502016-02-09 17:15:29 +0000243
Anton Kirilov74234da2017-01-13 14:42:47 +0000244void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) {
245 if (TryCombineMultiplyAccumulate(instruction, kArm)) {
246 RecordSimplification();
247 }
248}
249
250void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) {
251 if (TryMergeNegatedInput(instruction)) {
252 RecordSimplification();
253 }
254}
255
256void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) {
257 if (instruction->InputAt(1)->IsConstant()) {
258 TryMergeIntoUsersShifterOperand(instruction);
259 }
260}
261
262void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) {
263 if (instruction->InputAt(1)->IsConstant()) {
264 TryMergeIntoUsersShifterOperand(instruction);
265 }
266}
267
268void InstructionSimplifierArmVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100269 DataType::Type result_type = instruction->GetResultType();
270 DataType::Type input_type = instruction->GetInputType();
Anton Kirilov74234da2017-01-13 14:42:47 +0000271
272 if (input_type == result_type) {
273 // We let the arch-independent code handle this.
274 return;
275 }
276
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100277 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Anton Kirilov74234da2017-01-13 14:42:47 +0000278 TryMergeIntoUsersShifterOperand(instruction);
279 }
280}
281
282void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) {
283 if (instruction->InputAt(1)->IsConstant()) {
284 TryMergeIntoUsersShifterOperand(instruction);
285 }
286}
287
Vladimir Marko0f689e72017-10-02 12:38:21 +0100288void InstructionSimplifierArm::Run() {
289 InstructionSimplifierArmVisitor visitor(graph_, stats_);
290 visitor.VisitReversePostOrder();
291}
292
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300293} // namespace arm
294} // namespace art