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 | |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 17 | #include "code_generator.h" |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 18 | #include "common_arm.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 19 | #include "instruction_simplifier_arm.h" |
| 20 | #include "instruction_simplifier_shared.h" |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 21 | #include "mirror/array-inl.h" |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 22 | #include "nodes.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 23 | |
| 24 | namespace art { |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 25 | |
| 26 | using helpers::CanFitInShifterOperand; |
| 27 | using helpers::HasShifterOperand; |
| 28 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 29 | namespace arm { |
| 30 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 31 | using helpers::ShifterOperandSupportsExtension; |
| 32 | |
| 33 | bool 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)) { |
| 78 | if (!ShifterOperandSupportsExtension(use)) { |
| 79 | 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 Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 99 | RecordSimplification(); |
| 100 | } |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 101 | |
| 102 | return true; |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 103 | } |
| 104 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 105 | // Merge a bitfield move instruction into its uses if it can be merged in all of them. |
| 106 | bool InstructionSimplifierArmVisitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) { |
| 107 | DCHECK(CanFitInShifterOperand(bitfield_op)); |
| 108 | |
| 109 | if (bitfield_op->HasEnvironmentUses()) { |
| 110 | return false; |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 111 | } |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 112 | |
| 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 Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void InstructionSimplifierArmVisitor::VisitAnd(HAnd* instruction) { |
| 139 | if (TryMergeNegatedInput(instruction)) { |
| 140 | RecordSimplification(); |
| 141 | } |
| 142 | } |
| 143 | |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 144 | void InstructionSimplifierArmVisitor::VisitArrayGet(HArrayGet* instruction) { |
| 145 | size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction); |
| 146 | Primitive::Type type = instruction->GetType(); |
| 147 | |
jessicahandojo | 0576575 | 2016-09-09 19:01:32 -0700 | [diff] [blame] | 148 | // TODO: Implement reading (length + compression) for String compression feature from |
| 149 | // negative offset (count_offset - data_offset). Thumb2Assembler does not support T4 |
| 150 | // encoding of "LDR (immediate)" at the moment. |
| 151 | // 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 Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 156 | 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 | |
| 172 | void 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 Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 192 | |
Anton Kirilov | 74234da | 2017-01-13 14:42:47 +0000 | [diff] [blame] | 193 | void InstructionSimplifierArmVisitor::VisitMul(HMul* instruction) { |
| 194 | if (TryCombineMultiplyAccumulate(instruction, kArm)) { |
| 195 | RecordSimplification(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void InstructionSimplifierArmVisitor::VisitOr(HOr* instruction) { |
| 200 | if (TryMergeNegatedInput(instruction)) { |
| 201 | RecordSimplification(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void InstructionSimplifierArmVisitor::VisitShl(HShl* instruction) { |
| 206 | if (instruction->InputAt(1)->IsConstant()) { |
| 207 | TryMergeIntoUsersShifterOperand(instruction); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void InstructionSimplifierArmVisitor::VisitShr(HShr* instruction) { |
| 212 | if (instruction->InputAt(1)->IsConstant()) { |
| 213 | TryMergeIntoUsersShifterOperand(instruction); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void 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 | |
| 231 | void InstructionSimplifierArmVisitor::VisitUShr(HUShr* instruction) { |
| 232 | if (instruction->InputAt(1)->IsConstant()) { |
| 233 | TryMergeIntoUsersShifterOperand(instruction); |
| 234 | } |
| 235 | } |
| 236 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 237 | } // namespace arm |
| 238 | } // namespace art |