xref: /aosp_15_r20/art/compiler/optimizing/instruction_simplifier_x86.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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.h"
17*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_x86_shared.h"
18*795d594fSAndroid Build Coastguard Worker #include "code_generator_x86.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 {
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker class InstructionSimplifierX86Visitor final : public HGraphVisitor {
25*795d594fSAndroid Build Coastguard Worker  public:
InstructionSimplifierX86Visitor(HGraph * graph,CodeGenerator * codegen,OptimizingCompilerStats * stats)26*795d594fSAndroid Build Coastguard Worker   InstructionSimplifierX86Visitor(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*>(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* codegen_;
55*795d594fSAndroid Build Coastguard Worker   OptimizingCompilerStats* stats_;
56*795d594fSAndroid Build Coastguard Worker };
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker 
VisitAnd(HAnd * instruction)59*795d594fSAndroid Build Coastguard Worker void InstructionSimplifierX86Visitor::VisitAnd(HAnd* instruction) {
60*795d594fSAndroid Build Coastguard Worker   if (!HasAVX2()) {
61*795d594fSAndroid Build Coastguard Worker     return;
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   if (TryCombineAndNot(instruction)) {
65*795d594fSAndroid Build Coastguard Worker     RecordSimplification();
66*795d594fSAndroid Build Coastguard Worker   } else if (instruction->GetResultType() == DataType::Type::kInt32) {
67*795d594fSAndroid Build Coastguard Worker     if (TryGenerateResetLeastSetBit(instruction)) {
68*795d594fSAndroid Build Coastguard Worker       RecordSimplification();
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker 
VisitXor(HXor * instruction)73*795d594fSAndroid Build Coastguard Worker void InstructionSimplifierX86Visitor::VisitXor(HXor* instruction) {
74*795d594fSAndroid Build Coastguard Worker   if (!HasAVX2()) {
75*795d594fSAndroid Build Coastguard Worker     return;
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker   if (instruction->GetResultType() == DataType::Type::kInt32) {
79*795d594fSAndroid Build Coastguard Worker     if (TryGenerateMaskUptoLeastSetBit(instruction)) {
80*795d594fSAndroid Build Coastguard Worker       RecordSimplification();
81*795d594fSAndroid Build Coastguard Worker     }
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker 
Run()85*795d594fSAndroid Build Coastguard Worker bool InstructionSimplifierX86::Run() {
86*795d594fSAndroid Build Coastguard Worker   InstructionSimplifierX86Visitor visitor(graph_, codegen_, stats_);
87*795d594fSAndroid Build Coastguard Worker   visitor.VisitReversePostOrder();
88*795d594fSAndroid Build Coastguard Worker   return true;
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker }  // namespace x86
92*795d594fSAndroid Build Coastguard Worker }  // namespace art
93*795d594fSAndroid Build Coastguard Worker 
94