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_shared.h"
17 
18 #include "nodes_x86.h"
19 
20 namespace art HIDDEN {
21 
TryCombineAndNot(HAnd * instruction)22 bool TryCombineAndNot(HAnd* instruction) {
23   DataType::Type type = instruction->GetType();
24   if (!DataType::IsIntOrLongType(type)) {
25     return false;
26   }
27   // Replace code looking like
28   //    Not tmp, y
29   //    And dst, x, tmp
30   //  with
31   //    AndNot dst, x, y
32   HInstruction* left = instruction->GetLeft();
33   HInstruction* right = instruction->GetRight();
34   // Perform simplication only when either left or right
35   // is Not. When both are Not, instruction should be simplified with
36   // DeMorgan's Laws.
37   if (left->IsNot() ^ right->IsNot()) {
38     bool left_is_not = left->IsNot();
39     HInstruction* other_ins = (left_is_not ? right : left);
40     HNot* not_ins = (left_is_not ? left : right)->AsNot();
41     // Only do the simplification if instruction has only one use
42     // and thus can be safely removed.
43     if (not_ins->HasOnlyOneNonEnvironmentUse()) {
44       ArenaAllocator* arena = instruction->GetBlock()->GetGraph()->GetAllocator();
45       HX86AndNot* and_not = new (arena) HX86AndNot(type,
46                                                    not_ins->GetInput(),
47                                                    other_ins,
48                                                    instruction->GetDexPc());
49       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, and_not);
50       DCHECK(!not_ins->HasUses());
51       not_ins->GetBlock()->RemoveInstruction(not_ins);
52       return true;
53     }
54   }
55   return false;
56 }
57 
TryGenerateResetLeastSetBit(HAnd * instruction)58 bool TryGenerateResetLeastSetBit(HAnd* instruction) {
59   DataType::Type type = instruction->GetType();
60   if (!DataType::IsIntOrLongType(type)) {
61     return false;
62   }
63   // Replace code looking like
64   //    Add tmp, x, -1 or Sub tmp, x, 1
65   //    And dest x, tmp
66   //  with
67   //    MaskOrResetLeastSetBit dest, x
68   HInstruction* candidate = nullptr;
69   HInstruction* other = nullptr;
70   HInstruction* left = instruction->GetLeft();
71   HInstruction* right = instruction->GetRight();
72   if (AreLeastSetBitInputs(left, right)) {
73     candidate = left;
74     other = right;
75   } else if (AreLeastSetBitInputs(right, left)) {
76     candidate = right;
77     other = left;
78   }
79   if (candidate != nullptr && candidate->HasOnlyOneNonEnvironmentUse()) {
80     ArenaAllocator* arena = instruction->GetBlock()->GetGraph()->GetAllocator();
81     HX86MaskOrResetLeastSetBit* lsb = new (arena) HX86MaskOrResetLeastSetBit(
82         type, HInstruction::kAnd, other, instruction->GetDexPc());
83     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, lsb);
84     DCHECK(!candidate->HasUses());
85     candidate->GetBlock()->RemoveInstruction(candidate);
86     return true;
87   }
88   return false;
89 }
90 
TryGenerateMaskUptoLeastSetBit(HXor * instruction)91 bool TryGenerateMaskUptoLeastSetBit(HXor* instruction) {
92   DataType::Type type = instruction->GetType();
93   if (!DataType::IsIntOrLongType(type)) {
94     return false;
95   }
96   // Replace code looking like
97   //    Add tmp, x, -1 or Sub tmp, x, 1
98   //    Xor dest x, tmp
99   //  with
100   //    MaskOrResetLeastSetBit dest, x
101   HInstruction* left = instruction->GetLeft();
102   HInstruction* right = instruction->GetRight();
103   HInstruction* other = nullptr;
104   HInstruction* candidate = nullptr;
105   if (AreLeastSetBitInputs(left, right)) {
106     candidate = left;
107     other = right;
108   } else if (AreLeastSetBitInputs(right, left)) {
109     candidate = right;
110     other = left;
111   }
112   if (candidate != nullptr && candidate->HasOnlyOneNonEnvironmentUse()) {
113     ArenaAllocator* arena = instruction->GetBlock()->GetGraph()->GetAllocator();
114     HX86MaskOrResetLeastSetBit* lsb = new (arena) HX86MaskOrResetLeastSetBit(
115         type, HInstruction::kXor, other, instruction->GetDexPc());
116     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, lsb);
117     DCHECK(!candidate->HasUses());
118     candidate->GetBlock()->RemoveInstruction(candidate);
119     return true;
120   }
121   return false;
122 }
123 
AreLeastSetBitInputs(HInstruction * to_test,HInstruction * other)124 bool AreLeastSetBitInputs(HInstruction* to_test, HInstruction* other) {
125   if (to_test->IsAdd()) {
126     HAdd* add = to_test->AsAdd();
127     HConstant* cst = add->GetConstantRight();
128     return cst != nullptr && cst->IsMinusOne() && other == add->GetLeastConstantLeft();
129   }
130   if (to_test->IsSub()) {
131     HSub* sub = to_test->AsSub();
132     HConstant* cst = sub->GetConstantRight();
133     return cst != nullptr && cst->IsOne() && other == sub->GetLeastConstantLeft();
134   }
135   return false;
136 }
137 
138 }  // namespace art
139