xref: /aosp_15_r20/art/compiler/optimizing/nodes_shared.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker // Note: this include order may seem strange and is against the regular style. However it is the
18*795d594fSAndroid Build Coastguard Worker //       required order as nodes_shared does not have the right dependency chain and compilation
19*795d594fSAndroid Build Coastguard Worker //       will fail (as AsType on HInstruction will be defined before the full Instruction).
20*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "nodes_shared.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_shared.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker using helpers::CanFitInShifterOperand;
29*795d594fSAndroid Build Coastguard Worker 
GetOpInfoFromInstruction(HInstruction * instruction,OpKind * op_kind,int * shift_amount)30*795d594fSAndroid Build Coastguard Worker void HDataProcWithShifterOp::GetOpInfoFromInstruction(HInstruction* instruction,
31*795d594fSAndroid Build Coastguard Worker                                                       /*out*/OpKind* op_kind,
32*795d594fSAndroid Build Coastguard Worker                                                       /*out*/int* shift_amount) {
33*795d594fSAndroid Build Coastguard Worker   DCHECK(CanFitInShifterOperand(instruction));
34*795d594fSAndroid Build Coastguard Worker   if (instruction->IsShl()) {
35*795d594fSAndroid Build Coastguard Worker     *op_kind = kLSL;
36*795d594fSAndroid Build Coastguard Worker     *shift_amount = instruction->AsShl()->GetRight()->AsIntConstant()->GetValue();
37*795d594fSAndroid Build Coastguard Worker   } else if (instruction->IsShr()) {
38*795d594fSAndroid Build Coastguard Worker     *op_kind = kASR;
39*795d594fSAndroid Build Coastguard Worker     *shift_amount = instruction->AsShr()->GetRight()->AsIntConstant()->GetValue();
40*795d594fSAndroid Build Coastguard Worker   } else if (instruction->IsUShr()) {
41*795d594fSAndroid Build Coastguard Worker     *op_kind = kLSR;
42*795d594fSAndroid Build Coastguard Worker     *shift_amount = instruction->AsUShr()->GetRight()->AsIntConstant()->GetValue();
43*795d594fSAndroid Build Coastguard Worker   } else {
44*795d594fSAndroid Build Coastguard Worker     DCHECK(instruction->IsTypeConversion());
45*795d594fSAndroid Build Coastguard Worker     DataType::Type result_type = instruction->AsTypeConversion()->GetResultType();
46*795d594fSAndroid Build Coastguard Worker     DataType::Type input_type = instruction->AsTypeConversion()->GetInputType();
47*795d594fSAndroid Build Coastguard Worker     int result_size = DataType::Size(result_type);
48*795d594fSAndroid Build Coastguard Worker     int input_size = DataType::Size(input_type);
49*795d594fSAndroid Build Coastguard Worker     int min_size = std::min(result_size, input_size);
50*795d594fSAndroid Build Coastguard Worker     if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
51*795d594fSAndroid Build Coastguard Worker       // There is actually nothing to do. On ARM the high register from the
52*795d594fSAndroid Build Coastguard Worker       // pair will be ignored. On ARM64 the register will be used as a W
53*795d594fSAndroid Build Coastguard Worker       // register, discarding the top bits. This is represented by the
54*795d594fSAndroid Build Coastguard Worker       // default encoding 'LSL 0'.
55*795d594fSAndroid Build Coastguard Worker       *op_kind = kLSL;
56*795d594fSAndroid Build Coastguard Worker       *shift_amount = 0;
57*795d594fSAndroid Build Coastguard Worker     } else if (result_type == DataType::Type::kUint8 ||
58*795d594fSAndroid Build Coastguard Worker                (input_type == DataType::Type::kUint8 && input_size < result_size)) {
59*795d594fSAndroid Build Coastguard Worker       *op_kind = kUXTB;
60*795d594fSAndroid Build Coastguard Worker     } else if (result_type == DataType::Type::kUint16 ||
61*795d594fSAndroid Build Coastguard Worker                (input_type == DataType::Type::kUint16 && input_size < result_size)) {
62*795d594fSAndroid Build Coastguard Worker       *op_kind = kUXTH;
63*795d594fSAndroid Build Coastguard Worker     } else {
64*795d594fSAndroid Build Coastguard Worker       switch (min_size) {
65*795d594fSAndroid Build Coastguard Worker         case 1: *op_kind = kSXTB; break;
66*795d594fSAndroid Build Coastguard Worker         case 2: *op_kind = kSXTH; break;
67*795d594fSAndroid Build Coastguard Worker         case 4: *op_kind = kSXTW; break;
68*795d594fSAndroid Build Coastguard Worker         default:
69*795d594fSAndroid Build Coastguard Worker           LOG(FATAL) << "Unexpected min size " << min_size;
70*795d594fSAndroid Build Coastguard Worker       }
71*795d594fSAndroid Build Coastguard Worker     }
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker }
74*795d594fSAndroid Build Coastguard Worker 
operator <<(std::ostream & os,const HDataProcWithShifterOp::OpKind op)75*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const HDataProcWithShifterOp::OpKind op) {
76*795d594fSAndroid Build Coastguard Worker   switch (op) {
77*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kLSL:  return os << "LSL";
78*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kLSR:  return os << "LSR";
79*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kASR:  return os << "ASR";
80*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kUXTB: return os << "UXTB";
81*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kUXTH: return os << "UXTH";
82*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kUXTW: return os << "UXTW";
83*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kSXTB: return os << "SXTB";
84*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kSXTH: return os << "SXTH";
85*795d594fSAndroid Build Coastguard Worker     case HDataProcWithShifterOp::kSXTW: return os << "SXTW";
86*795d594fSAndroid Build Coastguard Worker     default:
87*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Invalid OpKind " << static_cast<int>(op);
88*795d594fSAndroid Build Coastguard Worker       UNREACHABLE();
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker }  // namespace art
93