@@ -313,11 +313,10 @@ BBIterator &BBIterator::operator--() {
313
313
314
314
const char *Instruction::getOpcodeName (Opcode Opc) {
315
315
switch (Opc) {
316
- #define DEF_VALUE (ID, CLASS )
317
- #define DEF_USER (ID, CLASS )
318
316
#define OP (OPC ) \
319
317
case Opcode::OPC: \
320
318
return #OPC;
319
+ #define OPCODES (...) __VA_ARGS__
321
320
#define DEF_INSTR (ID, OPC, CLASS ) OPC
322
321
#include " llvm/SandboxIR/SandboxIRValues.def"
323
322
}
@@ -1061,6 +1060,87 @@ void GetElementPtrInst::dump() const {
1061
1060
dump (dbgs ());
1062
1061
dbgs () << " \n " ;
1063
1062
}
1063
+ #endif // NDEBUG
1064
+
1065
+ static llvm::Instruction::CastOps getLLVMCastOp (Instruction::Opcode Opc) {
1066
+ switch (Opc) {
1067
+ case Instruction::Opcode::ZExt:
1068
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::ZExt);
1069
+ case Instruction::Opcode::SExt:
1070
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::SExt);
1071
+ case Instruction::Opcode::FPToUI:
1072
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::FPToUI);
1073
+ case Instruction::Opcode::FPToSI:
1074
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::FPToSI);
1075
+ case Instruction::Opcode::FPExt:
1076
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::FPExt);
1077
+ case Instruction::Opcode::PtrToInt:
1078
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::PtrToInt);
1079
+ case Instruction::Opcode::IntToPtr:
1080
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::IntToPtr);
1081
+ case Instruction::Opcode::SIToFP:
1082
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::SIToFP);
1083
+ case Instruction::Opcode::UIToFP:
1084
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::UIToFP);
1085
+ case Instruction::Opcode::Trunc:
1086
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::Trunc);
1087
+ case Instruction::Opcode::FPTrunc:
1088
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::FPTrunc);
1089
+ case Instruction::Opcode::BitCast:
1090
+ return static_cast <llvm::Instruction::CastOps>(llvm::Instruction::BitCast);
1091
+ case Instruction::Opcode::AddrSpaceCast:
1092
+ return static_cast <llvm::Instruction::CastOps>(
1093
+ llvm::Instruction::AddrSpaceCast);
1094
+ default :
1095
+ llvm_unreachable (" Opcode not suitable for CastInst!" );
1096
+ }
1097
+ }
1098
+
1099
+ Value *CastInst::create (Type *DestTy, Opcode Op, Value *Operand,
1100
+ BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1101
+ const Twine &Name) {
1102
+ assert (getLLVMCastOp (Op) && " Opcode not suitable for CastInst!" );
1103
+ auto &Builder = Ctx.getLLVMIRBuilder ();
1104
+ if (WhereIt == WhereBB->end ())
1105
+ Builder.SetInsertPoint (cast<llvm::BasicBlock>(WhereBB->Val ));
1106
+ else
1107
+ Builder.SetInsertPoint ((*WhereIt).getTopmostLLVMInstruction ());
1108
+ auto *NewV =
1109
+ Builder.CreateCast (getLLVMCastOp (Op), Operand->Val , DestTy, Name);
1110
+ if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
1111
+ return Ctx.createCastInst (NewCI);
1112
+ assert (isa<llvm::Constant>(NewV) && " Expected constant" );
1113
+ return Ctx.getOrCreateConstant (cast<llvm::Constant>(NewV));
1114
+ }
1115
+
1116
+ Value *CastInst::create (Type *DestTy, Opcode Op, Value *Operand,
1117
+ Instruction *InsertBefore, Context &Ctx,
1118
+ const Twine &Name) {
1119
+ return create (DestTy, Op, Operand, InsertBefore->getIterator (),
1120
+ InsertBefore->getParent (), Ctx, Name);
1121
+ }
1122
+
1123
+ Value *CastInst::create (Type *DestTy, Opcode Op, Value *Operand,
1124
+ BasicBlock *InsertAtEnd, Context &Ctx,
1125
+ const Twine &Name) {
1126
+ return create (DestTy, Op, Operand, InsertAtEnd->end (), InsertAtEnd, Ctx,
1127
+ Name);
1128
+ }
1129
+
1130
+ bool CastInst::classof (const Value *From) {
1131
+ return From->getSubclassID () == ClassID::Cast;
1132
+ }
1133
+
1134
+ #ifndef NDEBUG
1135
+ void CastInst::dump (raw_ostream &OS) const {
1136
+ dumpCommonPrefix (OS);
1137
+ dumpCommonSuffix (OS);
1138
+ }
1139
+
1140
+ void CastInst::dump () const {
1141
+ dump (dbgs ());
1142
+ dbgs () << " \n " ;
1143
+ }
1064
1144
1065
1145
void OpaqueInst::dump (raw_ostream &OS) const {
1066
1146
dumpCommonPrefix (OS);
@@ -1236,6 +1316,23 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
1236
1316
new GetElementPtrInst (LLVMGEP, *this ));
1237
1317
return It->second .get ();
1238
1318
}
1319
+ case llvm::Instruction::ZExt:
1320
+ case llvm::Instruction::SExt:
1321
+ case llvm::Instruction::FPToUI:
1322
+ case llvm::Instruction::FPToSI:
1323
+ case llvm::Instruction::FPExt:
1324
+ case llvm::Instruction::PtrToInt:
1325
+ case llvm::Instruction::IntToPtr:
1326
+ case llvm::Instruction::SIToFP:
1327
+ case llvm::Instruction::UIToFP:
1328
+ case llvm::Instruction::Trunc:
1329
+ case llvm::Instruction::FPTrunc:
1330
+ case llvm::Instruction::BitCast:
1331
+ case llvm::Instruction::AddrSpaceCast: {
1332
+ auto *LLVMCast = cast<llvm::CastInst>(LLVMV);
1333
+ It->second = std::unique_ptr<CastInst>(new CastInst (LLVMCast, *this ));
1334
+ return It->second .get ();
1335
+ }
1239
1336
default :
1240
1337
break ;
1241
1338
}
@@ -1301,6 +1398,11 @@ Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
1301
1398
return cast<GetElementPtrInst>(registerValue (std::move (NewPtr)));
1302
1399
}
1303
1400
1401
+ CastInst *Context::createCastInst (llvm::CastInst *I) {
1402
+ auto NewPtr = std::unique_ptr<CastInst>(new CastInst (I, *this ));
1403
+ return cast<CastInst>(registerValue (std::move (NewPtr)));
1404
+ }
1405
+
1304
1406
Value *Context::getValue (llvm::Value *V) const {
1305
1407
auto It = LLVMValueToValueMap.find (V);
1306
1408
if (It != LLVMValueToValueMap.end ())
0 commit comments