blob: a32d0ce42b06eb3355622a8f92b97991de626004 [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
Anton Kirilov74234da2017-01-13 14:42:47 +000033bool InstructionSimplifierArmVisitor::TryMergeIntoShifterOperand(HInstruction* use,
34 HInstruction* bitfield_op,
35 bool do_merge) {
36 DCHECK(HasShifterOperand(use, kArm));
37 DCHECK(use->IsBinaryOperation());
38 DCHECK(CanFitInShifterOperand(bitfield_op));
39 DCHECK(!bitfield_op->HasEnvironmentUses());
40
41 Primitive::Type type = use->GetType();
42 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
43 return false;
44 }
45
46 HInstruction* left = use->InputAt(0);
47 HInstruction* right = use->InputAt(1);
48 DCHECK(left == bitfield_op || right == bitfield_op);
49
50 if (left == right) {
51 // TODO: Handle special transformations in this situation?
52 // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
53 // Or should this be part of a separate transformation logic?
54 return false;
55 }
56
57 bool is_commutative = use->AsBinaryOperation()->IsCommutative();
58 HInstruction* other_input;
59 if (bitfield_op == right) {
60 other_input = left;
61 } else {
62 if (is_commutative) {
63 other_input = right;
64 } else {
65 return false;
66 }
67 }
68
69 HDataProcWithShifterOp::OpKind op_kind;
70 int shift_amount = 0;
71
72 HDataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
73 shift_amount &= use->GetType() == Primitive::kPrimInt
74 ? kMaxIntShiftDistance
75 : kMaxLongShiftDistance;
76
77 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Anton Kirilov420ee302017-02-21 18:10:26 +000078 if (!use->IsAdd() && (!use->IsSub() || use->GetType() != Primitive::kPrimLong)) {
Anton Kirilov74234da2017-01-13 14:42:47 +000079 return false;
80 }
81 // Shift by 1 is a special case that results in the same number and type of instructions
82 // as this simplification, but potentially shorter code.
83 } else if (type == Primitive::kPrimLong && shift_amount == 1) {
84 return false;
85 }
86
87 if (do_merge) {
88 HDataProcWithShifterOp* alu_with_op =
89 new (GetGraph()->GetArena()) HDataProcWithShifterOp(use,
90 other_input,
91 bitfield_op->InputAt(0),
92 op_kind,
93 shift_amount,
94 use->GetDexPc());
95 use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
96 if (bitfield_op->GetUses().empty()) {
97 bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
98 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030099 RecordSimplification();
100 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000101
102 return true;
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300103}
104
Anton Kirilov74234da2017-01-13 14:42:47 +0000105// Merge a bitfield move instruction into its uses if it can be merged in all of them.
106bool InstructionSimplifierArmVisitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
107 DCHECK(CanFitInShifterOperand(bitfield_op));
108
109 if (bitfield_op->HasEnvironmentUses()) {
110 return false;
Artem Serov7fc63502016-02-09 17:15:29 +0000111 }
Anton Kirilov74234da2017-01-13 14:42:47 +0000112
113 const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
114
115 // Check whether we can merge the instruction in all its users' shifter operand.
116 for (const HUseListNode<HInstruction*>& use : uses) {
117 HInstruction* user = use.GetUser();
118 if (!HasShifterOperand(user, kArm)) {
119 return false;
120 }
121 if (!CanMergeIntoShifterOperand(user, bitfield_op)) {
122 return false;
123 }
124 }
125
126 // Merge the instruction into its uses.
127 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
128 HInstruction* user = it->GetUser();
129 // Increment `it` now because `*it` will disappear thanks to MergeIntoShifterOperand().
130 ++it;
131 bool merged = MergeIntoShifterOperand(user, bitfield_op);
132 DCHECK(merged);
133 }
134
135 return true;
Artem Serov7fc63502016-02-09 17:15:29 +0000136}
137
138void InstructionSimplifierArmVisitor::VisitAnd(HAnd* instruction) {
139 if (TryMergeNegatedInput(instruction)) {
140 RecordSimplification();
141 }
142}
143
Artem Serov328429f2016-07-06 16:23:04 +0100144void InstructionSimplifierArmVisitor::VisitArrayGet(HArrayGet* instruction) {
145 size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
146 Primitive::Type type = instruction->GetType();
147
jessicahandojo05765752016-09-09 19:01:32 -0700148 // TODO: Implement reading (length + compression) for String compression feature from
Roland Levillainc043d002017-07-14 16:39:16 +0100149 // negative offset (count_offset - data_offset). Thumb2Assembler (now removed) did
150 // not support T4 encoding of "LDR (immediate)", but ArmVIXLMacroAssembler might.
jessicahandojo05765752016-09-09 19:01:32 -0700151 // Don't move array pointer if it is charAt because we need to take the count first.
152 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
153 return;
154 }
155
Artem Serov328429f2016-07-06 16:23:04 +0100156 if (type == Primitive::kPrimLong
157 || type == Primitive::kPrimFloat
158 || type == Primitive::kPrimDouble) {
159 // T32 doesn't support ShiftedRegOffset mem address mode for these types
160 // to enable optimization.
161 return;
162 }
163
164 if (TryExtractArrayAccessAddress(instruction,
165 instruction->GetArray(),
166 instruction->GetIndex(),
167 data_offset)) {
168 RecordSimplification();
169 }
170}
171
172void InstructionSimplifierArmVisitor::VisitArraySet(HArraySet* instruction) {
173 size_t access_size = Primitive::ComponentSize(instruction->GetComponentType());
174 size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value();
175 Primitive::Type type = instruction->GetComponentType();
176
177 if (type == Primitive::kPrimLong
178 || type == Primitive::kPrimFloat
179 || type == Primitive::kPrimDouble) {
180 // T32 doesn't support ShiftedRegOffset mem address mode for these types
181 // to enable optimization.
182 return;
183 }
184
185 if (TryExtractArrayAccessAddress(instruction,
186 instruction->GetArray(),
187 instruction->GetIndex(),
188 data_offset)) {
189 RecordSimplification();
190 }
191}
Artem Serov7fc63502016-02-09 17:15:29 +0000192
Anton Kirilov74234da2017-01-13 14:42:47 +0000193void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) {
194 if (TryCombineMultiplyAccumulate(instruction, kArm)) {
195 RecordSimplification();
196 }
197}
198
199void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) {
200 if (TryMergeNegatedInput(instruction)) {
201 RecordSimplification();
202 }
203}
204
205void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) {
206 if (instruction->InputAt(1)->IsConstant()) {
207 TryMergeIntoUsersShifterOperand(instruction);
208 }
209}
210
211void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) {
212 if (instruction->InputAt(1)->IsConstant()) {
213 TryMergeIntoUsersShifterOperand(instruction);
214 }
215}
216
217void InstructionSimplifierArmVisitor::VisitTypeConversion(HTypeConversion* instruction) {
218 Primitive::Type result_type = instruction->GetResultType();
219 Primitive::Type input_type = instruction->GetInputType();
220
221 if (input_type == result_type) {
222 // We let the arch-independent code handle this.
223 return;
224 }
225
226 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
227 TryMergeIntoUsersShifterOperand(instruction);
228 }
229}
230
231void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) {
232 if (instruction->InputAt(1)->IsConstant()) {
233 TryMergeIntoUsersShifterOperand(instruction);
234 }
235}
236
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300237} // namespace arm
238} // namespace art