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"
25#include <optional>
26
27namespace llvm::sandboxir {
28
29class Utils {
30public:
31 /// \Returns the expected type of \p Value V. For most Values this is
32 /// equivalent to getType, but for stores returns the stored type, rather
33 /// than void, and for ReturnInsts returns the returned type.
34 static Type *getExpectedType(const Value *V) {
35 if (auto *I = dyn_cast<Instruction>(V)) {
36 // A Return's value operand can be null if it returns void.
37 if (auto *RI = dyn_cast<ReturnInst>(I)) {
38 if (RI->getReturnValue() == nullptr)
39 return RI->getType();
40 }
41 return getExpectedValue(I)->getType();
42 }
43 return V->getType();
44 }
45
46 /// \Returns the expected Value for this instruction. For most instructions,
47 /// this is the instruction itself, but for stores returns the stored
48 /// operand, and for ReturnInstructions returns the returned value.
50 if (auto *SI = dyn_cast<StoreInst>(I))
51 return SI->getValueOperand();
52 if (auto *RI = dyn_cast<ReturnInst>(I))
53 return RI->getReturnValue();
54 return const_cast<Instruction *>(I);
55 }
56
57 /// \Returns the base Value for load or store instruction \p LSI.
58 template <typename LoadOrStoreT>
59 static Value *getMemInstructionBase(const LoadOrStoreT *LSI) {
60 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
61 std::is_same_v<LoadOrStoreT, StoreInst>,
62 "Expected sandboxir::Load or sandboxir::Store!");
63 return LSI->Ctx.getOrCreateValue(
64 getUnderlyingObject(LSI->getPointerOperand()->Val));
65 }
66
67 /// \Returns the number of bits of \p Ty.
68 static unsigned getNumBits(Type *Ty, const DataLayout &DL) {
69 return DL.getTypeSizeInBits(Ty->LLVMTy);
70 }
71
72 /// \Returns the number of bits required to represent the operands or return
73 /// value of \p V in \p DL.
74 static unsigned getNumBits(Value *V, const DataLayout &DL) {
75 Type *Ty = getExpectedType(V);
76 return getNumBits(Ty, DL);
77 }
78
79 /// \Returns the number of bits required to represent the operands or
80 /// return value of \p I.
81 static unsigned getNumBits(Instruction *I) {
82 return I->getDataLayout().getTypeSizeInBits(getExpectedType(I)->LLVMTy);
83 }
84
85 /// Equivalent to MemoryLocation::getOrNone(I).
86 static std::optional<llvm::MemoryLocation>
90
91 /// \Returns the gap between the memory locations accessed by \p I0 and
92 /// \p I1 in bytes. Returns nullopt if the gap can't be determined.
93 template <typename LoadOrStoreT>
94 static std::optional<int> getPointerDiffInBytes(LoadOrStoreT *I0,
95 LoadOrStoreT *I1,
96 ScalarEvolution &SE) {
97 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
98 std::is_same_v<LoadOrStoreT, StoreInst>,
99 "Expected sandboxir::Load or sandboxir::Store!");
100 llvm::Value *Opnd0 = I0->getPointerOperand()->Val;
101 llvm::Value *Opnd1 = I1->getPointerOperand()->Val;
102 llvm::Value *Ptr0 = getUnderlyingObject(Opnd0);
103 llvm::Value *Ptr1 = getUnderlyingObject(Opnd1);
104 if (Ptr0 != Ptr1)
105 return std::nullopt;
107 return getPointersDiff(ElemTy, Opnd0, ElemTy, Opnd1, I0->getDataLayout(),
108 SE, /*StrictCheck=*/false, /*CheckType=*/false);
109 }
110
111 /// \Returns true if \p I0 accesses a memory location lower than \p I1.
112 /// Returns false if the memory locations are equal, or if I1 accesses a
113 /// memory location greater than I0. Returns nullopt if the difference cannot
114 /// be determined.
115 template <typename LoadOrStoreT>
116 static std::optional<bool> atLowerAddress(LoadOrStoreT *I0, LoadOrStoreT *I1,
117 ScalarEvolution &SE) {
118 auto Diff = getPointerDiffInBytes(I0, I1, SE);
119 if (!Diff)
120 return std::nullopt;
121 return *Diff > 0;
122 }
123
124 /// Equivalent to BatchAA::getModRefInfo().
125 static ModRefInfo
127 const std::optional<MemoryLocation> &OptLoc) {
128 return BatchAA.getModRefInfo(cast<llvm::Instruction>(I->Val), OptLoc);
129 }
130
131 /// Equivalent to llvm::verifyFunction().
132 /// \Returns true if the IR is broken.
133 static bool verifyFunction(const Function *F, raw_ostream &OS) {
134 const auto &LLVMF = *cast<llvm::Function>(F->Val);
135 return llvm::verifyFunction(LLVMF, &OS);
136 }
137
139 Context &Ctx, unsigned BitWidth,
140 unsigned AddressSpace = 0,
141 Align Alignment = Align(1),
142 unsigned *Fast = nullptr) {
143 return TTI.allowsMisalignedMemoryAccesses(Ctx.LLVMCtx, BitWidth,
144 AddressSpace, Alignment, Fast);
145 }
146
147 template <typename ValueRangeT>
148 static void propagateMetadata(Instruction *I, const ValueRangeT &VL) {
149 auto *LLVM_I = cast<llvm::Instruction>(I->Val);
151 LLVM_VL.reserve(VL.size());
152 for (Value *V : VL)
153 LLVM_VL.push_back(V->Val);
154 llvm::propagateMetadata(LLVM_I, LLVM_VL);
155 }
156};
157
158} // namespace llvm::sandboxir
159
160#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:64
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:297
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:132
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:49
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:74
static bool TTIAllowsMisalignedMemoryAccesses(TargetTransformInfo &TTI, Context &Ctx, unsigned BitWidth, unsigned AddressSpace=0, Align Alignment=Align(1), unsigned *Fast=nullptr)
Definition Utils.h:138
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:94
static ModRefInfo aliasAnalysisGetModRefInfo(BatchAAResults &BatchAA, const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Equivalent to BatchAA::getModRefInfo().
Definition Utils.h:126
static void propagateMetadata(Instruction *I, const ValueRangeT &VL)
Definition Utils.h:148
static unsigned getNumBits(Type *Ty, const DataLayout &DL)
\Returns the number of bits of Ty.
Definition Utils.h:68
static Type * getExpectedType(const Value *V)
\Returns the expected type of Value V.
Definition Utils.h:34
static unsigned getNumBits(Instruction *I)
\Returns the number of bits required to represent the operands or return value of I.
Definition Utils.h:81
static std::optional< llvm::MemoryLocation > memoryLocationGetOrNone(const Instruction *I)
Equivalent to MemoryLocation::getOrNone(I).
Definition Utils.h:87
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:116
static Value * getMemInstructionBase(const LoadOrStoreT *LSI)
\Returns the base Value for load or store instruction LSI.
Definition Utils.h:59
static bool verifyFunction(const Function *F, raw_ostream &OS)
Equivalent to llvm::verifyFunction().
Definition Utils.h:133
static Value * getExpectedValue(const Instruction *I)
\Returns the expected Value for this instruction.
Definition Utils.h:49
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 * propagateMetadata(Instruction *I, ArrayRef< Value * > VL)
Specifically, let Kinds = [MD_tbaa, MD_alias_scope, MD_noalias, MD_fpmath, MD_nontemporal,...
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