LLVM 20.0.0git
SandboxIR.cpp
Go to the documentation of this file.
1//===- SandboxIR.cpp - A transactional overlay IR on top of LLVM IR -------===//
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
12#include "llvm/IR/Constants.h"
13#include "llvm/Support/Debug.h"
14#include <sstream>
15
16using namespace llvm::sandboxir;
17
18Value *Use::get() const { return Ctx->getValue(LLVMUse->get()); }
19
20void Use::set(Value *V) {
21 Ctx->getTracker().emplaceIfTracking<UseSet>(*this);
22 LLVMUse->set(V->Val);
23}
24
25unsigned Use::getOperandNo() const { return Usr->getUseOperandNo(*this); }
26
27void Use::swap(Use &OtherUse) {
28 Ctx->getTracker().emplaceIfTracking<UseSwap>(*this, OtherUse);
29 LLVMUse->swap(*OtherUse.LLVMUse);
30}
31
32#ifndef NDEBUG
34 Value *Def = nullptr;
35 if (LLVMUse == nullptr)
36 OS << "<null> LLVM Use! ";
37 else
38 Def = Ctx->getValue(LLVMUse->get());
39 OS << "Def: ";
40 if (Def == nullptr)
41 OS << "NULL";
42 else
43 OS << *Def;
44 OS << "\n";
45
46 OS << "User: ";
47 if (Usr == nullptr)
48 OS << "NULL";
49 else
50 OS << *Usr;
51 OS << "\n";
52
53 OS << "OperandNo: ";
54 if (Usr == nullptr)
55 OS << "N/A";
56 else
57 OS << getOperandNo();
58 OS << "\n";
59}
60
61void Use::dump() const { dumpOS(dbgs()); }
62#endif // NDEBUG
63
65
67 assert(Use.LLVMUse != nullptr && "Already at end!");
68 User *User = Use.getUser();
69 Use = User->getOperandUseInternal(Use.getOperandNo() + 1, /*Verify=*/false);
70 return *this;
71}
72
74 // Get the corresponding llvm::Use, get the next in the list, and update the
75 // sandboxir::Use.
76 llvm::Use *&LLVMUse = Use.LLVMUse;
77 assert(LLVMUse != nullptr && "Already at end!");
78 LLVMUse = LLVMUse->getNext();
79 if (LLVMUse == nullptr) {
80 Use.Usr = nullptr;
81 return *this;
82 }
83 auto *Ctx = Use.Ctx;
84 auto *LLVMUser = LLVMUse->getUser();
85 Use.Usr = cast_or_null<sandboxir::User>(Ctx->getValue(LLVMUser));
86 return *this;
87}
88
91 Use.getOperandNo() + Num, /*Verify=*/true);
92 return OperandUseIterator(U);
93}
94
96 assert(Use.getOperandNo() >= Num && "Out of bounds!");
98 Use.getOperandNo() - Num, /*Verify=*/true);
99 return OperandUseIterator(U);
100}
101
103 int ThisOpNo = Use.getOperandNo();
104 int OtherOpNo = Other.Use.getOperandNo();
105 return ThisOpNo - OtherOpNo;
106}
107
109 : SubclassID(SubclassID), Val(Val), Ctx(Ctx) {
110#ifndef NDEBUG
111 UID = Ctx.getNumValues();
112#endif
113}
114
116 llvm::Use *LLVMUse = nullptr;
117 if (Val->use_begin() != Val->use_end())
118 LLVMUse = &*Val->use_begin();
119 User *User = LLVMUse != nullptr ? cast_or_null<sandboxir::User>(Ctx.getValue(
120 Val->use_begin()->getUser()))
121 : nullptr;
122 return use_iterator(Use(LLVMUse, User, Ctx));
123}
124
126 auto UseBegin = Val->use_begin();
127 auto UseEnd = Val->use_end();
128 bool AtEnd = UseBegin == UseEnd;
129 llvm::Use *LLVMUse = AtEnd ? nullptr : &*UseBegin;
130 User *User =
131 AtEnd ? nullptr
132 : cast_or_null<sandboxir::User>(Ctx.getValue(&*LLVMUse->getUser()));
133 return user_iterator(Use(LLVMUse, User, Ctx), UseToUser());
134}
135
136unsigned Value::getNumUses() const { return range_size(Val->users()); }
137
139 Value *OtherV, llvm::function_ref<bool(const Use &)> ShouldReplace) {
140 assert(getType() == OtherV->getType() && "Can't replace with different type");
141 llvm::Value *OtherVal = OtherV->Val;
142 // We are delegating RUWIf to LLVM IR's RUWIf.
144 OtherVal, [&ShouldReplace, this](llvm::Use &LLVMUse) -> bool {
145 User *DstU = cast_or_null<User>(Ctx.getValue(LLVMUse.getUser()));
146 if (DstU == nullptr)
147 return false;
148 Use UseToReplace(&LLVMUse, DstU, Ctx);
149 if (!ShouldReplace(UseToReplace))
150 return false;
151 Ctx.getTracker().emplaceIfTracking<UseSet>(UseToReplace);
152 return true;
153 });
154}
155
157 assert(getType() == Other->getType() &&
158 "Replacing with Value of different type!");
159 auto &Tracker = Ctx.getTracker();
160 if (Tracker.isTracking()) {
161 for (auto Use : uses())
162 Tracker.track(std::make_unique<UseSet>(Use));
163 }
164 // We are delegating RAUW to LLVM IR's RAUW.
166}
167
168#ifndef NDEBUG
169std::string Value::getUid() const {
170 std::stringstream SS;
171 SS << "SB" << UID << ".";
172 return SS.str();
173}
174
176 OS << getUid() << " " << getSubclassIDStr(SubclassID) << " ";
177}
178
180 OS.indent(2) << "Val: ";
181 if (Val)
182 OS << *Val;
183 else
184 OS << "NULL";
185 OS << "\n";
186}
187
189 if (Val)
190 OS << *Val;
191 else
192 OS << "NULL ";
193}
194
196 OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")";
197}
198
200 if (Val)
202 else
203 OS << "NULL ";
204}
205
206void Value::dump() const {
207 dumpOS(dbgs());
208 dbgs() << "\n";
209}
210
213}
217}
218#endif // NDEBUG
219
220Use User::getOperandUseDefault(unsigned OpIdx, bool Verify) const {
221 assert((!Verify || OpIdx < getNumOperands()) && "Out of bounds!");
222 assert(isa<llvm::User>(Val) && "Non-users have no operands!");
223 llvm::Use *LLVMUse;
224 if (OpIdx != getNumOperands())
225 LLVMUse = &cast<llvm::User>(Val)->getOperandUse(OpIdx);
226 else
227 LLVMUse = cast<llvm::User>(Val)->op_end();
228 return Use(LLVMUse, const_cast<User *>(this), Ctx);
229}
230
231#ifndef NDEBUG
233 assert(Ctx.getValue(Use.getUser()) == this &&
234 "Use not found in this SBUser's operands!");
235}
236#endif
237
239 switch (From->getSubclassID()) {
240#define DEF_VALUE(ID, CLASS)
241#define DEF_USER(ID, CLASS) \
242 case ClassID::ID: \
243 return true;
244#define DEF_INSTR(ID, OPC, CLASS) \
245 case ClassID::ID: \
246 return true;
247#include "llvm/SandboxIR/SandboxIRValues.def"
248 default:
249 return false;
250 }
251}
252
253void User::setOperand(unsigned OperandIdx, Value *Operand) {
254 assert(isa<llvm::User>(Val) && "No operands!");
256 // We are delegating to llvm::User::setOperand().
257 cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);
258}
259
261 auto &Tracker = Ctx.getTracker();
262 if (Tracker.isTracking()) {
263 for (auto OpIdx : seq<unsigned>(0, getNumOperands())) {
264 auto Use = getOperandUse(OpIdx);
265 if (Use.get() == FromV)
267 }
268 }
269 // We are delegating RUOW to LLVM IR's RUOW.
270 return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);
271}
272
273#ifndef NDEBUG
276 // TODO: This is incomplete
277}
278#endif // NDEBUG
279
281 auto ItE = BB->end();
282 assert(It != ItE && "Already at end!");
283 ++It;
284 if (It == ItE)
285 return *this;
286 Instruction &NextI = *cast<sandboxir::Instruction>(Ctx->getValue(&*It));
287 unsigned Num = NextI.getNumOfIRInstrs();
288 assert(Num > 0 && "Bad getNumOfIRInstrs()");
289 It = std::next(It, Num - 1);
290 return *this;
291}
292
294 assert(It != BB->begin() && "Already at begin!");
295 if (It == BB->end()) {
296 --It;
297 return *this;
298 }
299 Instruction &CurrI = **this;
300 unsigned Num = CurrI.getNumOfIRInstrs();
301 assert(Num > 0 && "Bad getNumOfIRInstrs()");
302 assert(std::prev(It, Num - 1) != BB->begin() && "Already at begin!");
303 It = std::prev(It, Num);
304 return *this;
305}
306
308 switch (Opc) {
309#define OP(OPC) \
310 case Opcode::OPC: \
311 return #OPC;
312#define OPCODES(...) __VA_ARGS__
313#define DEF_INSTR(ID, OPC, CLASS) OPC
314#include "llvm/SandboxIR/SandboxIRValues.def"
315 }
316 llvm_unreachable("Unknown Opcode");
317}
318
320 Instruction *Prev = getPrevNode();
321 if (Prev == nullptr) {
322 // If at top of the BB, return the first BB instruction.
323 return &*cast<llvm::BasicBlock>(getParent()->Val)->begin();
324 }
325 // Else get the Previous sandbox IR instruction's bottom IR instruction and
326 // return its successor.
327 llvm::Instruction *PrevBotI = cast<llvm::Instruction>(Prev->Val);
328 return PrevBotI->getNextNode();
329}
330
332 auto *I = cast<llvm::Instruction>(Val);
333 return BasicBlock::iterator(I->getParent(), I->getIterator(), &Ctx);
334}
335
337 assert(getParent() != nullptr && "Detached!");
338 assert(getIterator() != getParent()->end() && "Already at end!");
339 // `Val` is the bottom-most LLVM IR instruction. Get the next in the chain,
340 // and get the corresponding sandboxir Instruction that maps to it. This works
341 // even for SandboxIR Instructions that map to more than one LLVM Instruction.
342 auto *LLVMI = cast<llvm::Instruction>(Val);
343 assert(LLVMI->getParent() != nullptr && "LLVM IR instr is detached!");
344 auto *NextLLVMI = LLVMI->getNextNode();
345 auto *NextI = cast_or_null<Instruction>(Ctx.getValue(NextLLVMI));
346 if (NextI == nullptr)
347 return nullptr;
348 return NextI;
349}
350
352 assert(getParent() != nullptr && "Detached!");
353 auto It = getIterator();
354 if (It != getParent()->begin())
355 return std::prev(getIterator()).get();
356 return nullptr;
357}
358
361
362 // Detach all the LLVM IR instructions from their parent BB.
364 I->removeFromParent();
365}
366
368 assert(users().empty() && "Still connected to users, can't erase!");
369 std::unique_ptr<Value> Detached = Ctx.detach(this);
370 auto LLVMInstrs = getLLVMInstrs();
371
372 auto &Tracker = Ctx.getTracker();
373 if (Tracker.isTracking()) {
374 Tracker.track(std::make_unique<EraseFromParent>(std::move(Detached)));
375 // We don't actually delete the IR instruction, because then it would be
376 // impossible to bring it back from the dead at the same memory location.
377 // Instead we remove it from its BB and track its current location.
378 for (llvm::Instruction *I : LLVMInstrs)
379 I->removeFromParent();
380 // TODO: Multi-instructions need special treatment because some of the
381 // references are internal to the instruction.
382 for (llvm::Instruction *I : LLVMInstrs)
383 I->dropAllReferences();
384 } else {
385 // Erase in reverse to avoid erasing nstructions with attached uses.
386 for (llvm::Instruction *I : reverse(LLVMInstrs))
387 I->eraseFromParent();
388 }
389}
390
392 if (std::next(getIterator()) == WhereIt)
393 // Destination is same as origin, nothing to do.
394 return;
395
397
398 auto *LLVMBB = cast<llvm::BasicBlock>(BB.Val);
400 if (WhereIt == BB.end()) {
401 It = LLVMBB->end();
402 } else {
403 Instruction *WhereI = &*WhereIt;
404 It = WhereI->getTopmostLLVMInstruction()->getIterator();
405 }
406 // TODO: Move this to the verifier of sandboxir::Instruction.
408 [](auto *I1, auto *I2) { return I1->comesBefore(I2); }) &&
409 "Expected program order!");
410 // Do the actual move in LLVM IR.
411 for (auto *I : getLLVMInstrs())
412 I->moveBefore(*LLVMBB, It);
413}
414
416 llvm::Instruction *BeforeTopI = BeforeI->getTopmostLLVMInstruction();
417 // TODO: Move this to the verifier of sandboxir::Instruction.
419 [](auto *I1, auto *I2) { return I1->comesBefore(I2); }) &&
420 "Expected program order!");
421
423
424 // Insert the LLVM IR Instructions in program order.
426 I->insertBefore(BeforeTopI);
427}
428
430 insertInto(AfterI->getParent(), std::next(AfterI->getIterator()));
431}
432
434 llvm::BasicBlock *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
435 llvm::Instruction *LLVMBeforeI;
436 llvm::BasicBlock::iterator LLVMBeforeIt;
437 Instruction *BeforeI;
438 if (WhereIt != BB->end()) {
439 BeforeI = &*WhereIt;
440 LLVMBeforeI = BeforeI->getTopmostLLVMInstruction();
441 LLVMBeforeIt = LLVMBeforeI->getIterator();
442 } else {
443 BeforeI = nullptr;
444 LLVMBeforeI = nullptr;
445 LLVMBeforeIt = LLVMBB->end();
446 }
447
449
450 // Insert the LLVM IR Instructions in program order.
452 I->insertInto(LLVMBB, LLVMBeforeIt);
453}
454
456 // Get the LLVM IR Instruction that this maps to, get its parent, and get the
457 // corresponding sandboxir::BasicBlock by looking it up in sandboxir::Context.
458 auto *BB = cast<llvm::Instruction>(Val)->getParent();
459 if (BB == nullptr)
460 return nullptr;
461 return cast<BasicBlock>(Ctx.getValue(BB));
462}
463
465 switch (From->getSubclassID()) {
466#define DEF_INSTR(ID, OPC, CLASS) \
467 case ClassID::ID: \
468 return true;
469#include "llvm/SandboxIR/SandboxIRValues.def"
470 default:
471 return false;
472 }
473}
474
479 this);
480 cast<llvm::Instruction>(Val)->setHasNoUnsignedWrap(B);
481}
482
487 cast<llvm::Instruction>(Val)->setHasNoSignedWrap(B);
488}
489
494 cast<llvm::Instruction>(Val)->setFast(B);
495}
496
501 cast<llvm::Instruction>(Val)->setIsExact(B);
502}
503
508 cast<llvm::Instruction>(Val)->setHasAllowReassoc(B);
509}
510
515 this);
516 cast<llvm::Instruction>(Val)->setHasNoNaNs(B);
517}
518
523 this);
524 cast<llvm::Instruction>(Val)->setHasNoInfs(B);
525}
526
531 this);
532 cast<llvm::Instruction>(Val)->setHasNoSignedZeros(B);
533}
534
539 this);
540 cast<llvm::Instruction>(Val)->setHasAllowReciprocal(B);
541}
542
547 this);
548 cast<llvm::Instruction>(Val)->setHasAllowContract(B);
549}
550
555 cast<llvm::Instruction>(Val)->setFastMathFlags(FMF);
556}
557
562 cast<llvm::Instruction>(Val)->copyFastMathFlags(FMF);
563}
564
569 cast<llvm::Instruction>(Val)->setHasApproxFunc(B);
570}
571
572#ifndef NDEBUG
574 OS << "Unimplemented! Please override dump().";
575}
576#endif // NDEBUG
577
578Value *SelectInst::createCommon(Value *Cond, Value *True, Value *False,
579 const Twine &Name, IRBuilder<> &Builder,
580 Context &Ctx) {
581 llvm::Value *NewV =
582 Builder.CreateSelect(Cond->Val, True->Val, False->Val, Name);
583 if (auto *NewSI = dyn_cast<llvm::SelectInst>(NewV))
584 return Ctx.createSelectInst(NewSI);
585 assert(isa<llvm::Constant>(NewV) && "Expected constant");
586 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
587}
588
590 Instruction *InsertBefore, Context &Ctx,
591 const Twine &Name) {
592 llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
593 auto &Builder = Ctx.getLLVMIRBuilder();
594 Builder.SetInsertPoint(BeforeIR);
595 return createCommon(Cond, True, False, Name, Builder, Ctx);
596}
597
599 BasicBlock *InsertAtEnd, Context &Ctx,
600 const Twine &Name) {
601 auto *IRInsertAtEnd = cast<llvm::BasicBlock>(InsertAtEnd->Val);
602 auto &Builder = Ctx.getLLVMIRBuilder();
603 Builder.SetInsertPoint(IRInsertAtEnd);
604 return createCommon(Cond, True, False, Name, Builder, Ctx);
605}
606
608 return From->getSubclassID() == ClassID::Select;
609}
610
612 Context &Ctx) {
613 auto &Builder = Ctx.getLLVMIRBuilder();
614 llvm::Instruction *LLVMBefore = InsertBefore->getTopmostLLVMInstruction();
615 Builder.SetInsertPoint(cast<llvm::Instruction>(LLVMBefore));
616 llvm::BranchInst *NewBr =
617 Builder.CreateBr(cast<llvm::BasicBlock>(IfTrue->Val));
618 return Ctx.createBranchInst(NewBr);
619}
620
622 Context &Ctx) {
623 auto &Builder = Ctx.getLLVMIRBuilder();
624 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
625 llvm::BranchInst *NewBr =
626 Builder.CreateBr(cast<llvm::BasicBlock>(IfTrue->Val));
627 return Ctx.createBranchInst(NewBr);
628}
629
631 Value *Cond, Instruction *InsertBefore,
632 Context &Ctx) {
633 auto &Builder = Ctx.getLLVMIRBuilder();
634 llvm::Instruction *LLVMBefore = InsertBefore->getTopmostLLVMInstruction();
635 Builder.SetInsertPoint(LLVMBefore);
636 llvm::BranchInst *NewBr =
637 Builder.CreateCondBr(Cond->Val, cast<llvm::BasicBlock>(IfTrue->Val),
638 cast<llvm::BasicBlock>(IfFalse->Val));
639 return Ctx.createBranchInst(NewBr);
640}
641
643 Value *Cond, BasicBlock *InsertAtEnd,
644 Context &Ctx) {
645 auto &Builder = Ctx.getLLVMIRBuilder();
646 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
647 llvm::BranchInst *NewBr =
648 Builder.CreateCondBr(Cond->Val, cast<llvm::BasicBlock>(IfTrue->Val),
649 cast<llvm::BasicBlock>(IfFalse->Val));
650 return Ctx.createBranchInst(NewBr);
651}
652
654 return From->getSubclassID() == ClassID::Br;
655}
656
658 assert(isConditional() && "Cannot get condition of an uncond branch!");
659 return Ctx.getValue(cast<llvm::BranchInst>(Val)->getCondition());
660}
661
662BasicBlock *BranchInst::getSuccessor(unsigned SuccIdx) const {
663 assert(SuccIdx < getNumSuccessors() &&
664 "Successor # out of range for Branch!");
665 return cast_or_null<BasicBlock>(
666 Ctx.getValue(cast<llvm::BranchInst>(Val)->getSuccessor(SuccIdx)));
667}
668
669void BranchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
670 assert((Idx == 0 || Idx == 1) && "Out of bounds!");
671 setOperand(2u - Idx, NewSucc);
672}
673
674BasicBlock *BranchInst::LLVMBBToSBBB::operator()(llvm::BasicBlock *BB) const {
675 return cast<BasicBlock>(Ctx.getValue(BB));
676}
677const BasicBlock *
678BranchInst::ConstLLVMBBToSBBB::operator()(const llvm::BasicBlock *BB) const {
679 return cast<BasicBlock>(Ctx.getValue(BB));
680}
681
686 cast<llvm::LoadInst>(Val)->setVolatile(V);
687}
688
690 Instruction *InsertBefore, Context &Ctx,
691 const Twine &Name) {
692 return create(Ty, Ptr, Align, InsertBefore, /*IsVolatile=*/false, Ctx, Name);
693}
694
696 Instruction *InsertBefore, bool IsVolatile,
697 Context &Ctx, const Twine &Name) {
698 llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
699 auto &Builder = Ctx.getLLVMIRBuilder();
700 Builder.SetInsertPoint(BeforeIR);
701 auto *NewLI =
702 Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, IsVolatile, Name);
703 auto *NewSBI = Ctx.createLoadInst(NewLI);
704 return NewSBI;
705}
706
708 BasicBlock *InsertAtEnd, Context &Ctx,
709 const Twine &Name) {
710 return create(Ty, Ptr, Align, InsertAtEnd, /*IsVolatile=*/false, Ctx, Name);
711}
712
714 BasicBlock *InsertAtEnd, bool IsVolatile,
715 Context &Ctx, const Twine &Name) {
716 auto &Builder = Ctx.getLLVMIRBuilder();
717 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
718 auto *NewLI =
719 Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, IsVolatile, Name);
720 auto *NewSBI = Ctx.createLoadInst(NewLI);
721 return NewSBI;
722}
723
725 return From->getSubclassID() == ClassID::Load;
726}
727
729 return Ctx.getValue(cast<llvm::LoadInst>(Val)->getPointerOperand());
730}
731
736 cast<llvm::StoreInst>(Val)->setVolatile(V);
737}
738
740 Instruction *InsertBefore, Context &Ctx) {
741 return create(V, Ptr, Align, InsertBefore, /*IsVolatile=*/false, Ctx);
742}
743
745 Instruction *InsertBefore, bool IsVolatile,
746 Context &Ctx) {
747 llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
748 auto &Builder = Ctx.getLLVMIRBuilder();
749 Builder.SetInsertPoint(BeforeIR);
750 auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
751 auto *NewSBI = Ctx.createStoreInst(NewSI);
752 return NewSBI;
753}
754
756 BasicBlock *InsertAtEnd, Context &Ctx) {
757 return create(V, Ptr, Align, InsertAtEnd, /*IsVolatile=*/false, Ctx);
758}
759
761 BasicBlock *InsertAtEnd, bool IsVolatile,
762 Context &Ctx) {
763 auto *InsertAtEndIR = cast<llvm::BasicBlock>(InsertAtEnd->Val);
764 auto &Builder = Ctx.getLLVMIRBuilder();
765 Builder.SetInsertPoint(InsertAtEndIR);
766 auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
767 auto *NewSBI = Ctx.createStoreInst(NewSI);
768 return NewSBI;
769}
770
772 return From->getSubclassID() == ClassID::Store;
773}
774
776 return Ctx.getValue(cast<llvm::StoreInst>(Val)->getValueOperand());
777}
778
780 return Ctx.getValue(cast<llvm::StoreInst>(Val)->getPointerOperand());
781}
782
784 Context &Ctx) {
785 auto &Builder = Ctx.getLLVMIRBuilder();
786 llvm::Instruction *LLVMBefore = InsertBefore->getTopmostLLVMInstruction();
787 Builder.SetInsertPoint(LLVMBefore);
788 llvm::UnreachableInst *NewUI = Builder.CreateUnreachable();
789 return Ctx.createUnreachableInst(NewUI);
790}
791
793 Context &Ctx) {
794 auto &Builder = Ctx.getLLVMIRBuilder();
795 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
796 llvm::UnreachableInst *NewUI = Builder.CreateUnreachable();
797 return Ctx.createUnreachableInst(NewUI);
798}
799
801 return From->getSubclassID() == ClassID::Unreachable;
802}
803
804ReturnInst *ReturnInst::createCommon(Value *RetVal, IRBuilder<> &Builder,
805 Context &Ctx) {
806 llvm::ReturnInst *NewRI;
807 if (RetVal != nullptr)
808 NewRI = Builder.CreateRet(RetVal->Val);
809 else
810 NewRI = Builder.CreateRetVoid();
811 return Ctx.createReturnInst(NewRI);
812}
813
815 Context &Ctx) {
816 llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
817 auto &Builder = Ctx.getLLVMIRBuilder();
818 Builder.SetInsertPoint(BeforeIR);
819 return createCommon(RetVal, Builder, Ctx);
820}
821
823 Context &Ctx) {
824 auto &Builder = Ctx.getLLVMIRBuilder();
825 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
826 return createCommon(RetVal, Builder, Ctx);
827}
828
830 auto *LLVMRetVal = cast<llvm::ReturnInst>(Val)->getReturnValue();
831 return LLVMRetVal != nullptr ? Ctx.getValue(LLVMRetVal) : nullptr;
832}
833
835 return Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledOperand());
836}
837
839 llvm::Use *LLVMUse = &cast<llvm::CallBase>(Val)->getCalledOperandUse();
840 return Use(LLVMUse, cast<User>(Ctx.getValue(LLVMUse->getUser())), Ctx);
841}
842
844 return cast_or_null<Function>(
845 Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledFunction()));
846}
848 return cast<Function>(Ctx.getValue(cast<llvm::CallBase>(Val)->getCaller()));
849}
850
852 // F's function type is private, so we rely on `setCalledFunction()` to update
853 // it. But even though we are calling `setCalledFunction()` we also need to
854 // track this change at the SandboxIR level, which is why we call
855 // `setCalledOperand()` here.
856 // Note: This may break if `setCalledFunction()` early returns if `F`
857 // is already set, but we do have a unit test for it.
859 cast<llvm::CallBase>(Val)->setCalledFunction(F->getFunctionType(),
860 cast<llvm::Function>(F->Val));
861}
862
865 BasicBlock *WhereBB, Context &Ctx,
866 const Twine &NameStr) {
867 auto &Builder = Ctx.getLLVMIRBuilder();
868 if (WhereIt != WhereBB->end())
869 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
870 else
871 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
873 LLVMArgs.reserve(Args.size());
874 for (Value *Arg : Args)
875 LLVMArgs.push_back(Arg->Val);
876 llvm::CallInst *NewCI = Builder.CreateCall(FTy, Func->Val, LLVMArgs, NameStr);
877 return Ctx.createCallInst(NewCI);
878}
879
881 ArrayRef<Value *> Args, Instruction *InsertBefore,
882 Context &Ctx, const Twine &NameStr) {
883 return CallInst::create(FTy, Func, Args, InsertBefore->getIterator(),
884 InsertBefore->getParent(), Ctx, NameStr);
885}
886
888 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
889 Context &Ctx, const Twine &NameStr) {
890 return CallInst::create(FTy, Func, Args, InsertAtEnd->end(), InsertAtEnd, Ctx,
891 NameStr);
892}
893
895 BasicBlock *IfNormal, BasicBlock *IfException,
896 ArrayRef<Value *> Args, BBIterator WhereIt,
897 BasicBlock *WhereBB, Context &Ctx,
898 const Twine &NameStr) {
899 auto &Builder = Ctx.getLLVMIRBuilder();
900 if (WhereIt != WhereBB->end())
901 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
902 else
903 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
905 LLVMArgs.reserve(Args.size());
906 for (Value *Arg : Args)
907 LLVMArgs.push_back(Arg->Val);
908 llvm::InvokeInst *Invoke = Builder.CreateInvoke(
909 FTy, Func->Val, cast<llvm::BasicBlock>(IfNormal->Val),
910 cast<llvm::BasicBlock>(IfException->Val), LLVMArgs, NameStr);
911 return Ctx.createInvokeInst(Invoke);
912}
913
915 BasicBlock *IfNormal, BasicBlock *IfException,
917 Instruction *InsertBefore, Context &Ctx,
918 const Twine &NameStr) {
919 return create(FTy, Func, IfNormal, IfException, Args,
920 InsertBefore->getIterator(), InsertBefore->getParent(), Ctx,
921 NameStr);
922}
923
925 BasicBlock *IfNormal, BasicBlock *IfException,
926 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
927 Context &Ctx, const Twine &NameStr) {
928 return create(FTy, Func, IfNormal, IfException, Args, InsertAtEnd->end(),
929 InsertAtEnd, Ctx, NameStr);
930}
931
933 return cast<BasicBlock>(
934 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getNormalDest()));
935}
937 return cast<BasicBlock>(
938 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getUnwindDest()));
939}
941 setOperand(1, BB);
942 assert(getNormalDest() == BB && "LLVM IR uses a different operan index!");
943}
945 setOperand(2, BB);
946 assert(getUnwindDest() == BB && "LLVM IR uses a different operan index!");
947}
949 return cast<Instruction>(
950 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getLandingPadInst()));
951 ;
952}
953BasicBlock *InvokeInst::getSuccessor(unsigned SuccIdx) const {
954 return cast<BasicBlock>(
955 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getSuccessor(SuccIdx)));
956}
957
959 BasicBlock *DefaultDest,
960 ArrayRef<BasicBlock *> IndirectDests,
961 ArrayRef<Value *> Args, BBIterator WhereIt,
962 BasicBlock *WhereBB, Context &Ctx,
963 const Twine &NameStr) {
964 auto &Builder = Ctx.getLLVMIRBuilder();
965 if (WhereIt != WhereBB->end())
966 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
967 else
968 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
969
970 SmallVector<llvm::BasicBlock *> LLVMIndirectDests;
971 LLVMIndirectDests.reserve(IndirectDests.size());
972 for (BasicBlock *IndDest : IndirectDests)
973 LLVMIndirectDests.push_back(cast<llvm::BasicBlock>(IndDest->Val));
974
976 LLVMArgs.reserve(Args.size());
977 for (Value *Arg : Args)
978 LLVMArgs.push_back(Arg->Val);
979
980 llvm::CallBrInst *CallBr = Builder.CreateCallBr(
981 FTy, Func->Val, cast<llvm::BasicBlock>(DefaultDest->Val),
982 LLVMIndirectDests, LLVMArgs, NameStr);
983 return Ctx.createCallBrInst(CallBr);
984}
985
987 BasicBlock *DefaultDest,
988 ArrayRef<BasicBlock *> IndirectDests,
990 Instruction *InsertBefore, Context &Ctx,
991 const Twine &NameStr) {
992 return create(FTy, Func, DefaultDest, IndirectDests, Args,
993 InsertBefore->getIterator(), InsertBefore->getParent(), Ctx,
994 NameStr);
995}
997 BasicBlock *DefaultDest,
998 ArrayRef<BasicBlock *> IndirectDests,
999 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
1000 Context &Ctx, const Twine &NameStr) {
1001 return create(FTy, Func, DefaultDest, IndirectDests, Args, InsertAtEnd->end(),
1002 InsertAtEnd, Ctx, NameStr);
1003}
1004
1006 return Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDestLabel(Idx));
1007}
1009 return Ctx.getValue(
1010 cast<llvm::CallBrInst>(Val)->getIndirectDestLabelUse(Idx));
1011}
1013 return cast<BasicBlock>(
1014 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getDefaultDest()));
1015}
1017 return cast<BasicBlock>(
1018 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDest(Idx)));
1019}
1022 for (llvm::BasicBlock *LLVMBB :
1023 cast<llvm::CallBrInst>(Val)->getIndirectDests())
1024 BBs.push_back(cast<BasicBlock>(Ctx.getValue(LLVMBB)));
1025 return BBs;
1026}
1028 Ctx.getTracker()
1031 cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val));
1032}
1035 cast<llvm::CallBrInst>(Val)->setIndirectDest(Idx,
1036 cast<llvm::BasicBlock>(BB->Val));
1037}
1039 return cast<BasicBlock>(
1040 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx)));
1041}
1042
1044 ArrayRef<Value *> IdxList,
1045 BasicBlock::iterator WhereIt,
1046 BasicBlock *WhereBB, Context &Ctx,
1047 const Twine &NameStr) {
1048 auto &Builder = Ctx.getLLVMIRBuilder();
1049 if (WhereIt != WhereBB->end())
1050 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1051 else
1052 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1053 SmallVector<llvm::Value *> LLVMIdxList;
1054 LLVMIdxList.reserve(IdxList.size());
1055 for (Value *Idx : IdxList)
1056 LLVMIdxList.push_back(Idx->Val);
1057 llvm::Value *NewV = Builder.CreateGEP(Ty, Ptr->Val, LLVMIdxList, NameStr);
1058 if (auto *NewGEP = dyn_cast<llvm::GetElementPtrInst>(NewV))
1059 return Ctx.createGetElementPtrInst(NewGEP);
1060 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1061 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1062}
1063
1065 ArrayRef<Value *> IdxList,
1066 Instruction *InsertBefore, Context &Ctx,
1067 const Twine &NameStr) {
1068 return GetElementPtrInst::create(Ty, Ptr, IdxList,
1069 InsertBefore->getIterator(),
1070 InsertBefore->getParent(), Ctx, NameStr);
1071}
1072
1074 ArrayRef<Value *> IdxList,
1075 BasicBlock *InsertAtEnd, Context &Ctx,
1076 const Twine &NameStr) {
1077 return GetElementPtrInst::create(Ty, Ptr, IdxList, InsertAtEnd->end(),
1078 InsertAtEnd, Ctx, NameStr);
1079}
1080
1082 return Ctx.getValue(cast<llvm::GetElementPtrInst>(Val)->getPointerOperand());
1083}
1084
1085BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const {
1086 return cast<BasicBlock>(Ctx.getValue(LLVMBB));
1087}
1088
1089PHINode *PHINode::create(Type *Ty, unsigned NumReservedValues,
1090 Instruction *InsertBefore, Context &Ctx,
1091 const Twine &Name) {
1093 Ty, NumReservedValues, Name, InsertBefore->getTopmostLLVMInstruction());
1094 return Ctx.createPHINode(NewPHI);
1095}
1096
1098 return From->getSubclassID() == ClassID::PHI;
1099}
1100
1102 return Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingValue(Idx));
1103}
1105 auto &Tracker = Ctx.getTracker();
1108
1109 cast<llvm::PHINode>(Val)->setIncomingValue(Idx, V->Val);
1110}
1112 return cast<BasicBlock>(
1113 Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingBlock(Idx)));
1114}
1116 llvm::Use *LLVMUse = U.LLVMUse;
1117 llvm::BasicBlock *BB = cast<llvm::PHINode>(Val)->getIncomingBlock(*LLVMUse);
1118 return cast<BasicBlock>(Ctx.getValue(BB));
1119}
1121 auto &Tracker = Ctx.getTracker();
1124 cast<llvm::PHINode>(Val)->setIncomingBlock(Idx,
1125 cast<llvm::BasicBlock>(BB->Val));
1126}
1128 auto &Tracker = Ctx.getTracker();
1130
1131 cast<llvm::PHINode>(Val)->addIncoming(V->Val,
1132 cast<llvm::BasicBlock>(BB->Val));
1133}
1135 auto &Tracker = Ctx.getTracker();
1137 llvm::Value *LLVMV =
1138 cast<llvm::PHINode>(Val)->removeIncomingValue(Idx,
1139 /*DeletePHIIfEmpty=*/false);
1140 return Ctx.getValue(LLVMV);
1141}
1143 auto &Tracker = Ctx.getTracker();
1145
1146 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
1147 llvm::Value *LLVMV =
1148 cast<llvm::PHINode>(Val)->removeIncomingValue(LLVMBB,
1149 /*DeletePHIIfEmpty=*/false);
1150 return Ctx.getValue(LLVMV);
1151}
1153 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
1154 return cast<llvm::PHINode>(Val)->getBasicBlockIndex(LLVMBB);
1155}
1157 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
1158 llvm::Value *LLVMV =
1159 cast<llvm::PHINode>(Val)->getIncomingValueForBlock(LLVMBB);
1160 return Ctx.getValue(LLVMV);
1161}
1163 llvm::Value *LLVMV = cast<llvm::PHINode>(Val)->hasConstantValue();
1164 return LLVMV != nullptr ? Ctx.getValue(LLVMV) : nullptr;
1165}
1167 assert(New && Old && "Sandbox IR PHI node got a null basic block!");
1168 for (unsigned Idx = 0, NumOps = cast<llvm::PHINode>(Val)->getNumOperands();
1169 Idx != NumOps; ++Idx)
1170 if (getIncomingBlock(Idx) == Old)
1171 setIncomingBlock(Idx, New);
1172}
1173void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate) {
1174 // Avoid duplicate tracking by going through this->removeIncomingValue here at
1175 // the expense of some performance. Copy PHI::removeIncomingValueIf more
1176 // directly if performance becomes an issue.
1177
1178 // Removing the element at index X, moves the element previously at X + 1
1179 // to X. Working from the end avoids complications from that.
1180 unsigned Idx = getNumIncomingValues();
1181 while (Idx > 0) {
1182 if (Predicate(Idx - 1))
1184 --Idx;
1185 }
1186}
1187
1189 switch (Opc) {
1190 case Instruction::Opcode::ZExt:
1191 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::ZExt);
1192 case Instruction::Opcode::SExt:
1193 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SExt);
1194 case Instruction::Opcode::FPToUI:
1195 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToUI);
1196 case Instruction::Opcode::FPToSI:
1197 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToSI);
1198 case Instruction::Opcode::FPExt:
1199 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPExt);
1200 case Instruction::Opcode::PtrToInt:
1201 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::PtrToInt);
1202 case Instruction::Opcode::IntToPtr:
1203 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::IntToPtr);
1204 case Instruction::Opcode::SIToFP:
1205 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SIToFP);
1206 case Instruction::Opcode::UIToFP:
1207 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::UIToFP);
1208 case Instruction::Opcode::Trunc:
1209 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::Trunc);
1210 case Instruction::Opcode::FPTrunc:
1211 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPTrunc);
1212 case Instruction::Opcode::BitCast:
1213 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::BitCast);
1214 case Instruction::Opcode::AddrSpaceCast:
1215 return static_cast<llvm::Instruction::CastOps>(
1216 llvm::Instruction::AddrSpaceCast);
1217 default:
1218 llvm_unreachable("Opcode not suitable for CastInst!");
1219 }
1220}
1221
1223 Ctx.getTracker()
1226 this);
1227 cast<llvm::AtomicCmpXchgInst>(Val)->setSyncScopeID(SSID);
1228}
1229
1231 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getPointerOperand());
1232}
1233
1235 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getCompareOperand());
1236}
1237
1239 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getNewValOperand());
1240}
1241
1244 AtomicOrdering SuccessOrdering,
1245 AtomicOrdering FailureOrdering, BBIterator WhereIt,
1246 BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID,
1247 const Twine &Name) {
1248 auto &Builder = Ctx.getLLVMIRBuilder();
1249 if (WhereIt == WhereBB->end())
1250 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1251 else
1252 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1253 auto *LLVMAtomicCmpXchg =
1254 Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
1255 SuccessOrdering, FailureOrdering, SSID);
1256 LLVMAtomicCmpXchg->setName(Name);
1258}
1259
1262 AtomicOrdering SuccessOrdering,
1263 AtomicOrdering FailureOrdering,
1264 Instruction *InsertBefore,
1265 Context &Ctx, SyncScope::ID SSID,
1266 const Twine &Name) {
1267 return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
1268 InsertBefore->getIterator(), InsertBefore->getParent(), Ctx,
1269 SSID, Name);
1270}
1271
1274 AtomicOrdering SuccessOrdering,
1275 AtomicOrdering FailureOrdering,
1276 BasicBlock *InsertAtEnd,
1277 Context &Ctx, SyncScope::ID SSID,
1278 const Twine &Name) {
1279 return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
1280 InsertAtEnd->end(), InsertAtEnd, Ctx, SSID, Name);
1281}
1282
1284 Ctx.getTracker()
1287 cast<llvm::AtomicCmpXchgInst>(Val)->setAlignment(Align);
1288}
1289
1291 Ctx.getTracker()
1294 cast<llvm::AtomicCmpXchgInst>(Val)->setVolatile(V);
1295}
1296
1298 Ctx.getTracker()
1301 cast<llvm::AtomicCmpXchgInst>(Val)->setWeak(IsWeak);
1302}
1303
1305 Ctx.getTracker()
1308 this);
1309 cast<llvm::AtomicCmpXchgInst>(Val)->setSuccessOrdering(Ordering);
1310}
1311
1313 Ctx.getTracker()
1316 this);
1317 cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
1318}
1319
1320AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
1321 BasicBlock *WhereBB, Context &Ctx,
1322 Value *ArraySize, const Twine &Name) {
1323 auto &Builder = Ctx.getLLVMIRBuilder();
1324 if (WhereIt == WhereBB->end())
1325 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1326 else
1327 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1328 auto *NewAlloca = Builder.CreateAlloca(Ty, AddrSpace, ArraySize->Val, Name);
1329 return Ctx.createAllocaInst(NewAlloca);
1330}
1331
1332AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
1333 Instruction *InsertBefore, Context &Ctx,
1334 Value *ArraySize, const Twine &Name) {
1335 return create(Ty, AddrSpace, InsertBefore->getIterator(),
1336 InsertBefore->getParent(), Ctx, ArraySize, Name);
1337}
1338
1339AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
1340 BasicBlock *InsertAtEnd, Context &Ctx,
1341 Value *ArraySize, const Twine &Name) {
1342 return create(Ty, AddrSpace, InsertAtEnd->end(), InsertAtEnd, Ctx, ArraySize,
1343 Name);
1344}
1345
1347 Ctx.getTracker()
1350 cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty);
1351}
1352
1354 Ctx.getTracker()
1357 this);
1358 cast<llvm::AllocaInst>(Val)->setAlignment(Align);
1359}
1360
1362 Ctx.getTracker()
1365 cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
1366}
1367
1369 return Ctx.getValue(cast<llvm::AllocaInst>(Val)->getArraySize());
1370}
1371
1373 BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1374 const Twine &Name) {
1375 assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
1376 auto &Builder = Ctx.getLLVMIRBuilder();
1377 if (WhereIt == WhereBB->end())
1378 Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1379 else
1380 Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1381 auto *NewV =
1382 Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy, Name);
1383 if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
1384 return Ctx.createCastInst(NewCI);
1385 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1386 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1387}
1388
1390 Instruction *InsertBefore, Context &Ctx,
1391 const Twine &Name) {
1392 return create(DestTy, Op, Operand, InsertBefore->getIterator(),
1393 InsertBefore->getParent(), Ctx, Name);
1394}
1395
1397 BasicBlock *InsertAtEnd, Context &Ctx,
1398 const Twine &Name) {
1399 return create(DestTy, Op, Operand, InsertAtEnd->end(), InsertAtEnd, Ctx,
1400 Name);
1401}
1402
1404 return From->getSubclassID() == ClassID::Cast;
1405}
1406
1408 Instruction *InsertBefore, Context &Ctx,
1409 const Twine &Name) {
1410 auto &Builder = Ctx.getLLVMIRBuilder();
1411 Builder.SetInsertPoint(InsertBefore->getTopmostLLVMInstruction());
1412 llvm::Value *NewV =
1413 Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
1414 if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
1415 return Ctx.createInsertElementInst(NewInsert);
1416 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1417 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1418}
1419
1421 BasicBlock *InsertAtEnd, Context &Ctx,
1422 const Twine &Name) {
1423 auto &Builder = Ctx.getLLVMIRBuilder();
1424 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
1425 llvm::Value *NewV =
1426 Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
1427 if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
1428 return Ctx.createInsertElementInst(NewInsert);
1429 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1430 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1431}
1432
1434 Instruction *InsertBefore, Context &Ctx,
1435 const Twine &Name) {
1436 auto &Builder = Ctx.getLLVMIRBuilder();
1437 Builder.SetInsertPoint(InsertBefore->getTopmostLLVMInstruction());
1438 llvm::Value *NewV = Builder.CreateExtractElement(Vec->Val, Idx->Val, Name);
1439 if (auto *NewExtract = dyn_cast<llvm::ExtractElementInst>(NewV))
1440 return Ctx.createExtractElementInst(NewExtract);
1441 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1442 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1443}
1444
1446 BasicBlock *InsertAtEnd, Context &Ctx,
1447 const Twine &Name) {
1448 auto &Builder = Ctx.getLLVMIRBuilder();
1449 Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
1450 llvm::Value *NewV = Builder.CreateExtractElement(Vec->Val, Idx->Val, Name);
1451 if (auto *NewExtract = dyn_cast<llvm::ExtractElementInst>(NewV))
1452 return Ctx.createExtractElementInst(NewExtract);
1453 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1454 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1455}
1456
1458 bool IsSigned) {
1459 llvm::Constant *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned);
1460 return Ctx.getOrCreateConstant(LLVMC);
1461}
1462
1463#ifndef NDEBUG
1467}
1468
1470 auto *F = cast<llvm::Function>(Val);
1471 OS << *F->getReturnType() << " @" << F->getName() << "(";
1472 interleave(
1473 F->args(),
1474 [this, &OS](const llvm::Argument &LLVMArg) {
1475 auto *SBArg = cast_or_null<Argument>(Ctx.getValue(&LLVMArg));
1476 if (SBArg == nullptr)
1477 OS << "NULL";
1478 else
1479 SBArg->printAsOperand(OS);
1480 },
1481 [&] { OS << ", "; });
1482 OS << ")";
1483}
1486 OS << " {\n";
1487 auto *LLVMF = cast<llvm::Function>(Val);
1488 interleave(
1489 *LLVMF,
1490 [this, &OS](const llvm::BasicBlock &LLVMBB) {
1491 auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
1492 if (BB == nullptr)
1493 OS << "NULL";
1494 else
1495 OS << *BB;
1496 },
1497 [&OS] { OS << "\n"; });
1498 OS << "}\n";
1499}
1500#endif // NDEBUG
1501
1503BasicBlock::iterator::getInstr(llvm::BasicBlock::iterator It) const {
1504 return cast_or_null<Instruction>(Ctx->getValue(&*It));
1505}
1506
1507std::unique_ptr<Value> Context::detachLLVMValue(llvm::Value *V) {
1508 std::unique_ptr<Value> Erased;
1509 auto It = LLVMValueToValueMap.find(V);
1510 if (It != LLVMValueToValueMap.end()) {
1511 auto *Val = It->second.release();
1512 Erased = std::unique_ptr<Value>(Val);
1513 LLVMValueToValueMap.erase(It);
1514 }
1515 return Erased;
1516}
1517
1518std::unique_ptr<Value> Context::detach(Value *V) {
1519 assert(V->getSubclassID() != Value::ClassID::Constant &&
1520 "Can't detach a constant!");
1521 assert(V->getSubclassID() != Value::ClassID::User && "Can't detach a user!");
1522 return detachLLVMValue(V->Val);
1523}
1524
1525Value *Context::registerValue(std::unique_ptr<Value> &&VPtr) {
1526 assert(VPtr->getSubclassID() != Value::ClassID::User &&
1527 "Can't register a user!");
1528
1529 // Track creation of instructions.
1530 // Please note that we don't allow the creation of detached instructions,
1531 // meaning that the instructions need to be inserted into a block upon
1532 // creation. This is why the tracker class combines creation and insertion.
1533 if (auto *I = dyn_cast<Instruction>(VPtr.get()))
1535
1536 Value *V = VPtr.get();
1537 [[maybe_unused]] auto Pair =
1538 LLVMValueToValueMap.insert({VPtr->Val, std::move(VPtr)});
1539 assert(Pair.second && "Already exists!");
1540 return V;
1541}
1542
1544 auto Pair = LLVMValueToValueMap.insert({LLVMV, nullptr});
1545 auto It = Pair.first;
1546 if (!Pair.second)
1547 return It->second.get();
1548
1549 if (auto *C = dyn_cast<llvm::Constant>(LLVMV)) {
1550 if (auto *F = dyn_cast<llvm::Function>(LLVMV))
1551 It->second = std::unique_ptr<Function>(new Function(F, *this));
1552 else
1553 It->second = std::unique_ptr<Constant>(new Constant(C, *this));
1554 auto *NewC = It->second.get();
1555 for (llvm::Value *COp : C->operands())
1557 return NewC;
1558 }
1559 if (auto *Arg = dyn_cast<llvm::Argument>(LLVMV)) {
1560 It->second = std::unique_ptr<Argument>(new Argument(Arg, *this));
1561 return It->second.get();
1562 }
1563 if (auto *BB = dyn_cast<llvm::BasicBlock>(LLVMV)) {
1564 assert(isa<BlockAddress>(U) &&
1565 "This won't create a SBBB, don't call this function directly!");
1566 if (auto *SBBB = getValue(BB))
1567 return SBBB;
1568 return nullptr;
1569 }
1570 assert(isa<llvm::Instruction>(LLVMV) && "Expected Instruction");
1571
1572 switch (cast<llvm::Instruction>(LLVMV)->getOpcode()) {
1573 case llvm::Instruction::Select: {
1574 auto *LLVMSel = cast<llvm::SelectInst>(LLVMV);
1575 It->second = std::unique_ptr<SelectInst>(new SelectInst(LLVMSel, *this));
1576 return It->second.get();
1577 }
1578 case llvm::Instruction::ExtractElement: {
1579 auto *LLVMIns = cast<llvm::ExtractElementInst>(LLVMV);
1580 It->second = std::unique_ptr<ExtractElementInst>(
1581 new ExtractElementInst(LLVMIns, *this));
1582 return It->second.get();
1583 }
1584 case llvm::Instruction::InsertElement: {
1585 auto *LLVMIns = cast<llvm::InsertElementInst>(LLVMV);
1586 It->second = std::unique_ptr<InsertElementInst>(
1587 new InsertElementInst(LLVMIns, *this));
1588 return It->second.get();
1589 }
1590 case llvm::Instruction::Br: {
1591 auto *LLVMBr = cast<llvm::BranchInst>(LLVMV);
1592 It->second = std::unique_ptr<BranchInst>(new BranchInst(LLVMBr, *this));
1593 return It->second.get();
1594 }
1595 case llvm::Instruction::Load: {
1596 auto *LLVMLd = cast<llvm::LoadInst>(LLVMV);
1597 It->second = std::unique_ptr<LoadInst>(new LoadInst(LLVMLd, *this));
1598 return It->second.get();
1599 }
1600 case llvm::Instruction::Store: {
1601 auto *LLVMSt = cast<llvm::StoreInst>(LLVMV);
1602 It->second = std::unique_ptr<StoreInst>(new StoreInst(LLVMSt, *this));
1603 return It->second.get();
1604 }
1605 case llvm::Instruction::Ret: {
1606 auto *LLVMRet = cast<llvm::ReturnInst>(LLVMV);
1607 It->second = std::unique_ptr<ReturnInst>(new ReturnInst(LLVMRet, *this));
1608 return It->second.get();
1609 }
1610 case llvm::Instruction::Call: {
1611 auto *LLVMCall = cast<llvm::CallInst>(LLVMV);
1612 It->second = std::unique_ptr<CallInst>(new CallInst(LLVMCall, *this));
1613 return It->second.get();
1614 }
1615 case llvm::Instruction::Invoke: {
1616 auto *LLVMInvoke = cast<llvm::InvokeInst>(LLVMV);
1617 It->second = std::unique_ptr<InvokeInst>(new InvokeInst(LLVMInvoke, *this));
1618 return It->second.get();
1619 }
1620 case llvm::Instruction::CallBr: {
1621 auto *LLVMCallBr = cast<llvm::CallBrInst>(LLVMV);
1622 It->second = std::unique_ptr<CallBrInst>(new CallBrInst(LLVMCallBr, *this));
1623 return It->second.get();
1624 }
1625 case llvm::Instruction::GetElementPtr: {
1626 auto *LLVMGEP = cast<llvm::GetElementPtrInst>(LLVMV);
1627 It->second = std::unique_ptr<GetElementPtrInst>(
1628 new GetElementPtrInst(LLVMGEP, *this));
1629 return It->second.get();
1630 }
1631 case llvm::Instruction::AtomicCmpXchg: {
1632 auto *LLVMAtomicCmpXchg = cast<llvm::AtomicCmpXchgInst>(LLVMV);
1633 It->second = std::unique_ptr<AtomicCmpXchgInst>(
1635 return It->second.get();
1636 }
1637 case llvm::Instruction::Alloca: {
1638 auto *LLVMAlloca = cast<llvm::AllocaInst>(LLVMV);
1639 It->second = std::unique_ptr<AllocaInst>(new AllocaInst(LLVMAlloca, *this));
1640 return It->second.get();
1641 }
1642 case llvm::Instruction::ZExt:
1643 case llvm::Instruction::SExt:
1644 case llvm::Instruction::FPToUI:
1645 case llvm::Instruction::FPToSI:
1646 case llvm::Instruction::FPExt:
1647 case llvm::Instruction::PtrToInt:
1648 case llvm::Instruction::IntToPtr:
1649 case llvm::Instruction::SIToFP:
1650 case llvm::Instruction::UIToFP:
1651 case llvm::Instruction::Trunc:
1652 case llvm::Instruction::FPTrunc:
1653 case llvm::Instruction::BitCast:
1654 case llvm::Instruction::AddrSpaceCast: {
1655 auto *LLVMCast = cast<llvm::CastInst>(LLVMV);
1656 It->second = std::unique_ptr<CastInst>(new CastInst(LLVMCast, *this));
1657 return It->second.get();
1658 }
1659 case llvm::Instruction::PHI: {
1660 auto *LLVMPhi = cast<llvm::PHINode>(LLVMV);
1661 It->second = std::unique_ptr<PHINode>(new PHINode(LLVMPhi, *this));
1662 return It->second.get();
1663 }
1664 case llvm::Instruction::Unreachable: {
1665 auto *LLVMUnreachable = cast<llvm::UnreachableInst>(LLVMV);
1666 It->second = std::unique_ptr<UnreachableInst>(
1667 new UnreachableInst(LLVMUnreachable, *this));
1668 return It->second.get();
1669 }
1670 default:
1671 break;
1672 }
1673
1674 It->second = std::unique_ptr<OpaqueInst>(
1675 new OpaqueInst(cast<llvm::Instruction>(LLVMV), *this));
1676 return It->second.get();
1677}
1678
1680 assert(getValue(LLVMBB) == nullptr && "Already exists!");
1681 auto NewBBPtr = std::unique_ptr<BasicBlock>(new BasicBlock(LLVMBB, *this));
1682 auto *BB = cast<BasicBlock>(registerValue(std::move(NewBBPtr)));
1683 // Create SandboxIR for BB's body.
1684 BB->buildBasicBlockFromLLVMIR(LLVMBB);
1685 return BB;
1686}
1687
1689 auto NewPtr = std::unique_ptr<SelectInst>(new SelectInst(SI, *this));
1690 return cast<SelectInst>(registerValue(std::move(NewPtr)));
1691}
1692
1695 auto NewPtr =
1696 std::unique_ptr<ExtractElementInst>(new ExtractElementInst(EEI, *this));
1697 return cast<ExtractElementInst>(registerValue(std::move(NewPtr)));
1698}
1699
1702 auto NewPtr =
1703 std::unique_ptr<InsertElementInst>(new InsertElementInst(IEI, *this));
1704 return cast<InsertElementInst>(registerValue(std::move(NewPtr)));
1705}
1706
1708 auto NewPtr = std::unique_ptr<BranchInst>(new BranchInst(BI, *this));
1709 return cast<BranchInst>(registerValue(std::move(NewPtr)));
1710}
1711
1713 auto NewPtr = std::unique_ptr<LoadInst>(new LoadInst(LI, *this));
1714 return cast<LoadInst>(registerValue(std::move(NewPtr)));
1715}
1716
1718 auto NewPtr = std::unique_ptr<StoreInst>(new StoreInst(SI, *this));
1719 return cast<StoreInst>(registerValue(std::move(NewPtr)));
1720}
1721
1723 auto NewPtr = std::unique_ptr<ReturnInst>(new ReturnInst(I, *this));
1724 return cast<ReturnInst>(registerValue(std::move(NewPtr)));
1725}
1726
1728 auto NewPtr = std::unique_ptr<CallInst>(new CallInst(I, *this));
1729 return cast<CallInst>(registerValue(std::move(NewPtr)));
1730}
1731
1733 auto NewPtr = std::unique_ptr<InvokeInst>(new InvokeInst(I, *this));
1734 return cast<InvokeInst>(registerValue(std::move(NewPtr)));
1735}
1736
1738 auto NewPtr = std::unique_ptr<CallBrInst>(new CallBrInst(I, *this));
1739 return cast<CallBrInst>(registerValue(std::move(NewPtr)));
1740}
1741
1743 auto NewPtr =
1744 std::unique_ptr<UnreachableInst>(new UnreachableInst(UI, *this));
1745 return cast<UnreachableInst>(registerValue(std::move(NewPtr)));
1746}
1747
1750 auto NewPtr =
1751 std::unique_ptr<GetElementPtrInst>(new GetElementPtrInst(I, *this));
1752 return cast<GetElementPtrInst>(registerValue(std::move(NewPtr)));
1753}
1756 auto NewPtr =
1757 std::unique_ptr<AtomicCmpXchgInst>(new AtomicCmpXchgInst(I, *this));
1758 return cast<AtomicCmpXchgInst>(registerValue(std::move(NewPtr)));
1759}
1761 auto NewPtr = std::unique_ptr<AllocaInst>(new AllocaInst(I, *this));
1762 return cast<AllocaInst>(registerValue(std::move(NewPtr)));
1763}
1765 auto NewPtr = std::unique_ptr<CastInst>(new CastInst(I, *this));
1766 return cast<CastInst>(registerValue(std::move(NewPtr)));
1767}
1769 auto NewPtr = std::unique_ptr<PHINode>(new PHINode(I, *this));
1770 return cast<PHINode>(registerValue(std::move(NewPtr)));
1771}
1772
1774 auto It = LLVMValueToValueMap.find(V);
1775 if (It != LLVMValueToValueMap.end())
1776 return It->second.get();
1777 return nullptr;
1778}
1779
1781 assert(getValue(F) == nullptr && "Already exists!");
1782 auto NewFPtr = std::unique_ptr<Function>(new Function(F, *this));
1783 auto *SBF = cast<Function>(registerValue(std::move(NewFPtr)));
1784 // Create arguments.
1785 for (auto &Arg : F->args())
1786 getOrCreateArgument(&Arg);
1787 // Create BBs.
1788 for (auto &BB : *F)
1789 createBasicBlock(&BB);
1790 return SBF;
1791}
1792
1794 auto *BB = cast<llvm::BasicBlock>(Val);
1795 auto *F = BB->getParent();
1796 if (F == nullptr)
1797 // Detached
1798 return nullptr;
1799 return cast_or_null<Function>(Ctx.getValue(F));
1800}
1801
1802void BasicBlock::buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB) {
1803 for (llvm::Instruction &IRef : reverse(*LLVMBB)) {
1804 llvm::Instruction *I = &IRef;
1806 for (auto [OpIdx, Op] : enumerate(I->operands())) {
1807 // Skip instruction's label operands
1808 if (isa<llvm::BasicBlock>(Op))
1809 continue;
1810 // Skip metadata
1811 if (isa<llvm::MetadataAsValue>(Op))
1812 continue;
1813 // Skip asm
1814 if (isa<llvm::InlineAsm>(Op))
1815 continue;
1817 }
1818 }
1819#if !defined(NDEBUG) && defined(SBVEC_EXPENSIVE_CHECKS)
1820 verify();
1821#endif
1822}
1823
1825 llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
1827 if (!BB->empty()) {
1828 auto *V = Ctx.getValue(&*BB->begin());
1829 assert(V != nullptr && "No SandboxIR for BB->begin()!");
1830 auto *I = cast<Instruction>(V);
1831 unsigned Num = I->getNumOfIRInstrs();
1832 assert(Num >= 1u && "Bad getNumOfIRInstrs()");
1833 It = std::next(It, Num - 1);
1834 }
1835 return iterator(BB, It, &Ctx);
1836}
1837
1839 auto *TerminatorV =
1840 Ctx.getValue(cast<llvm::BasicBlock>(Val)->getTerminator());
1841 return cast_or_null<Instruction>(TerminatorV);
1842}
1843
1845 auto *BB = cast<llvm::BasicBlock>(Val);
1846 assert(!BB->empty() && "Empty block!");
1847 auto *SBI = cast<Instruction>(getContext().getValue(&*BB->begin()));
1848 assert(SBI != nullptr && "Expected Instr!");
1849 return *SBI;
1850}
1851
1853 auto *BB = cast<llvm::BasicBlock>(Val);
1854 assert(!BB->empty() && "Empty block!");
1855 auto *SBI = cast<Instruction>(getContext().getValue(&*BB->rbegin()));
1856 assert(SBI != nullptr && "Expected Instr!");
1857 return *SBI;
1858}
1859
1860#ifndef NDEBUG
1862 llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
1863 const auto &Name = BB->getName();
1864 OS << Name;
1865 if (!Name.empty())
1866 OS << ":\n";
1867 // If there are Instructions in the BB that are not mapped to SandboxIR, then
1868 // use a crash-proof dump.
1869 if (any_of(*BB, [this](llvm::Instruction &I) {
1870 return Ctx.getValue(&I) == nullptr;
1871 })) {
1872 OS << "<Crash-proof mode!>\n";
1874 for (llvm::Instruction &IRef : *BB) {
1875 Value *SBV = Ctx.getValue(&IRef);
1876 if (SBV == nullptr)
1877 OS << IRef << " *** No SandboxIR ***\n";
1878 else {
1879 auto *SBI = dyn_cast<Instruction>(SBV);
1880 if (SBI == nullptr) {
1881 OS << IRef << " *** Not a SBInstruction!!! ***\n";
1882 } else {
1883 if (Visited.insert(SBI).second)
1884 OS << *SBI << "\n";
1885 }
1886 }
1887 }
1888 } else {
1889 for (auto &SBI : *this) {
1890 SBI.dumpOS(OS);
1891 OS << "\n";
1892 }
1893 }
1894}
1895#endif // NDEBUG
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ppc ctr loops PowerPC CTR Loops Verify
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static llvm::Instruction::CastOps getLLVMCastOp(Instruction::Opcode Opc)
Definition: SandboxIR.cpp:1188
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
an instruction to allocate memory on the stack
Definition: Instructions.h:61
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:495
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
bool empty() const
Definition: BasicBlock.h:470
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
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:530
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
This instruction extracts a single (scalar) element from a VectorType value.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
Class to represent function types.
Definition: DerivedTypes.h:103
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:915
CallBrInst * CreateCallBr(FunctionType *Ty, Value *Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="")
Create a callbr instruction.
Definition: IRBuilder.h:1202
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2480
AtomicCmpXchgInst * CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1846
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1778
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2468
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1812
UnreachableInst * CreateUnreachable()
Definition: IRBuilder.h:1268
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1091
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Create an invoke instruction.
Definition: IRBuilder.h:1163
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
Definition: IRBuilder.h:1100
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1871
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition: IRBuilder.h:1125
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1095
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition: IRBuilder.h:1119
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2169
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:177
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition: IRBuilder.h:1831
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2420
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2674
This instruction inserts a single (scalar) element into a VectorType value.
const char * getOpcodeName() const
Definition: Instruction.h:276
Invoke instruction.
An instruction for reading from memory.
Definition: Instructions.h:174
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Return a value (possibly void), from a function.
This class represents the LLVM 'select' instruction.
void reserve(size_type N)
Definition: SmallVector.h:677
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
An instruction for storing to memory.
Definition: Instructions.h:290
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
LLVM Value Representation.
Definition: Value.h:74
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
use_iterator use_begin()
Definition: Value.h:360
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:5106
void replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition: Value.cpp:542
use_iterator use_end()
Definition: Value.h:368
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:132
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:353
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: SandboxIR.h:1585
Value * getArraySize()
Get the number of elements allocated.
Definition: SandboxIR.cpp:1368
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: SandboxIR.h:1567
void setAlignment(Align Align)
Definition: SandboxIR.cpp:1353
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
Definition: SandboxIR.cpp:1361
static AllocaInst * create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, Value *ArraySize=nullptr, const Twine &Name="")
Definition: SandboxIR.cpp:1320
void setAllocatedType(Type *Ty)
for use only in special circumstances that need to generically transform a whole instruction (eg: IR ...
Definition: SandboxIR.cpp:1346
Argument of a sandboxir::Function.
Definition: SandboxIR.h:372
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:214
void printAsOperand(raw_ostream &OS) const
Definition: SandboxIR.cpp:211
void setSyncScopeID(SyncScope::ID SSID)
Definition: SandboxIR.cpp:1222
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
Definition: SandboxIR.cpp:1290
void setSuccessOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.cpp:1304
static AtomicCmpXchgInst * create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
Definition: SandboxIR.cpp:1243
AtomicOrdering getFailureOrdering() const
Definition: SandboxIR.h:1472
bool isVolatile() const
Return true if this is a cmpxchg from a volatile memory location.
Definition: SandboxIR.h:1453
AtomicOrdering getSuccessOrdering() const
Definition: SandboxIR.h:1467
void setFailureOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.cpp:1312
SyncScope::ID getSyncScopeID() const
Definition: SandboxIR.h:1479
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: SandboxIR.h:1446
bool isWeak() const
Return true if this cmpxchg may spuriously fail.
Definition: SandboxIR.h:1459
Iterator for Instructions in a `BasicBlock.
Definition: SandboxIR.h:514
Contains a list of sandboxir::Instruction's.
Definition: SandboxIR.h:556
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:1861
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:594
Function * getParent() const
Definition: SandboxIR.cpp:1793
Instruction & front() const
Definition: SandboxIR.cpp:1844
Instruction * getTerminator() const
Definition: SandboxIR.cpp:1838
Context & getContext() const
Definition: SandboxIR.h:587
Instruction & back() const
Definition: SandboxIR.cpp:1852
iterator end() const
Definition: SandboxIR.h:577
unsigned getNumSuccessors() const
Definition: SandboxIR.h:926
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:653
bool isConditional() const
Definition: SandboxIR.h:921
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
Definition: SandboxIR.cpp:669
BasicBlock * getSuccessor(unsigned SuccIdx) const
Definition: SandboxIR.cpp:662
static BranchInst * create(BasicBlock *IfTrue, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:611
Value * getCondition() const
Definition: SandboxIR.cpp:657
void setCalledFunction(Function *F)
Definition: SandboxIR.cpp:851
Function * getCalledFunction() const
Definition: SandboxIR.cpp:843
void setCalledOperand(Value *V)
Definition: SandboxIR.h:1223
Value * getCalledOperand() const
Definition: SandboxIR.cpp:834
Use getCalledOperandUse() const
Definition: SandboxIR.cpp:838
static CallBrInst * create(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
Definition: SandboxIR.cpp:958
BasicBlock * getIndirectDest(unsigned Idx) const
Definition: SandboxIR.cpp:1016
void setDefaultDest(BasicBlock *BB)
Definition: SandboxIR.cpp:1027
Value * getIndirectDestLabel(unsigned Idx) const
Definition: SandboxIR.cpp:1005
Value * getIndirectDestLabelUse(unsigned Idx) const
Definition: SandboxIR.cpp:1008
SmallVector< BasicBlock *, 16 > getIndirectDests() const
Definition: SandboxIR.cpp:1020
BasicBlock * getDefaultDest() const
Definition: SandboxIR.cpp:1012
BasicBlock * getSuccessor(unsigned Idx) const
Definition: SandboxIR.cpp:1038
void setIndirectDest(unsigned Idx, BasicBlock *BB)
Definition: SandboxIR.cpp:1033
static CallInst * create(FunctionType *FTy, Value *Func, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
static Value * create(Type *DestTy, Opcode Op, Value *Operand, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1372
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:1403
static Constant * createInt(Type *Ty, uint64_t V, Context &Ctx, bool IsSigned=false)
Definition: SandboxIR.cpp:1457
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.cpp:1464
CallBrInst * createCallBrInst(llvm::CallBrInst *I)
Definition: SandboxIR.cpp:1737
GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
Definition: SandboxIR.cpp:1749
DenseMap< llvm::Value *, std::unique_ptr< sandboxir::Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
Definition: SandboxIR.h:1815
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
Definition: SandboxIR.cpp:1525
sandboxir::Value * getValue(llvm::Value *V) const
Definition: SandboxIR.cpp:1773
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
Definition: SandboxIR.h:1830
Function * createFunction(llvm::Function *F)
Create a sandboxir::Function for an existing LLVM IR F, including all blocks and instructions.
Definition: SandboxIR.cpp:1780
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: SandboxIR.cpp:1543
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: SandboxIR.cpp:1518
UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
Definition: SandboxIR.cpp:1742
BranchInst * createBranchInst(llvm::BranchInst *I)
Definition: SandboxIR.cpp:1707
Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
Definition: SandboxIR.h:1844
BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Create a sandboxir::BasicBlock for an existing LLVM IR BB.
Definition: SandboxIR.cpp:1679
ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
Definition: SandboxIR.cpp:1694
LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: SandboxIR.cpp:1712
AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Definition: SandboxIR.cpp:1760
CallInst * createCallInst(llvm::CallInst *I)
Definition: SandboxIR.cpp:1727
std::unique_ptr< Value > detachLLVMValue(llvm::Value *V)
Remove V from the maps and returns the unique_ptr.
Definition: SandboxIR.cpp:1507
StoreInst * createStoreInst(llvm::StoreInst *SI)
Definition: SandboxIR.cpp:1717
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: SandboxIR.h:1840
InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
Definition: SandboxIR.cpp:1701
AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
Definition: SandboxIR.cpp:1755
ReturnInst * createReturnInst(llvm::ReturnInst *I)
Definition: SandboxIR.cpp:1722
PHINode * createPHINode(llvm::PHINode *I)
Definition: SandboxIR.cpp:1768
SelectInst * createSelectInst(llvm::SelectInst *SI)
Definition: SandboxIR.cpp:1688
CastInst * createCastInst(llvm::CastInst *I)
Definition: SandboxIR.cpp:1764
friend class BasicBlock
Various leaf nodes.
Definition: SandboxIR.h:1852
InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Definition: SandboxIR.cpp:1732
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: SandboxIR.h:1913
static Value * create(Value *Vec, Value *Idx, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1433
void dumpNameAndArgs(raw_ostream &OS) const
Definition: SandboxIR.cpp:1469
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:1484
This class can be used for tracking most instruction setters.
Definition: Tracker.h:228
static Value * create(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
static Value * create(Value *Vec, Value *NewElt, Value *Idx, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1407
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: SandboxIR.h:603
bool hasNoUnsignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: SandboxIR.h:686
BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
Definition: SandboxIR.cpp:331
void removeFromParent()
Detach this from its parent BasicBlock without deleting it.
Definition: SandboxIR.cpp:359
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
Definition: SandboxIR.h:706
virtual unsigned getNumOfIRInstrs() const =0
This is used by BasicBlock::iterator.
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Definition: SandboxIR.h:731
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
Definition: SandboxIR.cpp:558
void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
Definition: SandboxIR.cpp:527
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:464
bool hasAllowContract() const
Determine whether the allow-contract flag is set.
Definition: SandboxIR.h:747
void setHasAllowContract(bool B)
Set or clear the allow-contract flag on this instruction, which must be an operator which supports th...
Definition: SandboxIR.cpp:543
void setIsExact(bool B=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:497
bool hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
Definition: SandboxIR.h:755
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: SandboxIR.h:693
void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
Definition: SandboxIR.cpp:511
void insertInto(BasicBlock *BB, const BBIterator &WhereIt)
Insert this detached instruction into BB at WhereIt.
Definition: SandboxIR.cpp:433
void setHasApproxFunc(bool B)
Set or clear the approximate-math-functions flag on this instruction, which must be an operator which...
Definition: SandboxIR.cpp:565
void setHasAllowReassoc(bool B)
Set or clear the reassociation flag on this instruction, which must be an operator which supports thi...
Definition: SandboxIR.cpp:504
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
Definition: SandboxIR.cpp:551
void eraseFromParent()
Detach this Value from its parent and delete it.
Definition: SandboxIR.cpp:367
Instruction * getNextNode() const
\Returns the next sandboxir::Instruction in the block, or nullptr if at the end of the block.
Definition: SandboxIR.cpp:336
void moveBefore(BasicBlock &BB, const BBIterator &WhereIt)
Move this instruction to WhereIt.
Definition: SandboxIR.cpp:391
llvm::Instruction * getTopmostLLVMInstruction() const
A SandboxIR Instruction may map to multiple LLVM IR Instruction.
Definition: SandboxIR.cpp:319
void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
Definition: SandboxIR.cpp:519
void insertAfter(Instruction *AfterI)
Insert this detached instruction after AfterI.
Definition: SandboxIR.cpp:429
virtual SmallVector< llvm::Instruction *, 1 > getLLVMInstrs() const =0
\Returns the LLVM IR Instructions that this SandboxIR maps to in program order.
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.cpp:573
Instruction * getPrevNode() const
\Returns the previous sandboxir::Instruction in the block, or nullptr if at the beginning of the bloc...
Definition: SandboxIR.cpp:351
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition: SandboxIR.h:765
void setHasNoSignedWrap(bool B=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:483
void setHasAllowReciprocal(bool B)
Set or clear the allow-reciprocal flag on this instruction, which must be an operator which supports ...
Definition: SandboxIR.cpp:535
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
Definition: SandboxIR.h:739
void setFast(bool B)
Set or clear all fast-math-flags on this instruction, which must be an operator which supports this f...
Definition: SandboxIR.cpp:490
void setHasNoUnsignedWrap(bool B=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:475
void insertBefore(Instruction *BeforeI)
Insert this detached instruction before BeforeI.
Definition: SandboxIR.cpp:415
BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
Definition: SandboxIR.cpp:455
Instruction * getLandingPadInst() const
Definition: SandboxIR.cpp:948
BasicBlock * getSuccessor(unsigned SuccIdx) const
Definition: SandboxIR.cpp:953
void setNormalDest(BasicBlock *BB)
Definition: SandboxIR.cpp:940
static InvokeInst * create(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
Definition: SandboxIR.cpp:894
void setUnwindDest(BasicBlock *BB)
Definition: SandboxIR.cpp:944
BasicBlock * getNormalDest() const
Definition: SandboxIR.cpp:932
BasicBlock * getUnwindDest() const
Definition: SandboxIR.cpp:936
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:724
static LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:689
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Definition: SandboxIR.cpp:682
Value * getPointerOperand() const
Definition: SandboxIR.cpp:728
An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to an OpaqueInstr.
Definition: SandboxIR.h:1794
Iterator for the Use edges of a User's operands.
Definition: SandboxIR.h:137
OperandUseIterator operator+(unsigned Num) const
Definition: SandboxIR.cpp:89
OperandUseIterator operator-(unsigned Num) const
Definition: SandboxIR.cpp:95
OperandUseIterator & operator++()
Definition: SandboxIR.cpp:66
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:1097
void setIncomingBlock(unsigned Idx, BasicBlock *BB)
Definition: SandboxIR.cpp:1120
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
Definition: SandboxIR.cpp:1166
void setIncomingValue(unsigned Idx, Value *V)
Definition: SandboxIR.cpp:1104
unsigned getNumIncomingValues() const
Definition: SandboxIR.h:1755
void addIncoming(Value *V, BasicBlock *BB)
Definition: SandboxIR.cpp:1127
int getBasicBlockIndex(const BasicBlock *BB) const
Definition: SandboxIR.cpp:1152
Value * removeIncomingValue(unsigned Idx)
Definition: SandboxIR.cpp:1134
void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate)
Definition: SandboxIR.cpp:1173
Value * hasConstantValue() const
Definition: SandboxIR.cpp:1162
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition: SandboxIR.cpp:1156
BasicBlock * getIncomingBlock(unsigned Idx) const
Definition: SandboxIR.cpp:1111
static PHINode * create(Type *Ty, unsigned NumReservedValues, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1089
Value * getIncomingValue(unsigned Idx) const
Definition: SandboxIR.cpp:1101
Value * getReturnValue() const
\Returns null if there is no return value.
Definition: SandboxIR.cpp:829
static ReturnInst * create(Value *RetVal, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:814
static Value * create(Value *Cond, Value *True, Value *False, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:589
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:607
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:771
void setVolatile(bool V)
Specify whether this is a volatile store or not.
Definition: SandboxIR.cpp:732
static StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:739
Value * getPointerOperand() const
Definition: SandboxIR.cpp:779
Value * getValueOperand() const
Definition: SandboxIR.cpp:775
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:313
bool isTracking() const
\Returns true if the tracker is recording changes.
Definition: Tracker.h:363
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition: Tracker.h:339
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Definition: Tracker.h:356
static bool classof(const Value *From)
Definition: SandboxIR.cpp:800
static UnreachableInst * create(Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:783
Tracks the change of the source Value of a sandboxir::Use.
Definition: Tracker.h:85
Tracks swapping a Use with another Use.
Definition: Tracker.h:149
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
void dumpOS(raw_ostream &OS) const
Definition: SandboxIR.cpp:33
unsigned getOperandNo() const
Definition: SandboxIR.cpp:25
Value * get() const
Definition: SandboxIR.cpp:18
void set(Value *V)
Definition: SandboxIR.cpp:20
void swap(Use &OtherUse)
Definition: SandboxIR.cpp:27
class User * getUser() const
Definition: Use.h:54
void dump() const
Definition: SandboxIR.cpp:61
Iterator for the Use edges of a Value's users.
Definition: SandboxIR.h:173
UserUseIterator & operator++()
Definition: SandboxIR.cpp:73
A sandboxir::User has operands.
Definition: SandboxIR.h:391
virtual unsigned getUseOperandNo(const Use &Use) const =0
\Returns the operand index of Use.
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:238
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition: SandboxIR.cpp:260
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition: SandboxIR.cpp:232
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition: SandboxIR.cpp:220
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: SandboxIR.cpp:253
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const =0
\Returns the Use for the OpIdx'th operand.
virtual unsigned getNumOperands() const
Definition: SandboxIR.h:461
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: SandboxIR.h:458
void dumpCommonHeader(raw_ostream &OS) const final
Definition: SandboxIR.cpp:274
A SandboxIR Value has users. This is the base class.
Definition: SandboxIR.h:199
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: SandboxIR.h:297
LLVM_DUMP_METHOD void dump() const
Definition: SandboxIR.cpp:206
use_iterator use_begin()
Definition: SandboxIR.cpp:115
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: SandboxIR.h:235
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: SandboxIR.cpp:169
user_iterator user_begin()
Definition: SandboxIR.cpp:125
void replaceAllUsesWith(Value *Other)
Definition: SandboxIR.cpp:156
void dumpCommonFooter(raw_ostream &OS) const
Definition: SandboxIR.cpp:179
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: SandboxIR.cpp:175
UserUseIterator use_iterator
Definition: SandboxIR.h:273
Context & Ctx
All values point to the context.
Definition: SandboxIR.h:259
ClassID SubclassID
For isa/dyn_cast.
Definition: SandboxIR.h:226
void dumpCommonSuffix(raw_ostream &OS) const
Definition: SandboxIR.cpp:195
Type * getType() const
Definition: SandboxIR.h:341
friend class Use
Definition: SandboxIR.h:239
Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
Definition: SandboxIR.cpp:108
iterator_range< user_iterator > users()
Definition: SandboxIR.h:311
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: SandboxIR.cpp:138
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: SandboxIR.cpp:136
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: SandboxIR.h:229
virtual void dumpOS(raw_ostream &OS) const =0
iterator_range< use_iterator > uses()
Definition: SandboxIR.h:285
void dumpCommonPrefix(raw_ostream &OS) const
Definition: SandboxIR.cpp:188
void printAsOperandCommon(raw_ostream &OS) const
Definition: SandboxIR.cpp:199
static const char * getSubclassIDStr(ClassID ID)
Definition: SandboxIR.h:209
@ LLVMCallBr
Definition: Core.h:69
@ LLVMUnreachable
Definition: Core.h:68
@ LLVMCall
Definition: Core.h:121
@ LLVMRet
Definition: Core.h:62
@ LLVMInvoke
Definition: Core.h:66
@ LLVMBr
Definition: Core.h:63
@ LLVMAlloca
Definition: Core.h:97
@ LLVMAtomicCmpXchg
Definition: Core.h:135
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2406
void interleave(ForwardIterator begin, ForwardIterator end, UnaryFunctor each_fn, NullaryFunctor between_fn)
An STL-style algorithm similar to std::for_each that applies a second functor between every pair of e...
Definition: STLExtras.h:2127
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:419
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition: STLExtras.h:1705
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition: STLExtras.h:1902
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Other
Any other memory.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Helper for mapped_iterator.
Definition: SandboxIR.h:293