1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "instruction_simplifier_arm64.h"
#include "common_arm64.h"
#include "instruction_simplifier_shared.h"
#include "mirror/array-inl.h"
#include "mirror/string.h"
namespace art HIDDEN {
using helpers::CanFitInShifterOperand;
using helpers::HasShifterOperand;
using helpers::IsSubRightSubLeftShl;
namespace arm64 {
using helpers::ShifterOperandSupportsExtension;
class InstructionSimplifierArm64Visitor final : public HGraphVisitor {
public:
InstructionSimplifierArm64Visitor(HGraph* graph, OptimizingCompilerStats* stats)
: HGraphVisitor(graph), stats_(stats) {}
private:
void RecordSimplification() {
MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
}
bool TryMergeIntoUsersShifterOperand(HInstruction* instruction);
bool TryMergeIntoShifterOperand(HInstruction* use,
HInstruction* bitfield_op,
bool do_merge);
bool CanMergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ false);
}
bool MergeIntoShifterOperand(HInstruction* use, HInstruction* bitfield_op) {
DCHECK(CanMergeIntoShifterOperand(use, bitfield_op));
return TryMergeIntoShifterOperand(use, bitfield_op, /* do_merge= */ true);
}
/**
* This simplifier uses a special-purpose BB visitor.
* (1) No need to visit Phi nodes.
* (2) Since statements can be removed in a "forward" fashion,
* the visitor should test if each statement is still there.
*/
void VisitBasicBlock(HBasicBlock* block) override {
// TODO: fragile iteration, provide more robust iterators?
for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
HInstruction* instruction = it.Current();
if (instruction->IsInBlock()) {
instruction->Accept(this);
}
}
}
// HInstruction visitors, sorted alphabetically.
void VisitAnd(HAnd* instruction) override;
void VisitArrayGet(HArrayGet* instruction) override;
void VisitArraySet(HArraySet* instruction) override;
void VisitMul(HMul* instruction) override;
void VisitOr(HOr* instruction) override;
void VisitShl(HShl* instruction) override;
void VisitShr(HShr* instruction) override;
void VisitSub(HSub* instruction) override;
void VisitTypeConversion(HTypeConversion* instruction) override;
void VisitUShr(HUShr* instruction) override;
void VisitXor(HXor* instruction) override;
void VisitVecLoad(HVecLoad* instruction) override;
void VisitVecStore(HVecStore* instruction) override;
OptimizingCompilerStats* stats_;
};
bool InstructionSimplifierArm64Visitor::TryMergeIntoShifterOperand(HInstruction* use,
HInstruction* bitfield_op,
bool do_merge) {
DCHECK(HasShifterOperand(use, InstructionSet::kArm64));
DCHECK(use->IsBinaryOperation() || use->IsNeg());
DCHECK(CanFitInShifterOperand(bitfield_op));
DCHECK(!bitfield_op->HasEnvironmentUses());
DataType::Type type = use->GetType();
if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) {
return false;
}
HInstruction* left;
HInstruction* right;
if (use->IsBinaryOperation()) {
left = use->InputAt(0);
right = use->InputAt(1);
} else {
DCHECK(use->IsNeg());
right = use->AsNeg()->InputAt(0);
left = GetGraph()->GetConstant(right->GetType(), 0);
}
DCHECK(left == bitfield_op || right == bitfield_op);
if (left == right) {
// TODO: Handle special transformations in this situation?
// For example should we transform `(x << 1) + (x << 1)` into `(x << 2)`?
// Or should this be part of a separate transformation logic?
return false;
}
bool is_commutative = use->IsBinaryOperation() && use->AsBinaryOperation()->IsCommutative();
HInstruction* other_input;
if (bitfield_op == right) {
other_input = left;
} else {
if (is_commutative) {
other_input = right;
} else {
return false;
}
}
HDataProcWithShifterOp::OpKind op_kind;
int shift_amount = 0;
HDataProcWithShifterOp::GetOpInfoFromInstruction(bitfield_op, &op_kind, &shift_amount);
if (HDataProcWithShifterOp::IsExtensionOp(op_kind) && !ShifterOperandSupportsExtension(use)) {
return false;
}
if (do_merge) {
HDataProcWithShifterOp* alu_with_op =
new (GetGraph()->GetAllocator()) HDataProcWithShifterOp(use,
other_input,
bitfield_op->InputAt(0),
op_kind,
shift_amount,
use->GetDexPc());
use->GetBlock()->ReplaceAndRemoveInstructionWith(use, alu_with_op);
if (bitfield_op->GetUses().empty()) {
bitfield_op->GetBlock()->RemoveInstruction(bitfield_op);
}
RecordSimplification();
}
return true;
}
// Merge a bitfield move instruction into its uses if it can be merged in all of them.
bool InstructionSimplifierArm64Visitor::TryMergeIntoUsersShifterOperand(HInstruction* bitfield_op) {
DCHECK(CanFitInShifterOperand(bitfield_op));
if (bitfield_op->HasEnvironmentUses()) {
return false;
}
const HUseList<HInstruction*>& uses = bitfield_op->GetUses();
// Check whether we can merge the instruction in all its users' shifter operand.
for (const HUseListNode<HInstruction*>& use : uses) {
HInstruction* user = use.GetUser();
if (!HasShifterOperand(user, InstructionSet::kArm64)) {
return false;
}
if (!CanMergeIntoShifterOperand(user, bitfield_op)) {
return false;
}
}
// Merge the instruction into its uses.
for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
HInstruction* user = it->GetUser();
// Increment `it` now because `*it` will disappear thanks to MergeIntoShifterOperand().
++it;
bool merged = MergeIntoShifterOperand(user, bitfield_op);
DCHECK(merged);
}
return true;
}
void InstructionSimplifierArm64Visitor::VisitAnd(HAnd* instruction) {
if (TryMergeNegatedInput(instruction)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitArrayGet(HArrayGet* instruction) {
size_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
if (TryExtractArrayAccessAddress(instruction,
instruction->GetArray(),
instruction->GetIndex(),
data_offset)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitArraySet(HArraySet* instruction) {
size_t access_size = DataType::Size(instruction->GetComponentType());
size_t data_offset = mirror::Array::DataOffset(access_size).Uint32Value();
if (TryExtractArrayAccessAddress(instruction,
instruction->GetArray(),
instruction->GetIndex(),
data_offset)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitMul(HMul* instruction) {
if (TryCombineMultiplyAccumulate(instruction, InstructionSet::kArm64)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitOr(HOr* instruction) {
if (TryMergeNegatedInput(instruction)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitShl(HShl* instruction) {
if (instruction->InputAt(1)->IsConstant()) {
TryMergeIntoUsersShifterOperand(instruction);
}
}
void InstructionSimplifierArm64Visitor::VisitShr(HShr* instruction) {
if (instruction->InputAt(1)->IsConstant()) {
TryMergeIntoUsersShifterOperand(instruction);
}
}
void InstructionSimplifierArm64Visitor::VisitSub(HSub* instruction) {
if (IsSubRightSubLeftShl(instruction)) {
HInstruction* shl = instruction->GetRight()->InputAt(0);
if (shl->InputAt(1)->IsConstant() && TryReplaceSubSubWithSubAdd(instruction)) {
TryMergeIntoUsersShifterOperand(shl);
}
}
}
void InstructionSimplifierArm64Visitor::VisitTypeConversion(HTypeConversion* instruction) {
DataType::Type result_type = instruction->GetResultType();
DataType::Type input_type = instruction->GetInputType();
if (input_type == result_type) {
// We let the arch-independent code handle this.
return;
}
if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
TryMergeIntoUsersShifterOperand(instruction);
}
}
void InstructionSimplifierArm64Visitor::VisitUShr(HUShr* instruction) {
if (instruction->InputAt(1)->IsConstant()) {
TryMergeIntoUsersShifterOperand(instruction);
}
}
void InstructionSimplifierArm64Visitor::VisitXor(HXor* instruction) {
if (TryMergeNegatedInput(instruction)) {
RecordSimplification();
}
}
void InstructionSimplifierArm64Visitor::VisitVecLoad(HVecLoad* instruction) {
if (!instruction->IsPredicated() && !instruction->IsStringCharAt() &&
TryExtractVecArrayAccessAddress(instruction, instruction->GetIndex())) {
RecordSimplification();
} else if (instruction->IsPredicated()) {
size_t size = DataType::Size(instruction->GetPackedType());
size_t offset = mirror::Array::DataOffset(size).Uint32Value();
if (TryExtractArrayAccessAddress(
instruction, instruction->GetArray(), instruction->GetIndex(), offset)) {
RecordSimplification();
}
}
}
void InstructionSimplifierArm64Visitor::VisitVecStore(HVecStore* instruction) {
if (!instruction->IsPredicated() &&
TryExtractVecArrayAccessAddress(instruction, instruction->GetIndex())) {
RecordSimplification();
} else if (instruction->IsPredicated()) {
size_t size = DataType::Size(instruction->GetPackedType());
size_t offset = mirror::Array::DataOffset(size).Uint32Value();
if (TryExtractArrayAccessAddress(
instruction, instruction->GetArray(), instruction->GetIndex(), offset)) {
RecordSimplification();
}
}
}
bool InstructionSimplifierArm64::Run() {
InstructionSimplifierArm64Visitor visitor(graph_, stats_);
visitor.VisitReversePostOrder();
return true;
}
} // namespace arm64
} // namespace art
|