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