9#ifndef LLVM_SANDBOXIR_CONTEXT_H
10#define LLVM_SANDBOXIR_CONTEXT_H
42 std::function<void(
Instruction *,
const BBIterator &)>;
59 explicit CallbackID(
ValTy Val) : Val{Val} {
156#define DEF_CONST(ID, CLASS) friend class CLASS;
157#include "llvm/SandboxIR/Values.def"
248 return LLVMCtx.getLibraryOptions();
268 if (LLVMTy ==
nullptr)
271 auto It = Pair.first;
273 It->second = std::unique_ptr<Type, TypeDeleter>(
new Type(LLVMTy, *
this));
274 return It->second.get();
294 [[maybe_unused]]
static constexpr int MaxRegisteredCallbacks = 16;
296 template <
typename CBT,
typename CBVecT>
297 CallbackID registerCallbackCommon(CBT CB,
StringRef CBName,
298 bool AllowDuplicateNames, CBVecT &CBVec,
299 std::optional<CallbackID> BeforeID) {
300 assert(CBVec.size() <= MaxRegisteredCallbacks &&
301 "EraseInstrCallbacks size limit exceeded");
303 [CBName](
const auto &Entry) {
304 return Entry.CBName == CBName;
306 "Callback with CBName already exists!");
307 auto BeforeIt = CBVec.end();
309 BeforeIt =
find_if(CBVec, [BeforeID](
const auto &Entry) {
310 return Entry.ID == *BeforeID;
312 assert(BeforeIt != CBVec.end() &&
"Not found!");
314 CallbackID
ID{NextCallbackID++};
315 CBVec.insert(BeforeIt, CallbackEntry<CBT>(ID, std::move(CB), CBName));
329 registerEraseInstrCallback(EraseInstrCallback CB, StringRef CBName,
330 bool AllowDuplicateNames =
false,
331 std::optional<CallbackID> BeforeID = std::nullopt);
332 void unregisterEraseInstrCallback(CallbackID ID);
342 CallbackID registerCreateInstrCallback(
343 CreateInstrCallback CB, StringRef CBName,
344 bool AllowDuplicateNames =
false,
345 std::optional<CallbackID> BeforeID = std::nullopt);
346 void unregisterCreateInstrCallback(CallbackID ID);
356 registerMoveInstrCallback(MoveInstrCallback CB, StringRef CBName,
357 bool AllowDuplicateNames =
false,
358 std::optional<CallbackID> BeforeID = std::nullopt);
359 void unregisterMoveInstrCallback(CallbackID ID);
368 registerSetUseCallback(SetUseCallback CB, StringRef CBName,
369 bool AllowDuplicateNames =
false,
370 std::optional<CallbackID> BeforeID = std::nullopt);
371 void unregisterSetUseCallback(CallbackID ID);
382 return ReprInfo::getHashValue(ID.Val);
385 return ReprInfo::isEqual(
LHS.Val,
RHS.Val);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
static constexpr Value * getValue(Ty &ValueOrUse)
This file implements a map that provides insertion order iteration.
This file defines the SmallVector class.
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
This class is the base class for the comparison instructions.
Conditional Branch instruction.
This is an important base class in LLVM.
This instruction compares its operands according to the predicate given to the constructor.
An instruction for ordering other memory operations.
This class represents a freeze function that returns random concrete value if an operand is either a ...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This instruction compares its operands according to the predicate given to the constructor.
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
This is an important class for using LLVM in a threaded context.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
An instruction for reading from memory.
A Module instance is used to store all the information related to an LLVM module.
Resume the propagation of an exception.
Return a value (possibly void), from a function.
This class represents the LLVM 'select' instruction.
This instruction constructs a fixed permutation of two input vectors.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
Unconditional Branch instruction.
This function has undefined behavior.
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM Value Representation.
Argument of a sandboxir::Function.
An ID for a registered callback.
static constexpr ValTy InvalidVal
bool operator==(const CallbackID &Other) const
std::function< void(Instruction *)> CreateInstrCallback
GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
friend class Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
friend class EraseFromParent
CmpInst * createCmpInst(llvm::CmpInst *I)
CallBrInst * createCallBrInst(llvm::CallBrInst *I)
std::function< void(const Use &, Value *)> SetUseCallback
SmallVector< CallbackEntry< CreateInstrCallback > > CreateInstrCallbacks
Callbacks called when an IR instruction is about to get created.
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
void revert(bool RevertAll=false)
Convenience function for getTracker().revert()
friend class IRSnapshotChecker
Context & operator=(const Context &)=delete
ReturnInst * createReturnInst(llvm::ReturnInst *I)
UncondBrInst * createUncondBrInst(llvm::UncondBrInst *UBI)
void runEraseInstrCallbacks(Instruction *I)
SmallVector< CallbackEntry< EraseInstrCallback > > EraseInstrCallbacks
Callbacks called when an IR instruction is about to get erased.
friend ExtractElementInst
VAArgInst * createVAArgInst(llvm::VAArgInst *SI)
CleanupReturnInst * createCleanupReturnInst(llvm::CleanupReturnInst *I)
void accept(bool AcceptAll=false)
Convenience function for getTracker().accept()
AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Type * getType(llvm::Type *LLVMTy)
void runCreateInstrCallbacks(Instruction *I)
AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
InsertValueInst * createInsertValueInst(llvm::InsertValueInst *IVI)
const sandboxir::Value * getValue(const llvm::Value *V) const
DenseMap< llvm::Type *, std::unique_ptr< Type, TypeDeleter > > LLVMTypeToTypeMap
Maps LLVM Type to the corresonding sandboxir::Type.
FCmpInst * createFCmpInst(llvm::FCmpInst *I)
std::function< void(Instruction *)> EraseInstrCallback
friend class ConstantDataSequential
CallbackID::ValTy NextCallbackID
A counter used for assigning callback IDs during registration.
ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
ShuffleVectorInst * createShuffleVectorInst(llvm::ShuffleVectorInst *SVI)
BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
LoadInst * createLoadInst(llvm::LoadInst *LI)
DenseMap< llvm::Value *, std::unique_ptr< Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
FreezeInst * createFreezeInst(llvm::FreezeInst *SI)
PHINode * createPHINode(llvm::PHINode *I)
Context(LLVMContext &LLVMCtx)
CatchPadInst * createCatchPadInst(llvm::CatchPadInst *I)
void clear()
Clears function-level state.
CondBrInst * createCondBrInst(llvm::CondBrInst *CBI)
SmallVector< CallbackEntry< SetUseCallback > > SetUseCallbacks
Callbacks called when a Use gets its source set.
ICmpInst * createICmpInst(llvm::ICmpInst *I)
Context(const Context &)=delete
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
ExtractValueInst * createExtractValueInst(llvm::ExtractValueInst *IVI)
CastInst * createCastInst(llvm::CastInst *I)
DenseMap< llvm::Module *, std::unique_ptr< Module > > LLVMModuleToModuleMap
Maps an LLVM Module to the corresponding sandboxir::Module.
StoreInst * createStoreInst(llvm::StoreInst *SI)
CatchReturnInst * createCatchReturnInst(llvm::CatchReturnInst *I)
CatchSwitchInst * createCatchSwitchInst(llvm::CatchSwitchInst *I)
std::function< void(Instruction *, const BBIterator &)> MoveInstrCallback
void save()
Convenience function for getTracker().save()
CleanupPadInst * createCleanupPadInst(llvm::CleanupPadInst *I)
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
cl::LibraryOptionsContext & getLibraryOptions() const
FenceInst * createFenceInst(llvm::FenceInst *SI)
CallInst * createCallInst(llvm::CallInst *I)
SwitchInst * createSwitchInst(llvm::SwitchInst *I)
void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where)
UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
Value * getOrCreateValueInternal(llvm::Value *V, llvm::User *U=nullptr)
This is the actual function that creates sandboxir values for V, and among others handles all instruc...
InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
LandingPadInst * createLandingPadInst(llvm::LandingPadInst *I)
InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
SelectInst * createSelectInst(llvm::SelectInst *SI)
SmallVector< CallbackEntry< MoveInstrCallback > > MoveInstrCallbacks
Callbacks called when an IR instruction is about to get moved.
ResumeInst * createResumeInst(llvm::ResumeInst *I)
friend class BasicBlock
Various leaf nodes.
UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
void runSetUseCallbacks(const Use &U, Value *NewSrc)
size_t getNumValues() const
\Returns the number of values registered with Context.
std::unique_ptr< Value > detachLLVMValue(llvm::Value *V)
Remove V from the maps and returns the unique_ptr.
In SandboxIR the Module is mainly used to access the list of global objects.
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Represents a Def-use/Use-def edge in SandboxIR.
A SandboxIR Value has users. This is the base class.
This is an optimization pass for GlobalISel generic memory operations.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
static unsigned getHashValue(const CallbackID &ID)
DenseMapInfo< CallbackID::ValTy > ReprInfo
sandboxir::Context::CallbackID CallbackID
static bool isEqual(const CallbackID &LHS, const CallbackID &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
CallbackEntry(CallbackID ID, FnT Fn, StringRef CBName)
Type has a protected destructor to prohibit the user from managing the lifetime of the Type objects.
void operator()(Type *Ty)