xref: /aosp_15_r20/external/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- SystemZTargetTransformInfo.cpp - SystemZ-specific TTI -------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements a TargetTransformInfo analysis pass specific to the
11*9880d681SAndroid Build Coastguard Worker // SystemZ target machine. It uses the target's detailed information to provide
12*9880d681SAndroid Build Coastguard Worker // more precise answers to certain TTI queries, while letting the target
13*9880d681SAndroid Build Coastguard Worker // independent and default TTI implementations handle the rest.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetTransformInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/BasicTTIImpl.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/CostTable.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
24*9880d681SAndroid Build Coastguard Worker using namespace llvm;
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "systemztti"
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
29*9880d681SAndroid Build Coastguard Worker //
30*9880d681SAndroid Build Coastguard Worker // SystemZ cost model.
31*9880d681SAndroid Build Coastguard Worker //
32*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
33*9880d681SAndroid Build Coastguard Worker 
getIntImmCost(const APInt & Imm,Type * Ty)34*9880d681SAndroid Build Coastguard Worker int SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
35*9880d681SAndroid Build Coastguard Worker   assert(Ty->isIntegerTy());
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker   unsigned BitSize = Ty->getPrimitiveSizeInBits();
38*9880d681SAndroid Build Coastguard Worker   // There is no cost model for constants with a bit size of 0. Return TCC_Free
39*9880d681SAndroid Build Coastguard Worker   // here, so that constant hoisting will ignore this constant.
40*9880d681SAndroid Build Coastguard Worker   if (BitSize == 0)
41*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
42*9880d681SAndroid Build Coastguard Worker   // No cost model for operations on integers larger than 64 bit implemented yet.
43*9880d681SAndroid Build Coastguard Worker   if (BitSize > 64)
44*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker   if (Imm == 0)
47*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   if (Imm.getBitWidth() <= 64) {
50*9880d681SAndroid Build Coastguard Worker     // Constants loaded via lgfi.
51*9880d681SAndroid Build Coastguard Worker     if (isInt<32>(Imm.getSExtValue()))
52*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Basic;
53*9880d681SAndroid Build Coastguard Worker     // Constants loaded via llilf.
54*9880d681SAndroid Build Coastguard Worker     if (isUInt<32>(Imm.getZExtValue()))
55*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Basic;
56*9880d681SAndroid Build Coastguard Worker     // Constants loaded via llihf:
57*9880d681SAndroid Build Coastguard Worker     if ((Imm.getZExtValue() & 0xffffffff) == 0)
58*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Basic;
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker     return 2 * TTI::TCC_Basic;
61*9880d681SAndroid Build Coastguard Worker   }
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker   return 4 * TTI::TCC_Basic;
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker 
getIntImmCost(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty)66*9880d681SAndroid Build Coastguard Worker int SystemZTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx,
67*9880d681SAndroid Build Coastguard Worker                                   const APInt &Imm, Type *Ty) {
68*9880d681SAndroid Build Coastguard Worker   assert(Ty->isIntegerTy());
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   unsigned BitSize = Ty->getPrimitiveSizeInBits();
71*9880d681SAndroid Build Coastguard Worker   // There is no cost model for constants with a bit size of 0. Return TCC_Free
72*9880d681SAndroid Build Coastguard Worker   // here, so that constant hoisting will ignore this constant.
73*9880d681SAndroid Build Coastguard Worker   if (BitSize == 0)
74*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
75*9880d681SAndroid Build Coastguard Worker   // No cost model for operations on integers larger than 64 bit implemented yet.
76*9880d681SAndroid Build Coastguard Worker   if (BitSize > 64)
77*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   switch (Opcode) {
80*9880d681SAndroid Build Coastguard Worker   default:
81*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
82*9880d681SAndroid Build Coastguard Worker   case Instruction::GetElementPtr:
83*9880d681SAndroid Build Coastguard Worker     // Always hoist the base address of a GetElementPtr. This prevents the
84*9880d681SAndroid Build Coastguard Worker     // creation of new constants for every base constant that gets constant
85*9880d681SAndroid Build Coastguard Worker     // folded with the offset.
86*9880d681SAndroid Build Coastguard Worker     if (Idx == 0)
87*9880d681SAndroid Build Coastguard Worker       return 2 * TTI::TCC_Basic;
88*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
89*9880d681SAndroid Build Coastguard Worker   case Instruction::Store:
90*9880d681SAndroid Build Coastguard Worker     if (Idx == 0 && Imm.getBitWidth() <= 64) {
91*9880d681SAndroid Build Coastguard Worker       // Any 8-bit immediate store can by implemented via mvi.
92*9880d681SAndroid Build Coastguard Worker       if (BitSize == 8)
93*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
94*9880d681SAndroid Build Coastguard Worker       // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi.
95*9880d681SAndroid Build Coastguard Worker       if (isInt<16>(Imm.getSExtValue()))
96*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
97*9880d681SAndroid Build Coastguard Worker     }
98*9880d681SAndroid Build Coastguard Worker     break;
99*9880d681SAndroid Build Coastguard Worker   case Instruction::ICmp:
100*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
101*9880d681SAndroid Build Coastguard Worker       // Comparisons against signed 32-bit immediates implemented via cgfi.
102*9880d681SAndroid Build Coastguard Worker       if (isInt<32>(Imm.getSExtValue()))
103*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
104*9880d681SAndroid Build Coastguard Worker       // Comparisons against unsigned 32-bit immediates implemented via clgfi.
105*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(Imm.getZExtValue()))
106*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
107*9880d681SAndroid Build Coastguard Worker     }
108*9880d681SAndroid Build Coastguard Worker     break;
109*9880d681SAndroid Build Coastguard Worker   case Instruction::Add:
110*9880d681SAndroid Build Coastguard Worker   case Instruction::Sub:
111*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
112*9880d681SAndroid Build Coastguard Worker       // We use algfi/slgfi to add/subtract 32-bit unsigned immediates.
113*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(Imm.getZExtValue()))
114*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
115*9880d681SAndroid Build Coastguard Worker       // Or their negation, by swapping addition vs. subtraction.
116*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(-Imm.getSExtValue()))
117*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
118*9880d681SAndroid Build Coastguard Worker     }
119*9880d681SAndroid Build Coastguard Worker     break;
120*9880d681SAndroid Build Coastguard Worker   case Instruction::Mul:
121*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
122*9880d681SAndroid Build Coastguard Worker       // We use msgfi to multiply by 32-bit signed immediates.
123*9880d681SAndroid Build Coastguard Worker       if (isInt<32>(Imm.getSExtValue()))
124*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
125*9880d681SAndroid Build Coastguard Worker     }
126*9880d681SAndroid Build Coastguard Worker     break;
127*9880d681SAndroid Build Coastguard Worker   case Instruction::Or:
128*9880d681SAndroid Build Coastguard Worker   case Instruction::Xor:
129*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
130*9880d681SAndroid Build Coastguard Worker       // Masks supported by oilf/xilf.
131*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(Imm.getZExtValue()))
132*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
133*9880d681SAndroid Build Coastguard Worker       // Masks supported by oihf/xihf.
134*9880d681SAndroid Build Coastguard Worker       if ((Imm.getZExtValue() & 0xffffffff) == 0)
135*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
136*9880d681SAndroid Build Coastguard Worker     }
137*9880d681SAndroid Build Coastguard Worker     break;
138*9880d681SAndroid Build Coastguard Worker   case Instruction::And:
139*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
140*9880d681SAndroid Build Coastguard Worker       // Any 32-bit AND operation can by implemented via nilf.
141*9880d681SAndroid Build Coastguard Worker       if (BitSize <= 32)
142*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
143*9880d681SAndroid Build Coastguard Worker       // 64-bit masks supported by nilf.
144*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(~Imm.getZExtValue()))
145*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
146*9880d681SAndroid Build Coastguard Worker       // 64-bit masks supported by nilh.
147*9880d681SAndroid Build Coastguard Worker       if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff)
148*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
149*9880d681SAndroid Build Coastguard Worker       // Some 64-bit AND operations can be implemented via risbg.
150*9880d681SAndroid Build Coastguard Worker       const SystemZInstrInfo *TII = ST->getInstrInfo();
151*9880d681SAndroid Build Coastguard Worker       unsigned Start, End;
152*9880d681SAndroid Build Coastguard Worker       if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End))
153*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
154*9880d681SAndroid Build Coastguard Worker     }
155*9880d681SAndroid Build Coastguard Worker     break;
156*9880d681SAndroid Build Coastguard Worker   case Instruction::Shl:
157*9880d681SAndroid Build Coastguard Worker   case Instruction::LShr:
158*9880d681SAndroid Build Coastguard Worker   case Instruction::AShr:
159*9880d681SAndroid Build Coastguard Worker     // Always return TCC_Free for the shift value of a shift instruction.
160*9880d681SAndroid Build Coastguard Worker     if (Idx == 1)
161*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Free;
162*9880d681SAndroid Build Coastguard Worker     break;
163*9880d681SAndroid Build Coastguard Worker   case Instruction::UDiv:
164*9880d681SAndroid Build Coastguard Worker   case Instruction::SDiv:
165*9880d681SAndroid Build Coastguard Worker   case Instruction::URem:
166*9880d681SAndroid Build Coastguard Worker   case Instruction::SRem:
167*9880d681SAndroid Build Coastguard Worker   case Instruction::Trunc:
168*9880d681SAndroid Build Coastguard Worker   case Instruction::ZExt:
169*9880d681SAndroid Build Coastguard Worker   case Instruction::SExt:
170*9880d681SAndroid Build Coastguard Worker   case Instruction::IntToPtr:
171*9880d681SAndroid Build Coastguard Worker   case Instruction::PtrToInt:
172*9880d681SAndroid Build Coastguard Worker   case Instruction::BitCast:
173*9880d681SAndroid Build Coastguard Worker   case Instruction::PHI:
174*9880d681SAndroid Build Coastguard Worker   case Instruction::Call:
175*9880d681SAndroid Build Coastguard Worker   case Instruction::Select:
176*9880d681SAndroid Build Coastguard Worker   case Instruction::Ret:
177*9880d681SAndroid Build Coastguard Worker   case Instruction::Load:
178*9880d681SAndroid Build Coastguard Worker     break;
179*9880d681SAndroid Build Coastguard Worker   }
180*9880d681SAndroid Build Coastguard Worker 
181*9880d681SAndroid Build Coastguard Worker   return SystemZTTIImpl::getIntImmCost(Imm, Ty);
182*9880d681SAndroid Build Coastguard Worker }
183*9880d681SAndroid Build Coastguard Worker 
getIntImmCost(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty)184*9880d681SAndroid Build Coastguard Worker int SystemZTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
185*9880d681SAndroid Build Coastguard Worker                                   const APInt &Imm, Type *Ty) {
186*9880d681SAndroid Build Coastguard Worker   assert(Ty->isIntegerTy());
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker   unsigned BitSize = Ty->getPrimitiveSizeInBits();
189*9880d681SAndroid Build Coastguard Worker   // There is no cost model for constants with a bit size of 0. Return TCC_Free
190*9880d681SAndroid Build Coastguard Worker   // here, so that constant hoisting will ignore this constant.
191*9880d681SAndroid Build Coastguard Worker   if (BitSize == 0)
192*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
193*9880d681SAndroid Build Coastguard Worker   // No cost model for operations on integers larger than 64 bit implemented yet.
194*9880d681SAndroid Build Coastguard Worker   if (BitSize > 64)
195*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
196*9880d681SAndroid Build Coastguard Worker 
197*9880d681SAndroid Build Coastguard Worker   switch (IID) {
198*9880d681SAndroid Build Coastguard Worker   default:
199*9880d681SAndroid Build Coastguard Worker     return TTI::TCC_Free;
200*9880d681SAndroid Build Coastguard Worker   case Intrinsic::sadd_with_overflow:
201*9880d681SAndroid Build Coastguard Worker   case Intrinsic::uadd_with_overflow:
202*9880d681SAndroid Build Coastguard Worker   case Intrinsic::ssub_with_overflow:
203*9880d681SAndroid Build Coastguard Worker   case Intrinsic::usub_with_overflow:
204*9880d681SAndroid Build Coastguard Worker     // These get expanded to include a normal addition/subtraction.
205*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
206*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(Imm.getZExtValue()))
207*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
208*9880d681SAndroid Build Coastguard Worker       if (isUInt<32>(-Imm.getSExtValue()))
209*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
210*9880d681SAndroid Build Coastguard Worker     }
211*9880d681SAndroid Build Coastguard Worker     break;
212*9880d681SAndroid Build Coastguard Worker   case Intrinsic::smul_with_overflow:
213*9880d681SAndroid Build Coastguard Worker   case Intrinsic::umul_with_overflow:
214*9880d681SAndroid Build Coastguard Worker     // These get expanded to include a normal multiplication.
215*9880d681SAndroid Build Coastguard Worker     if (Idx == 1 && Imm.getBitWidth() <= 64) {
216*9880d681SAndroid Build Coastguard Worker       if (isInt<32>(Imm.getSExtValue()))
217*9880d681SAndroid Build Coastguard Worker         return TTI::TCC_Free;
218*9880d681SAndroid Build Coastguard Worker     }
219*9880d681SAndroid Build Coastguard Worker     break;
220*9880d681SAndroid Build Coastguard Worker   case Intrinsic::experimental_stackmap:
221*9880d681SAndroid Build Coastguard Worker     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
222*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Free;
223*9880d681SAndroid Build Coastguard Worker     break;
224*9880d681SAndroid Build Coastguard Worker   case Intrinsic::experimental_patchpoint_void:
225*9880d681SAndroid Build Coastguard Worker   case Intrinsic::experimental_patchpoint_i64:
226*9880d681SAndroid Build Coastguard Worker     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
227*9880d681SAndroid Build Coastguard Worker       return TTI::TCC_Free;
228*9880d681SAndroid Build Coastguard Worker     break;
229*9880d681SAndroid Build Coastguard Worker   }
230*9880d681SAndroid Build Coastguard Worker   return SystemZTTIImpl::getIntImmCost(Imm, Ty);
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker 
233*9880d681SAndroid Build Coastguard Worker TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth)234*9880d681SAndroid Build Coastguard Worker SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) {
235*9880d681SAndroid Build Coastguard Worker   assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
236*9880d681SAndroid Build Coastguard Worker   if (ST->hasPopulationCount() && TyWidth <= 64)
237*9880d681SAndroid Build Coastguard Worker     return TTI::PSK_FastHardware;
238*9880d681SAndroid Build Coastguard Worker   return TTI::PSK_Software;
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker 
getNumberOfRegisters(bool Vector)241*9880d681SAndroid Build Coastguard Worker unsigned SystemZTTIImpl::getNumberOfRegisters(bool Vector) {
242*9880d681SAndroid Build Coastguard Worker   if (!Vector)
243*9880d681SAndroid Build Coastguard Worker     // Discount the stack pointer.  Also leave out %r0, since it can't
244*9880d681SAndroid Build Coastguard Worker     // be used in an address.
245*9880d681SAndroid Build Coastguard Worker     return 14;
246*9880d681SAndroid Build Coastguard Worker   if (ST->hasVector())
247*9880d681SAndroid Build Coastguard Worker     return 32;
248*9880d681SAndroid Build Coastguard Worker   return 0;
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker 
getRegisterBitWidth(bool Vector)251*9880d681SAndroid Build Coastguard Worker unsigned SystemZTTIImpl::getRegisterBitWidth(bool Vector) {
252*9880d681SAndroid Build Coastguard Worker   if (!Vector)
253*9880d681SAndroid Build Coastguard Worker     return 64;
254*9880d681SAndroid Build Coastguard Worker   if (ST->hasVector())
255*9880d681SAndroid Build Coastguard Worker     return 128;
256*9880d681SAndroid Build Coastguard Worker   return 0;
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker 
259