LLVM 24.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.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 SandboxIR related convenience functions that don't belong in
10// other classes.
11
12#ifndef LLVM_SANDBOXIR_UTILS_H
13#define LLVM_SANDBOXIR_UTILS_H
14
22#include "llvm/IR/Verifier.h"
26#include <optional>
27
28namespace llvm::sandboxir {
29
30class Utils {
31public:
32 /// \Returns the expected type of \p Value V. For most Values this is
33 /// equivalent to getType, but for stores returns the stored type, rather
34 /// than void, and for ReturnInsts returns the returned type.
35 static Type *getExpectedType(const Value *V) {
36 if (auto *I = dyn_cast<Instruction>(V)) {
37 // A Return's value operand can be null if it returns void.
38 if (auto *RI = dyn_cast<ReturnInst>(I)) {
39 if (RI->getReturnValue() == nullptr)
40 return RI->getType();
41 }
42 return getExpectedValue(I)->getType();
43 }
44 return V->getType();
45 }
46
47 /// \Returns the expected Value for this instruction. For most instructions,
48 /// this is the instruction itself, but for stores returns the stored
49 /// operand, and for ReturnInstructions returns the returned value.
51 if (auto *SI = dyn_cast<StoreInst>(I))
52 return SI->getValueOperand();
53 if (auto *RI = dyn_cast<ReturnInst>(I))
54 return RI->getReturnValue();
55 return const_cast<Instruction *>(I);
56 }
57
58 /// \Returns the base Value for load or store instruction \p LSI.
59 template <typename LoadOrStoreT>
60 static Value *getMemInstructionBase(const LoadOrStoreT *LSI) {
61 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
62 std::is_same_v<LoadOrStoreT, StoreInst>,
63 "Expected sandboxir::Load or sandboxir::Store!");
64 return LSI->Ctx.getOrCreateValue(
65 getUnderlyingObject(LSI->getPointerOperand()->Val));
66 }
67
68 /// \Returns the number of bits of \p Ty.
69 static unsigned getNumBits(Type *Ty, const DataLayout &DL) {
70 return DL.getTypeSizeInBits(Ty->LLVMTy);
71 }
72
73 /// \Returns the number of bits required to represent the operands or return
74 /// value of \p V in \p DL.
75 static unsigned getNumBits(Value *V, const DataLayout &DL) {
76 Type *Ty = getExpectedType(V);
77 return getNumBits(Ty, DL);
78 }
79
80 /// \Returns the number of bits required to represent the operands or
81 /// return value of \p I.
82 static unsigned getNumBits(Instruction *I) {
83 return I->getDataLayout().getTypeSizeInBits(getExpectedType(I)->LLVMTy);
84 }
85
86 /// Equivalent to MemoryLocation::getOrNone(I).
87 static std::optional<llvm::MemoryLocation>
91
92 /// \Returns the gap between the memory locations accessed by \p I0 and
93 /// \p I1 in bytes. Returns nullopt if the gap can't be determined.
94 template <typename LoadOrStoreT>
95 static std::optional<int> getPointerDiffInBytes(LoadOrStoreT *I0,
96 LoadOrStoreT *I1,
97 ScalarEvolution &SE) {
98 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
99 std::is_same_v<LoadOrStoreT, StoreInst>,
100 "Expected sandboxir::Load or sandboxir::Store!");
101 llvm::Value *Opnd0 = I0->getPointerOperand()->Val;
102 llvm::Value *Opnd1 = I1->getPointerOperand()->Val;
103 llvm::Value *Ptr0 = getUnderlyingObject(Opnd0);
104 llvm::Value *Ptr1 = getUnderlyingObject(Opnd1);
105 if (Ptr0 != Ptr1)
106 return std::nullopt;
108 return getPointersDiff(ElemTy, Opnd0, ElemTy, Opnd1, I0->getDataLayout(),
109 SE, /*StrictCheck=*/false, /*CheckType=*/false);
110 }
111
112 /// \Returns true if \p I0 accesses a memory location lower than \p I1.
113 /// Returns false if the memory locations are equal, or if I1 accesses a
114 /// memory location greater than I0. Returns nullopt if the difference cannot
115 /// be determined.
116 template <typename LoadOrStoreT>
117 static std::optional<bool> atLowerAddress(LoadOrStoreT *I0, LoadOrStoreT *I1,
118 ScalarEvolution &SE) {
119 auto Diff = getPointerDiffInBytes(I0, I1, SE);
120 if (!Diff)
121 return std::nullopt;
122 return *Diff > 0;
123 }
124
125 /// Equivalent to BatchAA::getModRefInfo().
126 static ModRefInfo
128 const std::optional<MemoryLocation> &OptLoc) {
129 return BatchAA.getModRefInfo(cast<llvm::Instruction>(I->Val), OptLoc);
130 }
131
132 /// Equivalent to llvm::verifyFunction().
133 /// \Returns true if the IR is broken.
134 static bool verifyFunction(const Function *F, raw_ostream &OS) {
135 const auto &LLVMF = *cast<llvm::Function>(F->Val);
136 return llvm::verifyFunction(LLVMF, &OS);
137 }
138
140 Context &Ctx, unsigned BitWidth,
141 unsigned AddressSpace = 0,
142 Align Alignment = Align(1),
143 unsigned *Fast = nullptr) {
144 return TTI.allowsMisalignedMemoryAccesses(Ctx.LLVMCtx, BitWidth,
145 AddressSpace, Alignment, Fast);
146 }
147
148 template <typename ValueRangeT>
150 const ValueRangeT &VL) {
151 auto *LLVM_I = cast<llvm::Instruction>(I->Val);
153 LLVM_VL.reserve(VL.size());
154 for (Value *V : VL)
155 LLVM_VL.push_back(V->Val);
157 }
158};
159
160} // namespace llvm::sandboxir
161
162#endif // LLVM_SANDBOXIR_UTILS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
This pass exposes codegen information to IR-level passes.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static LLVM_ABI std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
The main scalar evolution driver.
LLVMContext & getContext() const
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:292
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition Context.h:139
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition Type.h:48
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:75
static bool TTIAllowsMisalignedMemoryAccesses(TargetTransformInfo &TTI, Context &Ctx, unsigned BitWidth, unsigned AddressSpace=0, Align Alignment=Align(1), unsigned *Fast=nullptr)
Definition Utils.h:139
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:95
static ModRefInfo aliasAnalysisGetModRefInfo(BatchAAResults &BatchAA, const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Equivalent to BatchAA::getModRefInfo().
Definition Utils.h:127
static unsigned getNumBits(Type *Ty, const DataLayout &DL)
\Returns the number of bits of Ty.
Definition Utils.h:69
static Type * getExpectedType(const Value *V)
\Returns the expected type of Value V.
Definition Utils.h:35
static unsigned getNumBits(Instruction *I)
\Returns the number of bits required to represent the operands or return value of I.
Definition Utils.h:82
static std::optional< llvm::MemoryLocation > memoryLocationGetOrNone(const Instruction *I)
Equivalent to MemoryLocation::getOrNone(I).
Definition Utils.h:88
static std::optional< bool > atLowerAddress(LoadOrStoreT *I0, LoadOrStoreT *I1, ScalarEvolution &SE)
\Returns true if I0 accesses a memory location lower than I1.
Definition Utils.h:117
static Value * getMemInstructionBase(const LoadOrStoreT *LSI)
\Returns the base Value for load or store instruction LSI.
Definition Utils.h:60
static bool verifyFunction(const Function *F, raw_ostream &OS)
Equivalent to llvm::verifyFunction().
Definition Utils.h:134
static void propagateMetadataForVectorization(Instruction *I, const ValueRangeT &VL)
Definition Utils.h:149
static Value * getExpectedValue(const Instruction *I)
\Returns the expected Value for this instruction.
Definition Utils.h:50
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
Context & Ctx
All values point to the context.
Definition Value.h:201
LLVM_ABI Type * getType() const
Definition Value.cpp:46
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_ABI Instruction * propagateMetadataForVectorization(Instruction *I, ArrayRef< Value * > VL)
Propagate metadata that can be preserved after vectorization from VL to I, retaining only information...
LLVM_ABI std::optional< int64_t > getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB, Value *PtrB, const DataLayout &DL, ScalarEvolution &SE, bool StrictCheck=false, bool CheckType=true)
Returns the distance between the pointers PtrA and PtrB iff they are compatible and it is possible to...
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
TargetTransformInfo TTI
@ Fast
Assign the register banks as fast as possible (default).
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39