@@ -1219,6 +1219,107 @@ static llvm::Instruction::CastOps getLLVMCastOp(Instruction::Opcode Opc) {
1219
1219
}
1220
1220
}
1221
1221
1222
+ // / \Returns the LLVM opcode that corresponds to \p Opc.
1223
+ static llvm::Instruction::BinaryOps getLLVMBinaryOp (Instruction::Opcode Opc) {
1224
+ switch (Opc) {
1225
+ case Instruction::Opcode::Add:
1226
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Add);
1227
+ case Instruction::Opcode::FAdd:
1228
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::FAdd);
1229
+ case Instruction::Opcode::Sub:
1230
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Sub);
1231
+ case Instruction::Opcode::FSub:
1232
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::FSub);
1233
+ case Instruction::Opcode::Mul:
1234
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Mul);
1235
+ case Instruction::Opcode::FMul:
1236
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::FMul);
1237
+ case Instruction::Opcode::UDiv:
1238
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::UDiv);
1239
+ case Instruction::Opcode::SDiv:
1240
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::SDiv);
1241
+ case Instruction::Opcode::FDiv:
1242
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::FDiv);
1243
+ case Instruction::Opcode::URem:
1244
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::URem);
1245
+ case Instruction::Opcode::SRem:
1246
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::SRem);
1247
+ case Instruction::Opcode::FRem:
1248
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::FRem);
1249
+ case Instruction::Opcode::Shl:
1250
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Shl);
1251
+ case Instruction::Opcode::LShr:
1252
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::LShr);
1253
+ case Instruction::Opcode::AShr:
1254
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::AShr);
1255
+ case Instruction::Opcode::And:
1256
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::And);
1257
+ case Instruction::Opcode::Or:
1258
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Or);
1259
+ case Instruction::Opcode::Xor:
1260
+ return static_cast <llvm::Instruction::BinaryOps>(llvm::Instruction::Xor);
1261
+ default :
1262
+ llvm_unreachable (" Not a binary op!" );
1263
+ }
1264
+ }
1265
+ Value *BinaryOperator::create (Instruction::Opcode Op, Value *LHS, Value *RHS,
1266
+ BBIterator WhereIt, BasicBlock *WhereBB,
1267
+ Context &Ctx, const Twine &Name) {
1268
+ auto &Builder = Ctx.getLLVMIRBuilder ();
1269
+ if (WhereIt == WhereBB->end ())
1270
+ Builder.SetInsertPoint (cast<llvm::BasicBlock>(WhereBB->Val ));
1271
+ else
1272
+ Builder.SetInsertPoint ((*WhereIt).getTopmostLLVMInstruction ());
1273
+ llvm::Value *NewV =
1274
+ Builder.CreateBinOp (getLLVMBinaryOp (Op), LHS->Val , RHS->Val , Name);
1275
+ if (auto *NewBinOp = dyn_cast<llvm::BinaryOperator>(NewV))
1276
+ return Ctx.createBinaryOperator (NewBinOp);
1277
+ assert (isa<llvm::Constant>(NewV) && " Expected constant" );
1278
+ return Ctx.getOrCreateConstant (cast<llvm::Constant>(NewV));
1279
+ }
1280
+
1281
+ Value *BinaryOperator::create (Instruction::Opcode Op, Value *LHS, Value *RHS,
1282
+ Instruction *InsertBefore, Context &Ctx,
1283
+ const Twine &Name) {
1284
+ return create (Op, LHS, RHS, InsertBefore->getIterator (),
1285
+ InsertBefore->getParent (), Ctx, Name);
1286
+ }
1287
+
1288
+ Value *BinaryOperator::create (Instruction::Opcode Op, Value *LHS, Value *RHS,
1289
+ BasicBlock *InsertAtEnd, Context &Ctx,
1290
+ const Twine &Name) {
1291
+ return create (Op, LHS, RHS, InsertAtEnd->end (), InsertAtEnd, Ctx, Name);
1292
+ }
1293
+
1294
+ Value *BinaryOperator::createWithCopiedFlags (Instruction::Opcode Op, Value *LHS,
1295
+ Value *RHS, Value *CopyFrom,
1296
+ BBIterator WhereIt,
1297
+ BasicBlock *WhereBB, Context &Ctx,
1298
+ const Twine &Name) {
1299
+
1300
+ Value *NewV = create (Op, LHS, RHS, WhereIt, WhereBB, Ctx, Name);
1301
+ if (auto *NewBO = dyn_cast<BinaryOperator>(NewV))
1302
+ cast<llvm::BinaryOperator>(NewBO->Val )->copyIRFlags (CopyFrom->Val );
1303
+ return NewV;
1304
+ }
1305
+
1306
+ Value *BinaryOperator::createWithCopiedFlags (Instruction::Opcode Op, Value *LHS,
1307
+ Value *RHS, Value *CopyFrom,
1308
+ Instruction *InsertBefore,
1309
+ Context &Ctx, const Twine &Name) {
1310
+ return createWithCopiedFlags (Op, LHS, RHS, CopyFrom,
1311
+ InsertBefore->getIterator (),
1312
+ InsertBefore->getParent (), Ctx, Name);
1313
+ }
1314
+
1315
+ Value *BinaryOperator::createWithCopiedFlags (Instruction::Opcode Op, Value *LHS,
1316
+ Value *RHS, Value *CopyFrom,
1317
+ BasicBlock *InsertAtEnd,
1318
+ Context &Ctx, const Twine &Name) {
1319
+ return createWithCopiedFlags (Op, LHS, RHS, CopyFrom, InsertAtEnd->end (),
1320
+ InsertAtEnd, Ctx, Name);
1321
+ }
1322
+
1222
1323
void AtomicCmpXchgInst::setSyncScopeID (SyncScope::ID SSID) {
1223
1324
Ctx.getTracker ()
1224
1325
.emplaceIfTracking <GenericSetter<&AtomicCmpXchgInst::getSyncScopeID,
@@ -1628,6 +1729,29 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
1628
1729
new GetElementPtrInst (LLVMGEP, *this ));
1629
1730
return It->second .get ();
1630
1731
}
1732
+ case llvm::Instruction::Add:
1733
+ case llvm::Instruction::FAdd:
1734
+ case llvm::Instruction::Sub:
1735
+ case llvm::Instruction::FSub:
1736
+ case llvm::Instruction::Mul:
1737
+ case llvm::Instruction::FMul:
1738
+ case llvm::Instruction::UDiv:
1739
+ case llvm::Instruction::SDiv:
1740
+ case llvm::Instruction::FDiv:
1741
+ case llvm::Instruction::URem:
1742
+ case llvm::Instruction::SRem:
1743
+ case llvm::Instruction::FRem:
1744
+ case llvm::Instruction::Shl:
1745
+ case llvm::Instruction::LShr:
1746
+ case llvm::Instruction::AShr:
1747
+ case llvm::Instruction::And:
1748
+ case llvm::Instruction::Or:
1749
+ case llvm::Instruction::Xor: {
1750
+ auto *LLVMBinaryOperator = cast<llvm::BinaryOperator>(LLVMV);
1751
+ It->second = std::unique_ptr<BinaryOperator>(
1752
+ new BinaryOperator (LLVMBinaryOperator, *this ));
1753
+ return It->second .get ();
1754
+ }
1631
1755
case llvm::Instruction::AtomicCmpXchg: {
1632
1756
auto *LLVMAtomicCmpXchg = cast<llvm::AtomicCmpXchgInst>(LLVMV);
1633
1757
It->second = std::unique_ptr<AtomicCmpXchgInst>(
@@ -1751,6 +1875,10 @@ Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
1751
1875
std::unique_ptr<GetElementPtrInst>(new GetElementPtrInst (I, *this ));
1752
1876
return cast<GetElementPtrInst>(registerValue (std::move (NewPtr)));
1753
1877
}
1878
+ BinaryOperator *Context::createBinaryOperator (llvm::BinaryOperator *I) {
1879
+ auto NewPtr = std::unique_ptr<BinaryOperator>(new BinaryOperator (I, *this ));
1880
+ return cast<BinaryOperator>(registerValue (std::move (NewPtr)));
1881
+ }
1754
1882
AtomicCmpXchgInst *
1755
1883
Context::createAtomicCmpXchgInst (llvm::AtomicCmpXchgInst *I) {
1756
1884
auto NewPtr =
0 commit comments