1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2018 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker *
3*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
4*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
5*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
6*795d594fSAndroid Build Coastguard Worker *
7*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
8*795d594fSAndroid Build Coastguard Worker *
9*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
10*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
11*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
13*795d594fSAndroid Build Coastguard Worker * limitations under the License.
14*795d594fSAndroid Build Coastguard Worker */
15*795d594fSAndroid Build Coastguard Worker
16*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_x86_64.h"
17*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_x86_shared.h"
18*795d594fSAndroid Build Coastguard Worker #include "code_generator_x86_64.h"
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker namespace x86_64 {
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker class InstructionSimplifierX86_64Visitor final : public HGraphVisitor {
25*795d594fSAndroid Build Coastguard Worker public:
InstructionSimplifierX86_64Visitor(HGraph * graph,CodeGenerator * codegen,OptimizingCompilerStats * stats)26*795d594fSAndroid Build Coastguard Worker InstructionSimplifierX86_64Visitor(HGraph* graph,
27*795d594fSAndroid Build Coastguard Worker CodeGenerator* codegen,
28*795d594fSAndroid Build Coastguard Worker OptimizingCompilerStats* stats)
29*795d594fSAndroid Build Coastguard Worker : HGraphVisitor(graph),
30*795d594fSAndroid Build Coastguard Worker codegen_(down_cast<CodeGeneratorX86_64*>(codegen)),
31*795d594fSAndroid Build Coastguard Worker stats_(stats) {}
32*795d594fSAndroid Build Coastguard Worker
RecordSimplification()33*795d594fSAndroid Build Coastguard Worker void RecordSimplification() {
34*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker
HasAVX2()37*795d594fSAndroid Build Coastguard Worker bool HasAVX2() {
38*795d594fSAndroid Build Coastguard Worker return codegen_->GetInstructionSetFeatures().HasAVX2();
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker
VisitBasicBlock(HBasicBlock * block)41*795d594fSAndroid Build Coastguard Worker void VisitBasicBlock(HBasicBlock* block) override {
42*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
43*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = it.Current();
44*795d594fSAndroid Build Coastguard Worker if (instruction->IsInBlock()) {
45*795d594fSAndroid Build Coastguard Worker instruction->Accept(this);
46*795d594fSAndroid Build Coastguard Worker }
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker void VisitAnd(HAnd* instruction) override;
51*795d594fSAndroid Build Coastguard Worker void VisitXor(HXor* instruction) override;
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker private:
54*795d594fSAndroid Build Coastguard Worker CodeGeneratorX86_64* codegen_;
55*795d594fSAndroid Build Coastguard Worker OptimizingCompilerStats* stats_;
56*795d594fSAndroid Build Coastguard Worker };
57*795d594fSAndroid Build Coastguard Worker
VisitAnd(HAnd * instruction)58*795d594fSAndroid Build Coastguard Worker void InstructionSimplifierX86_64Visitor::VisitAnd(HAnd* instruction) {
59*795d594fSAndroid Build Coastguard Worker if (TryCombineAndNot(instruction)) {
60*795d594fSAndroid Build Coastguard Worker RecordSimplification();
61*795d594fSAndroid Build Coastguard Worker } else if (TryGenerateResetLeastSetBit(instruction)) {
62*795d594fSAndroid Build Coastguard Worker RecordSimplification();
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker
VisitXor(HXor * instruction)67*795d594fSAndroid Build Coastguard Worker void InstructionSimplifierX86_64Visitor::VisitXor(HXor* instruction) {
68*795d594fSAndroid Build Coastguard Worker if (TryGenerateMaskUptoLeastSetBit(instruction)) {
69*795d594fSAndroid Build Coastguard Worker RecordSimplification();
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker
Run()73*795d594fSAndroid Build Coastguard Worker bool InstructionSimplifierX86_64::Run() {
74*795d594fSAndroid Build Coastguard Worker InstructionSimplifierX86_64Visitor visitor(graph_, codegen_, stats_);
75*795d594fSAndroid Build Coastguard Worker if (visitor.HasAVX2()) {
76*795d594fSAndroid Build Coastguard Worker visitor.VisitReversePostOrder();
77*795d594fSAndroid Build Coastguard Worker return true;
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker return false;
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker } // namespace x86_64
82*795d594fSAndroid Build Coastguard Worker } // namespace art
83