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 Function;
34class Value;
35class Instruction;
36class Type;
37class Constant;
38
39/// Reinterprets the bits of a value as another type of the same size, for
40/// example float 1.0 as i32 0x3f800000, a null pointer as i64 0, or <2 x i32>
41/// as double. Used by createConstantVector() to pack constants, and by
42/// vectorizeLoads() to unpack each load from the vector load.
44 /// A single cast in the chain built by getCastChain().
45 using CastStep = std::pair<Instruction::Opcode, Type *>;
46
47 /// \returns an integer type with the same layout as \p Ty: iN for scalars,
48 /// <N x iM> for vectors.
49 static Type *getIntTypeFor(Type *Ty, const DataLayout &DL);
50
51 /// \returns the casts that reinterpret \p SrcTy as \p DestTy, empty if the
52 /// types match. \p SrcTy and \p DestTy must be the same size, and
53 /// canReinterpret() must be true.
54 static SmallVector<CastStep, 3> getCastChain(Type *SrcTy, Type *DestTy,
55 const DataLayout &DL);
56
57public:
58 /// \returns true if \p SrcTy can be reinterpreted as \p DestTy. False when
59 /// either type is a non-integral pointer, which has no stable integer
60 /// representation.
61 LLVM_ABI static bool canReinterpret(Type *SrcTy, Type *DestTy,
62 const DataLayout &DL);
63
64 /// Reinterprets \p V as \p DestTy, which must have the same size in \p DL,
65 /// inserting casts before \p WhereIt. Casts of constants fold, so nothing is
66 /// inserted for those. \returns nullptr if a non-integral pointer is
67 /// involved.
68 LLVM_ABI static Value *reinterpretValue(Value *V, Type *DestTy,
70 Context &Ctx, const DataLayout &DL);
71
72 /// Reinterprets constant \p C as \p DestTy, e.g., float 1.0 to i32
73 /// 0x3f800000, or a null pointer to i64 0. \returns nullptr if a
74 /// non-integral pointer is involved.
76 const DataLayout &DL);
77};
78
79class LLVM_ABI LoadStoreVec final : public RegionPass {
80 const DataLayout *DL = nullptr;
81 /// The region saved by saveIR(), used by acceptOrRevert().
82 Region *Rgn = nullptr;
83 /// The region's cost at the time of saveIR().
84 InstructionCost CostBefore = 0;
85 const Analyses *A = nullptr;
86 Context *Ctx = nullptr;
87 LegalityAnalysis *Legality = nullptr;
88 VecUtils::DeadInstructionMorgue DeadInstrMorgue;
89
90 /// Saves the IR along with the current cost of \p Rgn, so that
91 /// acceptOrRevert() can tell whether vectorizing was profitable.
92 void saveIR(Region &Rgn);
93
94 /// Accepts the transaction saved by saveIR() if vectorizing was profitable,
95 /// reverts it otherwise. \returns true if the transaction was accepted.
96 bool acceptOrRevert();
97
98 /// Checks legality of vectorization and \returns the vector type on success,
99 /// nullopt otherwise.
100 std::optional<Type *> canVectorize(BndlRef<Instruction *> Bndl);
101
102 /// Builds a single vector load out of \p Loads. \returns the new load.
103 LoadInst *createVectorLoad(BndlRef<Instruction *> Loads);
104
105 /// Packs \p Constants into one constant vector. The element type is chosen
106 /// by VecUtils::getCombinedVectorTypeFor().
107 ///
108 /// \returns the packed constant vector, or nullptr if some constant cannot
109 /// be converted, e.g., a vector constant that is not yet flattened, a
110 /// non-integral pointer, or a wide constant without a known bit pattern such
111 /// as a ConstantExpr or a global's address.
112 Constant *createConstantVector(BndlRef<Constant *> Constants);
113
114 /// Vectorizes \p Stores and their operands if constants or consecutive
115 /// loads. \returns true on success.
116 bool vectorizeStores(BndlRef<Instruction *> Stores, Region &Rgn);
117
118 /// Vectorizes \p Loads into a single load and unpacks each original load
119 /// from it, casting the lanes back to the load's type if needed.
120 /// \return the packed load.
121 LoadInst *vectorizeLoads(BndlRef<Instruction *> Loads, Region &Rgn);
122
123public:
124 LoadStoreVec(StringRef AuxArg) : RegionPass("load-store-vec") {
125 assert(AuxArg.empty() && "This pass ignores aux arg!");
126 }
127 bool runOnRegion(Region &Rgn, const Analyses &A) final;
128};
129
130} // namespace sandboxir
131
132} // namespace llvm
133
134#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:170
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
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:88
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.