LLVM 20.0.0git
VecUtils.h
Go to the documentation of this file.
1//===- VecUtils.h -----------------------------------------------*- 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// Collector for SandboxVectorizer related convenience functions that don't
10// belong in other classes.
11
12#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_VECUTILS_H
13#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_VECUTILS_H
14
16#include "llvm/IR/DataLayout.h"
17#include "llvm/SandboxIR/Type.h"
19
20namespace llvm::sandboxir {
21
22class VecUtils {
23public:
24 /// \Returns the number of elements in \p Ty. That is the number of lanes if a
25 /// fixed vector or 1 if scalar. ScalableVectors have unknown size and
26 /// therefore are unsupported.
27 static int getNumElements(Type *Ty) {
28 assert(!isa<ScalableVectorType>(Ty));
29 return Ty->isVectorTy() ? cast<FixedVectorType>(Ty)->getNumElements() : 1;
30 }
31 /// Returns \p Ty if scalar or its element type if vector.
32 static Type *getElementType(Type *Ty) {
33 return Ty->isVectorTy() ? cast<FixedVectorType>(Ty)->getElementType() : Ty;
34 }
35
36 /// \Returns true if \p I1 and \p I2 are load/stores accessing consecutive
37 /// memory addresses.
38 template <typename LoadOrStoreT>
39 static bool areConsecutive(LoadOrStoreT *I1, LoadOrStoreT *I2,
40 ScalarEvolution &SE, const DataLayout &DL) {
41 static_assert(std::is_same<LoadOrStoreT, LoadInst>::value ||
42 std::is_same<LoadOrStoreT, StoreInst>::value,
43 "Expected Load or Store!");
44 auto Diff = Utils::getPointerDiffInBytes(I1, I2, SE);
45 if (!Diff)
46 return false;
47 int ElmBytes = Utils::getNumBits(I1) / 8;
48 return *Diff == ElmBytes;
49 }
50
51 template <typename LoadOrStoreT>
53 const DataLayout &DL) {
54 static_assert(std::is_same<LoadOrStoreT, LoadInst>::value ||
55 std::is_same<LoadOrStoreT, StoreInst>::value,
56 "Expected Load or Store!");
57 assert(isa<LoadOrStoreT>(Bndl[0]) && "Expected Load or Store!");
58 auto *LastLS = cast<LoadOrStoreT>(Bndl[0]);
59 for (Value *V : drop_begin(Bndl)) {
60 assert(isa<LoadOrStoreT>(V) &&
61 "Unimplemented: we only support StoreInst!");
62 auto *LS = cast<LoadOrStoreT>(V);
63 if (!VecUtils::areConsecutive(LastLS, LS, SE, DL))
64 return false;
65 LastLS = LS;
66 }
67 return true;
68 }
69
70 /// \Returns the number of vector lanes of \p Ty or 1 if not a vector.
71 /// NOTE: It asserts that \p Ty is a fixed vector type.
72 static unsigned getNumLanes(Type *Ty) {
73 assert(!isa<ScalableVectorType>(Ty) && "Expect scalar or fixed vector");
74 if (auto *FixedVecTy = dyn_cast<FixedVectorType>(Ty))
75 return FixedVecTy->getNumElements();
76 return 1u;
77 }
78
79 /// \Returns the expected vector lanes of \p V or 1 if not a vector.
80 /// NOTE: It asserts that \p V is a fixed vector.
81 static unsigned getNumLanes(Value *V) {
83 }
84
85 /// \Returns the total number of lanes across all values in \p Bndl.
86 static unsigned getNumLanes(ArrayRef<Value *> Bndl) {
87 unsigned Lanes = 0;
88 for (Value *V : Bndl)
89 Lanes += getNumLanes(V);
90 return Lanes;
91 }
92
93 /// \Returns <NumElts x ElemTy>.
94 /// It works for both scalar and vector \p ElemTy.
95 static Type *getWideType(Type *ElemTy, unsigned NumElts) {
96 if (ElemTy->isVectorTy()) {
97 auto *VecTy = cast<FixedVectorType>(ElemTy);
98 ElemTy = VecTy->getElementType();
99 NumElts = VecTy->getNumElements() * NumElts;
100 }
101 return FixedVectorType::get(ElemTy, NumElts);
102 }
104 Instruction *LowestI = Instrs.front();
105 for (auto *I : drop_begin(Instrs)) {
106 if (LowestI->comesBefore(I))
107 LowestI = I;
108 }
109 return LowestI;
110 }
111 /// If all values in \p Bndl are of the same scalar type then return it,
112 /// otherwise return nullptr.
114 Value *V0 = Bndl[0];
115 Type *Ty0 = Utils::getExpectedType(V0);
116 Type *ScalarTy = VecUtils::getElementType(Ty0);
117 for (auto *V : drop_begin(Bndl)) {
119 Type *NScalarTy = VecUtils::getElementType(NTy);
120 if (NScalarTy != ScalarTy)
121 return nullptr;
122 }
123 return ScalarTy;
124 }
125
126 /// Similar to tryGetCommonScalarType() but will assert that there is a common
127 /// type. So this is faster in release builds as it won't iterate through the
128 /// values.
130 Value *V0 = Bndl[0];
131 Type *Ty0 = Utils::getExpectedType(V0);
132 Type *ScalarTy = VecUtils::getElementType(Ty0);
133 assert(tryGetCommonScalarType(Bndl) && "Expected common scalar type!");
134 return ScalarTy;
135 }
136};
137
138} // namespace llvm::sandboxir
139
140#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_VECUTILS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:171
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
The main scalar evolution driver.
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
Definition: Instruction.h:214
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:196
static unsigned getNumBits(Value *V, const DataLayout &DL)
\Returns the number of bits required to represent the operands or return value of V in DL.
Definition: Utils.h:65
static std::optional< int > getPointerDiffInBytes(LoadOrStoreT *I0, LoadOrStoreT *I1, ScalarEvolution &SE)
\Returns the gap between the memory locations accessed by I0 and I1 in bytes.
Definition: Utils.h:85
static Type * getExpectedType(const Value *V)
\Returns the expected type of Value V.
Definition: Utils.h:30
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
static Type * tryGetCommonScalarType(ArrayRef< Value * > Bndl)
If all values in Bndl are of the same scalar type then return it, otherwise return nullptr.
Definition: VecUtils.h:113
static Instruction * getLowest(ArrayRef< Instruction * > Instrs)
Definition: VecUtils.h:103
static Type * getCommonScalarType(ArrayRef< Value * > Bndl)
Similar to tryGetCommonScalarType() but will assert that there is a common type.
Definition: VecUtils.h:129
static int getNumElements(Type *Ty)
\Returns the number of elements in Ty.
Definition: VecUtils.h:27
static unsigned getNumLanes(Type *Ty)
\Returns the number of vector lanes of Ty or 1 if not a vector.
Definition: VecUtils.h:72
static Type * getWideType(Type *ElemTy, unsigned NumElts)
\Returns <NumElts x ElemTy>.
Definition: VecUtils.h:95
static bool areConsecutive(LoadOrStoreT *I1, LoadOrStoreT *I2, ScalarEvolution &SE, const DataLayout &DL)
\Returns true if I1 and I2 are load/stores accessing consecutive memory addresses.
Definition: VecUtils.h:39
static Type * getElementType(Type *Ty)
Returns Ty if scalar or its element type if vector.
Definition: VecUtils.h:32
static bool areConsecutive(ArrayRef< Value * > &Bndl, ScalarEvolution &SE, const DataLayout &DL)
Definition: VecUtils.h:52
static unsigned getNumLanes(Value *V)
\Returns the expected vector lanes of V or 1 if not a vector.
Definition: VecUtils.h:81
static unsigned getNumLanes(ArrayRef< Value * > Bndl)
\Returns the total number of lanes across all values in Bndl.
Definition: VecUtils.h:86
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329