18 std::unique_ptr<Value> Erased;
21 auto *Val = It->second.release();
22 Erased = std::unique_ptr<Value>(Val);
29 assert(V->getSubclassID() != Value::ClassID::Constant &&
30 "Can't detach a constant!");
31 assert(V->getSubclassID() != Value::ClassID::User &&
"Can't detach a user!");
36 assert(VPtr->getSubclassID() != Value::ClassID::User &&
37 "Can't register a user!");
39 Value *V = VPtr.get();
40 [[maybe_unused]]
auto Pair =
42 assert(Pair.second &&
"Already exists!");
60 return It->second.get();
64 switch (LLVMI->getOpcode()) {
65 case llvm::Instruction::VAArg: {
68 return It->second.get();
70 case llvm::Instruction::Freeze: {
74 return It->second.get();
76 case llvm::Instruction::Fence: {
79 return It->second.get();
81 case llvm::Instruction::Select: {
83 It->second = std::unique_ptr<SelectInst>(
new SelectInst(LLVMSel, *
this));
84 return It->second.get();
86 case llvm::Instruction::ExtractElement: {
88 It->second = std::unique_ptr<ExtractElementInst>(
90 return It->second.get();
92 case llvm::Instruction::InsertElement: {
94 It->second = std::unique_ptr<InsertElementInst>(
96 return It->second.get();
98 case llvm::Instruction::ShuffleVector: {
100 It->second = std::unique_ptr<ShuffleVectorInst>(
102 return It->second.get();
104 case llvm::Instruction::ExtractValue: {
106 It->second = std::unique_ptr<ExtractValueInst>(
108 return It->second.get();
110 case llvm::Instruction::InsertValue: {
114 return It->second.get();
116 case llvm::Instruction::UncondBr:
117 case llvm::Instruction::CondBr: {
119 It->second = std::unique_ptr<BranchInst>(
new BranchInst(LLVMBr, *
this));
120 return It->second.get();
122 case llvm::Instruction::Load: {
124 It->second = std::unique_ptr<LoadInst>(
new LoadInst(LLVMLd, *
this));
125 return It->second.get();
127 case llvm::Instruction::Store: {
129 It->second = std::unique_ptr<StoreInst>(
new StoreInst(LLVMSt, *
this));
130 return It->second.get();
132 case llvm::Instruction::Ret: {
135 return It->second.get();
137 case llvm::Instruction::Call: {
140 return It->second.get();
142 case llvm::Instruction::Invoke: {
146 return It->second.get();
148 case llvm::Instruction::CallBr: {
152 return It->second.get();
154 case llvm::Instruction::LandingPad: {
157 std::unique_ptr<LandingPadInst>(
new LandingPadInst(LLVMLPad, *
this));
158 return It->second.get();
160 case llvm::Instruction::CatchPad: {
163 std::unique_ptr<CatchPadInst>(
new CatchPadInst(LLVMCPI, *
this));
164 return It->second.get();
166 case llvm::Instruction::CleanupPad: {
169 std::unique_ptr<CleanupPadInst>(
new CleanupPadInst(LLVMCPI, *
this));
170 return It->second.get();
172 case llvm::Instruction::CatchRet: {
176 return It->second.get();
178 case llvm::Instruction::CleanupRet: {
180 It->second = std::unique_ptr<CleanupReturnInst>(
182 return It->second.get();
184 case llvm::Instruction::GetElementPtr: {
186 It->second = std::unique_ptr<GetElementPtrInst>(
188 return It->second.get();
190 case llvm::Instruction::CatchSwitch: {
192 It->second = std::unique_ptr<CatchSwitchInst>(
194 return It->second.get();
196 case llvm::Instruction::Resume: {
199 std::unique_ptr<ResumeInst>(
new ResumeInst(LLVMResumeInst, *
this));
200 return It->second.get();
202 case llvm::Instruction::Switch: {
205 std::unique_ptr<SwitchInst>(
new SwitchInst(LLVMSwitchInst, *
this));
206 return It->second.get();
208 case llvm::Instruction::FNeg: {
210 It->second = std::unique_ptr<UnaryOperator>(
212 return It->second.get();
214 case llvm::Instruction::Add:
215 case llvm::Instruction::FAdd:
216 case llvm::Instruction::Sub:
217 case llvm::Instruction::FSub:
218 case llvm::Instruction::Mul:
219 case llvm::Instruction::FMul:
220 case llvm::Instruction::UDiv:
221 case llvm::Instruction::SDiv:
222 case llvm::Instruction::FDiv:
223 case llvm::Instruction::URem:
224 case llvm::Instruction::SRem:
225 case llvm::Instruction::FRem:
226 case llvm::Instruction::Shl:
227 case llvm::Instruction::LShr:
228 case llvm::Instruction::AShr:
229 case llvm::Instruction::And:
230 case llvm::Instruction::Or:
231 case llvm::Instruction::Xor: {
233 It->second = std::unique_ptr<BinaryOperator>(
235 return It->second.get();
237 case llvm::Instruction::AtomicRMW: {
239 It->second = std::unique_ptr<AtomicRMWInst>(
241 return It->second.get();
243 case llvm::Instruction::AtomicCmpXchg: {
245 It->second = std::unique_ptr<AtomicCmpXchgInst>(
247 return It->second.get();
249 case llvm::Instruction::Alloca: {
253 return It->second.get();
255 case llvm::Instruction::ZExt:
256 case llvm::Instruction::SExt:
257 case llvm::Instruction::FPToUI:
258 case llvm::Instruction::FPToSI:
259 case llvm::Instruction::FPExt:
260 case llvm::Instruction::PtrToAddr:
261 case llvm::Instruction::PtrToInt:
262 case llvm::Instruction::IntToPtr:
263 case llvm::Instruction::SIToFP:
264 case llvm::Instruction::UIToFP:
265 case llvm::Instruction::Trunc:
266 case llvm::Instruction::FPTrunc:
267 case llvm::Instruction::BitCast:
268 case llvm::Instruction::AddrSpaceCast: {
270 It->second = std::unique_ptr<CastInst>(
new CastInst(LLVMCast, *
this));
271 return It->second.get();
273 case llvm::Instruction::PHI: {
275 It->second = std::unique_ptr<PHINode>(
new PHINode(LLVMPhi, *
this));
276 return It->second.get();
278 case llvm::Instruction::ICmp: {
281 return It->second.get();
283 case llvm::Instruction::FCmp: {
286 return It->second.get();
288 case llvm::Instruction::Unreachable: {
290 It->second = std::unique_ptr<UnreachableInst>(
292 return It->second.get();
297 It->second = std::unique_ptr<OpaqueInst>(
299 return It->second.get();
303 switch (LLVMC->getValueID()) {
304 case llvm::Value::ConstantIntVal:
305 It->second = std::unique_ptr<ConstantInt>(
307 return It->second.get();
308 case llvm::Value::ConstantFPVal:
309 It->second = std::unique_ptr<ConstantFP>(
311 return It->second.get();
312 case llvm::Value::BlockAddressVal:
313 It->second = std::unique_ptr<BlockAddress>(
315 return It->second.get();
316 case llvm::Value::ConstantTokenNoneVal:
317 It->second = std::unique_ptr<ConstantTokenNone>(
319 return It->second.get();
320 case llvm::Value::ConstantAggregateZeroVal: {
322 It->second = std::unique_ptr<ConstantAggregateZero>(
324 auto *Ret = It->second.get();
326 auto EC = CAZ->getElementCount();
333 case llvm::Value::ConstantPointerNullVal:
336 return It->second.get();
337 case llvm::Value::PoisonValueVal:
338 It->second = std::unique_ptr<PoisonValue>(
340 return It->second.get();
341 case llvm::Value::UndefValueVal:
342 It->second = std::unique_ptr<UndefValue>(
344 return It->second.get();
345 case llvm::Value::DSOLocalEquivalentVal: {
347 It->second = std::unique_ptr<DSOLocalEquivalent>(
349 auto *Ret = It->second.get();
353 case llvm::Value::ConstantArrayVal:
354 It->second = std::unique_ptr<ConstantArray>(
357 case llvm::Value::ConstantStructVal:
358 It->second = std::unique_ptr<ConstantStruct>(
361 case llvm::Value::ConstantVectorVal:
362 It->second = std::unique_ptr<ConstantVector>(
365 case llvm::Value::ConstantDataArrayVal:
366 It->second = std::unique_ptr<ConstantDataArray>(
369 case llvm::Value::ConstantDataVectorVal:
370 It->second = std::unique_ptr<ConstantDataVector>(
373 case llvm::Value::FunctionVal:
374 It->second = std::unique_ptr<Function>(
377 case llvm::Value::GlobalIFuncVal:
378 It->second = std::unique_ptr<GlobalIFunc>(
381 case llvm::Value::GlobalVariableVal:
382 It->second = std::unique_ptr<GlobalVariable>(
385 case llvm::Value::GlobalAliasVal:
386 It->second = std::unique_ptr<GlobalAlias>(
389 case llvm::Value::NoCFIValueVal:
390 It->second = std::unique_ptr<NoCFIValue>(
393 case llvm::Value::ConstantPtrAuthVal:
394 It->second = std::unique_ptr<ConstantPtrAuth>(
397 case llvm::Value::ConstantExprVal:
398 It->second = std::unique_ptr<ConstantExpr>(
402 It->second = std::unique_ptr<Constant>(
new Constant(LLVMC, *
this));
405 auto *NewC = It->second.get();
412 It->second = std::unique_ptr<Argument>(
new Argument(LLVMArg, *
this));
413 return It->second.get();
418 "This won't create a SBBB, don't call this function directly!");
425 It->second = std::unique_ptr<OpaqueValue>(
new OpaqueValue(LLVMMD, *
this));
426 return It->second.get();
430 It->second = std::unique_ptr<OpaqueValue>(
new OpaqueValue(LLVMAsm, *
this));
431 return It->second.get();
438 auto It = Pair.first;
440 It->second = std::unique_ptr<Argument>(
new Argument(LLVMArg, *
this));
452 auto NewBBPtr = std::unique_ptr<BasicBlock>(
new BasicBlock(LLVMBB, *
this));
455 BB->buildBasicBlockFromLLVMIR(LLVMBB);
460 auto NewPtr = std::unique_ptr<VAArgInst>(
new VAArgInst(
SI, *
this));
465 auto NewPtr = std::unique_ptr<FreezeInst>(
new FreezeInst(
SI, *
this));
470 auto NewPtr = std::unique_ptr<FenceInst>(
new FenceInst(
SI, *
this));
475 auto NewPtr = std::unique_ptr<SelectInst>(
new SelectInst(
SI, *
this));
513 auto NewPtr = std::unique_ptr<BranchInst>(
new BranchInst(BI, *
this));
518 auto NewPtr = std::unique_ptr<LoadInst>(
new LoadInst(LI, *
this));
523 auto NewPtr = std::unique_ptr<StoreInst>(
new StoreInst(
SI, *
this));
528 auto NewPtr = std::unique_ptr<ReturnInst>(
new ReturnInst(
I, *
this));
533 auto NewPtr = std::unique_ptr<CallInst>(
new CallInst(
I, *
this));
538 auto NewPtr = std::unique_ptr<InvokeInst>(
new InvokeInst(
I, *
this));
543 auto NewPtr = std::unique_ptr<CallBrInst>(
new CallBrInst(
I, *
this));
553 auto NewPtr = std::unique_ptr<LandingPadInst>(
new LandingPadInst(
I, *
this));
557 auto NewPtr = std::unique_ptr<CatchPadInst>(
new CatchPadInst(
I, *
this));
561 auto NewPtr = std::unique_ptr<CleanupPadInst>(
new CleanupPadInst(
I, *
this));
565 auto NewPtr = std::unique_ptr<CatchReturnInst>(
new CatchReturnInst(
I, *
this));
581 auto NewPtr = std::unique_ptr<CatchSwitchInst>(
new CatchSwitchInst(
I, *
this));
585 auto NewPtr = std::unique_ptr<ResumeInst>(
new ResumeInst(
I, *
this));
589 auto NewPtr = std::unique_ptr<SwitchInst>(
new SwitchInst(
I, *
this));
593 auto NewPtr = std::unique_ptr<UnaryOperator>(
new UnaryOperator(
I, *
this));
597 auto NewPtr = std::unique_ptr<BinaryOperator>(
new BinaryOperator(
I, *
this));
601 auto NewPtr = std::unique_ptr<AtomicRMWInst>(
new AtomicRMWInst(
I, *
this));
611 auto NewPtr = std::unique_ptr<AllocaInst>(
new AllocaInst(
I, *
this));
615 auto NewPtr = std::unique_ptr<CastInst>(
new CastInst(
I, *
this));
619 auto NewPtr = std::unique_ptr<PHINode>(
new PHINode(
I, *
this));
623 auto NewPtr = std::unique_ptr<ICmpInst>(
new ICmpInst(
I, *
this));
627 auto NewPtr = std::unique_ptr<FCmpInst>(
new FCmpInst(
I, *
this));
633 return It->second.get();
652 return It->second.get();
658 auto It = Pair.first;
660 return It->second.get();
661 It->second = std::unique_ptr<Module>(
new Module(*LLVMM, *
this));
662 return It->second.get();
675 auto NewFPtr = std::unique_ptr<Function>(
new Function(
F, *
this));
678 for (
auto &Arg :
F->args())
689 for (
auto &LLVMF : *LLVMM)
695 for (
auto &Alias : LLVMM->
aliases())
698 for (
auto &IFunc : LLVMM->
ifuncs())
716 CBEntry.second(
I, WhereIt);
721 CBEntry.second(U, NewSrc);
731 "EraseInstrCallbacks size limit exceeded");
739 "Callback ID not found in EraseInstrCallbacks during deregistration");
745 "CreateInstrCallbacks size limit exceeded");
753 "Callback ID not found in CreateInstrCallbacks during deregistration");
758 "MoveInstrCallbacks size limit exceeded");
766 "Callback ID not found in MoveInstrCallbacks during deregistration");
771 "SetUseCallbacks size limit exceeded");
779 "Callback ID not found in SetUseCallbacks during deregistration");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Machine Check Debug Module
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.
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.
ConstantFolder - Create constants with minimum, target independent, folding.
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.
iterator_range< ifunc_iterator > ifuncs()
iterator_range< alias_iterator > aliases()
iterator_range< global_iterator > globals()
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.
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.
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
An ID for a registered callback.
LLVM_ABI BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Create a sandboxir::BasicBlock for an existing LLVM IR BB.
std::function< void(Instruction *)> CreateInstrCallback
LLVM_ABI GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
MapVector< CallbackID, CreateInstrCallback > CreateInstrCallbacks
Callbacks called when an IR instruction is about to get created.
LLVM_ABI CallBrInst * createCallBrInst(llvm::CallBrInst *I)
LLVM_ABI Module * createModule(llvm::Module *LLVMM)
Create a sandboxir::Module corresponding to LLVMM.
IRBuilder< ConstantFolder > LLVMIRBuilder
std::function< void(const Use &, Value *)> SetUseCallback
MapVector< CallbackID, EraseInstrCallback > EraseInstrCallbacks
Callbacks called when an IR instruction is about to get erased.
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
LLVM_ABI Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
LLVM_ABI void unregisterSetUseCallback(CallbackID ID)
LLVM_ABI ReturnInst * createReturnInst(llvm::ReturnInst *I)
LLVM_ABI void runEraseInstrCallbacks(Instruction *I)
LLVM_ABI Module * getModule(llvm::Module *LLVMM) const
friend ExtractElementInst
LLVM_ABI VAArgInst * createVAArgInst(llvm::VAArgInst *SI)
LLVM_ABI CleanupReturnInst * createCleanupReturnInst(llvm::CleanupReturnInst *I)
LLVM_ABI AllocaInst * createAllocaInst(llvm::AllocaInst *I)
LLVM_ABI void runCreateInstrCallbacks(Instruction *I)
LLVM_ABI AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
LLVM_ABI InsertValueInst * createInsertValueInst(llvm::InsertValueInst *IVI)
LLVM_ABI FCmpInst * createFCmpInst(llvm::FCmpInst *I)
std::function< void(Instruction *)> EraseInstrCallback
CallbackID::ValTy NextCallbackID
A counter used for assigning callback IDs during registration.
LLVM_ABI ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
LLVM_ABI Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
LLVM_ABI CallbackID registerSetUseCallback(SetUseCallback CB)
Register a callback that gets called when a Use gets set.
LLVM_ABI BranchInst * createBranchInst(llvm::BranchInst *I)
LLVM_ABI ShuffleVectorInst * createShuffleVectorInst(llvm::ShuffleVectorInst *SVI)
LLVM_ABI BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
LLVM_ABI Module * getOrCreateModule(llvm::Module *LLVMM)
LLVM_ABI LoadInst * createLoadInst(llvm::LoadInst *LI)
DenseMap< llvm::Value *, std::unique_ptr< Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
LLVM_ABI FreezeInst * createFreezeInst(llvm::FreezeInst *SI)
LLVM_ABI PHINode * createPHINode(llvm::PHINode *I)
LLVM_ABI CallbackID registerCreateInstrCallback(CreateInstrCallback CB)
Register a callback that gets called right after a SandboxIR instruction is created.
LLVM_ABI Context(LLVMContext &LLVMCtx)
LLVM_ABI CatchPadInst * createCatchPadInst(llvm::CatchPadInst *I)
LLVM_ABI void clear()
Clears function-level state.
LLVM_ABI ICmpInst * createICmpInst(llvm::ICmpInst *I)
MapVector< CallbackID, MoveInstrCallback > MoveInstrCallbacks
Callbacks called when an IR instruction is about to get moved.
LLVM_ABI std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
LLVM_ABI void unregisterCreateInstrCallback(CallbackID ID)
LLVM_ABI ExtractValueInst * createExtractValueInst(llvm::ExtractValueInst *IVI)
LLVM_ABI CastInst * createCastInst(llvm::CastInst *I)
DenseMap< llvm::Module *, std::unique_ptr< Module > > LLVMModuleToModuleMap
Maps an LLVM Module to the corresponding sandboxir::Module.
LLVM_ABI StoreInst * createStoreInst(llvm::StoreInst *SI)
LLVM_ABI CatchReturnInst * createCatchReturnInst(llvm::CatchReturnInst *I)
LLVM_ABI CatchSwitchInst * createCatchSwitchInst(llvm::CatchSwitchInst *I)
std::function< void(Instruction *, const BBIterator &)> MoveInstrCallback
LLVM_ABI void unregisterMoveInstrCallback(CallbackID ID)
LLVM_ABI CleanupPadInst * createCleanupPadInst(llvm::CleanupPadInst *I)
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
LLVM_ABI FenceInst * createFenceInst(llvm::FenceInst *SI)
LLVM_ABI CallInst * createCallInst(llvm::CallInst *I)
LLVM_ABI SwitchInst * createSwitchInst(llvm::SwitchInst *I)
LLVM_ABI void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where)
LLVM_ABI UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
LLVM_ABI 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...
LLVM_ABI InvokeInst * createInvokeInst(llvm::InvokeInst *I)
LLVM_ABI Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
LLVM_ABI LandingPadInst * createLandingPadInst(llvm::LandingPadInst *I)
LLVM_ABI InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
LLVM_ABI Function * createFunction(llvm::Function *F)
Create a sandboxir::Function for an existing LLVM IR F, including all blocks and instructions.
LLVM_ABI SelectInst * createSelectInst(llvm::SelectInst *SI)
LLVM_ABI ResumeInst * createResumeInst(llvm::ResumeInst *I)
LLVM_ABI void unregisterEraseInstrCallback(CallbackID ID)
MapVector< CallbackID, SetUseCallback > SetUseCallbacks
Callbacks called when a Use gets its source set.
friend class BasicBlock
Various leaf nodes.
LLVM_ABI CallbackID registerMoveInstrCallback(MoveInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be moved.
LLVM_ABI UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
LLVM_ABI AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
LLVM_ABI void runSetUseCallbacks(const Use &U, Value *NewSrc)
LLVM_ABI CallbackID registerEraseInstrCallback(EraseInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be removed from its par...
LLVM_ABI 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.
An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to an OpaqueInstr.
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Represents a Def-use/Use-def edge in SandboxIR.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
static constexpr int MaxRegisteredCallbacks
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto cast_or_null(const Y &Val)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
@ Global
Append to llvm.global_dtors.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.