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