LLVM 20.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();
36}
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
67 Ctx.getTracker().emplaceIfTracking<RemoveFromParent>(this);
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;
104
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
127
128 Ctx.getTracker().emplaceIfTracking<InsertIntoBB>(this);
129
130 // Insert the LLVM IR Instructions in program order.
132 I->insertBefore(BeforeTopI);
133}
134
136 insertInto(AfterI->getParent(), std::next(AfterI->getIterator()));
137}
138
140 llvm::BasicBlock *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
141 llvm::Instruction *LLVMBeforeI;
142 llvm::BasicBlock::iterator LLVMBeforeIt;
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
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}
200 cast<llvm::Instruction>(Val)->setFast(B);
201}
202
207 cast<llvm::Instruction>(Val)->setIsExact(B);
208}
209
214 cast<llvm::Instruction>(Val)->setHasAllowReassoc(B);
215}
216
221 this);
222 cast<llvm::Instruction>(Val)->setHasNoNaNs(B);
223}
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
317 GenericSetter<&FenceInst::getOrdering, &FenceInst::setOrdering>>(
318 this);
319 cast<llvm::FenceInst>(Val)->setOrdering(Ordering);
320}
321
326 cast<llvm::FenceInst>(Val)->setSyncScopeID(SSID);
327}
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));
338}
339
342 getOperandUse(2));
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}
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 {
378 "Successor # out of range for Branch!");
379 return cast_or_null<BasicBlock>(
380 Ctx.getValue(cast<llvm::BranchInst>(Val)->getSuccessor(SuccIdx)));
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;
411}
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}
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());
487}
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!");
555}
557 setOperand(2, BB);
558 assert(getUnwindDest() == BB && "LLVM IR uses a different operan index!");
561 return cast<LandingPadInst>(
562 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getLandingPadInst()));
563 ;
565BasicBlock *InvokeInst::getSuccessor(unsigned SuccIdx) const {
566 return cast<BasicBlock>(
567 Ctx.getValue(cast<llvm::InvokeInst>(Val)->getSuccessor(SuccIdx)));
568}
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));
601 return cast<BasicBlock>(
602 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getDefaultDest()));
603}
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;
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));
628}
630 return cast<BasicBlock>(
631 Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx)));
632}
633
634LandingPadInst *LandingPadInst::create(Type *RetTy, unsigned NumReservedClauses,
635 InsertPosition Pos, Context &Ctx,
636 const Twine &Name) {
637 auto &Builder = setInsertPos(Pos);
638 llvm::LandingPadInst *LLVMI =
639 Builder.CreateLandingPad(RetTy->LLVMTy, NumReservedClauses, Name);
640 return Ctx.createLandingPadInst(LLVMI);
641}
642
647 cast<llvm::LandingPadInst>(Val)->setCleanup(V);
648}
649
651 return cast<Constant>(
652 Ctx.getValue(cast<llvm::LandingPadInst>(Val)->getClause(Idx)));
653}
654
656 return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getParentPad());
657}
658
663 cast<llvm::FuncletPadInst>(Val)->setParentPad(ParentPad->Val);
664}
665
667 return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getArgOperand(Idx));
668}
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);
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()));
776}
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();
877 Tracker.emplaceIfTracking<PHIRemoveIncoming>(this, Idx);
878 llvm::Value *LLVMV =
879 cast<llvm::PHINode>(Val)->removeIncomingValue(Idx,
880 /*DeletePHIIfEmpty=*/false);
881 return Ctx.getValue(LLVMV);
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)
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 *LLVMI = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
933 if (dyn_cast<llvm::ICmpInst>(LLVMI))
934 return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMI));
935 return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMI));
938 const Instruction *F,
939 InsertPosition Pos, Context &Ctx,
940 const Twine &Name) {
941 CmpInst *Inst = create(P, S1, S2, Pos, Ctx, Name);
942 cast<llvm::CmpInst>(Inst->Val)->copyIRFlags(F->Val);
943 return Inst;
944}
945
947 if (auto *VT = dyn_cast<VectorType>(OpndType)) {
948 // TODO: Cleanup when we have more complete support for
949 // sandboxir::VectorType
950 return OpndType->getContext().getType(llvm::VectorType::get(
952 cast<llvm::VectorType>(VT->LLVMTy)->getElementCount()));
953 }
954 return Type::getInt1Ty(OpndType->getContext());
955}
956
961 cast<llvm::CmpInst>(Val)->setPredicate(P);
962}
963
965 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
966 IC->swapOperands();
967 else
968 cast<FCmpInst>(this)->swapOperands();
969}
970
973 cast<llvm::ICmpInst>(Val)->swapOperands();
975
978 cast<llvm::FCmpInst>(Val)->swapOperands();
979}
980
981#ifndef NDEBUG
985}
986
987void CmpInst::dump() const {
988 dumpOS(dbgs());
989 dbgs() << "\n";
990}
991#endif // NDEBUG
992
994 switch (Opc) {
995 case Instruction::Opcode::ZExt:
996 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::ZExt);
997 case Instruction::Opcode::SExt:
998 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SExt);
999 case Instruction::Opcode::FPToUI:
1000 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToUI);
1001 case Instruction::Opcode::FPToSI:
1002 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToSI);
1003 case Instruction::Opcode::FPExt:
1004 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPExt);
1005 case Instruction::Opcode::PtrToInt:
1006 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::PtrToInt);
1007 case Instruction::Opcode::IntToPtr:
1008 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::IntToPtr);
1009 case Instruction::Opcode::SIToFP:
1010 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SIToFP);
1011 case Instruction::Opcode::UIToFP:
1012 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::UIToFP);
1013 case Instruction::Opcode::Trunc:
1014 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::Trunc);
1015 case Instruction::Opcode::FPTrunc:
1016 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPTrunc);
1017 case Instruction::Opcode::BitCast:
1018 return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::BitCast);
1019 case Instruction::Opcode::AddrSpaceCast:
1020 return static_cast<llvm::Instruction::CastOps>(
1021 llvm::Instruction::AddrSpaceCast);
1022 default:
1023 llvm_unreachable("Opcode not suitable for CastInst!");
1024 }
1025}
1026
1027/// \Returns the LLVM opcode that corresponds to \p Opc.
1029 switch (Opc) {
1030 case Instruction::Opcode::FNeg:
1031 return static_cast<llvm::Instruction::UnaryOps>(llvm::Instruction::FNeg);
1032 default:
1033 llvm_unreachable("Not a unary op!");
1034 }
1035}
1036
1038 unsigned NumHandlers,
1039 InsertPosition Pos, Context &Ctx,
1040 const Twine &Name) {
1041 auto &Builder = setInsertPos(Pos);
1043 ParentPad->Val, cast<llvm::BasicBlock>(UnwindBB->Val), NumHandlers, Name);
1044 return Ctx.createCatchSwitchInst(LLVMCSI);
1045}
1046
1048 return Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getParentPad());
1049}
1050
1052 Ctx.getTracker()
1055 cast<llvm::CatchSwitchInst>(Val)->setParentPad(ParentPad->Val);
1056}
1057
1059 return cast_or_null<BasicBlock>(
1060 Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getUnwindDest()));
1061}
1062
1064 Ctx.getTracker()
1067 cast<llvm::CatchSwitchInst>(Val)->setUnwindDest(
1068 cast<llvm::BasicBlock>(UnwindDest->Val));
1069}
1070
1073 cast<llvm::CatchSwitchInst>(Val)->addHandler(
1074 cast<llvm::BasicBlock>(Dest->Val));
1075}
1076
1078 auto &Builder = setInsertPos(Pos);
1079 auto *LLVMI = cast<llvm::ResumeInst>(Builder.CreateResume(Exn->Val));
1080 return Ctx.createResumeInst(LLVMI);
1081}
1082
1084 return Ctx.getValue(cast<llvm::ResumeInst>(Val)->getValue());
1085}
1086
1087SwitchInst *SwitchInst::create(Value *V, BasicBlock *Dest, unsigned NumCases,
1088 InsertPosition Pos, Context &Ctx,
1089 const Twine &Name) {
1090 auto &Builder = setInsertPos(Pos);
1092 Builder.CreateSwitch(V->Val, cast<llvm::BasicBlock>(Dest->Val), NumCases);
1094}
1095
1097 return Ctx.getValue(cast<llvm::SwitchInst>(Val)->getCondition());
1098}
1101 Ctx.getTracker()
1104 this);
1105 cast<llvm::SwitchInst>(Val)->setCondition(V->Val);
1106}
1107
1109 return cast<BasicBlock>(
1110 Ctx.getValue(cast<llvm::SwitchInst>(Val)->getDefaultDest()));
1111}
1112
1114 Ctx.getTracker()
1117 cast<llvm::SwitchInst>(Val)->setDefaultDest(
1118 cast<llvm::BasicBlock>(DefaultCase->Val));
1119}
1121 auto *LLVMC = cast<llvm::SwitchInst>(Val)->findCaseDest(
1122 cast<llvm::BasicBlock>(BB->Val));
1123 return LLVMC != nullptr ? cast<ConstantInt>(Ctx.getValue(LLVMC)) : nullptr;
1124}
1125
1128 // TODO: Track this!
1129 cast<llvm::SwitchInst>(Val)->addCase(cast<llvm::ConstantInt>(OnVal->Val),
1130 cast<llvm::BasicBlock>(Dest->Val));
1131}
1132
1135
1136 auto *LLVMSwitch = cast<llvm::SwitchInst>(Val);
1137 unsigned CaseNum = It - case_begin();
1138 llvm::SwitchInst::CaseIt LLVMIt(LLVMSwitch, CaseNum);
1139 auto LLVMCaseIt = LLVMSwitch->removeCase(LLVMIt);
1140 unsigned Num = LLVMCaseIt - LLVMSwitch->case_begin();
1141 return CaseIt(this, Num);
1142}
1143
1145 return cast<BasicBlock>(
1146 Ctx.getValue(cast<llvm::SwitchInst>(Val)->getSuccessor(Idx)));
1147}
1149void SwitchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
1150 Ctx.getTracker()
1153 Idx);
1154 cast<llvm::SwitchInst>(Val)->setSuccessor(
1155 Idx, cast<llvm::BasicBlock>(NewSucc->Val));
1156}
1159 InsertPosition Pos, Context &Ctx,
1160 const Twine &Name) {
1161 auto &Builder = setInsertPos(Pos);
1162 auto *NewLLVMV = Builder.CreateUnOp(getLLVMUnaryOp(Op), OpV->Val, Name);
1163 if (auto *NewUnOpV = dyn_cast<llvm::UnaryOperator>(NewLLVMV)) {
1164 return Ctx.createUnaryOperator(NewUnOpV);
1165 }
1166 assert(isa<llvm::Constant>(NewLLVMV) && "Expected constant");
1167 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewLLVMV));
1168}
1169
1171 Value *CopyFrom, InsertPosition Pos,
1172 Context &Ctx, const Twine &Name) {
1173 auto *NewV = create(Op, OpV, Pos, Ctx, Name);
1174 if (auto *UnI = dyn_cast<llvm::UnaryOperator>(NewV->Val))
1175 UnI->copyIRFlags(CopyFrom->Val);
1176 return NewV;
1177}
1178
1179/// \Returns the LLVM opcode that corresponds to \p Opc.
1181 switch (Opc) {
1182 case Instruction::Opcode::Add:
1183 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Add);
1184 case Instruction::Opcode::FAdd:
1185 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FAdd);
1186 case Instruction::Opcode::Sub:
1187 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Sub);
1188 case Instruction::Opcode::FSub:
1189 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FSub);
1190 case Instruction::Opcode::Mul:
1191 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Mul);
1192 case Instruction::Opcode::FMul:
1193 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FMul);
1194 case Instruction::Opcode::UDiv:
1195 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::UDiv);
1196 case Instruction::Opcode::SDiv:
1197 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SDiv);
1198 case Instruction::Opcode::FDiv:
1199 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FDiv);
1200 case Instruction::Opcode::URem:
1201 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::URem);
1202 case Instruction::Opcode::SRem:
1203 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SRem);
1204 case Instruction::Opcode::FRem:
1205 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FRem);
1206 case Instruction::Opcode::Shl:
1207 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Shl);
1208 case Instruction::Opcode::LShr:
1209 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::LShr);
1210 case Instruction::Opcode::AShr:
1211 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::AShr);
1212 case Instruction::Opcode::And:
1213 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::And);
1214 case Instruction::Opcode::Or:
1215 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Or);
1216 case Instruction::Opcode::Xor:
1217 return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Xor);
1218 default:
1219 llvm_unreachable("Not a binary op!");
1220 }
1221}
1223 InsertPosition Pos, Context &Ctx,
1224 const Twine &Name) {
1225 auto &Builder = setInsertPos(Pos);
1226 llvm::Value *NewV =
1227 Builder.CreateBinOp(getLLVMBinaryOp(Op), LHS->Val, RHS->Val, Name);
1228 if (auto *NewBinOp = dyn_cast<llvm::BinaryOperator>(NewV))
1229 return Ctx.createBinaryOperator(NewBinOp);
1230 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1231 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1232}
1233
1235 Value *RHS, Value *CopyFrom,
1236 InsertPosition Pos, Context &Ctx,
1237 const Twine &Name) {
1239 Value *NewV = create(Op, LHS, RHS, Pos, Ctx, Name);
1240 if (auto *NewBO = dyn_cast<BinaryOperator>(NewV))
1241 cast<llvm::BinaryOperator>(NewBO->Val)->copyIRFlags(CopyFrom->Val);
1242 return NewV;
1243}
1244
1246 Ctx.getTracker()
1249 this);
1250 cast<llvm::PossiblyDisjointInst>(Val)->setIsDisjoint(B);
1251}
1252
1254 Ctx.getTracker()
1257 cast<llvm::AtomicRMWInst>(Val)->setAlignment(Align);
1258}
1259
1261 Ctx.getTracker()
1264 cast<llvm::AtomicRMWInst>(Val)->setVolatile(V);
1265}
1266
1268 Ctx.getTracker()
1271 cast<llvm::AtomicRMWInst>(Val)->setOrdering(Ordering);
1272}
1273
1275 Ctx.getTracker()
1278 cast<llvm::AtomicRMWInst>(Val)->setSyncScopeID(SSID);
1279}
1280
1282 return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getPointerOperand());
1283}
1284
1286 return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getValOperand());
1287}
1288
1291 InsertPosition Pos, Context &Ctx,
1292 SyncScope::ID SSID, const Twine &Name) {
1293 auto &Builder = setInsertPos(Pos);
1294 auto *LLVMAtomicRMW =
1295 Builder.CreateAtomicRMW(Op, Ptr->Val, Val->Val, Align, Ordering, SSID);
1296 LLVMAtomicRMW->setName(Name);
1298}
1299
1301 Ctx.getTracker()
1304 this);
1305 cast<llvm::AtomicCmpXchgInst>(Val)->setSyncScopeID(SSID);
1306}
1307
1309 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getPointerOperand());
1310}
1311
1313 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getCompareOperand());
1314}
1315
1317 return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getNewValOperand());
1318}
1319
1322 AtomicOrdering SuccessOrdering,
1323 AtomicOrdering FailureOrdering, InsertPosition Pos,
1324 Context &Ctx, SyncScope::ID SSID, const Twine &Name) {
1325 auto &Builder = setInsertPos(Pos);
1326 auto *LLVMAtomicCmpXchg =
1327 Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
1328 SuccessOrdering, FailureOrdering, SSID);
1329 LLVMAtomicCmpXchg->setName(Name);
1331}
1332
1334 Ctx.getTracker()
1337 cast<llvm::AtomicCmpXchgInst>(Val)->setAlignment(Align);
1338}
1339
1341 Ctx.getTracker()
1344 cast<llvm::AtomicCmpXchgInst>(Val)->setVolatile(V);
1345}
1346
1348 Ctx.getTracker()
1351 cast<llvm::AtomicCmpXchgInst>(Val)->setWeak(IsWeak);
1352}
1353
1355 Ctx.getTracker()
1358 this);
1359 cast<llvm::AtomicCmpXchgInst>(Val)->setSuccessOrdering(Ordering);
1360}
1361
1363 Ctx.getTracker()
1366 this);
1367 cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
1368}
1369
1370AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
1371 Context &Ctx, Value *ArraySize,
1372 const Twine &Name) {
1373 auto &Builder = setInsertPos(Pos);
1374 auto *NewAlloca =
1375 Builder.CreateAlloca(Ty->LLVMTy, AddrSpace, ArraySize->Val, Name);
1376 return Ctx.createAllocaInst(NewAlloca);
1377}
1378
1380 return Ctx.getType(cast<llvm::AllocaInst>(Val)->getAllocatedType());
1381}
1382
1384 Ctx.getTracker()
1387 cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty->LLVMTy);
1388}
1389
1391 Ctx.getTracker()
1394 this);
1395 cast<llvm::AllocaInst>(Val)->setAlignment(Align);
1396}
1397
1399 Ctx.getTracker()
1402 cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
1403}
1404
1406 return Ctx.getValue(cast<llvm::AllocaInst>(Val)->getArraySize());
1407}
1408
1410 return cast<PointerType>(Ctx.getType(cast<llvm::AllocaInst>(Val)->getType()));
1411}
1412
1414 InsertPosition Pos, Context &Ctx, const Twine &Name) {
1415 assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
1416 auto &Builder = setInsertPos(Pos);
1417 auto *NewV =
1418 Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy->LLVMTy, Name);
1419 if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
1420 return Ctx.createCastInst(NewCI);
1421 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1422 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1423}
1424
1426 return From->getSubclassID() == ClassID::Cast;
1427}
1428
1430 return Ctx.getType(cast<llvm::CastInst>(Val)->getSrcTy());
1431}
1432
1434 return Ctx.getType(cast<llvm::CastInst>(Val)->getDestTy());
1435}
1436
1438 Ctx.getTracker()
1441 cast<llvm::PossiblyNonNegInst>(Val)->setNonNeg(B);
1442}
1443
1445 InsertPosition Pos, Context &Ctx,
1446 const Twine &Name) {
1447 auto &Builder = Instruction::setInsertPos(Pos);
1448 llvm::Value *NewV =
1449 Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
1450 if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
1451 return Ctx.createInsertElementInst(NewInsert);
1452 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1453 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1454}
1455
1457 Context &Ctx, const Twine &Name) {
1458 auto &Builder = setInsertPos(Pos);
1459 llvm::Value *NewV = Builder.CreateExtractElement(Vec->Val, Idx->Val, Name);
1460 if (auto *NewExtract = dyn_cast<llvm::ExtractElementInst>(NewV))
1461 return Ctx.createExtractElementInst(NewExtract);
1462 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1463 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1464}
1465
1467 InsertPosition Pos, Context &Ctx,
1468 const Twine &Name) {
1469 auto &Builder = setInsertPos(Pos);
1470 llvm::Value *NewV =
1471 Builder.CreateShuffleVector(V1->Val, V2->Val, Mask->Val, Name);
1472 if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
1473 return Ctx.createShuffleVectorInst(NewShuffle);
1474 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1475 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1476}
1477
1479 InsertPosition Pos, Context &Ctx,
1480 const Twine &Name) {
1481 auto &Builder = setInsertPos(Pos);
1482 llvm::Value *NewV = Builder.CreateShuffleVector(V1->Val, V2->Val, Mask, Name);
1483 if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
1484 return Ctx.createShuffleVectorInst(NewShuffle);
1485 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1486 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1487}
1488
1491 cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
1492}
1493
1495 return cast<VectorType>(
1496 Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
1497}
1498
1502 getOperandUse(1));
1503 cast<llvm::ShuffleVectorInst>(Val)->commute();
1504}
1505
1507 return Ctx.getOrCreateConstant(
1508 cast<llvm::ShuffleVectorInst>(Val)->getShuffleMaskForBitcode());
1509}
1510
1512 Type *ResultTy) {
1513 return ResultTy->getContext().getOrCreateConstant(
1515 ResultTy->LLVMTy));
1516}
1517
1519 return cast<VectorType>(Ctx.getType(getVectorOperand()->getType()->LLVMTy));
1520}
1521
1523 InsertPosition Pos, Context &Ctx,
1524 const Twine &Name) {
1525 auto &Builder = setInsertPos(Pos);
1526 llvm::Value *NewV = Builder.CreateExtractValue(Agg->Val, Idxs, Name);
1527 if (auto *NewExtractValueInst = dyn_cast<llvm::ExtractValueInst>(NewV))
1528 return Ctx.createExtractValueInst(NewExtractValueInst);
1529 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1530 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1531}
1532
1534 auto *LLVMTy = llvm::ExtractValueInst::getIndexedType(Agg->LLVMTy, Idxs);
1535 return Agg->getContext().getType(LLVMTy);
1536}
1537
1539 InsertPosition Pos, Context &Ctx,
1540 const Twine &Name) {
1541 auto &Builder = setInsertPos(Pos);
1542 llvm::Value *NewV = Builder.CreateInsertValue(Agg->Val, Val->Val, Idxs, Name);
1543 if (auto *NewInsertValueInst = dyn_cast<llvm::InsertValueInst>(NewV))
1544 return Ctx.createInsertValueInst(NewInsertValueInst);
1545 assert(isa<llvm::Constant>(NewV) && "Expected constant");
1546 return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1547}
1548
1551 return cast<ConstantTokenNone>(Ctx.getOrCreateConstant(LLVMC));
1552}
1553
1554} // namespace llvm::sandboxir
static const 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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:168
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:716
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
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.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1522
This class represents an Operation in the Expression.
static 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:20
An instruction for ordering other memory operations.
Definition: Instructions.h:424
Class to represent function types.
Definition: DerivedTypes.h:105
CleanupPadInst * CreateCleanupPad(Value *ParentPad, ArrayRef< Value * > Args={}, const Twine &Name="")
Definition: IRBuilder.h:1276
CatchPadInst * CreateCatchPad(Value *ParentPad, ArrayRef< Value * > Args, const Twine &Name="")
Definition: IRBuilder.h:1271
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2503
AtomicCmpXchgInst * CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1864
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1796
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2554
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2491
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2547
LandingPadInst * CreateLandingPad(Type *Ty, unsigned NumClauses, const Twine &Name="")
Definition: IRBuilder.h:2561
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:1182
CallBrInst * CreateCallBr(FunctionType *Ty, Value *Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args={}, const Twine &Name="")
Create a callbr instruction.
Definition: IRBuilder.h:1220
CatchReturnInst * CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB)
Definition: IRBuilder.h:1282
CleanupReturnInst * CreateCleanupRet(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB=nullptr)
Definition: IRBuilder.h:1259
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
Definition: IRBuilder.h:1119
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1889
CatchSwitchInst * CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, unsigned NumHandlers, const Twine &Name="")
Definition: IRBuilder.h:1264
Value * CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1776
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2398
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2429
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:1167
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2525
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1114
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2189
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2444
AtomicRMWInst * CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1877
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1689
ResumeInst * CreateResume(Value *Exn)
Definition: IRBuilder.h:1255
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
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 Constant * convertShuffleMaskForBitcode(ArrayRef< int > Mask, Type *ResultTy)
void reserve(size_type N)
Definition: SmallVector.h:663
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Multiway switch.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static IntegerType * getInt1Ty(LLVMContext &C)
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
static 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: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: Instruction.h:2241
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
void setAllocatedType(Type *Ty)
for use only in special circumstances that need to generically transform a whole instruction (eg: IR ...
Value * getArraySize()
Get the number of elements allocated.
PointerType * getType() const
Overload to return most specific pointer type.
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
void setAlignment(Align Align)
static AllocaInst * create(Type *Ty, unsigned AddrSpace, InsertPosition Pos, Context &Ctx, Value *ArraySize=nullptr, const Twine &Name="")
void setSuccessOrdering(AtomicOrdering Ordering)
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
void setFailureOrdering(AtomicOrdering Ordering)
AtomicOrdering getFailureOrdering() const
Definition: Instruction.h:2144
static 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:2125
void setSyncScopeID(SyncScope::ID SSID)
AtomicOrdering getSuccessOrdering() const
Definition: Instruction.h:2139
SyncScope::ID getSyncScopeID() const
Definition: Instruction.h:2151
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instruction.h:2118
bool isWeak() const
Return true if this cmpxchg may spuriously fail.
Definition: Instruction.h:2131
static AtomicRMWInst * create(BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, InsertPosition Pos, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
void setSyncScopeID(SyncScope::ID SSID)
void setOrdering(AtomicOrdering Ordering)
SyncScope::ID getSyncScopeID() const
Definition: Instruction.h:2078
AtomicOrdering getOrdering() const
Definition: Instruction.h:2074
Iterator for Instructions in a `BasicBlock.
Definition: BasicBlock.h:23
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:67
Function * getParent() const
Definition: BasicBlock.cpp:53
iterator end() const
Definition: BasicBlock.h:88
static Value * create(Instruction::Opcode Op, Value *LHS, Value *RHS, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *LHS, Value *RHS, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
BasicBlock * getSuccessor(unsigned SuccIdx) const
Value * getCondition() const
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
unsigned getNumSuccessors() const
Definition: Instruction.h:1041
static BranchInst * create(BasicBlock *IfTrue, InsertPosition Pos, Context &Ctx)
static bool classof(const Value *From)
For isa/dyn_cast.
Function * getCalledFunction() const
FunctionType * getFunctionType() const
void setCalledFunction(Function *F)
Value * getCalledOperand() const
void setCalledOperand(Value *V)
Definition: Instruction.h:1414
Value * getIndirectDestLabelUse(unsigned Idx) const
static CallBrInst * create(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
Value * getIndirectDestLabel(unsigned Idx) const
BasicBlock * getSuccessor(unsigned Idx) const
void setIndirectDest(unsigned Idx, BasicBlock *BB)
BasicBlock * getDefaultDest() const
BasicBlock * getIndirectDest(unsigned Idx) const
SmallVector< BasicBlock *, 16 > getIndirectDests() const
void setDefaultDest(BasicBlock *BB)
static CallInst * create(FunctionType *FTy, Value *Func, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
static Value * create(Type *DestTy, Opcode Op, Value *Operand, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static bool classof(const Value *From)
For isa/dyn_cast.
static CatchPadInst * create(Value *ParentPad, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &Name="")
CatchSwitchInst * getCatchSwitch() const
CatchPadInst * getCatchPad() const
BasicBlock * getSuccessor() const
void setSuccessor(BasicBlock *NewSucc)
void setCatchPad(CatchPadInst *CatchPad)
Value * getCatchSwitchParentPad() const
static CatchReturnInst * create(CatchPadInst *CatchPad, BasicBlock *BB, InsertPosition Pos, Context &Ctx)
void addHandler(BasicBlock *Dest)
void setParentPad(Value *ParentPad)
void setUnwindDest(BasicBlock *UnwindDest)
static CatchSwitchInst * create(Value *ParentPad, BasicBlock *UnwindBB, unsigned NumHandlers, InsertPosition Pos, Context &Ctx, const Twine &Name="")
BasicBlock * getUnwindDest() const
static CleanupPadInst * create(Value *ParentPad, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &Name="")
CleanupPadInst * getCleanupPad() const
void setUnwindDest(BasicBlock *NewDest)
static CleanupReturnInst * create(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB, InsertPosition Pos, Context &Ctx)
BasicBlock * getUnwindDest() const
void setCleanupPad(CleanupPadInst *CleanupPad)
static CmpInst * create(Predicate Pred, Value *S1, Value *S2, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static CmpInst * createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2, const Instruction *FlagsSource, InsertPosition Pos, Context &Ctx, const Twine &Name="")
void dumpOS(raw_ostream &OS) const override
LLVM_DUMP_METHOD void dump() const
static Type * makeCmpResultType(Type *OpndType)
Create a result type for fcmp/icmp.
void setPredicate(Predicate P)
static ConstantTokenNone * get(Context &Ctx)
Return the ConstantTokenNone.
GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
Definition: Context.cpp:546
CallBrInst * createCallBrInst(llvm::CallBrInst *I)
Definition: Context.cpp:513
sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:601
ReturnInst * createReturnInst(llvm::ReturnInst *I)
Definition: Context.cpp:498
void runEraseInstrCallbacks(Instruction *I)
Definition: Context.cpp:666
VAArgInst * createVAArgInst(llvm::VAArgInst *SI)
Definition: Context.cpp:430
CleanupReturnInst * createCleanupReturnInst(llvm::CleanupReturnInst *I)
Definition: Context.cpp:540
AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Definition: Context.cpp:581
Type * getType(llvm::Type *LLVMTy)
Definition: Context.h:220
AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
Definition: Context.cpp:571
InsertValueInst * createInsertValueInst(llvm::InsertValueInst *IVI)
Definition: Context.cpp:477
FCmpInst * createFCmpInst(llvm::FCmpInst *I)
Definition: Context.cpp:597
ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
Definition: Context.cpp:451
Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
Definition: Context.cpp:417
BranchInst * createBranchInst(llvm::BranchInst *I)
Definition: Context.cpp:483
ShuffleVectorInst * createShuffleVectorInst(llvm::ShuffleVectorInst *SVI)
Definition: Context.cpp:465
BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
Definition: Context.cpp:567
LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: Context.cpp:488
FreezeInst * createFreezeInst(llvm::FreezeInst *SI)
Definition: Context.cpp:435
PHINode * createPHINode(llvm::PHINode *I)
Definition: Context.cpp:589
CatchPadInst * createCatchPadInst(llvm::CatchPadInst *I)
Definition: Context.cpp:527
ICmpInst * createICmpInst(llvm::ICmpInst *I)
Definition: Context.cpp:593
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: Context.cpp:27
ExtractValueInst * createExtractValueInst(llvm::ExtractValueInst *IVI)
Definition: Context.cpp:471
CastInst * createCastInst(llvm::CastInst *I)
Definition: Context.cpp:585
StoreInst * createStoreInst(llvm::StoreInst *SI)
Definition: Context.cpp:493
CatchReturnInst * createCatchReturnInst(llvm::CatchReturnInst *I)
Definition: Context.cpp:535
CatchSwitchInst * createCatchSwitchInst(llvm::CatchSwitchInst *I)
Definition: Context.cpp:551
CleanupPadInst * createCleanupPadInst(llvm::CleanupPadInst *I)
Definition: Context.cpp:531
FenceInst * createFenceInst(llvm::FenceInst *SI)
Definition: Context.cpp:440
CallInst * createCallInst(llvm::CallInst *I)
Definition: Context.cpp:503
SwitchInst * createSwitchInst(llvm::SwitchInst *I)
Definition: Context.cpp:559
void runMoveInstrCallbacks(Instruction *I, const BBIterator &Where)
Definition: Context.cpp:676
UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
Definition: Context.cpp:563
InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Definition: Context.cpp:508
LandingPadInst * createLandingPadInst(llvm::LandingPadInst *I)
Definition: Context.cpp:523
InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
Definition: Context.cpp:458
SelectInst * createSelectInst(llvm::SelectInst *SI)
Definition: Context.cpp:445
ResumeInst * createResumeInst(llvm::ResumeInst *I)
Definition: Context.cpp:555
LLVMContext & LLVMCtx
Definition: Context.h:46
Tracker & getTracker()
Definition: Context.h:203
UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
Definition: Context.cpp:518
AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
Definition: Context.cpp:576
static Value * create(Value *Vec, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
VectorType * getVectorOperandType() const
static Value * create(Value *Agg, ArrayRef< unsigned > Idxs, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
static FenceInst * create(AtomicOrdering Ordering, InsertPosition Pos, Context &Ctx, SyncScope::ID SSID=SyncScope::System)
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:428
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this fence instruction.
static FreezeInst * create(Value *V, InsertPosition Pos, Context &Ctx, const Twine &Name="")
Value * getArgOperand(unsigned Idx) const
Return the Idx-th funcletpad argument.
Value * getParentPad() const
Return the outer EH-pad this funclet is nested within.
void setParentPad(Value *ParentPad)
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:303
This class can be used for tracking most instruction setters.
Definition: Tracker.h:275
static Value * create(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
static Value * create(Value *Vec, Value *NewElt, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static 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:42
bool hasNoUnsignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: Instruction.h:225
static IRBuilder & setInsertPos(InsertPosition Pos)
Helper function for create().
Definition: Instruction.h:103
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:245
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:270
const char * getOpcodeName() const
Definition: Instruction.h:131
void setHasNoSignedWrap(bool B=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
void insertAfter(Instruction *AfterI)
Insert this detached instruction after AfterI.
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:286
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:294
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: Instruction.h:232
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
BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
Definition: Instruction.cpp:38
void setFast(bool B)
Set or clear all fast-math-flags on this instruction, which must be an operator which supports this f...
void setHasApproxFunc(bool B)
Set or clear the approximate-math-functions flag on this instruction, which must be an operator which...
void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
void insertInto(BasicBlock *BB, const BBIterator &WhereIt)
Insert this detached instruction into BB at WhereIt.
llvm::Instruction * getTopmostLLVMInstruction() const
A SandboxIR Instruction may map to multiple LLVM IR Instruction.
Definition: Instruction.cpp:26
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.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition: Instruction.h:304
Instruction * getNextNode() const
\Returns the next sandboxir::Instruction in the block, or nullptr if at the end of the block.
Definition: Instruction.cpp:43
void removeFromParent()
Detach this from its parent BasicBlock without deleting it.
Definition: Instruction.cpp:66
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:278
void insertBefore(Instruction *BeforeI)
Insert this detached instruction before BeforeI.
void eraseFromParent()
Detach this Value from its parent and delete it.
Definition: Instruction.cpp:74
void setHasAllowReciprocal(bool B)
Set or clear the allow-reciprocal flag on this instruction, which must be an operator which supports ...
void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
BasicBlock * getUnwindDest() const
static InvokeInst * create(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, InsertPosition Pos, Context &Ctx, const Twine &NameStr="")
void setNormalDest(BasicBlock *BB)
void setUnwindDest(BasicBlock *BB)
BasicBlock * getSuccessor(unsigned SuccIdx) const
BasicBlock * getNormalDest() const
LandingPadInst * getLandingPadInst() const
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
Definition: Instruction.h:1521
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
static LandingPadInst * create(Type *RetTy, unsigned NumReservedClauses, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx, const Twine &Name="")
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Value * getPointerOperand() const
static bool classof(const Value *From)
For isa/dyn_cast.
Value * hasConstantValue() const
int getBasicBlockIndex(const BasicBlock *BB) const
unsigned getNumIncomingValues() const
Definition: Instruction.h:2417
Value * getIncomingValue(unsigned Idx) const
void setIncomingBlock(unsigned Idx, BasicBlock *BB)
void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate)
static bool classof(const Value *From)
For isa/dyn_cast.
static PHINode * create(Type *Ty, unsigned NumReservedValues, InsertPosition Pos, Context &Ctx, const Twine &Name="")
Value * removeIncomingValue(unsigned Idx)
void setIncomingValue(unsigned Idx, Value *V)
BasicBlock * getIncomingBlock(unsigned Idx) const
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
void addIncoming(Value *V, BasicBlock *BB)
static ResumeInst * create(Value *Exn, InsertPosition Pos, Context &Ctx)
static ReturnInst * create(Value *RetVal, InsertPosition Pos, Context &Ctx)
Value * getReturnValue() const
\Returns null if there is no return value.
static bool classof(const Value *From)
For isa/dyn_cast.
static Value * create(Value *Cond, Value *True, Value *False, InsertPosition Pos, Context &Ctx, const Twine &Name="")
VectorType * getType() const
Overload to return most specific vector type.
Constant * getShuffleMaskForBitcode() const
Return the mask for this instruction, for use in bitcode.
void commute()
Swap the operands and adjust the mask to preserve the semantics of the instruction.
static Value * create(Value *V1, Value *V2, Value *Mask, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static Constant * convertShuffleMaskForBitcode(ArrayRef< int > Mask, Type *ResultTy)
void setShuffleMask(ArrayRef< int > Mask)
void setVolatile(bool V)
Specify whether this is a volatile store or not.
static bool classof(const Value *From)
For isa/dyn_cast.
static StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx)
Value * getPointerOperand() const
Value * getValueOperand() const
static SwitchInst * create(Value *V, BasicBlock *Dest, unsigned NumCases, InsertPosition Pos, Context &Ctx, const Twine &Name="")
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
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:1886
void setDefaultDest(BasicBlock *DefaultCase)
llvm::SwitchInst::CaseIteratorImpl< CaseHandle > CaseIt
Definition: Instruction.h:1881
BasicBlock * getDefaultDest() const
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
ConstantInt * findCaseDest(BasicBlock *BB)
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:440
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition: Tracker.h:478
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Definition: Tracker.h:495
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
llvm::Type * LLVMTy
Definition: Type.h:45
static Type * getInt1Ty(Context &Ctx)
Context & getContext() const
Definition: Type.h:89
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *OpV, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static Value * create(Instruction::Opcode Op, Value *OpV, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static UnreachableInst * create(InsertPosition Pos, Context &Ctx)
static bool classof(const Value *From)
Tracks swapping a Use with another Use.
Definition: Tracker.h:196
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: User.cpp:91
virtual unsigned getNumOperands() const
Definition: User.h:128
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: User.h:125
static 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:63
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:103
void dumpCommonSuffix(raw_ostream &OS) const
Definition: Value.cpp:105
Context & Ctx
All values point to the context.
Definition: Value.h:172
Type * getType() const
Definition: Value.cpp:46
iterator_range< user_iterator > users()
Definition: Value.h:224
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:98
@ LLVMAtomicRMW
Definition: Core.h:136
@ LLVMAtomicCmpXchg
Definition: Core.h:135
@ LLVMSwitch
Definition: Core.h:64
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:420
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:1926
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