|
8 | 8 |
|
9 | 9 | #include "llvm/SandboxIR/SandboxIR.h" |
10 | 10 | #include "llvm/ADT/SmallPtrSet.h" |
| 11 | +#include "llvm/ADT/SmallVector.h" |
11 | 12 | #include "llvm/IR/Constants.h" |
12 | 13 | #include "llvm/Support/Debug.h" |
13 | 14 | #include <sstream> |
@@ -896,6 +897,105 @@ void InvokeInst::dump() const { |
896 | 897 | dump(dbgs()); |
897 | 898 | dbgs() << "\n"; |
898 | 899 | } |
| 900 | +#endif // NDEBUG |
| 901 | + |
| 902 | +CallBrInst *CallBrInst::create(FunctionType *FTy, Value *Func, |
| 903 | + BasicBlock *DefaultDest, |
| 904 | + ArrayRef<BasicBlock *> IndirectDests, |
| 905 | + ArrayRef<Value *> Args, BBIterator WhereIt, |
| 906 | + BasicBlock *WhereBB, Context &Ctx, |
| 907 | + const Twine &NameStr) { |
| 908 | + auto &Builder = Ctx.getLLVMIRBuilder(); |
| 909 | + if (WhereIt != WhereBB->end()) |
| 910 | + Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction()); |
| 911 | + else |
| 912 | + Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val)); |
| 913 | + |
| 914 | + SmallVector<llvm::BasicBlock *> LLVMIndirectDests; |
| 915 | + LLVMIndirectDests.reserve(IndirectDests.size()); |
| 916 | + for (BasicBlock *IndDest : IndirectDests) |
| 917 | + LLVMIndirectDests.push_back(cast<llvm::BasicBlock>(IndDest->Val)); |
| 918 | + |
| 919 | + SmallVector<llvm::Value *> LLVMArgs; |
| 920 | + LLVMArgs.reserve(Args.size()); |
| 921 | + for (Value *Arg : Args) |
| 922 | + LLVMArgs.push_back(Arg->Val); |
| 923 | + |
| 924 | + llvm::CallBrInst *CallBr = Builder.CreateCallBr( |
| 925 | + FTy, Func->Val, cast<llvm::BasicBlock>(DefaultDest->Val), |
| 926 | + LLVMIndirectDests, LLVMArgs, NameStr); |
| 927 | + return Ctx.createCallBrInst(CallBr); |
| 928 | +} |
| 929 | + |
| 930 | +CallBrInst *CallBrInst::create(FunctionType *FTy, Value *Func, |
| 931 | + BasicBlock *DefaultDest, |
| 932 | + ArrayRef<BasicBlock *> IndirectDests, |
| 933 | + ArrayRef<Value *> Args, |
| 934 | + Instruction *InsertBefore, Context &Ctx, |
| 935 | + const Twine &NameStr) { |
| 936 | + return create(FTy, Func, DefaultDest, IndirectDests, Args, |
| 937 | + InsertBefore->getIterator(), InsertBefore->getParent(), Ctx, |
| 938 | + NameStr); |
| 939 | +} |
| 940 | +CallBrInst *CallBrInst::create(FunctionType *FTy, Value *Func, |
| 941 | + BasicBlock *DefaultDest, |
| 942 | + ArrayRef<BasicBlock *> IndirectDests, |
| 943 | + ArrayRef<Value *> Args, BasicBlock *InsertAtEnd, |
| 944 | + Context &Ctx, const Twine &NameStr) { |
| 945 | + return create(FTy, Func, DefaultDest, IndirectDests, Args, InsertAtEnd->end(), |
| 946 | + InsertAtEnd, Ctx, NameStr); |
| 947 | +} |
| 948 | + |
| 949 | +Value *CallBrInst::getIndirectDestLabel(unsigned Idx) const { |
| 950 | + return Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDestLabel(Idx)); |
| 951 | +} |
| 952 | +Value *CallBrInst::getIndirectDestLabelUse(unsigned Idx) const { |
| 953 | + return Ctx.getValue( |
| 954 | + cast<llvm::CallBrInst>(Val)->getIndirectDestLabelUse(Idx)); |
| 955 | +} |
| 956 | +BasicBlock *CallBrInst::getDefaultDest() const { |
| 957 | + return cast<BasicBlock>( |
| 958 | + Ctx.getValue(cast<llvm::CallBrInst>(Val)->getDefaultDest())); |
| 959 | +} |
| 960 | +BasicBlock *CallBrInst::getIndirectDest(unsigned Idx) const { |
| 961 | + return cast<BasicBlock>( |
| 962 | + Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDest(Idx))); |
| 963 | +} |
| 964 | +llvm::SmallVector<BasicBlock *, 16> CallBrInst::getIndirectDests() const { |
| 965 | + SmallVector<BasicBlock *, 16> BBs; |
| 966 | + for (llvm::BasicBlock *LLVMBB : |
| 967 | + cast<llvm::CallBrInst>(Val)->getIndirectDests()) |
| 968 | + BBs.push_back(cast<BasicBlock>(Ctx.getValue(LLVMBB))); |
| 969 | + return BBs; |
| 970 | +} |
| 971 | +void CallBrInst::setDefaultDest(BasicBlock *BB) { |
| 972 | + auto &Tracker = Ctx.getTracker(); |
| 973 | + if (Tracker.isTracking()) |
| 974 | + Tracker.track(std::make_unique<CallBrInstSetDefaultDest>(this, Tracker)); |
| 975 | + cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val)); |
| 976 | +} |
| 977 | +void CallBrInst::setIndirectDest(unsigned Idx, BasicBlock *BB) { |
| 978 | + auto &Tracker = Ctx.getTracker(); |
| 979 | + if (Tracker.isTracking()) |
| 980 | + Tracker.track( |
| 981 | + std::make_unique<CallBrInstSetIndirectDest>(this, Idx, Tracker)); |
| 982 | + cast<llvm::CallBrInst>(Val)->setIndirectDest(Idx, |
| 983 | + cast<llvm::BasicBlock>(BB->Val)); |
| 984 | +} |
| 985 | +BasicBlock *CallBrInst::getSuccessor(unsigned Idx) const { |
| 986 | + return cast<BasicBlock>( |
| 987 | + Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx))); |
| 988 | +} |
| 989 | + |
| 990 | +#ifndef NDEBUG |
| 991 | +void CallBrInst::dump(raw_ostream &OS) const { |
| 992 | + dumpCommonPrefix(OS); |
| 993 | + dumpCommonSuffix(OS); |
| 994 | +} |
| 995 | +void CallBrInst::dump() const { |
| 996 | + dump(dbgs()); |
| 997 | + dbgs() << "\n"; |
| 998 | +} |
899 | 999 |
|
900 | 1000 | void OpaqueInst::dump(raw_ostream &OS) const { |
901 | 1001 | dumpCommonPrefix(OS); |
@@ -1060,6 +1160,11 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) { |
1060 | 1160 | It->second = std::unique_ptr<InvokeInst>(new InvokeInst(LLVMInvoke, *this)); |
1061 | 1161 | return It->second.get(); |
1062 | 1162 | } |
| 1163 | + case llvm::Instruction::CallBr: { |
| 1164 | + auto *LLVMCallBr = cast<llvm::CallBrInst>(LLVMV); |
| 1165 | + It->second = std::unique_ptr<CallBrInst>(new CallBrInst(LLVMCallBr, *this)); |
| 1166 | + return It->second.get(); |
| 1167 | + } |
1063 | 1168 | default: |
1064 | 1169 | break; |
1065 | 1170 | } |
@@ -1113,6 +1218,11 @@ InvokeInst *Context::createInvokeInst(llvm::InvokeInst *I) { |
1113 | 1218 | return cast<InvokeInst>(registerValue(std::move(NewPtr))); |
1114 | 1219 | } |
1115 | 1220 |
|
| 1221 | +CallBrInst *Context::createCallBrInst(llvm::CallBrInst *I) { |
| 1222 | + auto NewPtr = std::unique_ptr<CallBrInst>(new CallBrInst(I, *this)); |
| 1223 | + return cast<CallBrInst>(registerValue(std::move(NewPtr))); |
| 1224 | +} |
| 1225 | + |
1116 | 1226 | Value *Context::getValue(llvm::Value *V) const { |
1117 | 1227 | auto It = LLVMValueToValueMap.find(V); |
1118 | 1228 | if (It != LLVMValueToValueMap.end()) |
|
0 commit comments