LLVM 20.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 <optional>
22
23namespace llvm::sandboxir {
24
25class Utils {
26public:
27 /// \Returns the expected type of \p Value V. For most Values this is
28 /// equivalent to getType, but for stores returns the stored type, rather
29 /// than void, and for ReturnInsts returns the returned type.
30 static Type *getExpectedType(const Value *V) {
31 if (auto *I = dyn_cast<Instruction>(V)) {
32 // A Return's value operand can be null if it returns void.
33 if (auto *RI = dyn_cast<ReturnInst>(I)) {
34 if (RI->getReturnValue() == nullptr)
35 return RI->getType();
36 }
37 return getExpectedValue(I)->getType();
38 }
39 return V->getType();
40 }
41
42 /// \Returns the expected Value for this instruction. For most instructions,
43 /// this is the instruction itself, but for stores returns the stored
44 /// operand, and for ReturnInstructions returns the returned value.
46 if (auto *SI = dyn_cast<StoreInst>(I))
47 return SI->getValueOperand();
48 if (auto *RI = dyn_cast<ReturnInst>(I))
49 return RI->getReturnValue();
50 return const_cast<Instruction *>(I);
51 }
52
53 /// \Returns the base Value for load or store instruction \p LSI.
54 template <typename LoadOrStoreT>
55 static Value *getMemInstructionBase(const LoadOrStoreT *LSI) {
56 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
57 std::is_same_v<LoadOrStoreT, StoreInst>,
58 "Expected sandboxir::Load or sandboxir::Store!");
59 return LSI->Ctx.getOrCreateValue(
60 getUnderlyingObject(LSI->getPointerOperand()->Val));
61 }
62
63 /// \Returns the number of bits of \p Ty.
64 static unsigned getNumBits(Type *Ty, const DataLayout &DL) {
65 return DL.getTypeSizeInBits(Ty->LLVMTy);
66 }
67
68 /// \Returns the number of bits required to represent the operands or return
69 /// value of \p V in \p DL.
70 static unsigned getNumBits(Value *V, const DataLayout &DL) {
71 Type *Ty = getExpectedType(V);
72 return getNumBits(Ty, DL);
73 }
74
75 /// \Returns the number of bits required to represent the operands or
76 /// return value of \p I.
77 static unsigned getNumBits(Instruction *I) {
78 return I->getDataLayout().getTypeSizeInBits(getExpectedType(I)->LLVMTy);
79 }
80
81 /// Equivalent to MemoryLocation::getOrNone(I).
82 static std::optional<llvm::MemoryLocation>
84 return llvm::MemoryLocation::getOrNone(cast<llvm::Instruction>(I->Val));
85 }
86
87 /// \Returns the gap between the memory locations accessed by \p I0 and
88 /// \p I1 in bytes.
89 template <typename LoadOrStoreT>
90 static std::optional<int> getPointerDiffInBytes(LoadOrStoreT *I0,
91 LoadOrStoreT *I1,
92 ScalarEvolution &SE) {
93 static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
94 std::is_same_v<LoadOrStoreT, StoreInst>,
95 "Expected sandboxir::Load or sandboxir::Store!");
96 llvm::Value *Opnd0 = I0->getPointerOperand()->Val;
97 llvm::Value *Opnd1 = I1->getPointerOperand()->Val;
98 llvm::Value *Ptr0 = getUnderlyingObject(Opnd0);
99 llvm::Value *Ptr1 = getUnderlyingObject(Opnd1);
100 if (Ptr0 != Ptr1)
101 return false;
103 return getPointersDiff(ElemTy, Opnd0, ElemTy, Opnd1, I0->getDataLayout(),
104 SE, /*StrictCheck=*/false, /*CheckType=*/false);
105 }
106
107 /// \Returns true if \p I0 accesses a memory location lower than \p I1.
108 /// Returns false if the difference cannot be determined, if the memory
109 /// locations are equal, or if I1 accesses a memory location greater than I0.
110 template <typename LoadOrStoreT>
111 static bool atLowerAddress(LoadOrStoreT *I0, LoadOrStoreT *I1,
112 ScalarEvolution &SE) {
113 auto Diff = getPointerDiffInBytes(I0, I1, SE);
114 if (!Diff)
115 return false;
116 return *Diff > 0;
117 }
118
119 /// Equivalent to BatchAA::getModRefInfo().
120 static ModRefInfo
122 const std::optional<MemoryLocation> &OptLoc) {
123 return BatchAA.getModRefInfo(cast<llvm::Instruction>(I->Val), OptLoc);
124 }
125};
126
127} // namespace llvm::sandboxir
128
129#endif // LLVM_SANDBOXIR_UTILS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define I(x, y, z)
Definition: MD5.cpp:58
This file provides utility analysis objects describing memory locations.
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 std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
The main scalar evolution driver.
LLVMContext & getContext() const
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt8Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: Context.h:122
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
llvm::Type * LLVMTy
Definition: Type.h:45
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:70
static bool atLowerAddress(LoadOrStoreT *I0, LoadOrStoreT *I1, ScalarEvolution &SE)
\Returns true if I0 accesses a memory location lower than I1.
Definition: Utils.h:111
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:90
static ModRefInfo aliasAnalysisGetModRefInfo(BatchAAResults &BatchAA, const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Equivalent to BatchAA::getModRefInfo().
Definition: Utils.h:121
static unsigned getNumBits(Type *Ty, const DataLayout &DL)
\Returns the number of bits of Ty.
Definition: Utils.h:64
static Type * getExpectedType(const Value *V)
\Returns the expected type of Value V.
Definition: Utils.h:30
static unsigned getNumBits(Instruction *I)
\Returns the number of bits required to represent the operands or return value of I.
Definition: Utils.h:77
static std::optional< llvm::MemoryLocation > memoryLocationGetOrNone(const Instruction *I)
Equivalent to MemoryLocation::getOrNone(I).
Definition: Utils.h:83
static Value * getMemInstructionBase(const LoadOrStoreT *LSI)
\Returns the base Value for load or store instruction LSI.
Definition: Utils.h:55
static Value * getExpectedValue(const Instruction *I)
\Returns the expected Value for this instruction.
Definition: Utils.h:45
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
Context & Ctx
All values point to the context.
Definition: Value.h:172
Type * getType() const
Definition: Value.cpp:46
std::optional< int > 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...
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
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:27