Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [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 | |
| 17 | #include "instruction_simplifier_arm64.h" |
| 18 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 19 | #include "common_arm64.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 20 | #include "instruction_simplifier_shared.h" |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 21 | #include "mirror/array-inl.h" |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 22 | #include "mirror/string.h" |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 23 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 24 | namespace art { |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 25 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 26 | using helpers::CanFitInShifterOperand; |
| 27 | using helpers::HasShifterOperand; |
Evgeny Astigeevich | 3d190c0 | 2020-06-17 15:37:02 +0100 | [diff] [blame] | 28 | using helpers::IsSubRightSubLeftShl; |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm64 { |
| 31 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 32 | using helpers::ShifterOperandSupportsExtension; |
| 33 | |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 34 | class InstructionSimplifierArm64Visitor : public HGraphVisitor { |
| 35 | public: |
| 36 | InstructionSimplifierArm64Visitor(HGraph* graph, OptimizingCompilerStats* stats) |
| 37 | : HGraphVisitor(graph), stats_(stats) {} |
| 38 | |
| 39 | private: |
| 40 | void RecordSimplification() { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame] | 41 | MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch); |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool TryMergeIntoUsersShifterOperand(HInstruction* instruction); |
| 45 | bool TryMergeIntoShifterOperand(HInstruction* use, |
| 46 | HInstruction* bitfield_op, |
| 47 | bool do_merge); |
| 48 | bool CanMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) { |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 49 | return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ false); |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 50 | } |
| 51 | bool MergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) { |
| 52 | DCHECK(CanMergeIntoShifterOperand(use, bitfield_op)); |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 53 | return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ true); |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * This simplifier uses a special-purpose BB visitor. |
| 58 | * (1) No need to visit Phi nodes. |
| 59 | * (2) Since statements can be removed in a "forward" fashion, |
| 60 | * the visitor should test if each statement is still there. |
| 61 | */ |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 62 | void VisitBasicBlock(HBasicBlock* block) override { |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 63 | // TODO: fragile iteration, provide more robust iterators? |
| 64 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 65 | HInstruction* instruction = it.Current(); |
| 66 | if (instruction->IsInBlock()) { |
| 67 | instruction->Accept(this); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // HInstruction visitors, sorted alphabetically. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 73 | void VisitAnd(HAnd* instruction) override; |
| 74 | void VisitArrayGet(HArrayGet* instruction) override; |
| 75 | void VisitArraySet(HArraySet* instruction) override; |
| 76 | void VisitMul(HMul* instruction) override; |
| 77 | void VisitOr(HOr* instruction) override; |
| 78 | void VisitShl(HShl* instruction) override; |
| 79 | void VisitShr(HShr* instruction) override; |
Evgeny Astigeevich | 3d190c0 | 2020-06-17 15:37:02 +0100 | [diff] [blame] | 80 | void VisitSub(HSub* instruction) override; |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 81 | void VisitTypeConversion(HTypeConversion* instruction) override; |
| 82 | void VisitUShr(HUShr* instruction) override; |
| 83 | void VisitXor(HXor* instruction) override; |
| 84 | void VisitVecLoad(HVecLoad* instruction) override; |
| 85 | void VisitVecStore(HVecStore* instruction) override; |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 86 | |
| 87 | OptimizingCompilerStats* stats_; |
| 88 | }; |
| 89 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 90 | bool InstructionSimplifierArm64Visitor::TryMergeIntoShifterOperand(HInstruction* use, |
| 91 | HInstruction* bitfield_op, |
| 92 | bool do_merge) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 93 | DCHECK(HasShifterOperand(use, InstructionSet::kArm64)); |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 94 | DCHECK(use->IsBinaryOperation() || use->IsNeg()); |
| 95 | DCHECK(CanFitInShifterOperand(bitfield_op)); |
| 96 | DCHECK(!bitfield_op->HasEnvironmentUses()); |
| 97 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 98 | DataType::Type type = use->GetType(); |
| 99 | if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | HInstruction* left; |
| 104 | HInstruction* right; |
| 105 | if (use->IsBinaryOperation()) { |
| 106 | left = use->InputAt(0); |
| 107 | right = use->InputAt(1); |
| 108 | } else { |
| 109 | DCHECK(use->IsNeg()); |
| 110 | right = use->AsNeg()->InputAt(0); |
| 111 | left = GetGraph()->GetConstant(right->GetType(), 0); |
| 112 | } |
| 113 | DCHECK(left == bitfield_op || right == bitfield_op); |
| 114 | |
| 115 | if (left == right) { |
| 116 | // TODO: Handle special transformations in this situation? |
| 117 | // For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`? |
| 118 | // Or should this be part of a separate transformation logic? |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | bool is_commutative = use->IsBinaryOperation() && use->AsBinaryOperation()->IsCommutative(); |
| 123 | HInstruction* other_input; |
| 124 | if (bitfield_op == right) { |
| 125 | other_input = left; |
| 126 | } else { |
| 127 | if (is_commutative) { |
| 128 | other_input = right; |
| 129 | } else { |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 134 | HDataProcWithShifterOp::OpKind op_kind; |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 135 | int shift_amount = 0; |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 136 | HDataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount); |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 137 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 138 | if (HDataProcWithShifterOp::IsExtensionOp(op_kind) && !ShifterOperandSupportsExtension(use)) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if (do_merge) { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 143 | HDataProcWithShifterOp* alu_with_op = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 144 | new (GetGraph()->GetAllocator()) HDataProcWithShifterOp(use, |
| 145 | other_input, |
| 146 | bitfield_op->InputAt(0), |
| 147 | op_kind, |
| 148 | shift_amount, |
| 149 | use->GetDexPc()); |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 150 | use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 151 | if (bitfield_op->GetUses().empty()) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 152 | bitfield_op->GetBlock()->RemoveInstruction(bitfield_op); |
| 153 | } |
| 154 | RecordSimplification(); |
| 155 | } |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | // Merge a bitfield move instruction into its uses if it can be merged in all of them. |
| 161 | bool InstructionSimplifierArm64Visitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) { |
| 162 | DCHECK(CanFitInShifterOperand(bitfield_op)); |
| 163 | |
| 164 | if (bitfield_op->HasEnvironmentUses()) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | const HUseList<HInstruction*>& uses = bitfield_op->GetUses(); |
| 169 | |
| 170 | // Check whether we can merge the instruction in all its users' shifter operand. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 171 | for (const HUseListNode<HInstruction*>& use : uses) { |
| 172 | HInstruction* user = use.GetUser(); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 173 | if (!HasShifterOperand(user, InstructionSet::kArm64)) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 176 | if (!CanMergeIntoShifterOperand(user, bitfield_op)) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Merge the instruction into its uses. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 182 | for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) { |
| 183 | HInstruction* user = it->GetUser(); |
| 184 | // Increment `it` now because `*it` will disappear thanks to MergeIntoShifterOperand(). |
| 185 | ++it; |
| 186 | bool merged = MergeIntoShifterOperand(user, bitfield_op); |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 187 | DCHECK(merged); |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 193 | void InstructionSimplifierArm64Visitor::VisitAnd(HAnd* instruction) { |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 194 | if (TryMergeNegatedInput(instruction)) { |
| 195 | RecordSimplification(); |
| 196 | } |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 199 | void InstructionSimplifierArm64Visitor::VisitArrayGet(HArrayGet* instruction) { |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 200 | size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 201 | if (TryExtractArrayAccessAddress(instruction, |
| 202 | instruction->GetArray(), |
| 203 | instruction->GetIndex(), |
| 204 | data_offset)) { |
| 205 | RecordSimplification(); |
| 206 | } |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void InstructionSimplifierArm64Visitor::VisitArraySet(HArraySet* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 210 | size_t access_size = DataType::Size(instruction->GetComponentType()); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 211 | size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value(); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 212 | if (TryExtractArrayAccessAddress(instruction, |
| 213 | instruction->GetArray(), |
| 214 | instruction->GetIndex(), |
| 215 | data_offset)) { |
| 216 | RecordSimplification(); |
| 217 | } |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Alexandre Rames | 418318f | 2015-11-20 15:55:47 +0000 | [diff] [blame] | 220 | void InstructionSimplifierArm64Visitor::VisitMul(HMul* instruction) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 221 | if (TryCombineMultiplyAccumulate(instruction, InstructionSet::kArm64)) { |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 222 | RecordSimplification(); |
Alexandre Rames | 418318f | 2015-11-20 15:55:47 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 226 | void InstructionSimplifierArm64Visitor::VisitOr(HOr* instruction) { |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 227 | if (TryMergeNegatedInput(instruction)) { |
| 228 | RecordSimplification(); |
| 229 | } |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 232 | void InstructionSimplifierArm64Visitor::VisitShl(HShl* instruction) { |
| 233 | if (instruction->InputAt(1)->IsConstant()) { |
| 234 | TryMergeIntoUsersShifterOperand(instruction); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void InstructionSimplifierArm64Visitor::VisitShr(HShr* instruction) { |
| 239 | if (instruction->InputAt(1)->IsConstant()) { |
| 240 | TryMergeIntoUsersShifterOperand(instruction); |
| 241 | } |
| 242 | } |
| 243 | |
Evgeny Astigeevich | 3d190c0 | 2020-06-17 15:37:02 +0100 | [diff] [blame] | 244 | void InstructionSimplifierArm64Visitor::VisitSub(HSub* instruction) { |
| 245 | if (IsSubRightSubLeftShl(instruction)) { |
| 246 | HInstruction* shl = instruction->GetRight()->InputAt(0); |
| 247 | if (shl->InputAt(1)->IsConstant() && TryReplaceSubSubWithSubAdd(instruction)) { |
| 248 | TryMergeIntoUsersShifterOperand(shl); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 253 | void InstructionSimplifierArm64Visitor::VisitTypeConversion(HTypeConversion* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 254 | DataType::Type result_type = instruction->GetResultType(); |
| 255 | DataType::Type input_type = instruction->GetInputType(); |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 256 | |
| 257 | if (input_type == result_type) { |
| 258 | // We let the arch-independent code handle this. |
| 259 | return; |
| 260 | } |
| 261 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 262 | if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) { |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 263 | TryMergeIntoUsersShifterOperand(instruction); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void InstructionSimplifierArm64Visitor::VisitUShr(HUShr* instruction) { |
| 268 | if (instruction->InputAt(1)->IsConstant()) { |
| 269 | TryMergeIntoUsersShifterOperand(instruction); |
| 270 | } |
| 271 | } |
| 272 | |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 273 | void InstructionSimplifierArm64Visitor::VisitXor(HXor* instruction) { |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 274 | if (TryMergeNegatedInput(instruction)) { |
| 275 | RecordSimplification(); |
| 276 | } |
Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Artem Serov | e1811ed | 2017-04-27 16:50:47 +0100 | [diff] [blame] | 279 | void InstructionSimplifierArm64Visitor::VisitVecLoad(HVecLoad* instruction) { |
| 280 | if (!instruction->IsStringCharAt() |
| 281 | && TryExtractVecArrayAccessAddress(instruction, instruction->GetIndex())) { |
| 282 | RecordSimplification(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void InstructionSimplifierArm64Visitor::VisitVecStore(HVecStore* instruction) { |
| 287 | if (TryExtractVecArrayAccessAddress(instruction, instruction->GetIndex())) { |
| 288 | RecordSimplification(); |
| 289 | } |
| 290 | } |
| 291 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 292 | bool InstructionSimplifierArm64::Run() { |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 293 | InstructionSimplifierArm64Visitor visitor(graph_, stats_); |
| 294 | visitor.VisitReversePostOrder(); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 295 | return true; |
Vladimir Marko | 0f689e7 | 2017-10-02 12:38:21 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 298 | } // namespace arm64 |
| 299 | } // namespace art |