Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 1 | /* |
| 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 Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "instruction_simplifier_arm.h" |
| 18 | |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 19 | #include "code_generator.h" |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 20 | #include "common_arm.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 21 | #include "instruction_simplifier_shared.h" |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 22 | #include "mirror/array-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 23 | #include "mirror/string.h" |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 24 | #include "nodes.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 25 | |
| 26 | namespace art { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 27 | |
| 28 | using helpers::CanFitInShifterOperand; |
| 29 | using helpers::HasShifterOperand; |
| 30 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 31 | namespace arm { |
| 32 | |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 33 | class 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 Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 84 | bool 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 Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 92 | DataType::Type type = use->GetType(); |
| 93 | if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 94 | 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 Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 124 | shift_amount &= use->GetType() == DataType::Type::kInt32 |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 125 | ? kMaxIntShiftDistance |
| 126 | : kMaxLongShiftDistance; |
| 127 | |
| 128 | if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 129 | if (!use->IsAdd() && (!use->IsSub() || use->GetType() != DataType::Type::kInt64)) { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 130 | 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 Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 134 | } else if (type == DataType::Type::kInt64 && shift_amount == 1) { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if (do_merge) { |
| 139 | HDataProcWithShifterOp* alu_with_op = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame^] | 140 | new (GetGraph()->GetAllocator()) HDataProcWithShifterOp(use, |
| 141 | other_input, |
| 142 | bitfield_op->InputAt(0), |
| 143 | op_kind, |
| 144 | shift_amount, |
| 145 | use->GetDexPc()); |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 146 | use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op); |
| 147 | if (bitfield_op->GetUses().empty()) { |
| 148 | bitfield_op->GetBlock()->RemoveInstruction(bitfield_op); |
| 149 | } |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 150 | RecordSimplification(); |
| 151 | } |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 152 | |
| 153 | return true; |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 154 | } |
| 155 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 156 | // Merge a bitfield move instruction into its uses if it can be merged in all of them. |
| 157 | bool InstructionSimplifierArmVisitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) { |
| 158 | DCHECK(CanFitInShifterOperand(bitfield_op)); |
| 159 | |
| 160 | if (bitfield_op->HasEnvironmentUses()) { |
| 161 | return false; |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 162 | } |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 163 | |
| 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 Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void InstructionSimplifierArmVisitor::VisitAnd(HAnd* instruction) { |
| 190 | if (TryMergeNegatedInput(instruction)) { |
| 191 | RecordSimplification(); |
| 192 | } |
| 193 | } |
| 194 | |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 195 | void InstructionSimplifierArmVisitor::VisitArrayGet(HArrayGet* instruction) { |
| 196 | size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 197 | DataType::Type type = instruction->GetType(); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 198 | |
jessicahandojo | 0576575 | 2016-09-09 19:01:32 -0700 | [diff] [blame] | 199 | // TODO: Implement reading (length + compression) for String compression feature from |
Roland Levillain | c043d00 | 2017-07-14 16:39:16 +0100 | [diff] [blame] | 200 | // negative offset (count_offset - data_offset). Thumb2Assembler (now removed) did |
| 201 | // not support T4 encoding of "LDR (immediate)", but ArmVIXLMacroAssembler might. |
jessicahandojo | 0576575 | 2016-09-09 19:01:32 -0700 | [diff] [blame] | 202 | // 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 Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 207 | if (type == DataType::Type::kInt64 |
| 208 | || type == DataType::Type::kFloat32 |
| 209 | || type == DataType::Type::kFloat64) { |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 210 | // 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 | |
| 223 | void InstructionSimplifierArmVisitor::VisitArraySet(HArraySet* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 224 | size_t access_size = DataType::Size(instruction->GetComponentType()); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 225 | size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 226 | DataType::Type type = instruction->GetComponentType(); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 227 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 228 | if (type == DataType::Type::kInt64 |
| 229 | || type == DataType::Type::kFloat32 |
| 230 | || type == DataType::Type::kFloat64) { |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 231 | // 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 Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 243 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 244 | void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) { |
| 245 | if (TryCombineMultiplyAccumulate(instruction, kArm)) { |
| 246 | RecordSimplification(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) { |
| 251 | if (TryMergeNegatedInput(instruction)) { |
| 252 | RecordSimplification(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) { |
| 257 | if (instruction->InputAt(1)->IsConstant()) { |
| 258 | TryMergeIntoUsersShifterOperand(instruction); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) { |
| 263 | if (instruction->InputAt(1)->IsConstant()) { |
| 264 | TryMergeIntoUsersShifterOperand(instruction); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void InstructionSimplifierArmVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 269 | DataType::Type result_type = instruction->GetResultType(); |
| 270 | DataType::Type input_type = instruction->GetInputType(); |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 271 | |
| 272 | if (input_type == result_type) { |
| 273 | // We let the arch-independent code handle this. |
| 274 | return; |
| 275 | } |
| 276 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 277 | if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 278 | TryMergeIntoUsersShifterOperand(instruction); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) { |
| 283 | if (instruction->InputAt(1)->IsConstant()) { |
| 284 | TryMergeIntoUsersShifterOperand(instruction); |
| 285 | } |
| 286 | } |
| 287 | |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 288 | void InstructionSimplifierArm::Run() { |
| 289 | InstructionSimplifierArmVisitor visitor(graph_, stats_); |
| 290 | visitor.VisitReversePostOrder(); |
| 291 | } |
| 292 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 293 | } // namespace arm |
| 294 | } // namespace art |