LLVM 20.0.0git
Context.h
Go to the documentation of this file.
1//===- Context.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#ifndef LLVM_SANDBOXIR_CONTEXT_H
10#define LLVM_SANDBOXIR_CONTEXT_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/MapVector.h"
15#include "llvm/IR/LLVMContext.h"
17#include "llvm/SandboxIR/Type.h"
18
19#include <cstdint>
20
21namespace llvm::sandboxir {
22
23class Argument;
24class BBIterator;
25class Constant;
26class Module;
27class Value;
28
29class Context {
30public:
31 // A EraseInstrCallback receives the instruction about to be erased.
32 using EraseInstrCallback = std::function<void(Instruction *)>;
33 // A CreateInstrCallback receives the instruction about to be created.
34 using CreateInstrCallback = std::function<void(Instruction *)>;
35 // A MoveInstrCallback receives the instruction about to be moved, the
36 // destination BB and an iterator pointing to the insertion position.
38 std::function<void(Instruction *, const BBIterator &)>;
39
40 /// An ID for a registered callback. Used for deregistration. Using a 64-bit
41 /// integer so we don't have to worry about the unlikely case of overflowing
42 /// a 32-bit counter.
44
45protected:
47 friend class Type; // For LLVMCtx.
48 friend class PointerType; // For LLVMCtx.
49 friend class IntegerType; // For LLVMCtx.
50 friend class StructType; // For LLVMCtx.
51 friend class Region; // For LLVMCtx.
52 friend class IRSnapshotChecker; // To snapshot LLVMModuleToModuleMap.
53
55
56 /// Maps LLVM Value to the corresponding sandboxir::Value. Owns all
57 /// SandboxIR objects.
59
60 /// Maps an LLVM Module to the corresponding sandboxir::Module.
62
63 /// Type has a protected destructor to prohibit the user from managing the
64 /// lifetime of the Type objects. Context is friend of Type, and this custom
65 /// deleter can destroy Type.
66 struct TypeDeleter {
67 void operator()(Type *Ty) { delete Ty; }
68 };
69 /// Maps LLVM Type to the corresonding sandboxir::Type. Owns all Sandbox IR
70 /// Type objects.
72
73 /// Callbacks called when an IR instruction is about to get erased. Keys are
74 /// used as IDs for deregistration.
76 /// Callbacks called when an IR instruction is about to get created. Keys are
77 /// used as IDs for deregistration.
79 /// Callbacks called when an IR instruction is about to get moved. Keys are
80 /// used as IDs for deregistration.
82
83 /// A counter used for assigning callback IDs during registration. The same
84 /// counter is used for all kinds of callbacks so we can detect mismatched
85 /// registration/deregistration.
87
88 /// Remove \p V from the maps and returns the unique_ptr.
89 std::unique_ptr<Value> detachLLVMValue(llvm::Value *V);
90 /// Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
91 /// detaches \p V from the underlying IR.
92 std::unique_ptr<Value> detach(Value *V);
93 friend class Instruction; // For detach().
94 /// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
95 Value *registerValue(std::unique_ptr<Value> &&VPtr);
96 friend class EraseFromParent; // For registerValue().
97 /// This is the actual function that creates sandboxir values for \p V,
98 /// and among others handles all instruction types.
100 /// Get or create a sandboxir::Argument for an existing LLVM IR \p LLVMArg.
102 /// Get or create a sandboxir::Value for an existing LLVM IR \p LLVMV.
104 return getOrCreateValueInternal(LLVMV, 0);
105 }
106 /// Get or create a sandboxir::Constant from an existing LLVM IR \p LLVMC.
108 friend class Utils; // For getMemoryBase
109
112 void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where);
113
114 // Friends for getOrCreateConstant().
115#define DEF_CONST(ID, CLASS) friend class CLASS;
116#include "llvm/SandboxIR/Values.def"
117
118 /// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
119 /// also create all contents of the block.
121 friend class BasicBlock; // For getOrCreateValue().
122
124 auto &getLLVMIRBuilder() { return LLVMIRBuilder; }
125
127 friend VAArgInst; // For createVAArgInst()
129 friend FreezeInst; // For createFreezeInst()
131 friend FenceInst; // For createFenceInst()
133 friend SelectInst; // For createSelectInst()
135 friend InsertElementInst; // For createInsertElementInst()
137 friend ExtractElementInst; // For createExtractElementInst()
139 friend ShuffleVectorInst; // For createShuffleVectorInst()
141 friend ExtractValueInst; // For createExtractValueInst()
143 friend InsertValueInst; // For createInsertValueInst()
145 friend BranchInst; // For createBranchInst()
147 friend LoadInst; // For createLoadInst()
149 friend StoreInst; // For createStoreInst()
151 friend ReturnInst; // For createReturnInst()
153 friend CallInst; // For createCallInst()
155 friend InvokeInst; // For createInvokeInst()
157 friend CallBrInst; // For createCallBrInst()
159 friend LandingPadInst; // For createLandingPadInst()
161 friend CatchPadInst; // For createCatchPadInst()
163 friend CleanupPadInst; // For createCleanupPadInst()
165 friend CatchReturnInst; // For createCatchReturnInst()
167 friend CleanupReturnInst; // For createCleanupReturnInst()
169 friend GetElementPtrInst; // For createGetElementPtrInst()
171 friend CatchSwitchInst; // For createCatchSwitchInst()
173 friend ResumeInst; // For createResumeInst()
175 friend SwitchInst; // For createSwitchInst()
177 friend UnaryOperator; // For createUnaryOperator()
179 friend BinaryOperator; // For createBinaryOperator()
181 friend AtomicRMWInst; // For createAtomicRMWInst()
183 friend AtomicCmpXchgInst; // For createAtomicCmpXchgInst()
185 friend AllocaInst; // For createAllocaInst()
187 friend CastInst; // For createCastInst()
189 friend PHINode; // For createPHINode()
191 friend UnreachableInst; // For createUnreachableInst()
193 friend CmpInst; // For createCmpInst()
195 friend ICmpInst; // For createICmpInst()
197 friend FCmpInst; // For createFCmpInst()
198
199public:
201 ~Context();
202
204 /// Convenience function for `getTracker().save()`
205 void save() { IRTracker.save(); }
206 /// Convenience function for `getTracker().revert()`
207 void revert() { IRTracker.revert(); }
208 /// Convenience function for `getTracker().accept()`
209 void accept() { IRTracker.accept(); }
210
212 const sandboxir::Value *getValue(const llvm::Value *V) const {
213 return getValue(const_cast<llvm::Value *>(V));
214 }
215
216 Module *getModule(llvm::Module *LLVMM) const;
217
219
221 if (LLVMTy == nullptr)
222 return nullptr;
223 auto Pair = LLVMTypeToTypeMap.insert({LLVMTy, nullptr});
224 auto It = Pair.first;
225 if (Pair.second)
226 It->second = std::unique_ptr<Type, TypeDeleter>(new Type(LLVMTy, *this));
227 return It->second.get();
228 }
229
230 /// Create a sandboxir::Function for an existing LLVM IR \p F, including all
231 /// blocks and instructions.
232 /// This is the main API function for creating Sandbox IR.
233 /// Note: this will not fully populate its parent module. The only globals
234 /// that will be available are those used within the function.
236
237 /// Create a sandboxir::Module corresponding to \p LLVMM.
239
240 /// \Returns the number of values registered with Context.
241 size_t getNumValues() const { return LLVMValueToValueMap.size(); }
242
243 /// Register a callback that gets called when a SandboxIR instruction is about
244 /// to be removed from its parent. Note that this will also be called when
245 /// reverting the creation of an instruction.
246 /// \Returns a callback ID for later deregistration.
249
250 /// Register a callback that gets called right after a SandboxIR instruction
251 /// is created. Note that this will also be called when reverting the removal
252 /// of an instruction.
253 /// \Returns a callback ID for later deregistration.
256
257 /// Register a callback that gets called when a SandboxIR instruction is about
258 /// to be moved. Note that this will also be called when reverting a move.
259 /// \Returns a callback ID for later deregistration.
262
263 // TODO: Add callbacks for instructions inserted/removed if needed.
264};
265
266} // namespace llvm::sandboxir
267
268#endif // LLVM_SANDBOXIR_CONTEXT_H
This file defines the DenseMap class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
This file implements a map that provides insertion order iteration.
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
This file defines the SmallVector class.
an instruction to allocate memory on the stack
Definition: Instructions.h:63
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:501
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:704
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Conditional or Unconditional Branch instruction.
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.
Definition: InstrTypes.h:444
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
This is an important base class in LLVM.
Definition: Constant.h:42
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
An instruction for ordering other memory operations.
Definition: Instructions.h:424
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
Definition: Instructions.h:933
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
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.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
An instruction for reading from memory.
Definition: Instructions.h:176
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
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.
An instruction for storing to memory.
Definition: Instructions.h:292
Multiway switch.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
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.
Definition: Value.h:74
Argument of a sandboxir::Function.
Definition: Argument.h:18
Iterator for Instructions in a `BasicBlock.
Definition: BasicBlock.h:23
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:67
BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Create a sandboxir::BasicBlock for an existing LLVM IR BB.
Definition: Context.cpp:421
std::function< void(Instruction *)> CreateInstrCallback
Definition: Context.h:34
GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
Definition: Context.cpp:546
MapVector< CallbackID, CreateInstrCallback > CreateInstrCallbacks
Callbacks called when an IR instruction is about to get created.
Definition: Context.h:78
friend class Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: Context.h:47
CmpInst * createCmpInst(llvm::CmpInst *I)
CallBrInst * createCallBrInst(llvm::CallBrInst *I)
Definition: Context.cpp:513
Module * createModule(llvm::Module *LLVMM)
Create a sandboxir::Module corresponding to LLVMM.
Definition: Context.cpp:648
IRBuilder< ConstantFolder > LLVMIRBuilder
Definition: Context.h:123
MapVector< CallbackID, EraseInstrCallback > EraseInstrCallbacks
Callbacks called when an IR instruction is about to get erased.
Definition: Context.h:75
sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:601
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
Definition: Context.cpp:407
ReturnInst * createReturnInst(llvm::ReturnInst *I)
Definition: Context.cpp:498
void runEraseInstrCallbacks(Instruction *I)
Definition: Context.cpp:666
Module * getModule(llvm::Module *LLVMM) const
Definition: Context.cpp:614
VAArgInst * createVAArgInst(llvm::VAArgInst *SI)
Definition: Context.cpp:430
auto & getLLVMIRBuilder()
Definition: Context.h:124
CleanupReturnInst * createCleanupReturnInst(llvm::CleanupReturnInst *I)
Definition: Context.cpp:540
AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Definition: Context.cpp:581
Type * getType(llvm::Type *LLVMTy)
Definition: Context.h:220
void runCreateInstrCallbacks(Instruction *I)
Definition: Context.cpp:671
AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
Definition: Context.cpp:571
InsertValueInst * createInsertValueInst(llvm::InsertValueInst *IVI)
Definition: Context.cpp:477
const sandboxir::Value * getValue(const llvm::Value *V) const
Definition: Context.h:212
DenseMap< llvm::Type *, std::unique_ptr< Type, TypeDeleter > > LLVMTypeToTypeMap
Maps LLVM Type to the corresonding sandboxir::Type.
Definition: Context.h:71
void accept()
Convenience function for getTracker().accept()
Definition: Context.h:209
FCmpInst * createFCmpInst(llvm::FCmpInst *I)
Definition: Context.cpp:597
std::function< void(Instruction *)> EraseInstrCallback
Definition: Context.h:32
std::function< void(Instruction *, const BBIterator &)> MoveInstrCallback
Definition: Context.h:38
ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
Definition: Context.cpp:451
Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
Definition: Context.cpp:417
BranchInst * createBranchInst(llvm::BranchInst *I)
Definition: Context.cpp:483
ShuffleVectorInst * createShuffleVectorInst(llvm::ShuffleVectorInst *SVI)
Definition: Context.cpp:465
BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
Definition: Context.cpp:567
Module * getOrCreateModule(llvm::Module *LLVMM)
Definition: Context.cpp:621
LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: Context.cpp:488
DenseMap< llvm::Value *, std::unique_ptr< Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
Definition: Context.h:58
FreezeInst * createFreezeInst(llvm::FreezeInst *SI)
Definition: Context.cpp:435
PHINode * createPHINode(llvm::PHINode *I)
Definition: Context.cpp:589
CallbackID registerCreateInstrCallback(CreateInstrCallback CB)
Register a callback that gets called right after a SandboxIR instruction is created.
Definition: Context.cpp:700
CatchPadInst * createCatchPadInst(llvm::CatchPadInst *I)
Definition: Context.cpp:527
ICmpInst * createICmpInst(llvm::ICmpInst *I)
Definition: Context.cpp:593
MapVector< CallbackID, MoveInstrCallback > MoveInstrCallbacks
Callbacks called when an IR instruction is about to get moved.
Definition: Context.h:81
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: Context.cpp:27
void unregisterCreateInstrCallback(CallbackID ID)
Definition: Context.cpp:707
ExtractValueInst * createExtractValueInst(llvm::ExtractValueInst *IVI)
Definition: Context.cpp:471
void revert()
Convenience function for getTracker().revert()
Definition: Context.h:207
uint64_t CallbackID
An ID for a registered callback.
Definition: Context.h:43
CastInst * createCastInst(llvm::CastInst *I)
Definition: Context.cpp:585
DenseMap< llvm::Module *, std::unique_ptr< Module > > LLVMModuleToModuleMap
Maps an LLVM Module to the corresponding sandboxir::Module.
Definition: Context.h:61
CallbackID NextCallbackID
A counter used for assigning callback IDs during registration.
Definition: Context.h:86
StoreInst * createStoreInst(llvm::StoreInst *SI)
Definition: Context.cpp:493
CatchReturnInst * createCatchReturnInst(llvm::CatchReturnInst *I)
Definition: Context.cpp:535
CatchSwitchInst * createCatchSwitchInst(llvm::CatchSwitchInst *I)
Definition: Context.cpp:551
void unregisterMoveInstrCallback(CallbackID ID)
Definition: Context.cpp:720
void save()
Convenience function for getTracker().save()
Definition: Context.h:205
CleanupPadInst * createCleanupPadInst(llvm::CleanupPadInst *I)
Definition: Context.cpp:531
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: Context.h:103
FenceInst * createFenceInst(llvm::FenceInst *SI)
Definition: Context.cpp:440
CallInst * createCallInst(llvm::CallInst *I)
Definition: Context.cpp:503
SwitchInst * createSwitchInst(llvm::SwitchInst *I)
Definition: Context.cpp:559
void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where)
Definition: Context.cpp:676
UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
Definition: Context.cpp:563
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...
Definition: Context.cpp:55
InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Definition: Context.cpp:508
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
Definition: Context.cpp:34
LandingPadInst * createLandingPadInst(llvm::LandingPadInst *I)
Definition: Context.cpp:523
InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
Definition: Context.cpp:458
Function * createFunction(llvm::Function *F)
Create a sandboxir::Function for an existing LLVM IR F, including all blocks and instructions.
Definition: Context.cpp:630
SelectInst * createSelectInst(llvm::SelectInst *SI)
Definition: Context.cpp:445
ResumeInst * createResumeInst(llvm::ResumeInst *I)
Definition: Context.cpp:555
void unregisterEraseInstrCallback(CallbackID ID)
Definition: Context.cpp:693
LLVMContext & LLVMCtx
Definition: Context.h:46
Tracker & getTracker()
Definition: Context.h:203
friend class BasicBlock
Various leaf nodes.
Definition: Context.h:121
CallbackID registerMoveInstrCallback(MoveInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be moved.
Definition: Context.cpp:713
UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
Definition: Context.cpp:518
AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
Definition: Context.cpp:576
CallbackID registerEraseInstrCallback(EraseInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be removed from its par...
Definition: Context.cpp:686
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: Context.h:241
std::unique_ptr< Value > detachLLVMValue(llvm::Value *V)
Remove V from the maps and returns the unique_ptr.
Definition: Context.cpp:16
A class that saves hashes and textual IR snapshots of functions in a SandboxIR Context,...
Definition: Tracker.h:77
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
Class to represent integer types.
Definition: Type.h:462
In SandboxIR the Module is mainly used to access the list of global objects.
Definition: Module.h:31
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition: Region.h:54
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:440
void revert()
Stops tracking and reverts to saved state.
Definition: Tracker.cpp:348
void save()
Turns on IR tracking.
Definition: Tracker.cpp:341
void accept()
Stops tracking and accept changes.
Definition: Tracker.cpp:359
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
Type has a protected destructor to prohibit the user from managing the lifetime of the Type objects.
Definition: Context.h:66