1ff0cc061SDimitry Andric //===-- SystemZTargetTransformInfo.cpp - SystemZ-specific TTI -------------===//
2ff0cc061SDimitry Andric //
3ff0cc061SDimitry Andric // The LLVM Compiler Infrastructure
4ff0cc061SDimitry Andric //
5ff0cc061SDimitry Andric // This file is distributed under the University of Illinois Open Source
6ff0cc061SDimitry Andric // License. See LICENSE.TXT for details.
7ff0cc061SDimitry Andric //
8ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
9ff0cc061SDimitry Andric //
10ff0cc061SDimitry Andric // This file implements a TargetTransformInfo analysis pass specific to the
11ff0cc061SDimitry Andric // SystemZ target machine. It uses the target's detailed information to provide
12ff0cc061SDimitry Andric // more precise answers to certain TTI queries, while letting the target
13ff0cc061SDimitry Andric // independent and default TTI implementations handle the rest.
14ff0cc061SDimitry Andric //
15ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
16ff0cc061SDimitry Andric
17ff0cc061SDimitry Andric #include "SystemZTargetTransformInfo.h"
18ff0cc061SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
19ff0cc061SDimitry Andric #include "llvm/CodeGen/BasicTTIImpl.h"
202cab237bSDimitry Andric #include "llvm/CodeGen/CostTable.h"
212cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
22ff0cc061SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
23ff0cc061SDimitry Andric #include "llvm/Support/Debug.h"
24ff0cc061SDimitry Andric using namespace llvm;
25ff0cc061SDimitry Andric
26ff0cc061SDimitry Andric #define DEBUG_TYPE "systemztti"
27ff0cc061SDimitry Andric
28ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
29ff0cc061SDimitry Andric //
30ff0cc061SDimitry Andric // SystemZ cost model.
31ff0cc061SDimitry Andric //
32ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
33ff0cc061SDimitry Andric
getIntImmCost(const APInt & Imm,Type * Ty)347d523365SDimitry Andric int SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
35ff0cc061SDimitry Andric assert(Ty->isIntegerTy());
36ff0cc061SDimitry Andric
37ff0cc061SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits();
38ff0cc061SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free
39ff0cc061SDimitry Andric // here, so that constant hoisting will ignore this constant.
40ff0cc061SDimitry Andric if (BitSize == 0)
41ff0cc061SDimitry Andric return TTI::TCC_Free;
42ff0cc061SDimitry Andric // No cost model for operations on integers larger than 64 bit implemented yet.
43ff0cc061SDimitry Andric if (BitSize > 64)
44ff0cc061SDimitry Andric return TTI::TCC_Free;
45ff0cc061SDimitry Andric
46ff0cc061SDimitry Andric if (Imm == 0)
47ff0cc061SDimitry Andric return TTI::TCC_Free;
48ff0cc061SDimitry Andric
49ff0cc061SDimitry Andric if (Imm.getBitWidth() <= 64) {
50ff0cc061SDimitry Andric // Constants loaded via lgfi.
51ff0cc061SDimitry Andric if (isInt<32>(Imm.getSExtValue()))
52ff0cc061SDimitry Andric return TTI::TCC_Basic;
53ff0cc061SDimitry Andric // Constants loaded via llilf.
54ff0cc061SDimitry Andric if (isUInt<32>(Imm.getZExtValue()))
55ff0cc061SDimitry Andric return TTI::TCC_Basic;
56ff0cc061SDimitry Andric // Constants loaded via llihf:
57ff0cc061SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0)
58ff0cc061SDimitry Andric return TTI::TCC_Basic;
59ff0cc061SDimitry Andric
60ff0cc061SDimitry Andric return 2 * TTI::TCC_Basic;
61ff0cc061SDimitry Andric }
62ff0cc061SDimitry Andric
63ff0cc061SDimitry Andric return 4 * TTI::TCC_Basic;
64ff0cc061SDimitry Andric }
65ff0cc061SDimitry Andric
getIntImmCost(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty)667d523365SDimitry Andric int SystemZTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx,
67ff0cc061SDimitry Andric const APInt &Imm, Type *Ty) {
68ff0cc061SDimitry Andric assert(Ty->isIntegerTy());
69ff0cc061SDimitry Andric
70ff0cc061SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits();
71ff0cc061SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free
72ff0cc061SDimitry Andric // here, so that constant hoisting will ignore this constant.
73ff0cc061SDimitry Andric if (BitSize == 0)
74ff0cc061SDimitry Andric return TTI::TCC_Free;
75ff0cc061SDimitry Andric // No cost model for operations on integers larger than 64 bit implemented yet.
76ff0cc061SDimitry Andric if (BitSize > 64)
77ff0cc061SDimitry Andric return TTI::TCC_Free;
78ff0cc061SDimitry Andric
79ff0cc061SDimitry Andric switch (Opcode) {
80ff0cc061SDimitry Andric default:
81ff0cc061SDimitry Andric return TTI::TCC_Free;
82ff0cc061SDimitry Andric case Instruction::GetElementPtr:
83ff0cc061SDimitry Andric // Always hoist the base address of a GetElementPtr. This prevents the
84ff0cc061SDimitry Andric // creation of new constants for every base constant that gets constant
85ff0cc061SDimitry Andric // folded with the offset.
86ff0cc061SDimitry Andric if (Idx == 0)
87ff0cc061SDimitry Andric return 2 * TTI::TCC_Basic;
88ff0cc061SDimitry Andric return TTI::TCC_Free;
89ff0cc061SDimitry Andric case Instruction::Store:
90ff0cc061SDimitry Andric if (Idx == 0 && Imm.getBitWidth() <= 64) {
91ff0cc061SDimitry Andric // Any 8-bit immediate store can by implemented via mvi.
92ff0cc061SDimitry Andric if (BitSize == 8)
93ff0cc061SDimitry Andric return TTI::TCC_Free;
94ff0cc061SDimitry Andric // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi.
95ff0cc061SDimitry Andric if (isInt<16>(Imm.getSExtValue()))
96ff0cc061SDimitry Andric return TTI::TCC_Free;
97ff0cc061SDimitry Andric }
98ff0cc061SDimitry Andric break;
99ff0cc061SDimitry Andric case Instruction::ICmp:
100ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
101ff0cc061SDimitry Andric // Comparisons against signed 32-bit immediates implemented via cgfi.
102ff0cc061SDimitry Andric if (isInt<32>(Imm.getSExtValue()))
103ff0cc061SDimitry Andric return TTI::TCC_Free;
104ff0cc061SDimitry Andric // Comparisons against unsigned 32-bit immediates implemented via clgfi.
105ff0cc061SDimitry Andric if (isUInt<32>(Imm.getZExtValue()))
106ff0cc061SDimitry Andric return TTI::TCC_Free;
107ff0cc061SDimitry Andric }
108ff0cc061SDimitry Andric break;
109ff0cc061SDimitry Andric case Instruction::Add:
110ff0cc061SDimitry Andric case Instruction::Sub:
111ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
112ff0cc061SDimitry Andric // We use algfi/slgfi to add/subtract 32-bit unsigned immediates.
113ff0cc061SDimitry Andric if (isUInt<32>(Imm.getZExtValue()))
114ff0cc061SDimitry Andric return TTI::TCC_Free;
115ff0cc061SDimitry Andric // Or their negation, by swapping addition vs. subtraction.
116ff0cc061SDimitry Andric if (isUInt<32>(-Imm.getSExtValue()))
117ff0cc061SDimitry Andric return TTI::TCC_Free;
118ff0cc061SDimitry Andric }
119ff0cc061SDimitry Andric break;
120ff0cc061SDimitry Andric case Instruction::Mul:
121ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
122ff0cc061SDimitry Andric // We use msgfi to multiply by 32-bit signed immediates.
123ff0cc061SDimitry Andric if (isInt<32>(Imm.getSExtValue()))
124ff0cc061SDimitry Andric return TTI::TCC_Free;
125ff0cc061SDimitry Andric }
126ff0cc061SDimitry Andric break;
127ff0cc061SDimitry Andric case Instruction::Or:
128ff0cc061SDimitry Andric case Instruction::Xor:
129ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
130ff0cc061SDimitry Andric // Masks supported by oilf/xilf.
131ff0cc061SDimitry Andric if (isUInt<32>(Imm.getZExtValue()))
132ff0cc061SDimitry Andric return TTI::TCC_Free;
133ff0cc061SDimitry Andric // Masks supported by oihf/xihf.
134ff0cc061SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0)
135ff0cc061SDimitry Andric return TTI::TCC_Free;
136ff0cc061SDimitry Andric }
137ff0cc061SDimitry Andric break;
138ff0cc061SDimitry Andric case Instruction::And:
139ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
140ff0cc061SDimitry Andric // Any 32-bit AND operation can by implemented via nilf.
141ff0cc061SDimitry Andric if (BitSize <= 32)
142ff0cc061SDimitry Andric return TTI::TCC_Free;
143ff0cc061SDimitry Andric // 64-bit masks supported by nilf.
144ff0cc061SDimitry Andric if (isUInt<32>(~Imm.getZExtValue()))
145ff0cc061SDimitry Andric return TTI::TCC_Free;
146ff0cc061SDimitry Andric // 64-bit masks supported by nilh.
147ff0cc061SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff)
148ff0cc061SDimitry Andric return TTI::TCC_Free;
149ff0cc061SDimitry Andric // Some 64-bit AND operations can be implemented via risbg.
150ff0cc061SDimitry Andric const SystemZInstrInfo *TII = ST->getInstrInfo();
151ff0cc061SDimitry Andric unsigned Start, End;
152ff0cc061SDimitry Andric if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End))
153ff0cc061SDimitry Andric return TTI::TCC_Free;
154ff0cc061SDimitry Andric }
155ff0cc061SDimitry Andric break;
156ff0cc061SDimitry Andric case Instruction::Shl:
157ff0cc061SDimitry Andric case Instruction::LShr:
158ff0cc061SDimitry Andric case Instruction::AShr:
159ff0cc061SDimitry Andric // Always return TCC_Free for the shift value of a shift instruction.
160ff0cc061SDimitry Andric if (Idx == 1)
161ff0cc061SDimitry Andric return TTI::TCC_Free;
162ff0cc061SDimitry Andric break;
163ff0cc061SDimitry Andric case Instruction::UDiv:
164ff0cc061SDimitry Andric case Instruction::SDiv:
165ff0cc061SDimitry Andric case Instruction::URem:
166ff0cc061SDimitry Andric case Instruction::SRem:
167ff0cc061SDimitry Andric case Instruction::Trunc:
168ff0cc061SDimitry Andric case Instruction::ZExt:
169ff0cc061SDimitry Andric case Instruction::SExt:
170ff0cc061SDimitry Andric case Instruction::IntToPtr:
171ff0cc061SDimitry Andric case Instruction::PtrToInt:
172ff0cc061SDimitry Andric case Instruction::BitCast:
173ff0cc061SDimitry Andric case Instruction::PHI:
174ff0cc061SDimitry Andric case Instruction::Call:
175ff0cc061SDimitry Andric case Instruction::Select:
176ff0cc061SDimitry Andric case Instruction::Ret:
177ff0cc061SDimitry Andric case Instruction::Load:
178ff0cc061SDimitry Andric break;
179ff0cc061SDimitry Andric }
180ff0cc061SDimitry Andric
181ff0cc061SDimitry Andric return SystemZTTIImpl::getIntImmCost(Imm, Ty);
182ff0cc061SDimitry Andric }
183ff0cc061SDimitry Andric
getIntImmCost(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty)1847d523365SDimitry Andric int SystemZTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
185ff0cc061SDimitry Andric const APInt &Imm, Type *Ty) {
186ff0cc061SDimitry Andric assert(Ty->isIntegerTy());
187ff0cc061SDimitry Andric
188ff0cc061SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits();
189ff0cc061SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free
190ff0cc061SDimitry Andric // here, so that constant hoisting will ignore this constant.
191ff0cc061SDimitry Andric if (BitSize == 0)
192ff0cc061SDimitry Andric return TTI::TCC_Free;
193ff0cc061SDimitry Andric // No cost model for operations on integers larger than 64 bit implemented yet.
194ff0cc061SDimitry Andric if (BitSize > 64)
195ff0cc061SDimitry Andric return TTI::TCC_Free;
196ff0cc061SDimitry Andric
197ff0cc061SDimitry Andric switch (IID) {
198ff0cc061SDimitry Andric default:
199ff0cc061SDimitry Andric return TTI::TCC_Free;
200ff0cc061SDimitry Andric case Intrinsic::sadd_with_overflow:
201ff0cc061SDimitry Andric case Intrinsic::uadd_with_overflow:
202ff0cc061SDimitry Andric case Intrinsic::ssub_with_overflow:
203ff0cc061SDimitry Andric case Intrinsic::usub_with_overflow:
204ff0cc061SDimitry Andric // These get expanded to include a normal addition/subtraction.
205ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
206ff0cc061SDimitry Andric if (isUInt<32>(Imm.getZExtValue()))
207ff0cc061SDimitry Andric return TTI::TCC_Free;
208ff0cc061SDimitry Andric if (isUInt<32>(-Imm.getSExtValue()))
209ff0cc061SDimitry Andric return TTI::TCC_Free;
210ff0cc061SDimitry Andric }
211ff0cc061SDimitry Andric break;
212ff0cc061SDimitry Andric case Intrinsic::smul_with_overflow:
213ff0cc061SDimitry Andric case Intrinsic::umul_with_overflow:
214ff0cc061SDimitry Andric // These get expanded to include a normal multiplication.
215ff0cc061SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) {
216ff0cc061SDimitry Andric if (isInt<32>(Imm.getSExtValue()))
217ff0cc061SDimitry Andric return TTI::TCC_Free;
218ff0cc061SDimitry Andric }
219ff0cc061SDimitry Andric break;
220ff0cc061SDimitry Andric case Intrinsic::experimental_stackmap:
221ff0cc061SDimitry Andric if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
222ff0cc061SDimitry Andric return TTI::TCC_Free;
223ff0cc061SDimitry Andric break;
224ff0cc061SDimitry Andric case Intrinsic::experimental_patchpoint_void:
225ff0cc061SDimitry Andric case Intrinsic::experimental_patchpoint_i64:
226ff0cc061SDimitry Andric if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
227ff0cc061SDimitry Andric return TTI::TCC_Free;
228ff0cc061SDimitry Andric break;
229ff0cc061SDimitry Andric }
230ff0cc061SDimitry Andric return SystemZTTIImpl::getIntImmCost(Imm, Ty);
231ff0cc061SDimitry Andric }
232ff0cc061SDimitry Andric
233ff0cc061SDimitry Andric TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth)234ff0cc061SDimitry Andric SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) {
235ff0cc061SDimitry Andric assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
236ff0cc061SDimitry Andric if (ST->hasPopulationCount() && TyWidth <= 64)
237ff0cc061SDimitry Andric return TTI::PSK_FastHardware;
238ff0cc061SDimitry Andric return TTI::PSK_Software;
239ff0cc061SDimitry Andric }
240ff0cc061SDimitry Andric
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP)241a580b014SDimitry Andric void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
242d88c1a5aSDimitry Andric TTI::UnrollingPreferences &UP) {
243d88c1a5aSDimitry Andric // Find out if L contains a call, what the machine instruction count
244d88c1a5aSDimitry Andric // estimate is, and how many stores there are.
245d88c1a5aSDimitry Andric bool HasCall = false;
246d88c1a5aSDimitry Andric unsigned NumStores = 0;
247d88c1a5aSDimitry Andric for (auto &BB : L->blocks())
248d88c1a5aSDimitry Andric for (auto &I : *BB) {
249d88c1a5aSDimitry Andric if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) {
250d88c1a5aSDimitry Andric ImmutableCallSite CS(&I);
251d88c1a5aSDimitry Andric if (const Function *F = CS.getCalledFunction()) {
252d88c1a5aSDimitry Andric if (isLoweredToCall(F))
253d88c1a5aSDimitry Andric HasCall = true;
254d88c1a5aSDimitry Andric if (F->getIntrinsicID() == Intrinsic::memcpy ||
255d88c1a5aSDimitry Andric F->getIntrinsicID() == Intrinsic::memset)
256d88c1a5aSDimitry Andric NumStores++;
257d88c1a5aSDimitry Andric } else { // indirect call.
258d88c1a5aSDimitry Andric HasCall = true;
259d88c1a5aSDimitry Andric }
260d88c1a5aSDimitry Andric }
261d88c1a5aSDimitry Andric if (isa<StoreInst>(&I)) {
262d88c1a5aSDimitry Andric Type *MemAccessTy = I.getOperand(0)->getType();
2637a7e6055SDimitry Andric NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy, 0, 0);
264d88c1a5aSDimitry Andric }
265d88c1a5aSDimitry Andric }
266d88c1a5aSDimitry Andric
267d88c1a5aSDimitry Andric // The z13 processor will run out of store tags if too many stores
268d88c1a5aSDimitry Andric // are fed into it too quickly. Therefore make sure there are not
269d88c1a5aSDimitry Andric // too many stores in the resulting unrolled loop.
270d88c1a5aSDimitry Andric unsigned const Max = (NumStores ? (12 / NumStores) : UINT_MAX);
271d88c1a5aSDimitry Andric
272d88c1a5aSDimitry Andric if (HasCall) {
273d88c1a5aSDimitry Andric // Only allow full unrolling if loop has any calls.
274d88c1a5aSDimitry Andric UP.FullUnrollMaxCount = Max;
275d88c1a5aSDimitry Andric UP.MaxCount = 1;
276d88c1a5aSDimitry Andric return;
277d88c1a5aSDimitry Andric }
278d88c1a5aSDimitry Andric
279d88c1a5aSDimitry Andric UP.MaxCount = Max;
280d88c1a5aSDimitry Andric if (UP.MaxCount <= 1)
281d88c1a5aSDimitry Andric return;
282d88c1a5aSDimitry Andric
283d88c1a5aSDimitry Andric // Allow partial and runtime trip count unrolling.
284d88c1a5aSDimitry Andric UP.Partial = UP.Runtime = true;
285d88c1a5aSDimitry Andric
286d88c1a5aSDimitry Andric UP.PartialThreshold = 75;
287d88c1a5aSDimitry Andric UP.DefaultUnrollRuntimeCount = 4;
288d88c1a5aSDimitry Andric
289d88c1a5aSDimitry Andric // Allow expensive instructions in the pre-header of the loop.
290d88c1a5aSDimitry Andric UP.AllowExpensiveTripCount = true;
291d88c1a5aSDimitry Andric
292d88c1a5aSDimitry Andric UP.Force = true;
293d88c1a5aSDimitry Andric }
294d88c1a5aSDimitry Andric
2952cab237bSDimitry Andric
isLSRCostLess(TargetTransformInfo::LSRCost & C1,TargetTransformInfo::LSRCost & C2)2962cab237bSDimitry Andric bool SystemZTTIImpl::isLSRCostLess(TargetTransformInfo::LSRCost &C1,
2972cab237bSDimitry Andric TargetTransformInfo::LSRCost &C2) {
2982cab237bSDimitry Andric // SystemZ specific: check instruction count (first), and don't care about
2992cab237bSDimitry Andric // ImmCost, since offsets are checked explicitly.
3002cab237bSDimitry Andric return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
3012cab237bSDimitry Andric C1.NumIVMuls, C1.NumBaseAdds,
3022cab237bSDimitry Andric C1.ScaleCost, C1.SetupCost) <
3032cab237bSDimitry Andric std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
3042cab237bSDimitry Andric C2.NumIVMuls, C2.NumBaseAdds,
3052cab237bSDimitry Andric C2.ScaleCost, C2.SetupCost);
3062cab237bSDimitry Andric }
3072cab237bSDimitry Andric
getNumberOfRegisters(bool Vector)308ff0cc061SDimitry Andric unsigned SystemZTTIImpl::getNumberOfRegisters(bool Vector) {
309ff0cc061SDimitry Andric if (!Vector)
310ff0cc061SDimitry Andric // Discount the stack pointer. Also leave out %r0, since it can't
311ff0cc061SDimitry Andric // be used in an address.
312ff0cc061SDimitry Andric return 14;
313ff0cc061SDimitry Andric if (ST->hasVector())
314ff0cc061SDimitry Andric return 32;
315ff0cc061SDimitry Andric return 0;
316ff0cc061SDimitry Andric }
317ff0cc061SDimitry Andric
getRegisterBitWidth(bool Vector) const31824d58133SDimitry Andric unsigned SystemZTTIImpl::getRegisterBitWidth(bool Vector) const {
319ff0cc061SDimitry Andric if (!Vector)
320ff0cc061SDimitry Andric return 64;
321ff0cc061SDimitry Andric if (ST->hasVector())
322ff0cc061SDimitry Andric return 128;
323ff0cc061SDimitry Andric return 0;
324ff0cc061SDimitry Andric }
325ff0cc061SDimitry Andric
hasDivRemOp(Type * DataType,bool IsSigned)3262cab237bSDimitry Andric bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
3272cab237bSDimitry Andric EVT VT = TLI->getValueType(DL, DataType);
3282cab237bSDimitry Andric return (VT.isScalarInteger() && TLI->isTypeLegal(VT));
3292cab237bSDimitry Andric }
3302cab237bSDimitry Andric
331*b5893f02SDimitry Andric // Return the bit size for the scalar type or vector element
332*b5893f02SDimitry Andric // type. getScalarSizeInBits() returns 0 for a pointer type.
getScalarSizeInBits(Type * Ty)333*b5893f02SDimitry Andric static unsigned getScalarSizeInBits(Type *Ty) {
334*b5893f02SDimitry Andric unsigned Size =
335*b5893f02SDimitry Andric (Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits());
336*b5893f02SDimitry Andric assert(Size > 0 && "Element must have non-zero size.");
337*b5893f02SDimitry Andric return Size;
338*b5893f02SDimitry Andric }
339*b5893f02SDimitry Andric
340*b5893f02SDimitry Andric // getNumberOfParts() calls getTypeLegalizationCost() which splits the vector
341*b5893f02SDimitry Andric // type until it is legal. This would e.g. return 4 for <6 x i64>, instead of
342*b5893f02SDimitry Andric // 3.
getNumVectorRegs(Type * Ty)343*b5893f02SDimitry Andric static unsigned getNumVectorRegs(Type *Ty) {
344*b5893f02SDimitry Andric assert(Ty->isVectorTy() && "Expected vector type");
345*b5893f02SDimitry Andric unsigned WideBits = getScalarSizeInBits(Ty) * Ty->getVectorNumElements();
346*b5893f02SDimitry Andric assert(WideBits > 0 && "Could not compute size of vector");
347*b5893f02SDimitry Andric return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U));
348*b5893f02SDimitry Andric }
349*b5893f02SDimitry Andric
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::OperandValueKind Op1Info,TTI::OperandValueKind Op2Info,TTI::OperandValueProperties Opd1PropInfo,TTI::OperandValueProperties Opd2PropInfo,ArrayRef<const Value * > Args)3507a7e6055SDimitry Andric int SystemZTTIImpl::getArithmeticInstrCost(
3517a7e6055SDimitry Andric unsigned Opcode, Type *Ty,
3527a7e6055SDimitry Andric TTI::OperandValueKind Op1Info, TTI::OperandValueKind Op2Info,
3537a7e6055SDimitry Andric TTI::OperandValueProperties Opd1PropInfo,
3547a7e6055SDimitry Andric TTI::OperandValueProperties Opd2PropInfo,
3557a7e6055SDimitry Andric ArrayRef<const Value *> Args) {
3567a7e6055SDimitry Andric
3577a7e6055SDimitry Andric // TODO: return a good value for BB-VECTORIZER that includes the
3587a7e6055SDimitry Andric // immediate loads, which we do not want to count for the loop
3597a7e6055SDimitry Andric // vectorizer, since they are hopefully hoisted out of the loop. This
3607a7e6055SDimitry Andric // would require a new parameter 'InLoop', but not sure if constant
3617a7e6055SDimitry Andric // args are common enough to motivate this.
3627a7e6055SDimitry Andric
3637a7e6055SDimitry Andric unsigned ScalarBits = Ty->getScalarSizeInBits();
3647a7e6055SDimitry Andric
365*b5893f02SDimitry Andric // There are thre cases of division and remainder: Dividing with a register
366*b5893f02SDimitry Andric // needs a divide instruction. A divisor which is a power of two constant
367*b5893f02SDimitry Andric // can be implemented with a sequence of shifts. Any other constant needs a
368*b5893f02SDimitry Andric // multiply and shifts.
369*b5893f02SDimitry Andric const unsigned DivInstrCost = 20;
370*b5893f02SDimitry Andric const unsigned DivMulSeqCost = 10;
371*b5893f02SDimitry Andric const unsigned SDivPow2Cost = 4;
372*b5893f02SDimitry Andric
373*b5893f02SDimitry Andric bool SignedDivRem =
374*b5893f02SDimitry Andric Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
375*b5893f02SDimitry Andric bool UnsignedDivRem =
376*b5893f02SDimitry Andric Opcode == Instruction::UDiv || Opcode == Instruction::URem;
377*b5893f02SDimitry Andric
378*b5893f02SDimitry Andric // Check for a constant divisor.
379*b5893f02SDimitry Andric bool DivRemConst = false;
380*b5893f02SDimitry Andric bool DivRemConstPow2 = false;
381*b5893f02SDimitry Andric if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) {
38260ff8e32SDimitry Andric if (const Constant *C = dyn_cast<Constant>(Args[1])) {
383*b5893f02SDimitry Andric const ConstantInt *CVal =
384*b5893f02SDimitry Andric (C->getType()->isVectorTy()
385*b5893f02SDimitry Andric ? dyn_cast_or_null<const ConstantInt>(C->getSplatValue())
386*b5893f02SDimitry Andric : dyn_cast<const ConstantInt>(C));
387*b5893f02SDimitry Andric if (CVal != nullptr &&
388*b5893f02SDimitry Andric (CVal->getValue().isPowerOf2() || (-CVal->getValue()).isPowerOf2()))
389*b5893f02SDimitry Andric DivRemConstPow2 = true;
39060ff8e32SDimitry Andric else
391*b5893f02SDimitry Andric DivRemConst = true;
39260ff8e32SDimitry Andric }
39360ff8e32SDimitry Andric }
39460ff8e32SDimitry Andric
3957a7e6055SDimitry Andric if (Ty->isVectorTy()) {
396*b5893f02SDimitry Andric assert(ST->hasVector() &&
397*b5893f02SDimitry Andric "getArithmeticInstrCost() called with vector type.");
3987a7e6055SDimitry Andric unsigned VF = Ty->getVectorNumElements();
399*b5893f02SDimitry Andric unsigned NumVectors = getNumVectorRegs(Ty);
4007a7e6055SDimitry Andric
4017a7e6055SDimitry Andric // These vector operations are custom handled, but are still supported
4027a7e6055SDimitry Andric // with one instruction per vector, regardless of element size.
4037a7e6055SDimitry Andric if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
404*b5893f02SDimitry Andric Opcode == Instruction::AShr) {
4057a7e6055SDimitry Andric return NumVectors;
4067a7e6055SDimitry Andric }
4077a7e6055SDimitry Andric
408*b5893f02SDimitry Andric if (DivRemConstPow2)
409*b5893f02SDimitry Andric return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1));
410*b5893f02SDimitry Andric if (DivRemConst)
411*b5893f02SDimitry Andric return VF * DivMulSeqCost + getScalarizationOverhead(Ty, Args);
412*b5893f02SDimitry Andric if ((SignedDivRem || UnsignedDivRem) && VF > 4)
413*b5893f02SDimitry Andric // Temporary hack: disable high vectorization factors with integer
414*b5893f02SDimitry Andric // division/remainder, which will get scalarized and handled with
415*b5893f02SDimitry Andric // GR128 registers. The mischeduler is not clever enough to avoid
416*b5893f02SDimitry Andric // spilling yet.
417*b5893f02SDimitry Andric return 1000;
41860ff8e32SDimitry Andric
4197a7e6055SDimitry Andric // These FP operations are supported with a single vector instruction for
4207a7e6055SDimitry Andric // double (base implementation assumes float generally costs 2). For
4217a7e6055SDimitry Andric // FP128, the scalar cost is 1, and there is no overhead since the values
4227a7e6055SDimitry Andric // are already in scalar registers.
4237a7e6055SDimitry Andric if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
4247a7e6055SDimitry Andric Opcode == Instruction::FMul || Opcode == Instruction::FDiv) {
4257a7e6055SDimitry Andric switch (ScalarBits) {
4267a7e6055SDimitry Andric case 32: {
427b40b48b8SDimitry Andric // The vector enhancements facility 1 provides v4f32 instructions.
428b40b48b8SDimitry Andric if (ST->hasVectorEnhancements1())
429b40b48b8SDimitry Andric return NumVectors;
4307a7e6055SDimitry Andric // Return the cost of multiple scalar invocation plus the cost of
4317a7e6055SDimitry Andric // inserting and extracting the values.
432*b5893f02SDimitry Andric unsigned ScalarCost =
433*b5893f02SDimitry Andric getArithmeticInstrCost(Opcode, Ty->getScalarType());
4347a7e6055SDimitry Andric unsigned Cost = (VF * ScalarCost) + getScalarizationOverhead(Ty, Args);
4357a7e6055SDimitry Andric // FIXME: VF 2 for these FP operations are currently just as
4367a7e6055SDimitry Andric // expensive as for VF 4.
4377a7e6055SDimitry Andric if (VF == 2)
4387a7e6055SDimitry Andric Cost *= 2;
4397a7e6055SDimitry Andric return Cost;
4407a7e6055SDimitry Andric }
4417a7e6055SDimitry Andric case 64:
4427a7e6055SDimitry Andric case 128:
4437a7e6055SDimitry Andric return NumVectors;
4447a7e6055SDimitry Andric default:
4457a7e6055SDimitry Andric break;
4467a7e6055SDimitry Andric }
4477a7e6055SDimitry Andric }
4487a7e6055SDimitry Andric
4497a7e6055SDimitry Andric // There is no native support for FRem.
4507a7e6055SDimitry Andric if (Opcode == Instruction::FRem) {
4517a7e6055SDimitry Andric unsigned Cost = (VF * LIBCALL_COST) + getScalarizationOverhead(Ty, Args);
4527a7e6055SDimitry Andric // FIXME: VF 2 for float is currently just as expensive as for VF 4.
4537a7e6055SDimitry Andric if (VF == 2 && ScalarBits == 32)
4547a7e6055SDimitry Andric Cost *= 2;
4557a7e6055SDimitry Andric return Cost;
4567a7e6055SDimitry Andric }
4577a7e6055SDimitry Andric }
4587a7e6055SDimitry Andric else { // Scalar:
4597a7e6055SDimitry Andric // These FP operations are supported with a dedicated instruction for
4607a7e6055SDimitry Andric // float, double and fp128 (base implementation assumes float generally
4617a7e6055SDimitry Andric // costs 2).
4627a7e6055SDimitry Andric if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
4637a7e6055SDimitry Andric Opcode == Instruction::FMul || Opcode == Instruction::FDiv)
4647a7e6055SDimitry Andric return 1;
4657a7e6055SDimitry Andric
4667a7e6055SDimitry Andric // There is no native support for FRem.
4677a7e6055SDimitry Andric if (Opcode == Instruction::FRem)
4687a7e6055SDimitry Andric return LIBCALL_COST;
4697a7e6055SDimitry Andric
4707a7e6055SDimitry Andric // Or requires one instruction, although it has custom handling for i64.
4717a7e6055SDimitry Andric if (Opcode == Instruction::Or)
4727a7e6055SDimitry Andric return 1;
4737a7e6055SDimitry Andric
474*b5893f02SDimitry Andric if (Opcode == Instruction::Xor && ScalarBits == 1) {
475*b5893f02SDimitry Andric if (ST->hasLoadStoreOnCond2())
476*b5893f02SDimitry Andric return 5; // 2 * (li 0; loc 1); xor
477*b5893f02SDimitry Andric return 7; // 2 * ipm sequences ; xor ; shift ; compare
478*b5893f02SDimitry Andric }
4797a7e6055SDimitry Andric
480*b5893f02SDimitry Andric if (DivRemConstPow2)
481*b5893f02SDimitry Andric return (SignedDivRem ? SDivPow2Cost : 1);
482*b5893f02SDimitry Andric if (DivRemConst)
483*b5893f02SDimitry Andric return DivMulSeqCost;
484*b5893f02SDimitry Andric if (SignedDivRem || UnsignedDivRem)
485*b5893f02SDimitry Andric return DivInstrCost;
4867a7e6055SDimitry Andric }
4877a7e6055SDimitry Andric
4887a7e6055SDimitry Andric // Fallback to the default implementation.
4897a7e6055SDimitry Andric return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
4907a7e6055SDimitry Andric Opd1PropInfo, Opd2PropInfo, Args);
4917a7e6055SDimitry Andric }
4927a7e6055SDimitry Andric
getShuffleCost(TTI::ShuffleKind Kind,Type * Tp,int Index,Type * SubTp)4937a7e6055SDimitry Andric int SystemZTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
4947a7e6055SDimitry Andric Type *SubTp) {
4957a7e6055SDimitry Andric assert (Tp->isVectorTy());
4967a7e6055SDimitry Andric assert (ST->hasVector() && "getShuffleCost() called.");
497*b5893f02SDimitry Andric unsigned NumVectors = getNumVectorRegs(Tp);
4987a7e6055SDimitry Andric
4997a7e6055SDimitry Andric // TODO: Since fp32 is expanded, the shuffle cost should always be 0.
5007a7e6055SDimitry Andric
5017a7e6055SDimitry Andric // FP128 values are always in scalar registers, so there is no work
5027a7e6055SDimitry Andric // involved with a shuffle, except for broadcast. In that case register
5037a7e6055SDimitry Andric // moves are done with a single instruction per element.
5047a7e6055SDimitry Andric if (Tp->getScalarType()->isFP128Ty())
5057a7e6055SDimitry Andric return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0);
5067a7e6055SDimitry Andric
5077a7e6055SDimitry Andric switch (Kind) {
5087a7e6055SDimitry Andric case TargetTransformInfo::SK_ExtractSubvector:
5097a7e6055SDimitry Andric // ExtractSubvector Index indicates start offset.
5107a7e6055SDimitry Andric
5117a7e6055SDimitry Andric // Extracting a subvector from first index is a noop.
5127a7e6055SDimitry Andric return (Index == 0 ? 0 : NumVectors);
5137a7e6055SDimitry Andric
5147a7e6055SDimitry Andric case TargetTransformInfo::SK_Broadcast:
5157a7e6055SDimitry Andric // Loop vectorizer calls here to figure out the extra cost of
5167a7e6055SDimitry Andric // broadcasting a loaded value to all elements of a vector. Since vlrep
5177a7e6055SDimitry Andric // loads and replicates with a single instruction, adjust the returned
5187a7e6055SDimitry Andric // value.
5197a7e6055SDimitry Andric return NumVectors - 1;
5207a7e6055SDimitry Andric
5217a7e6055SDimitry Andric default:
5227a7e6055SDimitry Andric
5237a7e6055SDimitry Andric // SystemZ supports single instruction permutation / replication.
5247a7e6055SDimitry Andric return NumVectors;
5257a7e6055SDimitry Andric }
5267a7e6055SDimitry Andric
5277a7e6055SDimitry Andric return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
5287a7e6055SDimitry Andric }
5297a7e6055SDimitry Andric
5307a7e6055SDimitry Andric // Return the log2 difference of the element sizes of the two vector types.
getElSizeLog2Diff(Type * Ty0,Type * Ty1)5317a7e6055SDimitry Andric static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) {
5327a7e6055SDimitry Andric unsigned Bits0 = Ty0->getScalarSizeInBits();
5337a7e6055SDimitry Andric unsigned Bits1 = Ty1->getScalarSizeInBits();
5347a7e6055SDimitry Andric
5357a7e6055SDimitry Andric if (Bits1 > Bits0)
5367a7e6055SDimitry Andric return (Log2_32(Bits1) - Log2_32(Bits0));
5377a7e6055SDimitry Andric
5387a7e6055SDimitry Andric return (Log2_32(Bits0) - Log2_32(Bits1));
5397a7e6055SDimitry Andric }
5407a7e6055SDimitry Andric
5417a7e6055SDimitry Andric // Return the number of instructions needed to truncate SrcTy to DstTy.
5427a7e6055SDimitry Andric unsigned SystemZTTIImpl::
getVectorTruncCost(Type * SrcTy,Type * DstTy)5437a7e6055SDimitry Andric getVectorTruncCost(Type *SrcTy, Type *DstTy) {
5447a7e6055SDimitry Andric assert (SrcTy->isVectorTy() && DstTy->isVectorTy());
5457a7e6055SDimitry Andric assert (SrcTy->getPrimitiveSizeInBits() > DstTy->getPrimitiveSizeInBits() &&
5467a7e6055SDimitry Andric "Packing must reduce size of vector type.");
5477a7e6055SDimitry Andric assert (SrcTy->getVectorNumElements() == DstTy->getVectorNumElements() &&
5487a7e6055SDimitry Andric "Packing should not change number of elements.");
5497a7e6055SDimitry Andric
5507a7e6055SDimitry Andric // TODO: Since fp32 is expanded, the extract cost should always be 0.
5517a7e6055SDimitry Andric
552*b5893f02SDimitry Andric unsigned NumParts = getNumVectorRegs(SrcTy);
5537a7e6055SDimitry Andric if (NumParts <= 2)
5547a7e6055SDimitry Andric // Up to 2 vector registers can be truncated efficiently with pack or
5557a7e6055SDimitry Andric // permute. The latter requires an immediate mask to be loaded, which
5567a7e6055SDimitry Andric // typically gets hoisted out of a loop. TODO: return a good value for
5577a7e6055SDimitry Andric // BB-VECTORIZER that includes the immediate loads, which we do not want
5587a7e6055SDimitry Andric // to count for the loop vectorizer.
5597a7e6055SDimitry Andric return 1;
5607a7e6055SDimitry Andric
5617a7e6055SDimitry Andric unsigned Cost = 0;
5627a7e6055SDimitry Andric unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
5637a7e6055SDimitry Andric unsigned VF = SrcTy->getVectorNumElements();
5647a7e6055SDimitry Andric for (unsigned P = 0; P < Log2Diff; ++P) {
5657a7e6055SDimitry Andric if (NumParts > 1)
5667a7e6055SDimitry Andric NumParts /= 2;
5677a7e6055SDimitry Andric Cost += NumParts;
5687a7e6055SDimitry Andric }
5697a7e6055SDimitry Andric
5707a7e6055SDimitry Andric // Currently, a general mix of permutes and pack instructions is output by
5717a7e6055SDimitry Andric // isel, which follow the cost computation above except for this case which
5727a7e6055SDimitry Andric // is one instruction less:
5737a7e6055SDimitry Andric if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 &&
5747a7e6055SDimitry Andric DstTy->getScalarSizeInBits() == 8)
5757a7e6055SDimitry Andric Cost--;
5767a7e6055SDimitry Andric
5777a7e6055SDimitry Andric return Cost;
5787a7e6055SDimitry Andric }
5797a7e6055SDimitry Andric
5807a7e6055SDimitry Andric // Return the cost of converting a vector bitmask produced by a compare
5817a7e6055SDimitry Andric // (SrcTy), to the type of the select or extend instruction (DstTy).
5827a7e6055SDimitry Andric unsigned SystemZTTIImpl::
getVectorBitmaskConversionCost(Type * SrcTy,Type * DstTy)5837a7e6055SDimitry Andric getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) {
5847a7e6055SDimitry Andric assert (SrcTy->isVectorTy() && DstTy->isVectorTy() &&
5857a7e6055SDimitry Andric "Should only be called with vector types.");
5867a7e6055SDimitry Andric
5877a7e6055SDimitry Andric unsigned PackCost = 0;
5887a7e6055SDimitry Andric unsigned SrcScalarBits = SrcTy->getScalarSizeInBits();
5897a7e6055SDimitry Andric unsigned DstScalarBits = DstTy->getScalarSizeInBits();
5907a7e6055SDimitry Andric unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
5917a7e6055SDimitry Andric if (SrcScalarBits > DstScalarBits)
5927a7e6055SDimitry Andric // The bitmask will be truncated.
5937a7e6055SDimitry Andric PackCost = getVectorTruncCost(SrcTy, DstTy);
5947a7e6055SDimitry Andric else if (SrcScalarBits < DstScalarBits) {
595*b5893f02SDimitry Andric unsigned DstNumParts = getNumVectorRegs(DstTy);
5967a7e6055SDimitry Andric // Each vector select needs its part of the bitmask unpacked.
5977a7e6055SDimitry Andric PackCost = Log2Diff * DstNumParts;
5987a7e6055SDimitry Andric // Extra cost for moving part of mask before unpacking.
5997a7e6055SDimitry Andric PackCost += DstNumParts - 1;
6007a7e6055SDimitry Andric }
6017a7e6055SDimitry Andric
6027a7e6055SDimitry Andric return PackCost;
6037a7e6055SDimitry Andric }
6047a7e6055SDimitry Andric
6057a7e6055SDimitry Andric // Return the type of the compared operands. This is needed to compute the
6067a7e6055SDimitry Andric // cost for a Select / ZExt or SExt instruction.
getCmpOpsType(const Instruction * I,unsigned VF=1)6077a7e6055SDimitry Andric static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) {
6087a7e6055SDimitry Andric Type *OpTy = nullptr;
6097a7e6055SDimitry Andric if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0)))
6107a7e6055SDimitry Andric OpTy = CI->getOperand(0)->getType();
6117a7e6055SDimitry Andric else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0)))
612f37b6182SDimitry Andric if (LogicI->getNumOperands() == 2)
6137a7e6055SDimitry Andric if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0)))
6147a7e6055SDimitry Andric if (isa<CmpInst>(LogicI->getOperand(1)))
6157a7e6055SDimitry Andric OpTy = CI0->getOperand(0)->getType();
6167a7e6055SDimitry Andric
6177a7e6055SDimitry Andric if (OpTy != nullptr) {
6187a7e6055SDimitry Andric if (VF == 1) {
6197a7e6055SDimitry Andric assert (!OpTy->isVectorTy() && "Expected scalar type");
6207a7e6055SDimitry Andric return OpTy;
6217a7e6055SDimitry Andric }
6227a7e6055SDimitry Andric // Return the potentially vectorized type based on 'I' and 'VF'. 'I' may
6237a7e6055SDimitry Andric // be either scalar or already vectorized with a same or lesser VF.
6247a7e6055SDimitry Andric Type *ElTy = OpTy->getScalarType();
6257a7e6055SDimitry Andric return VectorType::get(ElTy, VF);
6267a7e6055SDimitry Andric }
6277a7e6055SDimitry Andric
6287a7e6055SDimitry Andric return nullptr;
6297a7e6055SDimitry Andric }
6307a7e6055SDimitry Andric
631*b5893f02SDimitry Andric // Get the cost of converting a boolean vector to a vector with same width
632*b5893f02SDimitry Andric // and element size as Dst, plus the cost of zero extending if needed.
633*b5893f02SDimitry Andric unsigned SystemZTTIImpl::
getBoolVecToIntConversionCost(unsigned Opcode,Type * Dst,const Instruction * I)634*b5893f02SDimitry Andric getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst,
635*b5893f02SDimitry Andric const Instruction *I) {
636*b5893f02SDimitry Andric assert (Dst->isVectorTy());
637*b5893f02SDimitry Andric unsigned VF = Dst->getVectorNumElements();
638*b5893f02SDimitry Andric unsigned Cost = 0;
639*b5893f02SDimitry Andric // If we know what the widths of the compared operands, get any cost of
640*b5893f02SDimitry Andric // converting it to match Dst. Otherwise assume same widths.
641*b5893f02SDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
642*b5893f02SDimitry Andric if (CmpOpTy != nullptr)
643*b5893f02SDimitry Andric Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst);
644*b5893f02SDimitry Andric if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP)
645*b5893f02SDimitry Andric // One 'vn' per dst vector with an immediate mask.
646*b5893f02SDimitry Andric Cost += getNumVectorRegs(Dst);
647*b5893f02SDimitry Andric return Cost;
648*b5893f02SDimitry Andric }
649*b5893f02SDimitry Andric
getCastInstrCost(unsigned Opcode,Type * Dst,Type * Src,const Instruction * I)6507a7e6055SDimitry Andric int SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
6517a7e6055SDimitry Andric const Instruction *I) {
6527a7e6055SDimitry Andric unsigned DstScalarBits = Dst->getScalarSizeInBits();
6537a7e6055SDimitry Andric unsigned SrcScalarBits = Src->getScalarSizeInBits();
6547a7e6055SDimitry Andric
6557a7e6055SDimitry Andric if (Src->isVectorTy()) {
6567a7e6055SDimitry Andric assert (ST->hasVector() && "getCastInstrCost() called with vector type.");
6577a7e6055SDimitry Andric assert (Dst->isVectorTy());
6587a7e6055SDimitry Andric unsigned VF = Src->getVectorNumElements();
659*b5893f02SDimitry Andric unsigned NumDstVectors = getNumVectorRegs(Dst);
660*b5893f02SDimitry Andric unsigned NumSrcVectors = getNumVectorRegs(Src);
6617a7e6055SDimitry Andric
6627a7e6055SDimitry Andric if (Opcode == Instruction::Trunc) {
6637a7e6055SDimitry Andric if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits())
6647a7e6055SDimitry Andric return 0; // Check for NOOP conversions.
6657a7e6055SDimitry Andric return getVectorTruncCost(Src, Dst);
6667a7e6055SDimitry Andric }
6677a7e6055SDimitry Andric
6687a7e6055SDimitry Andric if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
6697a7e6055SDimitry Andric if (SrcScalarBits >= 8) {
6707a7e6055SDimitry Andric // ZExt/SExt will be handled with one unpack per doubling of width.
6717a7e6055SDimitry Andric unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst);
6727a7e6055SDimitry Andric
6737a7e6055SDimitry Andric // For types that spans multiple vector registers, some additional
6747a7e6055SDimitry Andric // instructions are used to setup the unpacking.
6757a7e6055SDimitry Andric unsigned NumSrcVectorOps =
6767a7e6055SDimitry Andric (NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors)
6777a7e6055SDimitry Andric : (NumDstVectors / 2));
6787a7e6055SDimitry Andric
6797a7e6055SDimitry Andric return (NumUnpacks * NumDstVectors) + NumSrcVectorOps;
6807a7e6055SDimitry Andric }
681*b5893f02SDimitry Andric else if (SrcScalarBits == 1)
682*b5893f02SDimitry Andric return getBoolVecToIntConversionCost(Opcode, Dst, I);
6837a7e6055SDimitry Andric }
6847a7e6055SDimitry Andric
6857a7e6055SDimitry Andric if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP ||
6867a7e6055SDimitry Andric Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) {
6877a7e6055SDimitry Andric // TODO: Fix base implementation which could simplify things a bit here
6887a7e6055SDimitry Andric // (seems to miss on differentiating on scalar/vector types).
6897a7e6055SDimitry Andric
6907a7e6055SDimitry Andric // Only 64 bit vector conversions are natively supported.
691*b5893f02SDimitry Andric if (DstScalarBits == 64) {
692*b5893f02SDimitry Andric if (SrcScalarBits == 64)
6937a7e6055SDimitry Andric return NumDstVectors;
6947a7e6055SDimitry Andric
695*b5893f02SDimitry Andric if (SrcScalarBits == 1)
696*b5893f02SDimitry Andric return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors;
697*b5893f02SDimitry Andric }
698*b5893f02SDimitry Andric
6997a7e6055SDimitry Andric // Return the cost of multiple scalar invocation plus the cost of
7007a7e6055SDimitry Andric // inserting and extracting the values. Base implementation does not
7017a7e6055SDimitry Andric // realize float->int gets scalarized.
7027a7e6055SDimitry Andric unsigned ScalarCost = getCastInstrCost(Opcode, Dst->getScalarType(),
7037a7e6055SDimitry Andric Src->getScalarType());
7047a7e6055SDimitry Andric unsigned TotCost = VF * ScalarCost;
7057a7e6055SDimitry Andric bool NeedsInserts = true, NeedsExtracts = true;
7067a7e6055SDimitry Andric // FP128 registers do not get inserted or extracted.
7077a7e6055SDimitry Andric if (DstScalarBits == 128 &&
7087a7e6055SDimitry Andric (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP))
7097a7e6055SDimitry Andric NeedsInserts = false;
7107a7e6055SDimitry Andric if (SrcScalarBits == 128 &&
7117a7e6055SDimitry Andric (Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI))
7127a7e6055SDimitry Andric NeedsExtracts = false;
7137a7e6055SDimitry Andric
714*b5893f02SDimitry Andric TotCost += getScalarizationOverhead(Src, false, NeedsExtracts);
715*b5893f02SDimitry Andric TotCost += getScalarizationOverhead(Dst, NeedsInserts, false);
7167a7e6055SDimitry Andric
7177a7e6055SDimitry Andric // FIXME: VF 2 for float<->i32 is currently just as expensive as for VF 4.
7187a7e6055SDimitry Andric if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32)
7197a7e6055SDimitry Andric TotCost *= 2;
7207a7e6055SDimitry Andric
7217a7e6055SDimitry Andric return TotCost;
7227a7e6055SDimitry Andric }
7237a7e6055SDimitry Andric
7247a7e6055SDimitry Andric if (Opcode == Instruction::FPTrunc) {
7257a7e6055SDimitry Andric if (SrcScalarBits == 128) // fp128 -> double/float + inserts of elements.
7267a7e6055SDimitry Andric return VF /*ldxbr/lexbr*/ + getScalarizationOverhead(Dst, true, false);
7277a7e6055SDimitry Andric else // double -> float
7287a7e6055SDimitry Andric return VF / 2 /*vledb*/ + std::max(1U, VF / 4 /*vperm*/);
7297a7e6055SDimitry Andric }
7307a7e6055SDimitry Andric
7317a7e6055SDimitry Andric if (Opcode == Instruction::FPExt) {
7327a7e6055SDimitry Andric if (SrcScalarBits == 32 && DstScalarBits == 64) {
7337a7e6055SDimitry Andric // float -> double is very rare and currently unoptimized. Instead of
7347a7e6055SDimitry Andric // using vldeb, which can do two at a time, all conversions are
7357a7e6055SDimitry Andric // scalarized.
7367a7e6055SDimitry Andric return VF * 2;
7377a7e6055SDimitry Andric }
7387a7e6055SDimitry Andric // -> fp128. VF * lxdb/lxeb + extraction of elements.
7397a7e6055SDimitry Andric return VF + getScalarizationOverhead(Src, false, true);
7407a7e6055SDimitry Andric }
7417a7e6055SDimitry Andric }
7427a7e6055SDimitry Andric else { // Scalar
7437a7e6055SDimitry Andric assert (!Dst->isVectorTy());
7447a7e6055SDimitry Andric
745*b5893f02SDimitry Andric if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) {
746*b5893f02SDimitry Andric if (SrcScalarBits >= 32 ||
747*b5893f02SDimitry Andric (I != nullptr && isa<LoadInst>(I->getOperand(0))))
748*b5893f02SDimitry Andric return 1;
749*b5893f02SDimitry Andric return SrcScalarBits > 1 ? 2 /*i8/i16 extend*/ : 5 /*branch seq.*/;
750*b5893f02SDimitry Andric }
7517a7e6055SDimitry Andric
7527a7e6055SDimitry Andric if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
7537a7e6055SDimitry Andric Src->isIntegerTy(1)) {
754*b5893f02SDimitry Andric if (ST->hasLoadStoreOnCond2())
755*b5893f02SDimitry Andric return 2; // li 0; loc 1
756*b5893f02SDimitry Andric
7577a7e6055SDimitry Andric // This should be extension of a compare i1 result, which is done with
7587a7e6055SDimitry Andric // ipm and a varying sequence of instructions.
7597a7e6055SDimitry Andric unsigned Cost = 0;
7607a7e6055SDimitry Andric if (Opcode == Instruction::SExt)
7617a7e6055SDimitry Andric Cost = (DstScalarBits < 64 ? 3 : 4);
7627a7e6055SDimitry Andric if (Opcode == Instruction::ZExt)
7637a7e6055SDimitry Andric Cost = 3;
7647a7e6055SDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr);
7657a7e6055SDimitry Andric if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy())
7667a7e6055SDimitry Andric // If operands of an fp-type was compared, this costs +1.
7677a7e6055SDimitry Andric Cost++;
7687a7e6055SDimitry Andric return Cost;
7697a7e6055SDimitry Andric }
7707a7e6055SDimitry Andric }
7717a7e6055SDimitry Andric
7727a7e6055SDimitry Andric return BaseT::getCastInstrCost(Opcode, Dst, Src, I);
7737a7e6055SDimitry Andric }
7747a7e6055SDimitry Andric
775*b5893f02SDimitry Andric // Scalar i8 / i16 operations will typically be made after first extending
776*b5893f02SDimitry Andric // the operands to i32.
getOperandsExtensionCost(const Instruction * I)777*b5893f02SDimitry Andric static unsigned getOperandsExtensionCost(const Instruction *I) {
778*b5893f02SDimitry Andric unsigned ExtCost = 0;
779*b5893f02SDimitry Andric for (Value *Op : I->operands())
780*b5893f02SDimitry Andric // A load of i8 or i16 sign/zero extends to i32.
781*b5893f02SDimitry Andric if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op))
782*b5893f02SDimitry Andric ExtCost++;
783*b5893f02SDimitry Andric
784*b5893f02SDimitry Andric return ExtCost;
785*b5893f02SDimitry Andric }
786*b5893f02SDimitry Andric
getCmpSelInstrCost(unsigned Opcode,Type * ValTy,Type * CondTy,const Instruction * I)787*b5893f02SDimitry Andric int SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
788*b5893f02SDimitry Andric Type *CondTy, const Instruction *I) {
7897a7e6055SDimitry Andric if (ValTy->isVectorTy()) {
7907a7e6055SDimitry Andric assert (ST->hasVector() && "getCmpSelInstrCost() called with vector type.");
7917a7e6055SDimitry Andric unsigned VF = ValTy->getVectorNumElements();
7927a7e6055SDimitry Andric
7937a7e6055SDimitry Andric // Called with a compare instruction.
7947a7e6055SDimitry Andric if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) {
7957a7e6055SDimitry Andric unsigned PredicateExtraCost = 0;
7967a7e6055SDimitry Andric if (I != nullptr) {
7977a7e6055SDimitry Andric // Some predicates cost one or two extra instructions.
7984ba319b5SDimitry Andric switch (cast<CmpInst>(I)->getPredicate()) {
7997a7e6055SDimitry Andric case CmpInst::Predicate::ICMP_NE:
8007a7e6055SDimitry Andric case CmpInst::Predicate::ICMP_UGE:
8017a7e6055SDimitry Andric case CmpInst::Predicate::ICMP_ULE:
8027a7e6055SDimitry Andric case CmpInst::Predicate::ICMP_SGE:
8037a7e6055SDimitry Andric case CmpInst::Predicate::ICMP_SLE:
8047a7e6055SDimitry Andric PredicateExtraCost = 1;
8057a7e6055SDimitry Andric break;
8067a7e6055SDimitry Andric case CmpInst::Predicate::FCMP_ONE:
8077a7e6055SDimitry Andric case CmpInst::Predicate::FCMP_ORD:
8087a7e6055SDimitry Andric case CmpInst::Predicate::FCMP_UEQ:
8097a7e6055SDimitry Andric case CmpInst::Predicate::FCMP_UNO:
8107a7e6055SDimitry Andric PredicateExtraCost = 2;
8117a7e6055SDimitry Andric break;
8127a7e6055SDimitry Andric default:
8137a7e6055SDimitry Andric break;
8147a7e6055SDimitry Andric }
8157a7e6055SDimitry Andric }
8167a7e6055SDimitry Andric
8177a7e6055SDimitry Andric // Float is handled with 2*vmr[lh]f + 2*vldeb + vfchdb for each pair of
8187a7e6055SDimitry Andric // floats. FIXME: <2 x float> generates same code as <4 x float>.
8197a7e6055SDimitry Andric unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1);
820*b5893f02SDimitry Andric unsigned NumVecs_cmp = getNumVectorRegs(ValTy);
8217a7e6055SDimitry Andric
8227a7e6055SDimitry Andric unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost));
8237a7e6055SDimitry Andric return Cost;
8247a7e6055SDimitry Andric }
8257a7e6055SDimitry Andric else { // Called with a select instruction.
8267a7e6055SDimitry Andric assert (Opcode == Instruction::Select);
8277a7e6055SDimitry Andric
8287a7e6055SDimitry Andric // We can figure out the extra cost of packing / unpacking if the
8297a7e6055SDimitry Andric // instruction was passed and the compare instruction is found.
8307a7e6055SDimitry Andric unsigned PackCost = 0;
8317a7e6055SDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
8327a7e6055SDimitry Andric if (CmpOpTy != nullptr)
8337a7e6055SDimitry Andric PackCost =
8347a7e6055SDimitry Andric getVectorBitmaskConversionCost(CmpOpTy, ValTy);
8357a7e6055SDimitry Andric
836*b5893f02SDimitry Andric return getNumVectorRegs(ValTy) /*vsel*/ + PackCost;
8377a7e6055SDimitry Andric }
8387a7e6055SDimitry Andric }
8397a7e6055SDimitry Andric else { // Scalar
8407a7e6055SDimitry Andric switch (Opcode) {
8417a7e6055SDimitry Andric case Instruction::ICmp: {
842*b5893f02SDimitry Andric // A loaded value compared with 0 with multiple users becomes Load and
843*b5893f02SDimitry Andric // Test. The load is then not foldable, so return 0 cost for the ICmp.
844*b5893f02SDimitry Andric unsigned ScalarBits = ValTy->getScalarSizeInBits();
845*b5893f02SDimitry Andric if (I != nullptr && ScalarBits >= 32)
846*b5893f02SDimitry Andric if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
847*b5893f02SDimitry Andric if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
848*b5893f02SDimitry Andric if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() &&
849*b5893f02SDimitry Andric C->getZExtValue() == 0)
850*b5893f02SDimitry Andric return 0;
851*b5893f02SDimitry Andric
8527a7e6055SDimitry Andric unsigned Cost = 1;
8537a7e6055SDimitry Andric if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16)
854*b5893f02SDimitry Andric Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2);
8557a7e6055SDimitry Andric return Cost;
8567a7e6055SDimitry Andric }
8577a7e6055SDimitry Andric case Instruction::Select:
8587a7e6055SDimitry Andric if (ValTy->isFloatingPointTy())
859*b5893f02SDimitry Andric return 4; // No load on condition for FP - costs a conditional jump.
8607a7e6055SDimitry Andric return 1; // Load On Condition.
8617a7e6055SDimitry Andric }
8627a7e6055SDimitry Andric }
8637a7e6055SDimitry Andric
8647a7e6055SDimitry Andric return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, nullptr);
8657a7e6055SDimitry Andric }
8667a7e6055SDimitry Andric
8677a7e6055SDimitry Andric int SystemZTTIImpl::
getVectorInstrCost(unsigned Opcode,Type * Val,unsigned Index)8687a7e6055SDimitry Andric getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
8697a7e6055SDimitry Andric // vlvgp will insert two grs into a vector register, so only count half the
8707a7e6055SDimitry Andric // number of instructions.
871c4394386SDimitry Andric if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64))
8727a7e6055SDimitry Andric return ((Index % 2 == 0) ? 1 : 0);
8737a7e6055SDimitry Andric
8747a7e6055SDimitry Andric if (Opcode == Instruction::ExtractElement) {
875*b5893f02SDimitry Andric int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 /*+test-under-mask*/ : 1);
8767a7e6055SDimitry Andric
8777a7e6055SDimitry Andric // Give a slight penalty for moving out of vector pipeline to FXU unit.
878c4394386SDimitry Andric if (Index == 0 && Val->isIntOrIntVectorTy())
8797a7e6055SDimitry Andric Cost += 1;
8807a7e6055SDimitry Andric
8817a7e6055SDimitry Andric return Cost;
8827a7e6055SDimitry Andric }
8837a7e6055SDimitry Andric
8847a7e6055SDimitry Andric return BaseT::getVectorInstrCost(Opcode, Val, Index);
8857a7e6055SDimitry Andric }
8867a7e6055SDimitry Andric
887*b5893f02SDimitry Andric // Check if a load may be folded as a memory operand in its user.
888*b5893f02SDimitry Andric bool SystemZTTIImpl::
isFoldableLoad(const LoadInst * Ld,const Instruction * & FoldedValue)889*b5893f02SDimitry Andric isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) {
890*b5893f02SDimitry Andric if (!Ld->hasOneUse())
891*b5893f02SDimitry Andric return false;
892*b5893f02SDimitry Andric FoldedValue = Ld;
893*b5893f02SDimitry Andric const Instruction *UserI = cast<Instruction>(*Ld->user_begin());
894*b5893f02SDimitry Andric unsigned LoadedBits = getScalarSizeInBits(Ld->getType());
895*b5893f02SDimitry Andric unsigned TruncBits = 0;
896*b5893f02SDimitry Andric unsigned SExtBits = 0;
897*b5893f02SDimitry Andric unsigned ZExtBits = 0;
898*b5893f02SDimitry Andric if (UserI->hasOneUse()) {
899*b5893f02SDimitry Andric unsigned UserBits = UserI->getType()->getScalarSizeInBits();
900*b5893f02SDimitry Andric if (isa<TruncInst>(UserI))
901*b5893f02SDimitry Andric TruncBits = UserBits;
902*b5893f02SDimitry Andric else if (isa<SExtInst>(UserI))
903*b5893f02SDimitry Andric SExtBits = UserBits;
904*b5893f02SDimitry Andric else if (isa<ZExtInst>(UserI))
905*b5893f02SDimitry Andric ZExtBits = UserBits;
906*b5893f02SDimitry Andric }
907*b5893f02SDimitry Andric if (TruncBits || SExtBits || ZExtBits) {
908*b5893f02SDimitry Andric FoldedValue = UserI;
909*b5893f02SDimitry Andric UserI = cast<Instruction>(*UserI->user_begin());
910*b5893f02SDimitry Andric // Load (single use) -> trunc/extend (single use) -> UserI
911*b5893f02SDimitry Andric }
912*b5893f02SDimitry Andric if ((UserI->getOpcode() == Instruction::Sub ||
913*b5893f02SDimitry Andric UserI->getOpcode() == Instruction::SDiv ||
914*b5893f02SDimitry Andric UserI->getOpcode() == Instruction::UDiv) &&
915*b5893f02SDimitry Andric UserI->getOperand(1) != FoldedValue)
916*b5893f02SDimitry Andric return false; // Not commutative, only RHS foldable.
917*b5893f02SDimitry Andric // LoadOrTruncBits holds the number of effectively loaded bits, but 0 if an
918*b5893f02SDimitry Andric // extension was made of the load.
919*b5893f02SDimitry Andric unsigned LoadOrTruncBits =
920*b5893f02SDimitry Andric ((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits));
9217a7e6055SDimitry Andric switch (UserI->getOpcode()) {
922*b5893f02SDimitry Andric case Instruction::Add: // SE: 16->32, 16/32->64, z14:16->64. ZE: 32->64
9237a7e6055SDimitry Andric case Instruction::Sub:
924*b5893f02SDimitry Andric case Instruction::ICmp:
925*b5893f02SDimitry Andric if (LoadedBits == 32 && ZExtBits == 64)
926*b5893f02SDimitry Andric return true;
927*b5893f02SDimitry Andric LLVM_FALLTHROUGH;
928*b5893f02SDimitry Andric case Instruction::Mul: // SE: 16->32, 32->64, z14:16->64
929*b5893f02SDimitry Andric if (UserI->getOpcode() != Instruction::ICmp) {
930*b5893f02SDimitry Andric if (LoadedBits == 16 &&
931*b5893f02SDimitry Andric (SExtBits == 32 ||
932*b5893f02SDimitry Andric (SExtBits == 64 && ST->hasMiscellaneousExtensions2())))
933*b5893f02SDimitry Andric return true;
934*b5893f02SDimitry Andric if (LoadOrTruncBits == 16)
935*b5893f02SDimitry Andric return true;
936*b5893f02SDimitry Andric }
937*b5893f02SDimitry Andric LLVM_FALLTHROUGH;
938*b5893f02SDimitry Andric case Instruction::SDiv:// SE: 32->64
939*b5893f02SDimitry Andric if (LoadedBits == 32 && SExtBits == 64)
940*b5893f02SDimitry Andric return true;
941*b5893f02SDimitry Andric LLVM_FALLTHROUGH;
9427a7e6055SDimitry Andric case Instruction::UDiv:
9437a7e6055SDimitry Andric case Instruction::And:
9447a7e6055SDimitry Andric case Instruction::Or:
9457a7e6055SDimitry Andric case Instruction::Xor:
9467a7e6055SDimitry Andric // This also makes sense for float operations, but disabled for now due
9477a7e6055SDimitry Andric // to regressions.
9487a7e6055SDimitry Andric // case Instruction::FCmp:
9497a7e6055SDimitry Andric // case Instruction::FAdd:
9507a7e6055SDimitry Andric // case Instruction::FSub:
9517a7e6055SDimitry Andric // case Instruction::FMul:
9527a7e6055SDimitry Andric // case Instruction::FDiv:
953*b5893f02SDimitry Andric
954*b5893f02SDimitry Andric // All possible extensions of memory checked above.
955*b5893f02SDimitry Andric
956*b5893f02SDimitry Andric // Comparison between memory and immediate.
957*b5893f02SDimitry Andric if (UserI->getOpcode() == Instruction::ICmp)
958*b5893f02SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1)))
959*b5893f02SDimitry Andric if (isUInt<16>(CI->getZExtValue()))
960*b5893f02SDimitry Andric return true;
961*b5893f02SDimitry Andric return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64);
9627a7e6055SDimitry Andric break;
9637a7e6055SDimitry Andric }
964*b5893f02SDimitry Andric return false;
965*b5893f02SDimitry Andric }
9667a7e6055SDimitry Andric
isBswapIntrinsicCall(const Value * V)967*b5893f02SDimitry Andric static bool isBswapIntrinsicCall(const Value *V) {
968*b5893f02SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V))
969*b5893f02SDimitry Andric if (auto *CI = dyn_cast<CallInst>(I))
970*b5893f02SDimitry Andric if (auto *F = CI->getCalledFunction())
971*b5893f02SDimitry Andric if (F->getIntrinsicID() == Intrinsic::bswap)
972*b5893f02SDimitry Andric return true;
973*b5893f02SDimitry Andric return false;
974*b5893f02SDimitry Andric }
975*b5893f02SDimitry Andric
getMemoryOpCost(unsigned Opcode,Type * Src,unsigned Alignment,unsigned AddressSpace,const Instruction * I)976*b5893f02SDimitry Andric int SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
977*b5893f02SDimitry Andric unsigned Alignment, unsigned AddressSpace,
978*b5893f02SDimitry Andric const Instruction *I) {
979*b5893f02SDimitry Andric assert(!Src->isVoidTy() && "Invalid type");
980*b5893f02SDimitry Andric
981*b5893f02SDimitry Andric if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) {
982*b5893f02SDimitry Andric // Store the load or its truncated or extended value in FoldedValue.
983*b5893f02SDimitry Andric const Instruction *FoldedValue = nullptr;
984*b5893f02SDimitry Andric if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) {
985*b5893f02SDimitry Andric const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin());
986*b5893f02SDimitry Andric assert (UserI->getNumOperands() == 2 && "Expected a binop.");
9877a7e6055SDimitry Andric
9887a7e6055SDimitry Andric // UserI can't fold two loads, so in that case return 0 cost only
9897a7e6055SDimitry Andric // half of the time.
9907a7e6055SDimitry Andric for (unsigned i = 0; i < 2; ++i) {
991*b5893f02SDimitry Andric if (UserI->getOperand(i) == FoldedValue)
9927a7e6055SDimitry Andric continue;
993*b5893f02SDimitry Andric
994*b5893f02SDimitry Andric if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){
995*b5893f02SDimitry Andric LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp);
996*b5893f02SDimitry Andric if (!OtherLoad &&
997*b5893f02SDimitry Andric (isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) ||
998*b5893f02SDimitry Andric isa<ZExtInst>(OtherOp)))
999*b5893f02SDimitry Andric OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0));
1000*b5893f02SDimitry Andric if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue/*dummy*/))
1001*b5893f02SDimitry Andric return i == 0; // Both operands foldable.
10027a7e6055SDimitry Andric }
10037a7e6055SDimitry Andric }
10047a7e6055SDimitry Andric
1005*b5893f02SDimitry Andric return 0; // Only I is foldable in user.
1006*b5893f02SDimitry Andric }
1007*b5893f02SDimitry Andric }
1008*b5893f02SDimitry Andric
1009*b5893f02SDimitry Andric unsigned NumOps =
1010*b5893f02SDimitry Andric (Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src));
1011*b5893f02SDimitry Andric
1012*b5893f02SDimitry Andric // Store/Load reversed saves one instruction.
1013*b5893f02SDimitry Andric if (!Src->isVectorTy() && NumOps == 1 && I != nullptr) {
1014*b5893f02SDimitry Andric if (Opcode == Instruction::Load && I->hasOneUse()) {
1015*b5893f02SDimitry Andric const Instruction *LdUser = cast<Instruction>(*I->user_begin());
1016*b5893f02SDimitry Andric // In case of load -> bswap -> store, return normal cost for the load.
1017*b5893f02SDimitry Andric if (isBswapIntrinsicCall(LdUser) &&
1018*b5893f02SDimitry Andric (!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin())))
1019*b5893f02SDimitry Andric return 0;
1020*b5893f02SDimitry Andric }
1021*b5893f02SDimitry Andric else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
1022*b5893f02SDimitry Andric const Value *StoredVal = SI->getValueOperand();
1023*b5893f02SDimitry Andric if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal))
10247a7e6055SDimitry Andric return 0;
10257a7e6055SDimitry Andric }
10267a7e6055SDimitry Andric }
10277a7e6055SDimitry Andric
10287a7e6055SDimitry Andric if (Src->getScalarSizeInBits() == 128)
10297a7e6055SDimitry Andric // 128 bit scalars are held in a pair of two 64 bit registers.
10307a7e6055SDimitry Andric NumOps *= 2;
10317a7e6055SDimitry Andric
10327a7e6055SDimitry Andric return NumOps;
10337a7e6055SDimitry Andric }
10347a7e6055SDimitry Andric
1035*b5893f02SDimitry Andric // The generic implementation of getInterleavedMemoryOpCost() is based on
1036*b5893f02SDimitry Andric // adding costs of the memory operations plus all the extracts and inserts
1037*b5893f02SDimitry Andric // needed for using / defining the vector operands. The SystemZ version does
1038*b5893f02SDimitry Andric // roughly the same but bases the computations on vector permutations
1039*b5893f02SDimitry Andric // instead.
getInterleavedMemoryOpCost(unsigned Opcode,Type * VecTy,unsigned Factor,ArrayRef<unsigned> Indices,unsigned Alignment,unsigned AddressSpace,bool UseMaskForCond,bool UseMaskForGaps)10407a7e6055SDimitry Andric int SystemZTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
10417a7e6055SDimitry Andric unsigned Factor,
10427a7e6055SDimitry Andric ArrayRef<unsigned> Indices,
10437a7e6055SDimitry Andric unsigned Alignment,
1044*b5893f02SDimitry Andric unsigned AddressSpace,
1045*b5893f02SDimitry Andric bool UseMaskForCond,
1046*b5893f02SDimitry Andric bool UseMaskForGaps) {
1047*b5893f02SDimitry Andric if (UseMaskForCond || UseMaskForGaps)
1048*b5893f02SDimitry Andric return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
1049*b5893f02SDimitry Andric Alignment, AddressSpace,
1050*b5893f02SDimitry Andric UseMaskForCond, UseMaskForGaps);
10517a7e6055SDimitry Andric assert(isa<VectorType>(VecTy) &&
10527a7e6055SDimitry Andric "Expect a vector type for interleaved memory op");
10537a7e6055SDimitry Andric
1054*b5893f02SDimitry Andric // Return the ceiling of dividing A by B.
1055*b5893f02SDimitry Andric auto ceil = [](unsigned A, unsigned B) { return (A + B - 1) / B; };
10567a7e6055SDimitry Andric
1057*b5893f02SDimitry Andric unsigned NumElts = VecTy->getVectorNumElements();
1058*b5893f02SDimitry Andric assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor");
1059*b5893f02SDimitry Andric unsigned VF = NumElts / Factor;
1060*b5893f02SDimitry Andric unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy));
1061*b5893f02SDimitry Andric unsigned NumVectorMemOps = getNumVectorRegs(VecTy);
1062*b5893f02SDimitry Andric unsigned NumPermutes = 0;
10637a7e6055SDimitry Andric
1064*b5893f02SDimitry Andric if (Opcode == Instruction::Load) {
1065*b5893f02SDimitry Andric // Loading interleave groups may have gaps, which may mean fewer
1066*b5893f02SDimitry Andric // loads. Find out how many vectors will be loaded in total, and in how
1067*b5893f02SDimitry Andric // many of them each value will be in.
1068*b5893f02SDimitry Andric BitVector UsedInsts(NumVectorMemOps, false);
1069*b5893f02SDimitry Andric std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false));
1070*b5893f02SDimitry Andric for (unsigned Index : Indices)
1071*b5893f02SDimitry Andric for (unsigned Elt = 0; Elt < VF; ++Elt) {
1072*b5893f02SDimitry Andric unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg;
1073*b5893f02SDimitry Andric UsedInsts.set(Vec);
1074*b5893f02SDimitry Andric ValueVecs[Index].set(Vec);
1075*b5893f02SDimitry Andric }
1076*b5893f02SDimitry Andric NumVectorMemOps = UsedInsts.count();
10777a7e6055SDimitry Andric
1078*b5893f02SDimitry Andric for (unsigned Index : Indices) {
1079*b5893f02SDimitry Andric // Estimate that each loaded source vector containing this Index
1080*b5893f02SDimitry Andric // requires one operation, except that vperm can handle two input
1081*b5893f02SDimitry Andric // registers first time for each dst vector.
1082*b5893f02SDimitry Andric unsigned NumSrcVecs = ValueVecs[Index].count();
1083*b5893f02SDimitry Andric unsigned NumDstVecs = ceil(VF * getScalarSizeInBits(VecTy), 128U);
1084*b5893f02SDimitry Andric assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources");
1085*b5893f02SDimitry Andric NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs);
1086*b5893f02SDimitry Andric }
1087*b5893f02SDimitry Andric } else {
1088*b5893f02SDimitry Andric // Estimate the permutes for each stored vector as the smaller of the
1089*b5893f02SDimitry Andric // number of elements and the number of source vectors. Subtract one per
1090*b5893f02SDimitry Andric // dst vector for vperm (S.A.).
1091*b5893f02SDimitry Andric unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor);
1092*b5893f02SDimitry Andric unsigned NumDstVecs = NumVectorMemOps;
1093*b5893f02SDimitry Andric assert (NumSrcVecs > 1 && "Expected at least two source vectors.");
1094*b5893f02SDimitry Andric NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs;
1095*b5893f02SDimitry Andric }
10967a7e6055SDimitry Andric
10977a7e6055SDimitry Andric // Cost of load/store operations and the permutations needed.
1098*b5893f02SDimitry Andric return NumVectorMemOps + NumPermutes;
1099*b5893f02SDimitry Andric }
1100*b5893f02SDimitry Andric
getVectorIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy)1101*b5893f02SDimitry Andric static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy) {
1102*b5893f02SDimitry Andric if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
1103*b5893f02SDimitry Andric return getNumVectorRegs(RetTy); // VPERM
1104*b5893f02SDimitry Andric return -1;
1105*b5893f02SDimitry Andric }
1106*b5893f02SDimitry Andric
getIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy,ArrayRef<Value * > Args,FastMathFlags FMF,unsigned VF)1107*b5893f02SDimitry Andric int SystemZTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
1108*b5893f02SDimitry Andric ArrayRef<Value *> Args,
1109*b5893f02SDimitry Andric FastMathFlags FMF, unsigned VF) {
1110*b5893f02SDimitry Andric int Cost = getVectorIntrinsicInstrCost(ID, RetTy);
1111*b5893f02SDimitry Andric if (Cost != -1)
1112*b5893f02SDimitry Andric return Cost;
1113*b5893f02SDimitry Andric return BaseT::getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
1114*b5893f02SDimitry Andric }
1115*b5893f02SDimitry Andric
getIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy,ArrayRef<Type * > Tys,FastMathFlags FMF,unsigned ScalarizationCostPassed)1116*b5893f02SDimitry Andric int SystemZTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
1117*b5893f02SDimitry Andric ArrayRef<Type *> Tys,
1118*b5893f02SDimitry Andric FastMathFlags FMF,
1119*b5893f02SDimitry Andric unsigned ScalarizationCostPassed) {
1120*b5893f02SDimitry Andric int Cost = getVectorIntrinsicInstrCost(ID, RetTy);
1121*b5893f02SDimitry Andric if (Cost != -1)
1122*b5893f02SDimitry Andric return Cost;
1123*b5893f02SDimitry Andric return BaseT::getIntrinsicInstrCost(ID, RetTy, Tys,
1124*b5893f02SDimitry Andric FMF, ScalarizationCostPassed);
11257a7e6055SDimitry Andric }
1126