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