LLVM 22.0.0git
Instruction.cpp
Go to the documentation of this file.
1//===- Instruction.cpp - The Instructions of Sandbox 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
11
12namespace llvm::sandboxir {
13
15 switch (Opc) {
16#define OP(OPC) \
17 case Opcode::OPC: \
18 return #OPC;
19#define OPCODES(...) __VA_ARGS__
20#define DEF_INSTR(ID, OPC, CLASS) OPC
21#include "llvm/SandboxIR/Values.def"
22 }
23 llvm_unreachable("Unknown Opcode");
24}
25
27 Instruction *Prev = getPrevNode();
28 if (Prev == nullptr) {
29 // If at top of the BB, return the first BB instruction.
30 return &*cast<llvm::BasicBlock>(getParent()->Val)->begin();
31 }
32 // Else get the Previous sandbox IR instruction's bottom IR instruction and
33 // return its successor.
34 llvm::Instruction *PrevBotI = cast<llvm::Instruction>(Prev->Val);
35 return PrevBotI->getNextNode();
37
39 auto *I = cast<llvm::Instruction>(Val);
40 return BasicBlock::iterator(I->getParent(), I->getIterator(), &Ctx);
41}
42
44 assert(getParent() != nullptr && "Detached!");
45 assert(getIterator() != getParent()->end() && "Already at end!");
46 // `Val` is the bottom-most LLVM IR instruction. Get the next in the chain,
47 // and get the corresponding sandboxir Instruction that maps to it. This works
48 // even for SandboxIR Instructions that map to more than one LLVM Instruction.
49 auto *LLVMI = cast<llvm::Instruction>(Val);
50 assert(LLVMI->getParent() != nullptr && "LLVM IR instr is detached!");
51 auto *NextLLVMI = LLVMI->getNextNode();
52 auto *NextI = cast_or_null<Instruction>(Ctx.getValue(NextLLVMI));
53 if (NextI == nullptr)
54 return nullptr;
55 return NextI;
56}
57
59 assert(getParent() != nullptr && "Detached!");
60 auto It = getIterator();
61 if (It != getParent()->begin())
62 return std::prev(getIterator()).get();
63 return nullptr;
64}
65
68
69 // Detach all the LLVM IR instructions from their parent BB.
71 I->removeFromParent();
72}
73
75 assert(users().empty() && "Still connected to users, can't erase!");
76
78 std::unique_ptr<Value> Detached = Ctx.detach(this);
79 auto LLVMInstrs = getLLVMInstrs();
80
81 auto &Tracker = Ctx.getTracker();
82 if (Tracker.isTracking()) {
83 Tracker.track(std::make_unique<EraseFromParent>(std::move(Detached)));
84 // We don't actually delete the IR instruction, because then it would be
85 // impossible to bring it back from the dead at the same memory location.
86 // Instead we remove it from its BB and track its current location.
87 for (llvm::Instruction *I : LLVMInstrs)
88 I->removeFromParent();
89 // TODO: Multi-instructions need special treatment because some of the
90 // references are internal to the instruction.
91 for (llvm::Instruction *I : LLVMInstrs)
92 I->dropAllReferences();
93 } else {
94 // Erase in reverse to avoid erasing nstructions with attached uses.
95 for (llvm::Instruction *I : reverse(LLVMInstrs))
96 I->eraseFromParent();
97 }
98}
99
101 if (std::next(getIterator()) == WhereIt)
102 // Destination is same as origin, nothing to do.
103 return;
105 Ctx.runMoveInstrCallbacks(this, WhereIt);
106 Ctx.getTracker().emplaceIfTracking<MoveInstr>(this);
107
108 auto *LLVMBB = cast<llvm::BasicBlock>(BB.Val);
110 if (WhereIt == BB.end()) {
111 It = LLVMBB->end();
112 } else {
113 Instruction *WhereI = &*WhereIt;
114 It = WhereI->getTopmostLLVMInstruction()->getIterator();
116 // TODO: Move this to the verifier of sandboxir::Instruction.
118 [](auto *I1, auto *I2) { return I1->comesBefore(I2); }) &&
119 "Expected program order!");
120 // Do the actual move in LLVM IR.
121 for (auto *I : getLLVMInstrs())
122 I->moveBefore(*LLVMBB, It);
123}
124
126 llvm::Instruction *BeforeTopI = BeforeI->getTopmostLLVMInstruction();
128 Ctx.getTracker().emplaceIfTracking<InsertIntoBB>(this);
129
130 // Insert the LLVM IR Instructions in program order.
132 I->insertBefore(BeforeTopI->getIterator());
134
136 insertInto(AfterI->getParent(), std::next(AfterI->getIterator()));
137}
138
140 llvm::BasicBlock *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
141 llvm::Instruction *LLVMBeforeI;
143 Instruction *BeforeI;
144 if (WhereIt != BB->end()) {
145 BeforeI = &*WhereIt;
146 LLVMBeforeI = BeforeI->getTopmostLLVMInstruction();
147 LLVMBeforeIt = LLVMBeforeI->getIterator();
148 } else {
149 BeforeI = nullptr;
150 LLVMBeforeI = nullptr;
151 LLVMBeforeIt = LLVMBB->end();
152 }
153
154 Ctx.getTracker().emplaceIfTracking<InsertIntoBB>(this);
155
156 // Insert the LLVM IR Instructions in program order.
158 I->insertInto(LLVMBB, LLVMBeforeIt);
159}
160
162 // Get the LLVM IR Instruction that this maps to, get its parent, and get the
163 // corresponding sandboxir::BasicBlock by looking it up in sandboxir::Context.
164 auto *BB = cast<llvm::Instruction>(Val)->getParent();
165 if (BB == nullptr)
166 return nullptr;
167 return cast<BasicBlock>(Ctx.getValue(BB));
168}
169
171 switch (From->getSubclassID()) {
172#define DEF_INSTR(ID, OPC, CLASS) \
173 case ClassID::ID: \
174 return true;
175#include "llvm/SandboxIR/Values.def"
176 default:
177 return false;
178 }
179}
180
185 this);
186 cast<llvm::Instruction>(Val)->setHasNoUnsignedWrap(B);
187}
188
193 cast<llvm::Instruction>(Val)->setHasNoSignedWrap(B);
194}
195
199 GenericSetter<&Instruction::isFast, &Instruction::setFast>>(this);
200 cast<llvm::Instruction>(Val)->setFast(B);
201}
202
206 GenericSetter<&Instruction::isExact, &Instruction::setIsExact>>(this);
207 cast<llvm::Instruction>(Val)->setIsExact(B);
208}
209
214 cast<llvm::Instruction>(Val)->setHasAllowReassoc(B);
215}
216
220 GenericSetter<&Instruction::hasNoNaNs, &Instruction::setHasNoNaNs>>(
221 this);
222 cast<llvm::Instruction>(Val)->setHasNoNaNs(B);
224
229 this);
230 cast<llvm::Instruction>(Val)->setHasNoInfs(B);
231}
232
237 this);
238 cast<llvm::Instruction>(Val)->setHasNoSignedZeros(B);
239}
240
245 this);
246 cast<llvm::Instruction>(Val)->setHasAllowReciprocal(B);
247}
248
253 this);
254 cast<llvm::Instruction>(Val)->setHasAllowContract(B);
255}
256
261 cast<llvm::Instruction>(Val)->setFastMathFlags(FMF);
262}
263
268 cast<llvm::Instruction>(Val)->copyFastMathFlags(FMF);
269}
270
272 return Ctx.getType(cast<llvm::Instruction>(Val)->getAccessType());
273}
274
279 cast<llvm::Instruction>(Val)->setHasApproxFunc(B);
280}
281
282#ifndef NDEBUG
284 OS << "Unimplemented! Please override dump().";
285}
286#endif // NDEBUG
287
289 Context &Ctx, const Twine &Name) {
290 auto &Builder = setInsertPos(Pos);
291 auto *LLVMI =
292 cast<llvm::VAArgInst>(Builder.CreateVAArg(List->Val, Ty->LLVMTy, Name));
293 return Ctx.createVAArgInst(LLVMI);
294}
295
297 return Ctx.getValue(cast<llvm::VAArgInst>(Val)->getPointerOperand());
298}
299
301 const Twine &Name) {
302 auto &Builder = setInsertPos(Pos);
303 auto *LLVMI = cast<llvm::FreezeInst>(Builder.CreateFreeze(V->Val, Name));
304 return Ctx.createFreezeInst(LLVMI);
305}
306
308 Context &Ctx, SyncScope::ID SSID) {
309 auto &Builder = Instruction::setInsertPos(Pos);
310 llvm::FenceInst *LLVMI = Builder.CreateFence(Ordering, SSID);
311 return Ctx.createFenceInst(LLVMI);
312}
313
318 this);
319 cast<llvm::FenceInst>(Val)->setOrdering(Ordering);
320}
321
326 cast<llvm::FenceInst>(Val)->setSyncScopeID(SSID);
327}
328
330 InsertPosition Pos, Context &Ctx, const Twine &Name) {
331 auto &Builder = Instruction::setInsertPos(Pos);
332 llvm::Value *NewV =
333 Builder.CreateSelect(Cond->Val, True->Val, False->Val, Name);
334 if (auto *NewSI = dyn_cast<llvm::SelectInst>(NewV))
335 return Ctx.createSelectInst(NewSI);
336 assert(isa<llvm::Constant>(NewV) && "Expected constant");
337 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
339
343 cast<llvm::SelectInst>(Val)->swapValues();
344}
345
347 return From->getSubclassID() == ClassID::Select;
348}
349
351 Context &Ctx) {
352 auto &Builder = setInsertPos(Pos);
353 llvm::BranchInst *NewBr =
354 Builder.CreateBr(cast<llvm::BasicBlock>(IfTrue->Val));
355 return Ctx.createBranchInst(NewBr);
356}
357
359 Value *Cond, InsertPosition Pos, Context &Ctx) {
360 auto &Builder = setInsertPos(Pos);
361 llvm::BranchInst *NewBr =
362 Builder.CreateCondBr(Cond->Val, cast<llvm::BasicBlock>(IfTrue->Val),
363 cast<llvm::BasicBlock>(IfFalse->Val));
364 return Ctx.createBranchInst(NewBr);
365}
366
368 return From->getSubclassID() == ClassID::Br;
369}
370
372 assert(isConditional() && "Cannot get condition of an uncond branch!");
373 return Ctx.getValue(cast<llvm::BranchInst>(Val)->getCondition());
374}
375
376BasicBlock *BranchInst::getSuccessor(unsigned SuccIdx) const {
377 assert(SuccIdx < getNumSuccessors() &&
378 "Successor # out of range for Branch!");
379 return cast_or_null<BasicBlock>(
380 Ctx.getValue(cast<llvm::BranchInst>(Val)->getSuccessor(SuccIdx)));
381}
382
383void BranchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
384 assert((Idx == 0 || Idx == 1) && "Out of bounds!");
385 setOperand(2u - Idx, NewSucc);
386}
388BasicBlock *BranchInst::LLVMBBToSBBB::operator()(llvm::BasicBlock *BB) const {
389 return cast<BasicBlock>(Ctx.getValue(BB));
390}
391const BasicBlock *
392BranchInst::ConstLLVMBBToSBBB::operator()(const llvm::BasicBlock *BB) const {
393 return cast<BasicBlock>(Ctx.getValue(BB));
395
400 cast<llvm::LoadInst>(Val)->setVolatile(V);
402
404 InsertPosition Pos, bool IsVolatile, Context &Ctx,
405 const Twine &Name) {
406 auto &Builder = setInsertPos(Pos);
407 auto *NewLI =
408 Builder.CreateAlignedLoad(Ty->LLVMTy, Ptr->Val, Align, IsVolatile, Name);
409 auto *NewSBI = Ctx.createLoadInst(NewLI);
410 return NewSBI;
412
414 return From->getSubclassID() == ClassID::Load;
415}
416
418 return Ctx.getValue(cast<llvm::LoadInst>(Val)->getPointerOperand());
419}
420
425 cast<llvm::StoreInst>(Val)->setVolatile(V);
426}
427
429 InsertPosition Pos, bool IsVolatile,
430 Context &Ctx) {
431 auto &Builder = setInsertPos(Pos);
432 auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
433 auto *NewSBI = Ctx.createStoreInst(NewSI);
434 return NewSBI;
435}
436
438 return From->getSubclassID() == ClassID::Store;
439}
440
442 return Ctx.getValue(cast<llvm::StoreInst>(Val)->getValueOperand());
443}
444
446 return Ctx.getValue(cast<llvm::StoreInst>(Val)->getPointerOperand());
447}
448
450 auto &Builder = setInsertPos(Pos);
451 llvm::UnreachableInst *NewUI = Builder.CreateUnreachable();
452 return Ctx.createUnreachableInst(NewUI);
453}
454
456 return From->getSubclassID() == ClassID::Unreachable;
457}
458
459ReturnInst *ReturnInst::createCommon(Value *RetVal, IRBuilder<> &Builder,
460 Context &Ctx) {
461 llvm::ReturnInst *NewRI;
462 if (RetVal != nullptr)
463 NewRI = Builder.CreateRet(RetVal->Val);
464 else
465 NewRI = Builder.CreateRetVoid();
466 return Ctx.createReturnInst(NewRI);
467}
468
470 Context &Ctx) {
471 auto &Builder = setInsertPos(Pos);
472 return createCommon(RetVal, Builder, Ctx);
473}
474
476 auto *LLVMRetVal = cast<llvm::ReturnInst>(Val)->getReturnValue();
477 return LLVMRetVal != nullptr ? Ctx.getValue(LLVMRetVal) : nullptr;
478}
479
481 return cast<FunctionType>(
482 Ctx.getType(cast<llvm::CallBase>(Val)->getFunctionType()));
483}
484
486 return Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledOperand());
488
490 llvm::Use *LLVMUse = &cast<llvm::CallBase>(Val)->getCalledOperandUse();
491 return Use(LLVMUse, cast<User>(Ctx.getValue(LLVMUse->getUser())), Ctx);
492}
493
495 return cast_or_null<Function>(
496 Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledFunction()));
497}
499 return cast<Function>(Ctx.getValue(cast<llvm::CallBase>(Val)->getCaller()));
500}
501
503 // F's function type is private, so we rely on `setCalledFunction()` to update
504 // it. But even though we are calling `setCalledFunction()` we also need to
505 // track this change at the SandboxIR level, which is why we call
506 // `setCalledOperand()` here.
507 // Note: This may break if `setCalledFunction()` early returns if `F`
508 // is already set, but we do have a unit test for it.
510 cast<llvm::CallBase>(Val)->setCalledFunction(
511 cast<llvm::FunctionType>(F->getFunctionType()->LLVMTy),
512 cast<llvm::Function>(F->Val));
513}
514
517 Context &Ctx, const Twine &NameStr) {
518 auto &Builder = setInsertPos(Pos);
520 LLVMArgs.reserve(Args.size());
521 for (Value *Arg : Args)
522 LLVMArgs.push_back(Arg->Val);
523 llvm::CallInst *NewCI = Builder.CreateCall(
524 cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val, LLVMArgs, NameStr);
525 return Ctx.createCallInst(NewCI);
526}
527
529 BasicBlock *IfNormal, BasicBlock *IfException,
531 Context &Ctx, const Twine &NameStr) {
532 auto &Builder = setInsertPos(Pos);
534 LLVMArgs.reserve(Args.size());
535 for (Value *Arg : Args)
536 LLVMArgs.push_back(Arg->Val);
537 llvm::InvokeInst *Invoke = Builder.CreateInvoke(
538 cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val,
539 cast<llvm::BasicBlock>(IfNormal->Val),
540 cast<llvm::BasicBlock>(IfException->Val), LLVMArgs, NameStr);
541 return Ctx.createInvokeInst(Invoke);
542}
543
545 return cast<BasicBlock>(
546 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getNormalDest()));
547}
549 return cast<BasicBlock>(
550 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getUnwindDest()));
551}
553 setOperand(1, BB);
554 assert(getNormalDest() == BB && "LLVM IR uses a different operan index!");
557 setOperand(2, BB);
558 assert(getUnwindDest() == BB && "LLVM IR uses a different operan index!");
559}
561 return cast<LandingPadInst>(
562 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getLandingPadInst()));
563 ;
564}
565BasicBlock *InvokeInst::getSuccessor(unsigned SuccIdx) const {
566 return cast<BasicBlock>(
567 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getSuccessor(SuccIdx)));
568}
569
571 BasicBlock *DefaultDest,
572 ArrayRef<BasicBlock *> IndirectDests,
574 Context &Ctx, const Twine &NameStr) {
575 auto &Builder = setInsertPos(Pos);
576 SmallVector<llvm::BasicBlock *> LLVMIndirectDests;
577 LLVMIndirectDests.reserve(IndirectDests.size());
578 for (BasicBlock *IndDest : IndirectDests)
579 LLVMIndirectDests.push_back(cast<llvm::BasicBlock>(IndDest->Val));
580
582 LLVMArgs.reserve(Args.size());
583 for (Value *Arg : Args)
584 LLVMArgs.push_back(Arg->Val);
585
586 llvm::CallBrInst *CallBr =
587 Builder.CreateCallBr(cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val,
588 cast<llvm::BasicBlock>(DefaultDest->Val),
589 LLVMIndirectDests, LLVMArgs, NameStr);
590 return Ctx.createCallBrInst(CallBr);
591}
592
594 return Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDestLabel(Idx));
595}
597 return Ctx.getValue(
598 cast<llvm::CallBrInst>(Val)->getIndirectDestLabelUse(Idx));
599}
601 return cast<BasicBlock>(
602 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getDefaultDest()));
605 return cast<BasicBlock>(
606 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDest(Idx)));
607}
610 for (llvm::BasicBlock *LLVMBB :
611 cast<llvm::CallBrInst>(Val)->getIndirectDests())
612 BBs.push_back(cast<BasicBlock>(Ctx.getValue(LLVMBB)));
613 return BBs;
614}
619 cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val));
620}
625 this, Idx);
626 cast<llvm::CallBrInst>(Val)->setIndirectDest(Idx,
627 cast<llvm::BasicBlock>(BB->Val));
630 return cast<BasicBlock>(
631 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx)));
632}
634LandingPadInst *LandingPadInst::create(Type *RetTy, unsigned NumReservedClauses,
635 InsertPosition Pos, Context &Ctx,
636 const Twine &Name) {
637 auto &Builder = setInsertPos(Pos);
639 Builder.CreateLandingPad(RetTy->LLVMTy, NumReservedClauses, Name);
640 return Ctx.createLandingPadInst(LLVMI);
641}
642
647 cast<llvm::LandingPadInst>(Val)->setCleanup(V);
649
651 return cast<Constant>(
652 Ctx.getValue(cast<llvm::LandingPadInst>(Val)->getClause(Idx)));
654
656 return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getParentPad());
657}
663 cast<llvm::FuncletPadInst>(Val)->setParentPad(ParentPad->Val);
664}
665
667 return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getArgOperand(Idx));
669
674 this, Idx);
675 cast<llvm::FuncletPadInst>(Val)->setArgOperand(Idx, V->Val);
676}
677
679 return cast<CatchSwitchInst>(
680 Ctx.getValue(cast<llvm::CatchPadInst>(Val)->getCatchSwitch()));
681}
682
684 InsertPosition Pos, Context &Ctx,
685 const Twine &Name) {
686 auto &Builder = setInsertPos(Pos);
688 LLVMArgs.reserve(Args.size());
689 for (auto *Arg : Args)
690 LLVMArgs.push_back(Arg->Val);
691 llvm::CatchPadInst *LLVMI =
692 Builder.CreateCatchPad(ParentPad->Val, LLVMArgs, Name);
693 return Ctx.createCatchPadInst(LLVMI);
694}
695
697 InsertPosition Pos, Context &Ctx,
698 const Twine &Name) {
699 auto &Builder = setInsertPos(Pos);
701 LLVMArgs.reserve(Args.size());
702 for (auto *Arg : Args)
703 LLVMArgs.push_back(Arg->Val);
704 llvm::CleanupPadInst *LLVMI =
705 Builder.CreateCleanupPad(ParentPad->Val, LLVMArgs, Name);
706 return Ctx.createCleanupPadInst(LLVMI);
707}
708
710 InsertPosition Pos, Context &Ctx) {
711 auto &Builder = setInsertPos(Pos);
712 llvm::CatchReturnInst *LLVMI = Builder.CreateCatchRet(
713 cast<llvm::CatchPadInst>(CatchPad->Val), cast<llvm::BasicBlock>(BB->Val));
714 return Ctx.createCatchReturnInst(LLVMI);
715}
716
718 return cast<CatchPadInst>(
719 Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getCatchPad()));
720}
721
726 cast<llvm::CatchReturnInst>(Val)->setCatchPad(
727 cast<llvm::CatchPadInst>(CatchPad->Val));
728}
729
731 return cast<BasicBlock>(
732 Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getSuccessor()));
733}
734
739 cast<llvm::CatchReturnInst>(Val)->setSuccessor(
740 cast<llvm::BasicBlock>(NewSucc->Val));
741}
742
744 return Ctx.getValue(
745 cast<llvm::CatchReturnInst>(Val)->getCatchSwitchParentPad());
746}
747
749 BasicBlock *UnwindBB,
750 InsertPosition Pos, Context &Ctx) {
751 auto &Builder = setInsertPos(Pos);
752 auto *LLVMUnwindBB =
753 UnwindBB != nullptr ? cast<llvm::BasicBlock>(UnwindBB->Val) : nullptr;
755 cast<llvm::CleanupPadInst>(CleanupPad->Val), LLVMUnwindBB);
756 return Ctx.createCleanupReturnInst(LLVMI);
757}
758
760 return cast<CleanupPadInst>(
761 Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getCleanupPad()));
762}
763
768 this);
769 cast<llvm::CleanupReturnInst>(Val)->setCleanupPad(
770 cast<llvm::CleanupPadInst>(CleanupPad->Val));
771}
772
774 return cast_or_null<BasicBlock>(
775 Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getUnwindDest()));
777
782 this);
783 cast<llvm::CleanupReturnInst>(Val)->setUnwindDest(
784 cast<llvm::BasicBlock>(NewDest->Val));
785}
786
789 Context &Ctx, const Twine &NameStr) {
790 auto &Builder = setInsertPos(Pos);
791 SmallVector<llvm::Value *> LLVMIdxList;
792 LLVMIdxList.reserve(IdxList.size());
793 for (Value *Idx : IdxList)
794 LLVMIdxList.push_back(Idx->Val);
795 llvm::Value *NewV =
796 Builder.CreateGEP(Ty->LLVMTy, Ptr->Val, LLVMIdxList, NameStr);
797 if (auto *NewGEP = dyn_cast<llvm::GetElementPtrInst>(NewV))
798 return Ctx.createGetElementPtrInst(NewGEP);
799 assert(isa<llvm::Constant>(NewV) && "Expected constant");
800 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
801}
802
804 return Ctx.getType(
805 cast<llvm::GetElementPtrInst>(Val)->getSourceElementType());
806}
807
809 return Ctx.getType(
810 cast<llvm::GetElementPtrInst>(Val)->getResultElementType());
811}
812
814 return Ctx.getValue(cast<llvm::GetElementPtrInst>(Val)->getPointerOperand());
815}
816
818 return Ctx.getType(
819 cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType());
820}
821
822BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const {
823 return cast<BasicBlock>(Ctx.getValue(LLVMBB));
824}
825
826PHINode *PHINode::create(Type *Ty, unsigned NumReservedValues,
827 InsertPosition Pos, Context &Ctx, const Twine &Name) {
828 auto &Builder = setInsertPos(Pos);
829 llvm::PHINode *NewPHI =
830 Builder.CreatePHI(Ty->LLVMTy, NumReservedValues, Name);
831 return Ctx.createPHINode(NewPHI);
832}
833
835 return From->getSubclassID() == ClassID::PHI;
836}
837
839 return Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingValue(Idx));
840}
845 Idx);
846 cast<llvm::PHINode>(Val)->setIncomingValue(Idx, V->Val);
847}
849 return cast<BasicBlock>(
850 Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingBlock(Idx)));
851}
853 llvm::Use *LLVMUse = U.LLVMUse;
854 llvm::BasicBlock *BB = cast<llvm::PHINode>(Val)->getIncomingBlock(*LLVMUse);
855 return cast<BasicBlock>(Ctx.getValue(BB));
856}
858 // Helper to disambiguate PHINode::getIncomingBlock(unsigned).
859 constexpr BasicBlock *(PHINode::*GetIncomingBlockFn)(unsigned) const =
864 this, Idx);
865 cast<llvm::PHINode>(Val)->setIncomingBlock(Idx,
866 cast<llvm::BasicBlock>(BB->Val));
867}
869 auto &Tracker = Ctx.getTracker();
871
872 cast<llvm::PHINode>(Val)->addIncoming(V->Val,
873 cast<llvm::BasicBlock>(BB->Val));
874}
876 auto &Tracker = Ctx.getTracker();
878 llvm::Value *LLVMV =
879 cast<llvm::PHINode>(Val)->removeIncomingValue(Idx,
880 /*DeletePHIIfEmpty=*/false);
881 return Ctx.getValue(LLVMV);
882}
884 auto &Tracker = Ctx.getTracker();
886
887 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
888 llvm::Value *LLVMV =
889 cast<llvm::PHINode>(Val)->removeIncomingValue(LLVMBB,
890 /*DeletePHIIfEmpty=*/false);
891 return Ctx.getValue(LLVMV);
892}
894 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
895 return cast<llvm::PHINode>(Val)->getBasicBlockIndex(LLVMBB);
896}
898 auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
899 llvm::Value *LLVMV =
900 cast<llvm::PHINode>(Val)->getIncomingValueForBlock(LLVMBB);
901 return Ctx.getValue(LLVMV);
902}
904 llvm::Value *LLVMV = cast<llvm::PHINode>(Val)->hasConstantValue();
905 return LLVMV != nullptr ? Ctx.getValue(LLVMV) : nullptr;
906}
908 assert(New && Old && "Sandbox IR PHI node got a null basic block!");
909 for (unsigned Idx = 0, NumOps = cast<llvm::PHINode>(Val)->getNumOperands();
910 Idx != NumOps; ++Idx)
911 if (getIncomingBlock(Idx) == Old)
912 setIncomingBlock(Idx, New);
913}
914void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate) {
915 // Avoid duplicate tracking by going through this->removeIncomingValue here at
916 // the expense of some performance. Copy PHI::removeIncomingValueIf more
917 // directly if performance becomes an issue.
918
919 // Removing the element at index X, moves the element previously at X + 1
920 // to X. Working from the end avoids complications from that.
921 unsigned Idx = getNumIncomingValues();
922 while (Idx > 0) {
923 if (Predicate(Idx - 1))
925 --Idx;
926 }
927}
928
930 Context &Ctx, const Twine &Name) {
931 auto &Builder = setInsertPos(Pos);
932 auto *LLVMV = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
933 // It may have been folded into a constant.
934 if (auto *LLVMC = dyn_cast<llvm::Constant>(LLVMV))
935 return Ctx.getOrCreateConstant(LLVMC);
936 if (isa<llvm::ICmpInst>(LLVMV))
937 return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMV));
938 return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMV));
939}
940
943 Context &Ctx, const Twine &Name) {
944 Value *V = create(P, S1, S2, Pos, Ctx, Name);
945 if (auto *C = dyn_cast<Constant>(V))
946 return C;
947 cast<llvm::CmpInst>(V->Val)->copyIRFlags(F->Val);
948 return V;
949}
950
952 if (auto *VT = dyn_cast<VectorType>(OpndType)) {
953 // TODO: Cleanup when we have more complete support for
954 // sandboxir::VectorType
955 return OpndType->getContext().getType(llvm::VectorType::get(
957 cast<llvm::VectorType>(VT->LLVMTy)->getElementCount()));
958 }
959 return Type::getInt1Ty(OpndType->getContext());
960}
961
966 cast<llvm::CmpInst>(Val)->setPredicate(P);
967}
968
970 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
971 IC->swapOperands();
972 else
973 cast<FCmpInst>(this)->swapOperands();
974}
978 cast<llvm::ICmpInst>(Val)->swapOperands();
979}
980
983 cast<llvm::FCmpInst>(Val)->swapOperands();
984}
985
986#ifndef NDEBUG
990}
991
992void CmpInst::dump() const {
993 dumpOS(dbgs());
994 dbgs() << "\n";
995}
996#endif // NDEBUG
997
999 switch (Opc) {
1000 case Instruction::Opcode::ZExt:
1001 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::ZExt);
1002 case Instruction::Opcode::SExt:
1003 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SExt);
1004 case Instruction::Opcode::FPToUI:
1005 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToUI);
1006 case Instruction::Opcode::FPToSI:
1007 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToSI);
1008 case Instruction::Opcode::FPExt:
1009 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPExt);
1010 case Instruction::Opcode::PtrToAddr:
1011 return static_cast<llvm::Instruction::CastOps>(
1012 llvm::Instruction::PtrToAddr);
1013 case Instruction::Opcode::PtrToInt:
1014 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::PtrToInt);
1015 case Instruction::Opcode::IntToPtr:
1016 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::IntToPtr);
1017 case Instruction::Opcode::SIToFP:
1018 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SIToFP);
1019 case Instruction::Opcode::UIToFP:
1020 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::UIToFP);
1021 case Instruction::Opcode::Trunc:
1022 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::Trunc);
1023 case Instruction::Opcode::FPTrunc:
1024 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPTrunc);
1025 case Instruction::Opcode::BitCast:
1026 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::BitCast);
1027 case Instruction::Opcode::AddrSpaceCast:
1028 return static_cast<llvm::Instruction::CastOps>(
1029 llvm::Instruction::AddrSpaceCast);
1030 default:
1031 llvm_unreachable("Opcode not suitable for CastInst!");
1032 }
1033}
1034
1035/// \Returns the LLVM opcode that corresponds to \p Opc.
1037 switch (Opc) {
1038 case Instruction::Opcode::FNeg:
1039 return static_cast<llvm::Instruction::UnaryOps>(llvm::Instruction::FNeg);
1040 default:
1041 llvm_unreachable("Not a unary op!");
1042 }
1043}
1044
1046 unsigned NumHandlers,
1047 InsertPosition Pos, Context &Ctx,
1048 const Twine &Name) {
1049 auto &Builder = setInsertPos(Pos);
1050 llvm::CatchSwitchInst *LLVMCSI = Builder.CreateCatchSwitch(
1051 ParentPad->Val, cast<llvm::BasicBlock>(UnwindBB->Val), NumHandlers, Name);
1052 return Ctx.createCatchSwitchInst(LLVMCSI);
1053}
1054
1056 return Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getParentPad());
1057}
1060 Ctx.getTracker()
1063 cast<llvm::CatchSwitchInst>(Val)->setParentPad(ParentPad->Val);
1064}
1065
1067 return cast_or_null<BasicBlock>(
1068 Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getUnwindDest()));
1069}
1070
1075 cast<llvm::CatchSwitchInst>(Val)->setUnwindDest(
1076 cast<llvm::BasicBlock>(UnwindDest->Val));
1077}
1078
1081 cast<llvm::CatchSwitchInst>(Val)->addHandler(
1082 cast<llvm::BasicBlock>(Dest->Val));
1083}
1084
1086 auto &Builder = setInsertPos(Pos);
1087 auto *LLVMI = cast<llvm::ResumeInst>(Builder.CreateResume(Exn->Val));
1088 return Ctx.createResumeInst(LLVMI);
1089}
1090
1092 return Ctx.getValue(cast<llvm::ResumeInst>(Val)->getValue());
1093}
1094
1095SwitchInst *SwitchInst::create(Value *V, BasicBlock *Dest, unsigned NumCases,
1096 InsertPosition Pos, Context &Ctx,
1097 const Twine &Name) {
1098 auto &Builder = setInsertPos(Pos);
1100 Builder.CreateSwitch(V->Val, cast<llvm::BasicBlock>(Dest->Val), NumCases);
1102}
1103
1105 return Ctx.getValue(cast<llvm::SwitchInst>(Val)->getCondition());
1106}
1107
1109 Ctx.getTracker()
1112 this);
1113 cast<llvm::SwitchInst>(Val)->setCondition(V->Val);
1114}
1115
1117 return cast<BasicBlock>(
1118 Ctx.getValue(cast<llvm::SwitchInst>(Val)->getDefaultDest()));
1119}
1120
1122 Ctx.getTracker()
1125 cast<llvm::SwitchInst>(Val)->setDefaultDest(
1126 cast<llvm::BasicBlock>(DefaultCase->Val));
1129 auto *LLVMC = cast<llvm::SwitchInst>(Val)->findCaseDest(
1130 cast<llvm::BasicBlock>(BB->Val));
1131 return LLVMC != nullptr ? cast<ConstantInt>(Ctx.getValue(LLVMC)) : nullptr;
1132}
1133
1136 // TODO: Track this!
1137 cast<llvm::SwitchInst>(Val)->addCase(cast<llvm::ConstantInt>(OnVal->Val),
1138 cast<llvm::BasicBlock>(Dest->Val));
1139}
1140
1143
1144 auto *LLVMSwitch = cast<llvm::SwitchInst>(Val);
1145 unsigned CaseNum = It - case_begin();
1146 llvm::SwitchInst::CaseIt LLVMIt(LLVMSwitch, CaseNum);
1147 auto LLVMCaseIt = LLVMSwitch->removeCase(LLVMIt);
1148 unsigned Num = LLVMCaseIt - LLVMSwitch->case_begin();
1149 return CaseIt(this, Num);
1150}
1151
1153 return cast<BasicBlock>(
1154 Ctx.getValue(cast<llvm::SwitchInst>(Val)->getSuccessor(Idx)));
1155}
1156
1157void SwitchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
1158 Ctx.getTracker()
1161 Idx);
1162 cast<llvm::SwitchInst>(Val)->setSuccessor(
1163 Idx, cast<llvm::BasicBlock>(NewSucc->Val));
1164}
1165
1167 InsertPosition Pos, Context &Ctx,
1168 const Twine &Name) {
1169 auto &Builder = setInsertPos(Pos);
1170 auto *NewLLVMV = Builder.CreateUnOp(getLLVMUnaryOp(Op), OpV->Val, Name);
1171 if (auto *NewUnOpV = dyn_cast<llvm::UnaryOperator>(NewLLVMV)) {
1172 return Ctx.createUnaryOperator(NewUnOpV);
1173 }
1174 assert(isa<llvm::Constant>(NewLLVMV) && "Expected constant");
1175 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewLLVMV));
1176}
1177
1179 Value *CopyFrom, InsertPosition Pos,
1180 Context &Ctx, const Twine &Name) {
1181 auto *NewV = create(Op, OpV, Pos, Ctx, Name);
1182 if (auto *UnI = dyn_cast<llvm::UnaryOperator>(NewV->Val))
1183 UnI->copyIRFlags(CopyFrom->Val);
1184 return NewV;
1186
1187/// \Returns the LLVM opcode that corresponds to \p Opc.
1189 switch (Opc) {
1190 case Instruction::Opcode::Add:
1191 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Add);
1192 case Instruction::Opcode::FAdd:
1193 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FAdd);
1194 case Instruction::Opcode::Sub:
1195 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Sub);
1196 case Instruction::Opcode::FSub:
1197 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FSub);
1198 case Instruction::Opcode::Mul:
1199 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Mul);
1200 case Instruction::Opcode::FMul:
1201 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FMul);
1202 case Instruction::Opcode::UDiv:
1203 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::UDiv);
1204 case Instruction::Opcode::SDiv:
1205 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SDiv);
1206 case Instruction::Opcode::FDiv:
1207 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FDiv);
1208 case Instruction::Opcode::URem:
1209 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::URem);
1210 case Instruction::Opcode::SRem:
1211 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SRem);
1212 case Instruction::Opcode::FRem:
1213 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FRem);
1214 case Instruction::Opcode::Shl:
1215 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Shl);
1216 case Instruction::Opcode::LShr:
1217 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::LShr);
1218 case Instruction::Opcode::AShr:
1219 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::AShr);
1220 case Instruction::Opcode::And:
1221 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::And);
1222 case Instruction::Opcode::Or:
1223 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Or);
1224 case Instruction::Opcode::Xor:
1225 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Xor);
1226 default:
1227 llvm_unreachable("Not a binary op!");
1228 }
1229}
1231 InsertPosition Pos, Context &Ctx,
1232 const Twine &Name) {
1233 auto &Builder = setInsertPos(Pos);
1234 llvm::Value *NewV =
1235 Builder.CreateBinOp(getLLVMBinaryOp(Op), LHS->Val, RHS->Val, Name);
1236 if (auto *NewBinOp = dyn_cast<llvm::BinaryOperator>(NewV))
1237 return Ctx.createBinaryOperator(NewBinOp);
1238 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1239 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1240}
1241
1243 Value *RHS, Value *CopyFrom,
1244 InsertPosition Pos, Context &Ctx,
1245 const Twine &Name) {
1246
1247 Value *NewV = create(Op, LHS, RHS, Pos, Ctx, Name);
1248 if (auto *NewBO = dyn_cast<BinaryOperator>(NewV))
1249 cast<llvm::BinaryOperator>(NewBO->Val)->copyIRFlags(CopyFrom->Val);
1250 return NewV;
1251}
1254 Ctx.getTracker()
1257 this);
1258 cast<llvm::PossiblyDisjointInst>(Val)->setIsDisjoint(B);
1259}
1260
1262 Ctx.getTracker()
1265 cast<llvm::AtomicRMWInst>(Val)->setAlignment(Align);
1266}
1267
1269 Ctx.getTracker()
1272 cast<llvm::AtomicRMWInst>(Val)->setVolatile(V);
1274
1276 Ctx.getTracker()
1279 cast<llvm::AtomicRMWInst>(Val)->setOrdering(Ordering);
1281
1283 Ctx.getTracker()
1286 cast<llvm::AtomicRMWInst>(Val)->setSyncScopeID(SSID);
1287}
1288
1290 return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getPointerOperand());
1291}
1294 return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getValOperand());
1295}
1296
1299 InsertPosition Pos, Context &Ctx,
1300 SyncScope::ID SSID, const Twine &Name) {
1301 auto &Builder = setInsertPos(Pos);
1302 auto *LLVMAtomicRMW =
1303 Builder.CreateAtomicRMW(Op, Ptr->Val, Val->Val, Align, Ordering, SSID);
1306}
1307
1309 Ctx.getTracker()
1312 this);
1313 cast<llvm::AtomicCmpXchgInst>(Val)->setSyncScopeID(SSID);
1314}
1315
1317 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getPointerOperand());
1318}
1319
1321 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getCompareOperand());
1322}
1323
1325 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getNewValOperand());
1326}
1330 AtomicOrdering SuccessOrdering,
1331 AtomicOrdering FailureOrdering, InsertPosition Pos,
1332 Context &Ctx, SyncScope::ID SSID, const Twine &Name) {
1333 auto &Builder = setInsertPos(Pos);
1334 auto *LLVMAtomicCmpXchg =
1335 Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
1336 SuccessOrdering, FailureOrdering, SSID);
1337 LLVMAtomicCmpXchg->setName(Name);
1339}
1340
1342 Ctx.getTracker()
1345 cast<llvm::AtomicCmpXchgInst>(Val)->setAlignment(Align);
1346}
1347
1349 Ctx.getTracker()
1352 cast<llvm::AtomicCmpXchgInst>(Val)->setVolatile(V);
1353}
1354
1356 Ctx.getTracker()
1359 cast<llvm::AtomicCmpXchgInst>(Val)->setWeak(IsWeak);
1360}
1361
1363 Ctx.getTracker()
1366 this);
1367 cast<llvm::AtomicCmpXchgInst>(Val)->setSuccessOrdering(Ordering);
1368}
1369
1371 Ctx.getTracker()
1374 this);
1375 cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
1376}
1377
1378AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
1379 Context &Ctx, Value *ArraySize,
1380 const Twine &Name) {
1381 auto &Builder = setInsertPos(Pos);
1382 auto *NewAlloca =
1383 Builder.CreateAlloca(Ty->LLVMTy, AddrSpace, ArraySize->Val, Name);
1384 return Ctx.createAllocaInst(NewAlloca);
1385}
1386
1388 return Ctx.getType(cast<llvm::AllocaInst>(Val)->getAllocatedType());
1389}
1390
1392 Ctx.getTracker()
1395 cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty->LLVMTy);
1396}
1397
1399 Ctx.getTracker()
1402 this);
1403 cast<llvm::AllocaInst>(Val)->setAlignment(Align);
1404}
1405
1407 Ctx.getTracker()
1410 cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
1411}
1412
1414 return Ctx.getValue(cast<llvm::AllocaInst>(Val)->getArraySize());
1415}
1416
1418 return cast<PointerType>(Ctx.getType(cast<llvm::AllocaInst>(Val)->getType()));
1419}
1420
1422 InsertPosition Pos, Context &Ctx, const Twine &Name) {
1423 assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
1424 auto &Builder = setInsertPos(Pos);
1425 auto *NewV =
1426 Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy->LLVMTy, Name);
1427 if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
1428 return Ctx.createCastInst(NewCI);
1429 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1430 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1431}
1432
1434 return From->getSubclassID() == ClassID::Cast;
1435}
1436
1438 return Ctx.getType(cast<llvm::CastInst>(Val)->getSrcTy());
1439}
1440
1442 return Ctx.getType(cast<llvm::CastInst>(Val)->getDestTy());
1443}
1444
1446 Ctx.getTracker()
1449 cast<llvm::PossiblyNonNegInst>(Val)->setNonNeg(B);
1450}
1451
1453 InsertPosition Pos, Context &Ctx,
1454 const Twine &Name) {
1455 auto &Builder = Instruction::setInsertPos(Pos);
1456 llvm::Value *NewV =
1457 Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
1458 if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
1459 return Ctx.createInsertElementInst(NewInsert);
1460 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1461 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1462}
1463
1465 Context &Ctx, const Twine &Name) {
1466 auto &Builder = setInsertPos(Pos);
1467 llvm::Value *NewV = Builder.CreateExtractElement(Vec->Val, Idx->Val, Name);
1468 if (auto *NewExtract = dyn_cast<llvm::ExtractElementInst>(NewV))
1469 return Ctx.createExtractElementInst(NewExtract);
1470 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1471 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1472}
1473
1475 InsertPosition Pos, Context &Ctx,
1476 const Twine &Name) {
1477 auto &Builder = setInsertPos(Pos);
1478 llvm::Value *NewV =
1479 Builder.CreateShuffleVector(V1->Val, V2->Val, Mask->Val, Name);
1480 if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
1481 return Ctx.createShuffleVectorInst(NewShuffle);
1482 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1483 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1484}
1485
1487 InsertPosition Pos, Context &Ctx,
1488 const Twine &Name) {
1489 auto &Builder = setInsertPos(Pos);
1490 llvm::Value *NewV = Builder.CreateShuffleVector(V1->Val, V2->Val, Mask, Name);
1491 if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
1492 return Ctx.createShuffleVectorInst(NewShuffle);
1493 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1494 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1495}
1496
1499 cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
1500}
1501
1503 return cast<VectorType>(
1504 Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
1505}
1506
1510 getOperandUse(1));
1511 cast<llvm::ShuffleVectorInst>(Val)->commute();
1512}
1513
1515 return Ctx.getOrCreateConstant(
1516 cast<llvm::ShuffleVectorInst>(Val)->getShuffleMaskForBitcode());
1517}
1518
1520 Type *ResultTy) {
1521 return ResultTy->getContext().getOrCreateConstant(
1523 ResultTy->LLVMTy));
1524}
1525
1527 return cast<VectorType>(Ctx.getType(getVectorOperand()->getType()->LLVMTy));
1528}
1529
1531 InsertPosition Pos, Context &Ctx,
1532 const Twine &Name) {
1533 auto &Builder = setInsertPos(Pos);
1534 llvm::Value *NewV = Builder.CreateExtractValue(Agg->Val, Idxs, Name);
1535 if (auto *NewExtractValueInst = dyn_cast<llvm::ExtractValueInst>(NewV))
1536 return Ctx.createExtractValueInst(NewExtractValueInst);
1537 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1538 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1539}
1540
1542 auto *LLVMTy = llvm::ExtractValueInst::getIndexedType(Agg->LLVMTy, Idxs);
1543 return Agg->getContext().getType(LLVMTy);
1544}
1545
1547 InsertPosition Pos, Context &Ctx,
1548 const Twine &Name) {
1549 auto &Builder = setInsertPos(Pos);
1550 llvm::Value *NewV = Builder.CreateInsertValue(Agg->Val, Val->Val, Idxs, Name);
1551 if (auto *NewInsertValueInst = dyn_cast<llvm::InsertValueInst>(NewV))
1552 return Ctx.createInsertValueInst(NewInsertValueInst);
1553 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1554 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1555}
1556
1559 return cast<ConstantTokenNone>(Ctx.getOrCreateConstant(LLVMC));
1560}
1561
1562} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
constexpr LLT S1
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
return RetTy
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
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
raw_pwrite_stream & OS
Value * RHS
Value * LHS
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:147
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:506
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:721
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator end()
Definition: BasicBlock.h:472
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
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.
static LLVM_ABI ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1526
This class represents an Operation in the Expression.
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
An instruction for ordering other memory operations.
Definition: Instructions.h:429
CleanupPadInst * CreateCleanupPad(Value *ParentPad, ArrayRef< Value * > Args={}, const Twine &Name="")
Definition: IRBuilder.h:1329
CatchPadInst * CreateCatchPad(Value *ParentPad, ArrayRef< Value * > Args, const Twine &Name="")
Definition: IRBuilder.h:1324
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2571
AtomicCmpXchgInst * CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1898
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1830
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2625
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2559
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2618
LandingPadInst * CreateLandingPad(Type *Ty, unsigned NumClauses, const Twine &Name="")
Definition: IRBuilder.h:2632
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:1235
CallBrInst * CreateCallBr(FunctionType *Ty, Value *Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args={}, const Twine &Name="")
Create a callbr instruction.
Definition: IRBuilder.h:1273
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2238
CatchReturnInst * CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB)
Definition: IRBuilder.h:1335
CleanupReturnInst * CreateCleanupRet(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB=nullptr)
Definition: IRBuilder.h:1312
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
Definition: IRBuilder.h:1172
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1923
CatchSwitchInst * CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, unsigned NumHandlers, const Twine &Name="")
Definition: IRBuilder.h:1317
Value * CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1809
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2463
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2494
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
Definition: IRBuilder.h:1220
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2593
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1167
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2508
AtomicRMWInst * CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1911
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1708
ResumeInst * CreateResume(Value *Exn)
Definition: IRBuilder.h:1308
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
Invoke instruction.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Return a value (possibly void), from a function.
static LLVM_ABI Constant * convertShuffleMaskForBitcode(ArrayRef< int > Mask, Type *ResultTy)
void reserve(size_type N)
Definition: SmallVector.h:664
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Multiway switch.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
LLVM Value Representation.
Definition: Value.h:75
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:134
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:359
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: Instruction.h:2255
LLVM_ABI Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
LLVM_ABI void setAllocatedType(Type *Ty)
for use only in special circumstances that need to generically transform a whole instruction (eg: IR ...
LLVM_ABI Value * getArraySize()
Get the number of elements allocated.
LLVM_ABI PointerType * getType() const
Overload to return most specific pointer type.
LLVM_ABI void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
LLVM_ABI void setAlignment(Align Align)
static LLVM_ABI AllocaInst * create(Type *Ty, unsigned AddrSpace, InsertPosition Pos, Context &Ctx, Value *ArraySize=nullptr, const Twine &Name="")
LLVM_ABI void setSuccessOrdering(AtomicOrdering Ordering)
LLVM_ABI void setWeak(bool IsWeak)
LLVM_ABI void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
LLVM_ABI void setFailureOrdering(AtomicOrdering Ordering)
AtomicOrdering getFailureOrdering() const
Definition: Instruction.h:2157
LLVM_ABI void setAlignment(Align Align)
static LLVM_ABI AtomicCmpXchgInst * create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, InsertPosition Pos, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
bool isVolatile() const
Return true if this is a cmpxchg from a volatile memory location.
Definition: Instruction.h:2138
LLVM_ABI void setSyncScopeID(SyncScope::ID SSID)
AtomicOrdering getSuccessOrdering() const
Definition: Instruction.h:2152
SyncScope::ID getSyncScopeID() const
Definition: Instruction.h:2164
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instruction.h:2131
bool isWeak() const
Return true if this cmpxchg may spuriously fail.
Definition: Instruction.h:2144
LLVM_ABI Value * getValOperand()
static LLVM_ABI AtomicRMWInst * create(BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, InsertPosition Pos, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
LLVM_ABI void setSyncScopeID(SyncScope::ID SSID)
LLVM_ABI void setOrdering(AtomicOrdering Ordering)
SyncScope::ID getSyncScopeID() const
Definition: Instruction.h:2092
LLVM_ABI void setVolatile(bool V)
AtomicOrdering getOrdering() const
Definition: Instruction.h:2088
LLVM_ABI Value * getPointerOperand()
LLVM_ABI void setAlignment(Align Align)
Iterator for Instructions in a `BasicBlock.
Definition: BasicBlock.h:24
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:68
LLVM_ABI Function * getParent() const
Definition: BasicBlock.cpp:53
iterator end() const
Definition: BasicBlock.h:89
static LLVM_ABI Value * create(Instruction::Opcode Op, Value *LHS, Value *RHS, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Value * createWithCopiedFlags(Instruction::Opcode Op, Value *LHS, Value *RHS, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI BasicBlock * getSuccessor(unsigned SuccIdx) const
LLVM_ABI Value * getCondition() const
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
unsigned getNumSuccessors() const
Definition: Instruction.h:1044
static LLVM_ABI BranchInst * create(BasicBlock *IfTrue, InsertPosition Pos, Context &Ctx)
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
LLVM_ABI Function * getCalledFunction() const
LLVM_ABI Use getCalledOperandUse() const
LLVM_ABI FunctionType * getFunctionType() const
LLVM_ABI void setCalledFunction(Function *F)
LLVM_ABI Function * getCaller()
LLVM_ABI Value * getCalledOperand() const
void setCalledOperand(Value *V)
Definition: Instruction.h:1420
LLVM_ABI Value * getIndirectDestLabelUse(unsigned Idx) const
static LLVM_ABI CallBrInst * create(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
LLVM_ABI Value * getIndirectDestLabel(unsigned Idx) const
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const
LLVM_ABI void setIndirectDest(unsigned Idx, BasicBlock *BB)
LLVM_ABI BasicBlock * getDefaultDest() const
LLVM_ABI BasicBlock * getIndirectDest(unsigned Idx) const
LLVM_ABI SmallVector< BasicBlock *, 16 > getIndirectDests() const
LLVM_ABI void setDefaultDest(BasicBlock *BB)
static LLVM_ABI CallInst * create(FunctionType *FTy, Value *Func, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
LLVM_ABI Type * getSrcTy() const
static LLVM_ABI Value * create(Type *DestTy, Opcode Op, Value *Operand, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI Type * getDestTy() const
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
static LLVM_ABI CatchPadInst * create(Value *ParentPad, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI CatchSwitchInst * getCatchSwitch() const
LLVM_ABI CatchPadInst * getCatchPad() const
LLVM_ABI BasicBlock * getSuccessor() const
LLVM_ABI void setSuccessor(BasicBlock *NewSucc)
LLVM_ABI void setCatchPad(CatchPadInst *CatchPad)
LLVM_ABI Value * getCatchSwitchParentPad() const
static LLVM_ABI CatchReturnInst * create(CatchPadInst *CatchPad, BasicBlock *BB, InsertPosition Pos, Context &Ctx)
LLVM_ABI void addHandler(BasicBlock *Dest)
LLVM_ABI void setParentPad(Value *ParentPad)
LLVM_ABI void setUnwindDest(BasicBlock *UnwindDest)
static LLVM_ABI CatchSwitchInst * create(Value *ParentPad, BasicBlock *UnwindBB, unsigned NumHandlers, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI BasicBlock * getUnwindDest() const
LLVM_ABI Value * getParentPad() const
static LLVM_ABI CleanupPadInst * create(Value *ParentPad, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI CleanupPadInst * getCleanupPad() const
LLVM_ABI void setUnwindDest(BasicBlock *NewDest)
static LLVM_ABI CleanupReturnInst * create(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB, InsertPosition Pos, Context &Ctx)
LLVM_ABI BasicBlock * getUnwindDest() const
LLVM_ABI void setCleanupPad(CleanupPadInst *CleanupPad)
void dumpOS(raw_ostream &OS) const override
LLVM_DUMP_METHOD void dump() const
static LLVM_ABI Value * createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2, const Instruction *FlagsSource, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Type * makeCmpResultType(Type *OpndType)
Create a result type for fcmp/icmp.
static LLVM_ABI Value * create(Predicate Pred, Value *S1, Value *S2, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI void setPredicate(Predicate P)
LLVM_ABI void swapOperands()
static LLVM_ABI ConstantTokenNone * get(Context &Ctx)
Return the ConstantTokenNone.
LLVM_ABI GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
Definition: Context.cpp:574
LLVM_ABI CallBrInst * createCallBrInst(llvm::CallBrInst *I)
Definition: Context.cpp:541
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:629
LLVM_ABI ReturnInst * createReturnInst(llvm::ReturnInst *I)
Definition: Context.cpp:526
LLVM_ABI void runEraseInstrCallbacks(Instruction *I)
Definition: Context.cpp:703
LLVM_ABI VAArgInst * createVAArgInst(llvm::VAArgInst *SI)
Definition: Context.cpp:458
LLVM_ABI CleanupReturnInst * createCleanupReturnInst(llvm::CleanupReturnInst *I)
Definition: Context.cpp:568
LLVM_ABI AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Definition: Context.cpp:609
Type * getType(llvm::Type *LLVMTy)
Definition: Context.h:262
LLVM_ABI AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
Definition: Context.cpp:599
LLVM_ABI InsertValueInst * createInsertValueInst(llvm::InsertValueInst *IVI)
Definition: Context.cpp:505
LLVM_ABI FCmpInst * createFCmpInst(llvm::FCmpInst *I)
Definition: Context.cpp:625
LLVM_ABI ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
Definition: Context.cpp:479
LLVM_ABI Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
Definition: Context.cpp:445
LLVM_ABI BranchInst * createBranchInst(llvm::BranchInst *I)
Definition: Context.cpp:511
LLVM_ABI ShuffleVectorInst * createShuffleVectorInst(llvm::ShuffleVectorInst *SVI)
Definition: Context.cpp:493
LLVM_ABI BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
Definition: Context.cpp:595
LLVM_ABI LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: Context.cpp:516
LLVM_ABI FreezeInst * createFreezeInst(llvm::FreezeInst *SI)
Definition: Context.cpp:463
LLVM_ABI PHINode * createPHINode(llvm::PHINode *I)
Definition: Context.cpp:617
LLVM_ABI CatchPadInst * createCatchPadInst(llvm::CatchPadInst *I)
Definition: Context.cpp:555
LLVM_ABI ICmpInst * createICmpInst(llvm::ICmpInst *I)
Definition: Context.cpp:621
LLVM_ABI std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: Context.cpp:28
LLVM_ABI ExtractValueInst * createExtractValueInst(llvm::ExtractValueInst *IVI)
Definition: Context.cpp:499
LLVM_ABI CastInst * createCastInst(llvm::CastInst *I)
Definition: Context.cpp:613
LLVM_ABI StoreInst * createStoreInst(llvm::StoreInst *SI)
Definition: Context.cpp:521
LLVM_ABI CatchReturnInst * createCatchReturnInst(llvm::CatchReturnInst *I)
Definition: Context.cpp:563
LLVM_ABI CatchSwitchInst * createCatchSwitchInst(llvm::CatchSwitchInst *I)
Definition: Context.cpp:579
LLVM_ABI CleanupPadInst * createCleanupPadInst(llvm::CleanupPadInst *I)
Definition: Context.cpp:559
LLVM_ABI FenceInst * createFenceInst(llvm::FenceInst *SI)
Definition: Context.cpp:468
LLVM_ABI CallInst * createCallInst(llvm::CallInst *I)
Definition: Context.cpp:531
LLVM_ABI SwitchInst * createSwitchInst(llvm::SwitchInst *I)
Definition: Context.cpp:587
LLVM_ABI void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where)
Definition: Context.cpp:713
LLVM_ABI UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
Definition: Context.cpp:591
LLVM_ABI InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Definition: Context.cpp:536
LLVM_ABI LandingPadInst * createLandingPadInst(llvm::LandingPadInst *I)
Definition: Context.cpp:551
LLVM_ABI InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
Definition: Context.cpp:486
LLVM_ABI SelectInst * createSelectInst(llvm::SelectInst *SI)
Definition: Context.cpp:473
LLVM_ABI ResumeInst * createResumeInst(llvm::ResumeInst *I)
Definition: Context.cpp:583
LLVMContext & LLVMCtx
Definition: Context.h:70
Tracker & getTracker()
Definition: Context.h:245
LLVM_ABI UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
Definition: Context.cpp:546
LLVM_ABI AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
Definition: Context.cpp:604
static LLVM_ABI Value * create(Value *Vec, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI VectorType * getVectorOperandType() const
static LLVM_ABI Value * create(Value *Agg, ArrayRef< unsigned > Idxs, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
LLVM_ABI void swapOperands()
static LLVM_ABI FenceInst * create(AtomicOrdering Ordering, InsertPosition Pos, Context &Ctx, SyncScope::ID SSID=SyncScope::System)
LLVM_ABI void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this fence instruction.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this fence instruction.
Definition: Instruction.h:429
LLVM_ABI void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this fence instruction.
static LLVM_ABI FreezeInst * create(Value *V, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI Value * getArgOperand(unsigned Idx) const
Return the Idx-th funcletpad argument.
LLVM_ABI Value * getParentPad() const
Return the outer EH-pad this funclet is nested within.
LLVM_ABI void setParentPad(Value *ParentPad)
LLVM_ABI void setArgOperand(unsigned Idx, Value *V)
Set the Idx-th funcletpad argument.
Similar to GenericSetter but the setters/getters have an index as their first argument.
Definition: Tracker.h:305
This class can be used for tracking most instruction setters.
Definition: Tracker.h:277
LLVM_ABI Type * getResultElementType() const
LLVM_ABI Type * getPointerOperandType() const
LLVM_ABI Type * getSourceElementType() const
LLVM_ABI Value * getPointerOperand() const
static LLVM_ABI Value * create(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
LLVM_ABI void swapOperands()
static LLVM_ABI Value * create(Value *Vec, Value *NewElt, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Value * create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, InsertPosition Pos, Context &Ctx, const Twine &Name="")
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:43
bool hasNoUnsignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: Instruction.h:226
static IRBuilder & setInsertPos(InsertPosition Pos)
Helper function for create().
Definition: Instruction.h:104
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
Definition: Instruction.h:246
LLVM_ABI void setHasAllowReassoc(bool B)
Set or clear the reassociation flag on this instruction, which must be an operator which supports thi...
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Definition: Instruction.h:271
const char * getOpcodeName() const
Definition: Instruction.h:132
LLVM_ABI void setHasNoSignedWrap(bool B=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI void insertAfter(Instruction *AfterI)
Insert this detached instruction after AfterI.
LLVM_ABI void moveBefore(BasicBlock &BB, const BBIterator &WhereIt)
Move this instruction to WhereIt.
bool hasAllowContract() const
Determine whether the allow-contract flag is set.
Definition: Instruction.h:287
LLVM_ABI void setIsExact(bool B=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
bool hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
Definition: Instruction.h:295
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: Instruction.h:233
LLVM_ABI void setHasNoUnsignedWrap(bool B=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
void dumpOS(raw_ostream &OS) const override
LLVM_ABI BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
Definition: Instruction.cpp:38
LLVM_ABI void setFast(bool B)
Set or clear all fast-math-flags on this instruction, which must be an operator which supports this f...
LLVM_ABI void setHasApproxFunc(bool B)
Set or clear the approximate-math-functions flag on this instruction, which must be an operator which...
LLVM_ABI void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
LLVM_ABI void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
LLVM_ABI void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
LLVM_ABI void insertInto(BasicBlock *BB, const BBIterator &WhereIt)
Insert this detached instruction into BB at WhereIt.
LLVM_ABI llvm::Instruction * getTopmostLLVMInstruction() const
A SandboxIR Instruction may map to multiple LLVM IR Instruction.
Definition: Instruction.cpp:26
LLVM_ABI void setHasAllowContract(bool B)
Set or clear the allow-contract flag on this instruction, which must be an operator which supports th...
virtual SmallVector< llvm::Instruction *, 1 > getLLVMInstrs() const =0
\Returns the LLVM IR Instructions that this SandboxIR maps to in program order.
LLVM_ABI Type * getAccessType() const
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition: Instruction.h:305
LLVM_ABI Instruction * getNextNode() const
\Returns the next sandboxir::Instruction in the block, or nullptr if at the end of the block.
Definition: Instruction.cpp:43
LLVM_ABI void removeFromParent()
Detach this from its parent BasicBlock without deleting it.
Definition: Instruction.cpp:66
LLVM_ABI Instruction * getPrevNode() const
\Returns the previous sandboxir::Instruction in the block, or nullptr if at the beginning of the bloc...
Definition: Instruction.cpp:58
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
Definition: Instruction.h:279
LLVM_ABI void insertBefore(Instruction *BeforeI)
Insert this detached instruction before BeforeI.
LLVM_ABI void eraseFromParent()
Detach this Value from its parent and delete it.
Definition: Instruction.cpp:74
LLVM_ABI void setHasAllowReciprocal(bool B)
Set or clear the allow-reciprocal flag on this instruction, which must be an operator which supports ...
LLVM_ABI void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
LLVM_ABI BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
static LLVM_ABI bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
LLVM_ABI BasicBlock * getUnwindDest() const
static LLVM_ABI InvokeInst * create(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
LLVM_ABI void setNormalDest(BasicBlock *BB)
LLVM_ABI void setUnwindDest(BasicBlock *BB)
LLVM_ABI BasicBlock * getSuccessor(unsigned SuccIdx) const
LLVM_ABI BasicBlock * getNormalDest() const
LLVM_ABI LandingPadInst * getLandingPadInst() const
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
Definition: Instruction.h:1529
LLVM_ABI void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
LLVM_ABI Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
static LLVM_ABI LandingPadInst * create(Type *RetTy, unsigned NumReservedClauses, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx, const Twine &Name="")
LLVM_ABI void setVolatile(bool V)
Specify whether this is a volatile load or not.
LLVM_ABI Value * getPointerOperand() const
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
LLVM_ABI Value * hasConstantValue() const
LLVM_ABI int getBasicBlockIndex(const BasicBlock *BB) const
unsigned getNumIncomingValues() const
Definition: Instruction.h:2435
LLVM_ABI Value * getIncomingValue(unsigned Idx) const
LLVM_ABI void setIncomingBlock(unsigned Idx, BasicBlock *BB)
LLVM_ABI void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate)
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
static LLVM_ABI PHINode * create(Type *Ty, unsigned NumReservedValues, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI Value * removeIncomingValue(unsigned Idx)
LLVM_ABI void setIncomingValue(unsigned Idx, Value *V)
LLVM_ABI BasicBlock * getIncomingBlock(unsigned Idx) const
LLVM_ABI void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
LLVM_ABI Value * getIncomingValueForBlock(const BasicBlock *BB) const
LLVM_ABI void addIncoming(Value *V, BasicBlock *BB)
static LLVM_ABI ResumeInst * create(Value *Exn, InsertPosition Pos, Context &Ctx)
LLVM_ABI Value * getValue() const
static LLVM_ABI ReturnInst * create(Value *RetVal, InsertPosition Pos, Context &Ctx)
LLVM_ABI Value * getReturnValue() const
\Returns null if there is no return value.
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
static LLVM_ABI Value * create(Value *Cond, Value *True, Value *False, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI VectorType * getType() const
Overload to return most specific vector type.
LLVM_ABI Constant * getShuffleMaskForBitcode() const
Return the mask for this instruction, for use in bitcode.
LLVM_ABI void commute()
Swap the operands and adjust the mask to preserve the semantics of the instruction.
static LLVM_ABI Value * create(Value *V1, Value *V2, Value *Mask, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Constant * convertShuffleMaskForBitcode(ArrayRef< int > Mask, Type *ResultTy)
LLVM_ABI void setShuffleMask(ArrayRef< int > Mask)
LLVM_ABI void setVolatile(bool V)
Specify whether this is a volatile store or not.
static LLVM_ABI bool classof(const Value *From)
For isa/dyn_cast.
static LLVM_ABI StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx)
LLVM_ABI Value * getPointerOperand() const
LLVM_ABI Value * getValueOperand() const
static LLVM_ABI SwitchInst * create(Value *V, BasicBlock *Dest, unsigned NumCases, InsertPosition Pos, Context &Ctx, const Twine &Name="")
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
Definition: Instruction.h:1897
LLVM_ABI void setDefaultDest(BasicBlock *DefaultCase)
llvm::SwitchInst::CaseIteratorImpl< CaseHandle > CaseIt
Definition: Instruction.h:1892
LLVM_ABI BasicBlock * getDefaultDest() const
LLVM_ABI Value * getCondition() const
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
LLVM_ABI void setCondition(Value *V)
LLVM_ABI ConstantInt * findCaseDest(BasicBlock *BB)
LLVM_ABI CaseIt removeCase(CaseIt It)
This method removes the specified case and its successor from the switch instruction.
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:442
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition: Tracker.h:483
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Definition: Tracker.h:500
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:47
llvm::Type * LLVMTy
Definition: Type.h:49
static LLVM_ABI Type * getInt1Ty(Context &Ctx)
Context & getContext() const
Definition: Type.h:94
static LLVM_ABI Value * createWithCopiedFlags(Instruction::Opcode Op, Value *OpV, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Value * create(Instruction::Opcode Op, Value *OpV, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI UnreachableInst * create(InsertPosition Pos, Context &Ctx)
static LLVM_ABI bool classof(const Value *From)
Tracks swapping a Use with another Use.
Definition: Tracker.h:198
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:33
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: User.cpp:91
virtual unsigned getNumOperands() const
Definition: User.h:129
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: User.h:126
LLVM_ABI Value * getPointerOperand()
static LLVM_ABI VAArgInst * create(Value *List, Type *Ty, InsertPosition Pos, Context &Ctx, const Twine &Name="")
A SandboxIR Value has users. This is the base class.
Definition: Value.h:66
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:106
void dumpCommonSuffix(raw_ostream &OS) const
Definition: Value.cpp:107
Context & Ctx
All values point to the context.
Definition: Value.h:179
LLVM_ABI Type * getType() const
Definition: Value.cpp:46
iterator_range< user_iterator > users()
Definition: Value.h:231
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:100
@ LLVMAtomicRMW
Definition: Core.h:138
@ LLVMAtomicCmpXchg
Definition: Core.h:137
@ LLVMSwitch
Definition: Core.h:65
#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
static llvm::Instruction::CastOps getLLVMCastOp(Instruction::Opcode Opc)
static llvm::Instruction::UnaryOps getLLVMUnaryOp(Instruction::Opcode Opc)
\Returns the LLVM opcode that corresponds to Opc.
static llvm::Instruction::BinaryOps getLLVMBinaryOp(Instruction::Opcode Opc)
\Returns the LLVM opcode that corresponds to Opc.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
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:1939
AtomicOrdering
Atomic ordering for LLVM's memory model.
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