LLVM 24.0.0git
LoadStoreVec.h
Go to the documentation of this file.
1//===- LoadStoreVec.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// A pass that vectorizes short store-load chains.
10// Unlike generic bundle vectorization, this pass can vectorize instructions
11// of different types.
12//
13
14#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_LOADSTOREVEC_H
15#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_LOADSTOREVEC_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/SandboxIR/Pass.h"
23
24#include <optional>
25
26namespace llvm {
27
28class DataLayout;
29
30namespace sandboxir {
31
32class Context;
33class Value;
34class Instruction;
35class Type;
36class Constant;
37
38/// Reinterprets the bits of a value as another type of the same size, for
39/// example float 1.0 as i32 0x3f800000, a null pointer as i64 0, or <2 x i32>
40/// as double. Used by createConstantVector() to pack constants, and by
41/// vectorizeLoads() to unpack each load from the vector load.
43 /// A single cast in the chain built by getCastChain().
44 using CastStep = std::pair<Instruction::Opcode, Type *>;
45
46 /// \returns an integer type with the same layout as \p Ty: iN for scalars,
47 /// <N x iM> for vectors.
48 static Type *getIntTypeFor(Type *Ty, const DataLayout &DL);
49
50 /// \returns the casts that reinterpret \p SrcTy as \p DestTy, empty if the
51 /// types match. \p SrcTy and \p DestTy must be the same size, and
52 /// canReinterpret() must be true.
53 static SmallVector<CastStep, 3> getCastChain(Type *SrcTy, Type *DestTy,
54 const DataLayout &DL);
55
56public:
57 /// \returns true if \p SrcTy can be reinterpreted as \p DestTy. False when
58 /// either type is a non-integral pointer, which has no stable integer
59 /// representation.
60 LLVM_ABI static bool canReinterpret(Type *SrcTy, Type *DestTy,
61 const DataLayout &DL);
62
63 /// Reinterprets \p V as \p DestTy, which must have the same size in \p DL,
64 /// inserting casts before \p WhereIt. Casts of constants fold, so nothing is
65 /// inserted for those. \returns nullptr if a non-integral pointer is
66 /// involved.
67 LLVM_ABI static Value *reinterpretValue(Value *V, Type *DestTy,
69 Context &Ctx, const DataLayout &DL);
70
71 /// Reinterprets constant \p C as \p DestTy, e.g., float 1.0 to i32
72 /// 0x3f800000, or a null pointer to i64 0. \returns nullptr if a
73 /// non-integral pointer is involved.
75 const DataLayout &DL);
76};
77
78class LLVM_ABI LoadStoreVec final : public RegionPass {
79 const DataLayout *DL = nullptr;
80 /// The region saved by saveIR(), used by acceptOrRevert().
81 Region *Rgn = nullptr;
82 /// The region's cost at the time of saveIR().
83 InstructionCost CostBefore = 0;
84 const Analyses *A = nullptr;
85 Context *Ctx = nullptr;
86 LegalityAnalysis *Legality = nullptr;
87 VecUtils::DeadInstructionMorgue DeadInstrMorgue;
88
89 /// Saves the IR along with the current cost of \p Rgn, so that
90 /// acceptOrRevert() can tell whether vectorizing was profitable.
91 void saveIR(Region &Rgn);
92
93 /// Accepts the transaction saved by saveIR() if vectorizing was profitable,
94 /// reverts it otherwise. \returns true if the transaction was accepted.
95 bool acceptOrRevert();
96
97 /// Checks legality of vectorization and \returns the vector type on success,
98 /// nullopt otherwise.
99 std::optional<Type *> canVectorize(BndlRef<Instruction *> Bndl);
100
101 /// Builds a single vector load out of \p Loads. \returns the new load.
102 LoadInst *createVectorLoad(BndlRef<Instruction *> Loads);
103
104 /// \returns true if a vector of type \p FromTy can be unpacked as each
105 /// of \p ToTypes.
106 template <typename TypeRangeT>
107 bool canUnpack(FixedVectorType *FromTy, TypeRangeT ToTypes);
108
109 /// \p VecLoad is the vectorized \p OrigLoads. This function unpacks the
110 /// elements of \p VecLoad that have uses, casting them back to the
111 /// element's type (as in \p OrigLoads) if needed.
112 void unpackIfNeeded(LoadInst *VecLoad, BndlRef<Instruction *> OrigLoads);
113
114 /// Packs \p Constants into one constant vector. The element type is chosen
115 /// by VecUtils::getCombinedVectorTypeFor().
116 ///
117 /// \returns the packed constant vector, or nullptr if some constant cannot
118 /// be converted, e.g., a vector constant that is not yet flattened, a
119 /// non-integral pointer, or a wide constant without a known bit pattern such
120 /// as a ConstantExpr or a global's address.
121 Constant *createConstantVector(BndlRef<Constant *> Constants);
122
123 /// Vectorizes \p Stores and their operands if constants or consecutive
124 /// loads. \returns true on success.
125 bool vectorizeStores(BndlRef<Instruction *> Stores, Region &Rgn);
126
127 /// Vectorizes \p Loads into a single load and unpacks each original load
128 /// from it, casting the lanes back to the load's type if needed.
129 /// \return the packed load.
130 LoadInst *vectorizeLoads(BndlRef<Instruction *> Loads, Region &Rgn);
131
132public:
133 LoadStoreVec(StringRef AuxArg) : RegionPass("load-store-vec") {
134 assert(AuxArg.empty() && "This pass ignores aux arg!");
135 }
136 bool runOnRegion(Region &Rgn, const Analyses &A) final;
137};
138
139} // namespace sandboxir
140
141} // namespace llvm
142
143#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_STRUCTINITVEC_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static bool canVectorize(const CharMatrix &DepMatrix, unsigned LoopId)
Return true if we can vectorize the loop specified by LoopId.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:169
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
An ArrayRef of Values or Instructions that we can print/dump for debugging.
Definition VecUtils.h:39
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
Performs the legality analysis and returns a LegalityResult object.
Definition Legality.h:322
LoadStoreVec(StringRef AuxArg)
RegionPass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:94
Reinterprets the bits of a value as another type of the same size, for example float 1....
static LLVM_ABI Constant * reinterpretConstant(Constant *C, Type *DestTy, const DataLayout &DL)
Reinterprets constant C as DestTy, e.g., float 1.0 to i32 0x3f800000, or a null pointer to i64 0.
static LLVM_ABI Value * reinterpretValue(Value *V, Type *DestTy, BasicBlock::iterator WhereIt, Context &Ctx, const DataLayout &DL)
Reinterprets V as DestTy, which must have the same size in DL, inserting casts before WhereIt.
static LLVM_ABI bool canReinterpret(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition Type.h:48
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
Utility class to collect and erase dead instructions.
Definition VecUtils.h:502
This is an optimization pass for GlobalISel generic memory operations.