1//===- SLPCostAnalysis.h - SLP Vectorizer free cost helpers ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Internal header used by SLPVectorizer.cpp. It declares free cost helpers
10// that do not depend on BoUpSLP or any other SLP-private type. The bulk of
11// the SLP cost model still lives in SLPVectorizer.cpp because it references
12// BoUpSLP internals.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
17#define LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
18
19#include "SLPUtils.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/Support/InstructionCost.h"
23
24#include <tuple>
25#include <utility>
26
27namespace llvm {
28class APInt;
29class FastMathFlags;
30class FixedVectorType;
31class Instruction;
32class TargetLibraryInfo;
33class Type;
34class User;
35class Value;
36class VectorType;
37enum class RecurKind;
38} // namespace llvm
39
40namespace llvm::slpvectorizer {
41
42/// Returns the cost of the shuffle instructions with the given \p Kind, vector
43/// type \p Tp and optional \p Mask. Adds SLP-specific cost estimation for
44/// insert subvector pattern.
45InstructionCost
46getShuffleCost(const TargetTransformInfo &TTI,
47 TargetTransformInfo::ShuffleKind Kind, VectorType *Tp,
48 const TargetTransformInfo::TargetCostKind CostKind,
49 ArrayRef<int> Mask = {}, int Index = 0,
50 VectorType *SubTp = nullptr, ArrayRef<const Value *> Args = {});
51
52/// Calculate the scalar and the vector costs from vectorizing set of GEPs.
53std::pair<InstructionCost, InstructionCost>
54getGEPCosts(const TargetTransformInfo &TTI, ArrayRef<Value *> Ptrs,
55 Value *BasePtr, unsigned Opcode,
56 const TargetTransformInfo::TargetCostKind CostKind, Type *ScalarTy,
57 VectorType *VecTy);
58
59/// Returns the cost of a BlendedLoadVectorize node loading \p VecTy: two masked
60/// loads (one per candidate base), a xor to negate the false-lane mask and a
61/// select. The blend mask is a separate operand node, so its cost is counted
62/// there, not here.
63InstructionCost
64getBlendedLoadCost(const TargetTransformInfo &TTI, Type *VecTy, Align Alignment,
65 unsigned AddressSpace,
66 const TargetTransformInfo::TargetCostKind CostKind);
67
68/// For a non-power-of-2 \p NumElts-wide integer div/rem \p Opcode, checks if
69/// padding to a full register and using the masked div/rem intrinsic is
70/// cheaper than the direct vector op. Returns the cost of the masked
71/// alternative, or an invalid cost if it is not applicable or not cheaper.
72InstructionCost
73getMaskedDivRemCost(const TargetTransformInfo &TTI, bool ReVec, unsigned Opcode,
74 Type *ScalarTy, unsigned NumElts,
75 const TargetTransformInfo::TargetCostKind CostKind,
76 FixedVectorType **PaddedTy = nullptr);
77
78/// Returns the cost of the booleanized logical and/or reduction of a vector
79/// of type \p VecTy with the i1 root \p Root, emitted as the wide reduction
80/// plus the result trunc.
81InstructionCost
82getBoolReduxWideRdxCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
83 FixedVectorType *VecTy, const Value *Root,
84 FastMathFlags FMF,
85 TargetTransformInfo::TargetCostKind CostKind);
86
87/// Returns the cost of the booleanized logical and/or reduction of a vector
88/// of type \p VecTy with the i1 root \p Root, emitted as trunc+bitcast+cmp,
89/// estimated in the context of the replaced cast chain \p ChainInsts.
90InstructionCost
91getBoolReduxBitcastCmpCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
92 FixedVectorType *VecTy, const Value *Root,
93 ArrayRef<Instruction *> ChainInsts,
94 TargetTransformInfo::TargetCostKind CostKind);
95
96/// This is similar to TargetTransformInfo::getScalarizationOverhead, but if
97/// ScalarTy is a FixedVectorType, a vector will be inserted or extracted
98/// instead of a scalar.
99InstructionCost
100getScalarizationOverhead(const TargetTransformInfo &TTI, bool ReVec,
101 Type *ScalarTy, VectorType *Ty,
102 const APInt &DemandedElts, bool Insert, bool Extract,
103 const TargetTransformInfo::TargetCostKind CostKind,
104 bool ForPoisonSrc = true, ArrayRef<Value *> VL = {},
105 TargetTransformInfo::VectorInstrContext VIC =
106 TargetTransformInfo::VectorInstrContext::None);
107
108/// This is similar to TargetTransformInfo::getVectorInstrCost, but if ScalarTy
109/// is a FixedVectorType, a vector will be extracted instead of a scalar.
110InstructionCost
111getVectorInstrCost(const TargetTransformInfo &TTI, bool ReVec, Type *ScalarTy,
112 unsigned Opcode, Type *Val,
113 const TargetTransformInfo::TargetCostKind CostKind,
114 unsigned Index, Value *Scalar,
115 ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx);
116
117/// This is similar to TargetTransformInfo::getExtractWithExtendCost, but if Dst
118/// is a FixedVectorType, a vector will be extracted instead of a scalar.
119InstructionCost
120getExtractWithExtendCost(const TargetTransformInfo &TTI, bool ReVec,
121 unsigned Opcode, Type *Dst, VectorType *VecTy,
122 unsigned Index,
123 const TargetTransformInfo::TargetCostKind CostKind);
124
125/// Returns the cost of the bitfield packing of \p SrcTy into \p ResultTy,
126/// picking the cheapest shift width. The packing is a trunc, an lshr, a byte
127/// shuffle and a bitcast. \p ZExtSrcWidth is the source width of the lanes if
128/// they are a plain zext (0 otherwise), so compacting them back to it is free.
129/// \p CCH is the context of the pack's source operand.
130InstructionCost getBitPackCost(const TargetTransformInfo &TTI,
131 FixedVectorType *SrcTy, Type *ResultTy,
132 const BitPackInfo &Info, unsigned ZExtSrcWidth,
133 TargetTransformInfo::CastContextHint CCH,
134 TargetTransformInfo::TargetCostKind CostKind,
135 const TargetLibraryInfo *TLI,
136 const Instruction *CxtI, unsigned &ShiftWidth);
137
138} // namespace llvm::slpvectorizer
139
140#endif // LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
141